LoginSignup
0
0

More than 1 year has passed since last update.

Python初心者のThousandeyesログ取得

Posted at

この記事を読む方へ

基本的には個人の備忘録です。
私自身、主業務がネットワークだったこともあり、Pythonは触ったことがありませんでしたが、ネットワークの品質測定でThousandeyesを使い始めたため、ThousandeyesのAPI経由でログ取得が必要となりました。
Thousandeyesはナレッジが充実しており、素養のある人間が調べればログの取得方法はすぐに分かるようになっていますが、Python素人にはハードルが高かったこと、Qiita等に簡単な取得方が掲載されていなかったこともあり、記事を残すことにしました。

下記コードの動作は確認していますが、ID/Token以外にもユーザ固有値が必要となることから、詳細はやはり正規リファレンスを参照頂ければと思います。
Thousandeyes Reference

動作環境

Google Colab

コードの概要

startで指定した日時から、endの日(時間帯指定なし)までのweb_http testのレスポンス情報を取得
編集後処理のためにDF化

from urllib.parse import quote
import json
import requests
import pandas as pd

email = quote("")               #thousandeyesのログインID(メールアドレス)の"@"を"%40"に変換
authToken= quote("")           #API Tokens

start = "2021-09-08T00:00:00"   #取得開始日時:thousandeyes指定のtime rangeで指定
end = "2021-09-19"              #取得終了日時:今回は適当な時間まで取得できればよかったので、thousandeyes指定のto=""での指定はしてません

url = "https://{}:{}@  ****  .json?from={}".format(email,authToken,start)
                                # 上記 **** は取得したいデータ毎にユーザ固有値を入力
                                # 例:api.thousandeyes.com/v6/web/http-server/2245279

page = requests.get(url)
test = page.json()

# レスポンス取得
def get_res():
  date_list = []
  response_list = []

  for i in test['web']['httpServer']:
    date_list.append(i['date'])
    try:                                      #測定エラーによりキーがない場合があるため例外処理を実施
      response_list.append(i['responseTime']) #レスポンス以外を取得したい場合はここのキーを変更、または増やせば複数取得可
    except KeyError:
      response_list.append(None)

  http_data = {"date":date_list,
               "response":response_list}
  return http_data

df = pd.DataFrame(get_res())                  #編集のためと、下で終了日確認のためDF化

# Next部分の取得 endの日付まで
# thousandeyesは一度に1000レコードまでしかはかないので、1000レコードを超える範囲は
# Nextキー内のURLにアクセスし、ループ処理で取得し続ける必要あり

while df.iloc[-1,0].split(" ")[0] != end:
  try:
    if test['pages']['next']:
      url = test['pages']['next']
      url = (url.split("//")[0]+"//{}:{}@"+url.split("//")[1]).format(email,authToken)
      page = requests.get(url)
      test = page.json()
  except KeyError:
    pass
  df = pd.concat([df, pd.DataFrame(get_res())], axis=0)

print(df)                 #indexは0-999がループしますが、df.reset_index(drop=True)等で対処ください
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0