実現したいこと
ここに実現したいことを箇条書きで書いてください。
- 1つめのAPI(dataservice/device)でデバイス一覧が出力され
- デバイス一覧をもとに2つ目のAPI(dataservice/statistics/approute/fec/aggregation)を実行し
- デバイスx紐付くTunnelの結果をすべて表示したい
前提
デバイス一覧
devicelist
11.1.1.1 21.1.1.2 31.1.1.3 41.1.1.4
例として、2つ目のAPI結果では、1デバイスに複数のTunnel結果が出力される。
1.1.1.1以下に存在する複数のトンネル(abc, edf, ghi, jki...)
example
1deviceId debice loss latency 21.1.1.1 abc 999 999 31.1.1.1 edf 999 999 41.1.1.1 ghi 999 999 51.1.1.1 jkl 999 999
指摘により、1デバイスxn個のトンネルが、デバイス一覧すべてをprintすることで
Logでは取得できていることを確認できたが、
以下の箇所のロジックにて、csvで出力することができない
Tunnel_level_quality_analysis_df = get_request(vmanage_info_df)
以上の一文でget_request(vmanage_info_df)をcsvに入れると認識している。
しかし、csvの結果が、デバイス一覧のみしか出力されていない。
df = df[["deviceId", "system-ip", "host-name", "device-model", "bfdSessionsUp", "bfdSessions"]]
の結果しか入っていない状況です。
想定結果のcsv
Result_from_csv
1System_ip loss_percentage ・・・・ vqoe_score dt 21.1.1.1 99.99・・・・5 2023/01/01 01:01:01 31.1.1.1 99.98・・・・5 2023/01/01 01:01:01 41.1.1.1 99.97・・・・5 2023/01/01 01:01:01 51.1.1.1 99.96・・・・5 2023/01/01 01:01:01 6System_ip loss_percentage ・・・・ vqoe_score dt 71.1.1.2 99.99・・・・5 2023/01/01 01:01:01 81.1.1.2 99.98・・・・5 2023/01/01 01:01:01 91.1.1.2 99.97・・・・5 2023/01/01 01:01:01 101.1.1.2 99.96・・・・5 2023/01/01 01:01:01 11System_ip loss_percentage ・・・・ vqoe_score dt 121.1.1.3 99.99・・・・5 2023/01/01 01:01:01 131.1.1.3 99.98・・・・5 2023/01/01 01:01:01 141.1.1.3 99.97・・・・5 2023/01/01 01:01:01 151.1.1.3 99.96・・・・5 2023/01/01 01:01:01 16System_ip loss_percentage ・・・・ vqoe_score dt 171.1.1.4 99.99・・・・5 2023/01/01 01:01:01 181.1.1.4 99.98・・・・5 2023/01/01 01:01:01 191.1.1.4 99.97・・・・5 2023/01/01 01:01:01 201.1.1.4 99.96・・・・5 2023/01/01 01:01:01
発生している問題・エラーメッセージ
log
1debug1 2debug2 3debug3 4device_url 5https://vmanage_ABC/dataservice/device 6filter_df 7deviceId system_ip・・・bdfSessionsUp bfdSessions 81.1.1.1 1.1.1.1 ・・・ 8 8 91.1.1.2 1.1.1.2 ・・・ 8 8 101.1.1.3 1.1.1.3 ・・・ 8 8 111.1.1.4 1.1.1.4 ・・・ 8 8 12 13query_data 14{'query':(略),{'value':['1.1.1.1'],(略). 'type':'sum'}]}} 15device_list 16<Response [200]> 17200 18df2 19System_ip loss_percentage ・・・・ vqoe_score dt 201.1.1.1 99.99・・・・5 2023/01/01 01:01:01 211.1.1.1 99.98・・・・5 2023/01/01 01:01:01 221.1.1.1 99.97・・・・5 2023/01/01 01:01:01 231.1.1.1 99.96・・・・5 2023/01/01 01:01:01 24df_itertuples 25deviceId system_ip・・・bdfSessionsUp bfdSessions 261.1.1.1 1.1.1.1 ・・・ 8 8 271.1.1.2 1.1.1.2 ・・・ 8 8 281.1.1.3 1.1.1.3 ・・・ 8 8 291.1.1.4 1.1.1.4 ・・・ 8 8 30 31query_data 32{'query':(略),{'value':['1.1.1.1'],(略). 'type':'sum'}]}} 33device_list 34<Response [200]> 35200 36df2 37System_ip loss_percentage ・・・・ vqoe_score dt 381.1.1.2 99.99・・・・5 2023/01/01 01:01:01 391.1.1.2 99.98・・・・5 2023/01/01 01:01:01 401.1.1.2 99.97・・・・5 2023/01/01 01:01:01 411.1.1.2 99.96・・・・5 2023/01/01 01:01:01 42df_itertuples 43deviceId system_ip・・・bdfSessionsUp bfdSessions 441.1.1.1 1.1.1.1 ・・・ 8 8 451.1.1.2 1.1.1.2 ・・・ 8 8 461.1.1.3 1.1.1.3 ・・・ 8 8 471.1.1.4 1.1.1.4 ・・・ 8 8 48 49query_data 50{'query':(略),{'value':['1.1.1.1'],(略). 'type':'sum'}]}} 51device_list 52<Response [200]> 53200 54df2 55System_ip loss_percentage ・・・・ vqoe_score dt 561.1.1.3 99.99・・・・5 2023/01/01 01:01:01 571.1.1.3 99.98・・・・5 2023/01/01 01:01:01 581.1.1.3 99.97・・・・5 2023/01/01 01:01:01 591.1.1.3 99.96・・・・5 2023/01/01 01:01:01 60df_itertuples 61deviceId system_ip・・・bdfSessionsUp bfdSessions 621.1.1.1 1.1.1.1 ・・・ 8 8 631.1.1.2 1.1.1.2 ・・・ 8 8 641.1.1.3 1.1.1.3 ・・・ 8 8 651.1.1.4 1.1.1.4 ・・・ 8 8 66 67query_data 68{'query':(略),{'value':['1.1.1.1'],(略). 'type':'sum'}]}} 69device_list 70<Response [200]> 71200 72df2 73System_ip loss_percentage ・・・・ vqoe_score dt 741.1.1.4 99.99・・・・5 2023/01/01 01:01:01 751.1.1.4 99.98・・・・5 2023/01/01 01:01:01 761.1.1.4 99.97・・・・5 2023/01/01 01:01:01 771.1.1.4 99.96・・・・5 2023/01/01 01:01:01 78df_itertuples 79deviceId system_ip・・・bdfSessionsUp bfdSessions 801.1.1.1 1.1.1.1 ・・・ 8 8 811.1.1.2 1.1.1.2 ・・・ 8 8 821.1.1.3 1.1.1.3 ・・・ 8 8 831.1.1.4 1.1.1.4 ・・・ 8 8
該当のソースコード(df2はprintで確認_回答反映
Python
1import json 2import boto3 3import csv 4import numpy as np 5import pandas as pd 6import requests 7import os 8import urllib3 9urllib3.disable_warnings(urlib3.exceptions.InsecureRequests.InsecureRequestWarning) 10from io import StringIO 11from datetime import datetime 12 13headers_post = ["accept": "application/json", "Content-Type": "application/json"] 14 15print("debug1") 16 17def lambda_handler(event, context): 18 s3 = boto3.resource(`s3`) 19 bucket_name = os.environ.get(`S3_BUCKET`) 20 21 vmanage_info = "tmp/vmanage_info.csv" 22 bucket.download_file(os.environ.get(`CSV_VMANAGE_INFO`), vmanage_info) 23 vmanage_info_df = pd.read.csv(vmanage_info) 24 25 device_info = "/tmp/device_info.csv" 26 bucket.download_file(os.environ.get(`CSV_DEVICE_INFO`), device_info) 27 device_info_df = pd.read.csv(device_info) 28 29 Tunnel_level_quality_analysis_df = get_request(vmanage_info_df) 30 31 tmp_csv = "/tmp/Tunnel_level_quality_analysis.csv" 32 Tunnel_level_quality_analysis_df.to_csv(tmp_csv, encoding='utf-8', index=False) 33 34 s3 = boto3.resource('s3') 35 s3.meta.client.upload_file(tmp_csvm 'utlink-vmanage-bucket', 'csv/Tunnel_level_quality_analysis.csv') 36 37 print("debug2") 38 39def get_request(vmanage_info_df): 40 #api_datalist = [] 41 for row in vmanage_info_df.itertuples(): 42 vmanage_host = row[1] 43 vmanage_port = row[2] 44 vmanage_username = row[3] 45 vmanage_password = row[4] 46 47 base_url = `https://%s:%s`%(vmanage_host, vmanage_port) 48 49 login_action = `/j_security_check` 50 51 login_data = {`j_username` : vmanage_username, `j_password` : vmanage_password} 52 53 login_url = base_url + login_action 54 55 session = requests.session() 56 57 pring("debug3") 58 59 login_response = session.post(url=login_url, data=login_data, verify=False) 60 if b`<html>` in login_response.content: 61 print("Login token Failed") 62 exit(1) 63 64 xsrf_token_url = base_url + '/dataservice/client/token' 65 66 login_token = session.get(url=xsrf_token_url, verify=False) 67 if login_token.status_code == 200: 68 if b`<html>` in login_token.content: 69 print("Login token Failed") 70 exit(1) 71 72 session.headers[`X-XSRF-TOKEN`] = login_token.content 73 74 75 device_url = base_url + `/dataservice/device` 76 print("device_url") 77 print(device_url) 78 79 device_list = session.get(url=device_url, verify=False) 80 if device_list.status_code = 200: 81 json_data = device_list.json() 82 else: 83 print (device_list.status_code) 84 85 df = pd.json_normalize(json_data["data"] 86 df = df[(df["device-type"] == "vedge") & (df["reachability"] == "reachable")] 87 88 print("filter_df") 89 print(df) 90 91 df = df[["deviceId", "system-ip", "host-name", "device-model", "bfdSessionsUp", "bfdSessions"]] 92 93 for data in df.itertuples(): 94 System_ip = data[1] 95 query_data = ["query":(略),["value":[System_ip],(略)"type":"sum"}]}} 96 print("query_data") 97 print(query_data) 98 99 device_url = base_url + '/dataservice/statistics/approute/fec/aggregation' 100 #print("device_url") 101 #print(device_url) 102 103 device_list = session.post(url=device_url, json=query_data, headers=headers_post, verify=False) 104 print("device_list") 105 print(device_list) 106 107 if device_list.status_code == 200: 108 print (device_list.status_code) 109 json_data = device_list.json() 110 111 df2 = pd.json_normalize(json_data["data"]) 112 113 #先頭にsystemip 114 df2.insert(0, 'System_ip', data[1]) 115 #末尾にdatetime 116 df2['dt'] = datetime.now(),strftime('%Y%m%d% %H:%M:%S') 117 c = df2.pop('dt') 118 df2.insert(df2.shape[1], c.name, c) 119 120 print("df2") 121 print(df2) 122 123 else: 124 print(device_list.status_code) 125 126 127 return df
試したこと
forやdfなどの位置を組み替えてデバッグ中なのですが、
いずれも末尾のデバイス結果しか出力されず
理想 以下のdf結果において、デバイス分出力したい
DeviceList
1deviceId system_ip・・・bdfSessionsUp bfdSessions 21.1.1.1 1.1.1.1 ・・・ 8 8 31.1.1.2 1.1.1.2 ・・・ 8 8 41.1.1.3 1.1.1.3 ・・・ 8 8 51.1.1.4 1.1.1.4 ・・・ 8 8
想定結果
Result
1System_ip loss_percentage ・・・・ vqoe_score dt 21.1.1.1 99.99・・・・5 2023/01/01 01:01:01 31.1.1.1 99.98・・・・5 2023/01/01 01:01:01 41.1.1.1 99.97・・・・5 2023/01/01 01:01:01 51.1.1.1 99.96・・・・5 2023/01/01 01:01:01 6System_ip loss_percentage ・・・・ vqoe_score dt 71.1.1.2 99.99・・・・5 2023/01/01 01:01:01 81.1.1.2 99.98・・・・5 2023/01/01 01:01:01 91.1.1.2 99.97・・・・5 2023/01/01 01:01:01 101.1.1.2 99.96・・・・5 2023/01/01 01:01:01 11System_ip loss_percentage ・・・・ vqoe_score dt 121.1.1.3 99.99・・・・5 2023/01/01 01:01:01 131.1.1.3 99.98・・・・5 2023/01/01 01:01:01 141.1.1.3 99.97・・・・5 2023/01/01 01:01:01 151.1.1.3 99.96・・・・5 2023/01/01 01:01:01 16System_ip loss_percentage ・・・・ vqoe_score dt 171.1.1.4 99.99・・・・5 2023/01/01 01:01:01 181.1.1.4 99.98・・・・5 2023/01/01 01:01:01 191.1.1.4 99.97・・・・5 2023/01/01 01:01:01 201.1.1.4 99.96・・・・5 2023/01/01 01:01:01
補足情報(FW/ツールのバージョンなど)
Python3.9
ここにより詳細な情報を記載してください。
回答動作確認コード
文字数制限のため追記箇所のみ記載
・・・・(略)・・・・ dfs = [] def lambda_handler(event, context): ・・・・(略)・・・・ print("df2") print(df2) dfs.append(df2) ・・・・(略)・・・・ df_con = pd.concat(dfs).reset_index(drop=True) print(df_con) return df_con
Result
1System_ip loss_percentage ・・・・ vqoe_score dt 21.1.1.1 99.99・・・・5 2023/01/01 01:01:01 31.1.1.1 99.98・・・・5 2023/01/01 01:01:01 41.1.1.1 99.97・・・・5 2023/01/01 01:01:01 51.1.1.1 99.96・・・・5 2023/01/01 01:01:01 6・・・・(略)・・・・ 71.1.1.4 99.99・・・・5 2023/01/01 01:01:01 81.1.1.4 99.98・・・・5 2023/01/01 01:01:01 91.1.1.4 99.97・・・・5 2023/01/01 01:01:01 101.1.1.4 99.96・・・・5 2023/01/01 01:01:01

回答1件
あなたの回答
tips
プレビュー