teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

8

回答結果を反映

2023/02/10 09:42

投稿

msy47
msy47

スコア26

title CHANGED
File without changes
body CHANGED
@@ -186,110 +186,99 @@
186
186
  Tunnel_level_quality_analysis_df.to_csv(tmp_csv, encoding='utf-8', index=False)
187
187
 
188
188
  s3 = boto3.resource('s3')
189
- s3.meta.client.upload_file(tmp_csv 'utlink-vmanage-bucket', 'csv/Tunnel_level_quality_analysis.csv')
189
+ s3.meta.client.upload_file(tmp_csvm 'utlink-vmanage-bucket', 'csv/Tunnel_level_quality_analysis.csv')
190
190
 
191
191
  print("debug2")
192
192
 
193
193
  def get_request(vmanage_info_df):
194
- dfs = []
194
+ #api_datalist = []
195
-
196
- for i in range(13):
197
-
198
- for row in vmanage_info_df.itertuples():
195
+ for row in vmanage_info_df.itertuples():
199
- vmanage_host = row[1]
196
+ vmanage_host = row[1]
200
- vmanage_port = row[2]
197
+ vmanage_port = row[2]
201
- vmanage_username = row[3]
198
+ vmanage_username = row[3]
202
- vmanage_password = row[4]
199
+ vmanage_password = row[4]
200
+
201
+ base_url = `https://%s:%s`%(vmanage_host, vmanage_port)
202
+
203
+ login_action = `/j_security_check`
204
+
205
+ login_data = {`j_username` : vmanage_username, `j_password` : vmanage_password}
206
+
207
+ login_url = base_url + login_action
208
+
209
+ session = requests.session()
210
+
211
+ pring("debug3")
212
+
213
+ login_response = session.post(url=login_url, data=login_data, verify=False)
214
+ if b`<html>` in login_response.content:
215
+ print("Login token Failed")
216
+ exit(1)
203
217
 
204
- base_url = `https://%s:%s`%(vmanage_host, vmanage_port)
218
+ xsrf_token_url = base_url + '/dataservice/client/token'
205
219
 
220
+ login_token = session.get(url=xsrf_token_url, verify=False)
206
- login_action = `/j_security_check`
221
+ if login_token.status_code == 200:
222
+ if b`<html>` in login_token.content:
223
+ print("Login token Failed")
224
+ exit(1)
225
+
226
+ session.headers[`X-XSRF-TOKEN`] = login_token.content
227
+
207
228
 
208
- login_data = {`j_username` : vmanage_username, `j_password` : vmanage_password}
229
+ device_url = base_url + `/dataservice/device`
230
+ print("device_url")
231
+ print(device_url)
209
232
 
233
+ device_list = session.get(url=device_url, verify=False)
234
+ if device_list.status_code = 200:
210
- login_url = base_url + login_action
235
+ json_data = device_list.json()
236
+ else:
237
+ print (device_list.status_code)
211
238
 
212
- session = requests.session()
239
+ df = pd.json_normalize(json_data["data"]
240
+ df = df[(df["device-type"] == "vedge") & (df["reachability"] == "reachable")]
213
241
 
214
- pring("debug3")
242
+ print("filter_df")
243
+ print(df)
215
244
 
216
- login_response = session.post(url=login_url, data=login_data, verify=False)
245
+ df = df[["deviceId", "system-ip", "host-name", "device-model", "bfdSessionsUp", "bfdSessions"]]
246
+
217
- if b`<html>` in login_response.content:
247
+ for data in df.itertuples():
248
+ System_ip = data[1]
249
+ query_data = ["query":(略),["value":[System_ip],(略)"type":"sum"}]}}
218
- print("Login token Failed")
250
+ print("query_data")
219
- exit(1)
251
+ print(query_data)
220
252
 
221
- xsrf_token_url = base_url + '/dataservice/client/token'
253
+ device_url = base_url + '/dataservice/statistics/approute/fec/aggregation'
254
+ #print("device_url")
255
+ #print(device_url)
222
256
 
223
- login_token = session.get(url=xsrf_token_url, verify=False)
257
+ device_list = session.post(url=device_url, json=query_data, headers=headers_post, verify=False)
224
- if login_token.status_code = 200:
225
- if b`<html>` in login_token.content:
226
- print("Login token Failed")
258
+ print("device_list")
227
- exit(1)
259
+ print(device_list)
228
-
229
- session.headers[`X-XSRF-TOKEN`] = login_token.content
230
-
231
260
 
232
- device_url = base_url + `/dataservice/device`
233
- print("device_url")
234
- print(device_url)
235
-
236
- device_list = session.get(url=device_url, verify=False)
237
- if device_list.status_code = 200:
261
+ if device_list.status_code == 200:
238
- json_data = vdevice_list.json()
239
- else:
240
262
  print (device_list.status_code)
241
-
242
- df = pd.json_normalize(json_data["data"]
243
- df = df[(df["device-type"] == "vedge") & (df["reachability"] == "reachable")]
244
-
245
- print("filter_df")
246
- print(df)
247
-
248
- df = df[["deviceId", "system-ip", "host-name", "device-model", "bfdSessionsUp", "bfdSessions"]]
249
-
250
- for data in df.itertuples():
263
+ json_data = device_list.json()
251
- System_ip = data[1]
252
- query_data = ["query":(略),["value":[System_ip],(略)"type":"sum"}]}}
253
- print("query_data")
254
- print(query_data)
255
264
 
256
- device_url = base_url + '/dataservice/statistics/approute/fec/aggregation'
257
- print("device_url")
265
+ df2 = pd.json_normalize(json_data["data"])
258
- print(device_url)
259
266
 
267
+ #先頭にsystemip
268
+ df2.insert(0, 'System_ip', data[1])
269
+ #末尾にdatetime
260
- device_list = session.post(url=device_url, json=query_data, headers=headers_post, verify=False)
270
+ df2['dt'] = datetime.now(),strftime('%Y%m%d% %H:%M:%S')
261
- print("device_list")
271
+ c = df2.pop('dt')
262
- print(device_list)
272
+ df2.insert(df2.shape[1], c.name, c)
263
273
 
264
- if device_list.status_code == 200:
265
- json_data = device_list.json()
266
-
267
- df2 = pd.json_normalize(json_data["data"])
268
-
269
- #先頭にsystemip
270
- df2.insert(0, 'System_ip', data[1])
271
- #末尾にdatetime
272
- df2['df'] = datetime.now(),strftime('%Y%m%d% %H:%M:%S')
273
- c = df2.pop('dt')
274
- df.insert(df2.shape[1], c.name, c)
275
-
276
- print("df2")
274
+ print("df2")
277
- print(df2)
275
+ print(df2)
278
- df2 = pd.DataFrame({'v1' :[i, i*10]})
279
- dfs.append(df2)
280
-
281
- else:
282
- print(device_list.status_code)
283
276
 
277
+ else:
284
- print("df_intertuples")
278
+ print(device_list.status_code)
285
- print(df)
279
+
286
-
287
- return df
288
280
 
289
- df_ret = pd.concat(dfs).rest_index(drop=True)
290
- return df_ret
281
+ return df
291
- #df_ret = get_request(vmanage_info_df)
292
- #print(df_ret)
293
282
  ```
294
283
 
295
284
 
@@ -334,3 +323,34 @@
334
323
  ### 補足情報(FW/ツールのバージョンなど)
335
324
  Python3.9
336
325
  ここにより詳細な情報を記載してください。
326
+
327
+ ### 回答動作確認コード
328
+ 文字数制限のため追記箇所のみ記載
329
+ ```
330
+ ・・・・(略)・・・・
331
+ dfs = []
332
+ def lambda_handler(event, context):
333
+ ・・・・(略)・・・・
334
+ print("df2")
335
+ print(df2)
336
+ dfs.append(df2)
337
+ ・・・・(略)・・・・
338
+ df_con = pd.concat(dfs).reset_index(drop=True)
339
+ print(df_con)
340
+ return df_con
341
+ ```
342
+
343
+ ```Result
344
+ System_ip loss_percentage ・・・・ vqoe_score dt
345
+ 1.1.1.1 99.99・・・・5 2023/01/01 01:01:01
346
+ 1.1.1.1 99.98・・・・5 2023/01/01 01:01:01
347
+ 1.1.1.1 99.97・・・・5 2023/01/01 01:01:01
348
+ 1.1.1.1 99.96・・・・5 2023/01/01 01:01:01
349
+ ・・・・(略)・・・・
350
+ 1.1.1.4 99.99・・・・5 2023/01/01 01:01:01
351
+ 1.1.1.4 99.98・・・・5 2023/01/01 01:01:01
352
+ 1.1.1.4 99.97・・・・5 2023/01/01 01:01:01
353
+ 1.1.1.4 99.96・・・・5 2023/01/01 01:01:01
354
+ ```
355
+
356
+ ```

7

ソース修正

2023/02/10 08:08

投稿

msy47
msy47

スコア26

title CHANGED
File without changes
body CHANGED
@@ -275,7 +275,7 @@
275
275
 
276
276
  print("df2")
277
277
  print(df2)
278
- df2 = pd.dataFrame({'v1' :[i, i*10]})
278
+ df2 = pd.DataFrame({'v1' :[i, i*10]})
279
279
  dfs.append(df2)
280
280
 
281
281
  else:

6

該当のソースコード(df2はprintで確認_回答反映

2023/02/10 06:30

投稿

msy47
msy47

スコア26

title CHANGED
File without changes
body CHANGED
@@ -149,7 +149,7 @@
149
149
  1.1.1.4 1.1.1.4 ・・・ 8 8
150
150
  ```
151
151
 
152
- ### 該当のソースコード(df2はprintで確認
152
+ ### 該当のソースコード(df2はprintで確認_回答反映
153
153
 
154
154
  ```Python
155
155
  import json
@@ -186,99 +186,110 @@
186
186
  Tunnel_level_quality_analysis_df.to_csv(tmp_csv, encoding='utf-8', index=False)
187
187
 
188
188
  s3 = boto3.resource('s3')
189
- s3.meta.client.upload_file(tmp_csvm 'utlink-vmanage-bucket', 'csv/Tunnel_level_quality_analysis.csv')
189
+ s3.meta.client.upload_file(tmp_csv 'utlink-vmanage-bucket', 'csv/Tunnel_level_quality_analysis.csv')
190
190
 
191
191
  print("debug2")
192
192
 
193
193
  def get_request(vmanage_info_df):
194
- #api_datalist = []
194
+ dfs = []
195
+
196
+ for i in range(13):
197
+
195
- for row in vmanage_info_df.itertuples():
198
+ for row in vmanage_info_df.itertuples():
196
- vmanage_host = row[1]
199
+ vmanage_host = row[1]
197
- vmanage_port = row[2]
200
+ vmanage_port = row[2]
198
- vmanage_username = row[3]
201
+ vmanage_username = row[3]
199
- vmanage_password = row[4]
202
+ vmanage_password = row[4]
200
-
201
- base_url = `https://%s:%s`%(vmanage_host, vmanage_port)
202
-
203
- login_action = `/j_security_check`
204
-
205
- login_data = {`j_username` : vmanage_username, `j_password` : vmanage_password}
206
-
207
- login_url = base_url + login_action
208
-
209
- session = requests.session()
210
-
211
- pring("debug3")
212
-
213
- login_response = session.post(url=login_url, data=login_data, verify=False)
214
- if b`<html>` in login_response.content:
215
- print("Login token Failed")
216
- exit(1)
217
203
 
218
- xsrf_token_url = base_url + '/dataservice/client/token'
204
+ base_url = `https://%s:%s`%(vmanage_host, vmanage_port)
219
205
 
220
- login_token = session.get(url=xsrf_token_url, verify=False)
221
- if login_token.status_code == 200:
206
+ login_action = `/j_security_check`
222
- if b`<html>` in login_token.content:
223
- print("Login token Failed")
224
- exit(1)
225
-
226
- session.headers[`X-XSRF-TOKEN`] = login_token.content
227
-
228
207
 
229
- device_url = base_url + `/dataservice/device`
208
+ login_data = {`j_username` : vmanage_username, `j_password` : vmanage_password}
230
- print("device_url")
231
- print(device_url)
232
209
 
233
- device_list = session.get(url=device_url, verify=False)
234
- if device_list.status_code = 200:
235
- json_data = device_list.json()
210
+ login_url = base_url + login_action
236
- else:
237
- print (device_list.status_code)
238
211
 
239
- df = pd.json_normalize(json_data["data"]
212
+ session = requests.session()
240
- df = df[(df["device-type"] == "vedge") & (df["reachability"] == "reachable")]
241
213
 
242
- print("filter_df")
214
+ pring("debug3")
243
- print(df)
244
215
 
245
- df = df[["deviceId", "system-ip", "host-name", "device-model", "bfdSessionsUp", "bfdSessions"]]
216
+ login_response = session.post(url=login_url, data=login_data, verify=False)
246
-
247
- for data in df.itertuples():
217
+ if b`<html>` in login_response.content:
248
- System_ip = data[1]
249
- query_data = ["query":(略),["value":[System_ip],(略)"type":"sum"}]}}
250
- print("query_data")
218
+ print("Login token Failed")
251
- print(query_data)
219
+ exit(1)
252
220
 
253
- device_url = base_url + '/dataservice/statistics/approute/fec/aggregation'
221
+ xsrf_token_url = base_url + '/dataservice/client/token'
254
- #print("device_url")
255
- #print(device_url)
256
222
 
257
- device_list = session.post(url=device_url, json=query_data, headers=headers_post, verify=False)
223
+ login_token = session.get(url=xsrf_token_url, verify=False)
224
+ if login_token.status_code = 200:
225
+ if b`<html>` in login_token.content:
258
- print("device_list")
226
+ print("Login token Failed")
259
- print(device_list)
227
+ exit(1)
228
+
229
+ session.headers[`X-XSRF-TOKEN`] = login_token.content
230
+
260
231
 
232
+ device_url = base_url + `/dataservice/device`
233
+ print("device_url")
234
+ print(device_url)
235
+
236
+ device_list = session.get(url=device_url, verify=False)
261
- if device_list.status_code == 200:
237
+ if device_list.status_code = 200:
238
+ json_data = vdevice_list.json()
239
+ else:
262
240
  print (device_list.status_code)
241
+
242
+ df = pd.json_normalize(json_data["data"]
243
+ df = df[(df["device-type"] == "vedge") & (df["reachability"] == "reachable")]
244
+
245
+ print("filter_df")
246
+ print(df)
247
+
248
+ df = df[["deviceId", "system-ip", "host-name", "device-model", "bfdSessionsUp", "bfdSessions"]]
249
+
263
- json_data = device_list.json()
250
+ for data in df.itertuples():
251
+ System_ip = data[1]
252
+ query_data = ["query":(略),["value":[System_ip],(略)"type":"sum"}]}}
253
+ print("query_data")
254
+ print(query_data)
264
255
 
256
+ device_url = base_url + '/dataservice/statistics/approute/fec/aggregation'
265
- df2 = pd.json_normalize(json_data["data"])
257
+ print("device_url")
258
+ print(device_url)
266
259
 
267
- #先頭にsystemip
268
- df2.insert(0, 'System_ip', data[1])
269
- #末尾にdatetime
270
- df2['dt'] = datetime.now(),strftime('%Y%m%d% %H:%M:%S')
260
+ device_list = session.post(url=device_url, json=query_data, headers=headers_post, verify=False)
271
- c = df2.pop('dt')
261
+ print("device_list")
272
- df2.insert(df2.shape[1], c.name, c)
262
+ print(device_list)
273
263
 
264
+ if device_list.status_code == 200:
265
+ json_data = device_list.json()
266
+
267
+ df2 = pd.json_normalize(json_data["data"])
268
+
269
+ #先頭にsystemip
270
+ df2.insert(0, 'System_ip', data[1])
271
+ #末尾にdatetime
272
+ df2['df'] = datetime.now(),strftime('%Y%m%d% %H:%M:%S')
273
+ c = df2.pop('dt')
274
+ df.insert(df2.shape[1], c.name, c)
275
+
274
- print("df2")
276
+ print("df2")
275
- print(df2)
277
+ print(df2)
278
+ df2 = pd.dataFrame({'v1' :[i, i*10]})
279
+ dfs.append(df2)
280
+
281
+ else:
282
+ print(device_list.status_code)
276
283
 
277
- else:
278
- print(device_list.status_code)
284
+ print("df_intertuples")
279
-
285
+ print(df)
286
+
287
+ return df
280
288
 
289
+ df_ret = pd.concat(dfs).rest_index(drop=True)
281
- return df
290
+ return df_ret
291
+ #df_ret = get_request(vmanage_info_df)
292
+ #print(df_ret)
282
293
  ```
283
294
 
284
295
 

5

ソースコードを修正

2023/02/10 06:14

投稿

msy47
msy47

スコア26

title CHANGED
File without changes
body CHANGED
@@ -149,9 +149,9 @@
149
149
  1.1.1.4 1.1.1.4 ・・・ 8 8
150
150
  ```
151
151
 
152
- ### 該当のソースコード(回答を反映
152
+ ### 該当のソースコード(df2はprintで確認
153
153
 
154
- ```
154
+ ```Python
155
155
  import json
156
156
  import boto3
157
157
  import csv
@@ -170,7 +170,7 @@
170
170
 
171
171
  def lambda_handler(event, context):
172
172
  s3 = boto3.resource(`s3`)
173
- bucket_name _ os.environ.get(`S3_BUCKET`)
173
+ bucket_name = os.environ.get(`S3_BUCKET`)
174
174
 
175
175
  vmanage_info = "tmp/vmanage_info.csv"
176
176
  bucket.download_file(os.environ.get(`CSV_VMANAGE_INFO`), vmanage_info)
@@ -186,111 +186,99 @@
186
186
  Tunnel_level_quality_analysis_df.to_csv(tmp_csv, encoding='utf-8', index=False)
187
187
 
188
188
  s3 = boto3.resource('s3')
189
- s3.meta.client.upload_file(tmp_csv 'utlink-vmanage-bucket', 'csv/Tunnel_level_quality_analysis.csv')
189
+ s3.meta.client.upload_file(tmp_csvm 'utlink-vmanage-bucket', 'csv/Tunnel_level_quality_analysis.csv')
190
190
 
191
191
  print("debug2")
192
192
 
193
193
  def get_request(vmanage_info_df):
194
- dfs = []
195
194
  #api_datalist = []
196
-
197
- for i in range(13):
198
-
199
- for row in vmanage_info_df.itertuples():
195
+ for row in vmanage_info_df.itertuples():
200
- vmanage_host = row[1]
196
+ vmanage_host = row[1]
201
- vmanage_port = row[2]
197
+ vmanage_port = row[2]
202
- vmanage_username = row[3]
198
+ vmanage_username = row[3]
203
- vmanage_password = row[4]
199
+ vmanage_password = row[4]
200
+
201
+ base_url = `https://%s:%s`%(vmanage_host, vmanage_port)
202
+
203
+ login_action = `/j_security_check`
204
+
205
+ login_data = {`j_username` : vmanage_username, `j_password` : vmanage_password}
206
+
207
+ login_url = base_url + login_action
208
+
209
+ session = requests.session()
210
+
211
+ pring("debug3")
212
+
213
+ login_response = session.post(url=login_url, data=login_data, verify=False)
214
+ if b`<html>` in login_response.content:
215
+ print("Login token Failed")
216
+ exit(1)
204
217
 
205
- base_url = `https://%s:%s`%(vmanage_host, vmanage_port)
218
+ xsrf_token_url = base_url + '/dataservice/client/token'
206
219
 
220
+ login_token = session.get(url=xsrf_token_url, verify=False)
207
- login_action = `/j_security_check`
221
+ if login_token.status_code == 200:
222
+ if b`<html>` in login_token.content:
223
+ print("Login token Failed")
224
+ exit(1)
225
+
226
+ session.headers[`X-XSRF-TOKEN`] = login_token.content
227
+
208
228
 
209
- login_data = {`j_username` : vmanage_username, `j_password` : vmanage_password}
229
+ device_url = base_url + `/dataservice/device`
230
+ print("device_url")
231
+ print(device_url)
210
232
 
233
+ device_list = session.get(url=device_url, verify=False)
234
+ if device_list.status_code = 200:
211
- login_url = base_url + login_action
235
+ json_data = device_list.json()
236
+ else:
237
+ print (device_list.status_code)
212
238
 
213
- session = requests.session()
239
+ df = pd.json_normalize(json_data["data"]
240
+ df = df[(df["device-type"] == "vedge") & (df["reachability"] == "reachable")]
214
241
 
215
- pring("debug3")
242
+ print("filter_df")
243
+ print(df)
216
244
 
217
- login_response = session.post(url=login_url, data=login_data, verify=False)
245
+ df = df[["deviceId", "system-ip", "host-name", "device-model", "bfdSessionsUp", "bfdSessions"]]
246
+
218
- if b`<html>` in login_response.content:
247
+ for data in df.itertuples():
248
+ System_ip = data[1]
249
+ query_data = ["query":(略),["value":[System_ip],(略)"type":"sum"}]}}
219
- print("Login token Failed")
250
+ print("query_data")
220
- exit(1)
251
+ print(query_data)
221
252
 
222
- xsrf_token_url = base_url + '/dataservice/client/token'
253
+ device_url = base_url + '/dataservice/statistics/approute/fec/aggregation'
254
+ #print("device_url")
255
+ #print(device_url)
223
256
 
224
- login_token = session.get(url=xsrf_token_url, verify=False)
257
+ device_list = session.post(url=device_url, json=query_data, headers=headers_post, verify=False)
225
- if login_token.status_code = 200:
226
- if b`<html>` in login_token.content:
227
- print("Login token Failed")
258
+ print("device_list")
228
- exit(1)
259
+ print(device_list)
229
-
230
- session.headers[`X-XSRF-TOKEN`] = login_token.content
231
-
232
260
 
233
- device_url = base_url + `/dataservice/device`
234
- print("device_url")
235
- print(device_url)
236
-
237
- device_list = session.get(url=device_url, verify=False)
238
- if device_list.status_code = 200:
261
+ if device_list.status_code == 200:
239
- json_data = vdevice_list.json()
240
- else:
241
262
  print (device_list.status_code)
242
-
243
- df = pd.json_normalize(json_data["data"]
244
- df = df[(df["device-type"] == "vedge") & (df["reachability"] == "reachable")]
245
-
246
- print("filter_df")
247
- print(df)
248
-
249
- df = df[["deviceId", "system-ip", "host-name", "device-model", "bfdSessionsUp", "bfdSessions"]]
250
-
251
- for data in df.itertuples():
263
+ json_data = device_list.json()
252
- System_ip = data[1]
253
- query_data = ["query":(略),["value":[System_ip],(略)"type":"sum"}]}}
254
- print("query_data")
255
- print(query_data)
256
264
 
257
- device_url = base_url + '/dataservice/statistics/approute/fec/aggregation'
258
- print("device_url")
265
+ df2 = pd.json_normalize(json_data["data"])
259
- print(device_url)
260
266
 
267
+ #先頭にsystemip
268
+ df2.insert(0, 'System_ip', data[1])
269
+ #末尾にdatetime
261
- device_list = session.post(url=device_url, json=query_data, headers=headers_post, verify=False)
270
+ df2['dt'] = datetime.now(),strftime('%Y%m%d% %H:%M:%S')
262
- print("device_list")
271
+ c = df2.pop('dt')
263
- print(device_list)
272
+ df2.insert(df2.shape[1], c.name, c)
264
273
 
265
- if device_list.status_code == 200:
266
- json_data = device_list.json()
267
-
268
- df2 = pd.json_normalize(json_data["data"])
269
-
270
- #先頭にsystemip
271
- df2.insert(0, 'System_ip', data[1])
272
- #末尾にdatetime
273
- df2['df'] = datetime.now(),strftime('%Y%m%d% %H:%M:%S')
274
- c = df2.pop('dt')
275
- df.insert(df2.shape[1], c.name, c)
276
-
277
- print("df2")
274
+ print("df2")
278
- print(df2)
275
+ print(df2)
279
- #df2 = pd.dataFrame({'v1' :[i, i*10]})
280
- dfs.append(df2)
281
-
282
- else:
283
- print(device_list.status_code)
284
276
 
277
+ else:
285
- print("df_intertuples")
278
+ print(device_list.status_code)
286
- print(df)
279
+
287
-
288
- return df
289
280
 
290
- df_ret = pd.concat(dfs).rest_index(drop=True)
291
- return df_ret
281
+ return df
292
- df_ret = get_request(vmanage_info_df)
293
- print(df_ret)
294
282
  ```
295
283
 
296
284
 

4

range修正

2023/02/10 01:55

投稿

msy47
msy47

スコア26

title CHANGED
File without changes
body CHANGED
@@ -194,7 +194,7 @@
194
194
  dfs = []
195
195
  #api_datalist = []
196
196
 
197
- for i in range(3):
197
+ for i in range(13):
198
198
 
199
199
  for row in vmanage_info_df.itertuples():
200
200
  vmanage_host = row[1]

3

回答を反映したソースを追記

2023/02/10 01:38

投稿

msy47
msy47

スコア26

title CHANGED
File without changes
body CHANGED
@@ -61,9 +61,6 @@
61
61
  1.1.1.4 99.96・・・・5 2023/01/01 01:01:01
62
62
  ```
63
63
 
64
-
65
-
66
-
67
64
  ### 発生している問題・エラーメッセージ
68
65
 
69
66
  ```log
@@ -152,9 +149,9 @@
152
149
  1.1.1.4 1.1.1.4 ・・・ 8 8
153
150
  ```
154
151
 
155
- ### 該当のソースコード
152
+ ### 該当のソースコード(回答を反映
156
153
 
157
- ```Python
154
+ ```
158
155
  import json
159
156
  import boto3
160
157
  import csv
@@ -189,102 +186,114 @@
189
186
  Tunnel_level_quality_analysis_df.to_csv(tmp_csv, encoding='utf-8', index=False)
190
187
 
191
188
  s3 = boto3.resource('s3')
192
- s3.meta.client.upload_file(tmp_csvm 'utlink-vmanage-bucket', 'csv/Tunnel_level_quality_analysis.csv')
189
+ s3.meta.client.upload_file(tmp_csv 'utlink-vmanage-bucket', 'csv/Tunnel_level_quality_analysis.csv')
193
190
 
194
191
  print("debug2")
195
192
 
196
193
  def get_request(vmanage_info_df):
194
+ dfs = []
197
- api_datalist = []
195
+ #api_datalist = []
196
+
197
+ for i in range(3):
198
+
198
- for row in vmanage_info_df.itertuples():
199
+ for row in vmanage_info_df.itertuples():
199
- vmanage_host = row[1]
200
+ vmanage_host = row[1]
200
- vmanage_port = row[2]
201
+ vmanage_port = row[2]
201
- vmanage_username = row[3]
202
+ vmanage_username = row[3]
202
- vmanage_password = row[4]
203
+ vmanage_password = row[4]
203
-
204
- base_url = `https://%s:%s`%(vmanage_host, vmanage_port)
205
-
206
- login_action = `/j_security_check`
207
-
208
- login_data = {`j_username` : vmanage_username, `j_password` : vmanage_password}
209
-
210
- login_url = base_url + login_action
211
-
212
- session = requests.session()
213
-
214
- pring("debug3")
215
-
216
- login_response = session.post(url=login_url, data=login_data, verify=False)
217
- if b`<html>` in login_response.content:
218
- print("Login token Failed")
219
- exit(1)
220
204
 
221
- xsrf_token_url = base_url + '/dataservice/client/token'
205
+ base_url = `https://%s:%s`%(vmanage_host, vmanage_port)
222
206
 
223
- login_token = session.get(url=xsrf_token_url, verify=False)
224
- if login_token.status_code = 200:
207
+ login_action = `/j_security_check`
225
- if b`<html>` in login_token.content:
226
- print("Login token Failed")
227
- exit(1)
228
-
229
- session.headers[`X-XSRF-TOKEN`] = login_token.content
230
-
231
208
 
232
- device_url = base_url + `/dataservice/device`
209
+ login_data = {`j_username` : vmanage_username, `j_password` : vmanage_password}
233
- print("device_url")
234
- print(device_url)
235
210
 
236
- device_list = session.get(url=device_url, verify=False)
237
- if device_list.status_code = 200:
238
- json_data = vdevice_list.json()
211
+ login_url = base_url + login_action
239
- else:
240
- print (device_list.status_code)
241
212
 
242
- df = pd.json_normalize(json_data["data"]
213
+ session = requests.session()
243
- df = df[(df["device-type"] == "vedge") & (df["reachability"] == "reachable")]
244
214
 
245
- print("filter_df")
215
+ pring("debug3")
246
- print(df)
247
216
 
248
- df = df[["deviceId", "system-ip", "host-name", "device-model", "bfdSessionsUp", "bfdSessions"]]
217
+ login_response = session.post(url=login_url, data=login_data, verify=False)
249
-
250
- for data in df.itertuples():
218
+ if b`<html>` in login_response.content:
251
- System_ip = data[1]
252
- query_data = ["query":(略),["value":[System_ip],(略)"type":"sum"}]}}
253
- print("query_data")
219
+ print("Login token Failed")
254
- print(query_data)
220
+ exit(1)
255
221
 
256
- device_url = base_url + '/dataservice/statistics/approute/fec/aggregation'
222
+ xsrf_token_url = base_url + '/dataservice/client/token'
223
+
224
+ login_token = session.get(url=xsrf_token_url, verify=False)
225
+ if login_token.status_code = 200:
226
+ if b`<html>` in login_token.content:
227
+ print("Login token Failed")
228
+ exit(1)
229
+
230
+ session.headers[`X-XSRF-TOKEN`] = login_token.content
231
+
232
+
233
+ device_url = base_url + `/dataservice/device`
257
234
  print("device_url")
258
235
  print(device_url)
259
236
 
260
- device_list = session.post(url=device_url, json=query_data, headers=headers_post, verify=False)
237
+ device_list = session.get(url=device_url, verify=False)
238
+ if device_list.status_code = 200:
261
- print("device_list")
239
+ json_data = vdevice_list.json()
240
+ else:
262
- print(device_list)
241
+ print (device_list.status_code)
263
242
 
264
- if device_list.status_code == 200:
243
+ df = pd.json_normalize(json_data["data"]
244
+ df = df[(df["device-type"] == "vedge") & (df["reachability"] == "reachable")]
245
+
246
+ print("filter_df")
247
+ print(df)
248
+
249
+ df = df[["deviceId", "system-ip", "host-name", "device-model", "bfdSessionsUp", "bfdSessions"]]
250
+
265
- json_data = device_list.json()
251
+ for data in df.itertuples():
252
+ System_ip = data[1]
253
+ query_data = ["query":(略),["value":[System_ip],(略)"type":"sum"}]}}
254
+ print("query_data")
255
+ print(query_data)
266
256
 
257
+ device_url = base_url + '/dataservice/statistics/approute/fec/aggregation'
267
- df2 = pd.json_normalize(json_data["data"])
258
+ print("device_url")
259
+ print(device_url)
268
260
 
269
- #先頭にsystemip
270
- df2.insert(0, 'System_ip', data[1])
271
- #末尾にdatetime
272
- df2['df'] = datetime.now(),strftime('%Y%m%d% %H:%M:%S')
261
+ device_list = session.post(url=device_url, json=query_data, headers=headers_post, verify=False)
273
- c = df2.pop('dt')
262
+ print("device_list")
274
- df.insert(df2.shape[1], c.name, c)
263
+ print(device_list)
275
264
 
265
+ if device_list.status_code == 200:
266
+ json_data = device_list.json()
267
+
268
+ df2 = pd.json_normalize(json_data["data"])
269
+
270
+ #先頭にsystemip
271
+ df2.insert(0, 'System_ip', data[1])
272
+ #末尾にdatetime
273
+ df2['df'] = datetime.now(),strftime('%Y%m%d% %H:%M:%S')
274
+ c = df2.pop('dt')
275
+ df.insert(df2.shape[1], c.name, c)
276
+
276
- print("df2")
277
+ print("df2")
277
- print(df2)
278
+ print(df2)
279
+ #df2 = pd.dataFrame({'v1' :[i, i*10]})
280
+ dfs.append(df2)
281
+
282
+ else:
283
+ print(device_list.status_code)
278
284
 
279
- else:
280
- print(device_list.status_code)
281
-
282
- print("df_itertuples")
285
+ print("df_intertuples")
283
- print(df)
286
+ print(df)
287
+
288
+ return df
284
289
 
290
+ df_ret = pd.concat(dfs).rest_index(drop=True)
285
- return df
291
+ return df_ret
292
+ df_ret = get_request(vmanage_info_df)
293
+ print(df_ret)
286
294
  ```
287
295
 
296
+
288
297
  ### 試したこと
289
298
 
290
299
  forやdfなどの位置を組み替えてデバッグ中なのですが、

2

タイトルを変更

2023/02/09 07:41

投稿

msy47
msy47

スコア26

title CHANGED
@@ -1,1 +1,1 @@
1
- Python APIの結果を次のAPIで読み込み結果をdataframeにすべて入れることができない
1
+ Python APIの結果をdataframeを使ってget_request()にすべての結果を入れることができない
body CHANGED
File without changes

1

指摘により修正し、再度検証した結果、次の問題について修正・追記

2023/02/09 05:12

投稿

msy47
msy47

スコア26

title CHANGED
File without changes
body CHANGED
@@ -15,7 +15,9 @@
15
15
  1.1.1.4
16
16
  ```
17
17
 
18
- 例として、2つ目のAPI結果では1デバイスに複数のTunnel結果が存在す
18
+ 例として、2つ目のAPI結果では1デバイスに複数のTunnel結果が出力され
19
+ 1.1.1.1以下に存在する複数のトンネル(abc, edf, ghi, jki...)
20
+
19
21
  ```example
20
22
  deviceId debice loss latency
21
23
  1.1.1.1 abc 999 999
@@ -23,10 +25,45 @@
23
25
  1.1.1.1 ghi 999 999
24
26
  1.1.1.1 jkl 999 999
25
27
  ```
26
- しかし実行結果は、末尾のデバイス結果しか帰ってこない
27
28
 
29
+ 指摘により、1デバイスxn個のトンネルが、デバイス一覧すべてをprintすることで
30
+ Logでは取得できていることを確認できたが、
31
+ 以下の箇所のロジックにて、csvで出力することができない
28
32
 
33
+ Tunnel_level_quality_analysis_df = get_request(vmanage_info_df)
34
+ 以上の一文でget_request(vmanage_info_df)をcsvに入れると認識している。
29
35
 
36
+ しかし、csvの結果が、デバイス一覧のみしか出力されていない。
37
+ df = df[["deviceId", "system-ip", "host-name", "device-model", "bfdSessionsUp", "bfdSessions"]]
38
+ の結果しか入っていない状況です。
39
+
40
+ 想定結果のcsv
41
+ ```Result_from_csv
42
+ System_ip loss_percentage ・・・・ vqoe_score dt
43
+ 1.1.1.1 99.99・・・・5 2023/01/01 01:01:01
44
+ 1.1.1.1 99.98・・・・5 2023/01/01 01:01:01
45
+ 1.1.1.1 99.97・・・・5 2023/01/01 01:01:01
46
+ 1.1.1.1 99.96・・・・5 2023/01/01 01:01:01
47
+ System_ip loss_percentage ・・・・ vqoe_score dt
48
+ 1.1.1.2 99.99・・・・5 2023/01/01 01:01:01
49
+ 1.1.1.2 99.98・・・・5 2023/01/01 01:01:01
50
+ 1.1.1.2 99.97・・・・5 2023/01/01 01:01:01
51
+ 1.1.1.2 99.96・・・・5 2023/01/01 01:01:01
52
+ System_ip loss_percentage ・・・・ vqoe_score dt
53
+ 1.1.1.3 99.99・・・・5 2023/01/01 01:01:01
54
+ 1.1.1.3 99.98・・・・5 2023/01/01 01:01:01
55
+ 1.1.1.3 99.97・・・・5 2023/01/01 01:01:01
56
+ 1.1.1.3 99.96・・・・5 2023/01/01 01:01:01
57
+ System_ip loss_percentage ・・・・ vqoe_score dt
58
+ 1.1.1.4 99.99・・・・5 2023/01/01 01:01:01
59
+ 1.1.1.4 99.98・・・・5 2023/01/01 01:01:01
60
+ 1.1.1.4 99.97・・・・5 2023/01/01 01:01:01
61
+ 1.1.1.4 99.96・・・・5 2023/01/01 01:01:01
62
+ ```
63
+
64
+
65
+
66
+
30
67
  ### 発生している問題・エラーメッセージ
31
68
 
32
69
  ```log
@@ -44,73 +81,75 @@
44
81
 
45
82
  query_data
46
83
  {'query':(略),{'value':['1.1.1.1'],(略). 'type':'sum'}]}}
47
- devide_url
48
- https://vmanage_ABC//dataservice/statistics/approute/fec/aggregation
49
84
  device_list
50
85
  <Response [200]>
51
86
  200
52
- df_json_data
53
- [{'loss_percentage': 99.99, (略), 'vqoe_score': 5}]
54
- df
87
+ df2
55
- loss_percentage・・・・vqoe_score
88
+ System_ip loss_percentage ・・・・ vqoe_score dt
89
+ 1.1.1.1 99.99・・・・5 2023/01/01 01:01:01
90
+ 1.1.1.1 99.98・・・・5 2023/01/01 01:01:01
91
+ 1.1.1.1 99.97・・・・5 2023/01/01 01:01:01
92
+ 1.1.1.1 99.96・・・・5 2023/01/01 01:01:01
56
- 99.99・・・・5
93
+ df_itertuples
94
+ deviceId system_ip・・・bdfSessionsUp bfdSessions
57
- 99.98・・・・5
95
+ 1.1.1.1 1.1.1.1 ・・・ 8 8
58
- 99.97・・・・5
96
+ 1.1.1.2 1.1.1.2 ・・・ 8 8
59
- 99.96・・・・5
97
+ 1.1.1.3 1.1.1.3 ・・・ 8 8
98
+ 1.1.1.4 1.1.1.4 ・・・ 8 8
60
99
 
61
100
  query_data
62
- {'query':(略),{'value':['1.1.1.2'],(略). 'type':'sum'}]}}
101
+ {'query':(略),{'value':['1.1.1.1'],(略). 'type':'sum'}]}}
63
- devide_url
64
- https://vmanage_ABC//dataservice/statistics/approute/fec/aggregation
65
102
  device_list
66
103
  <Response [200]>
67
104
  200
68
- df_json_data
69
- [{'loss_percentage': 99.99, (略), 'vqoe_score': 5}]
70
- df
105
+ df2
71
- loss_percentage・・・・vqoe_score
106
+ System_ip loss_percentage ・・・・ vqoe_score dt
107
+ 1.1.1.2 99.99・・・・5 2023/01/01 01:01:01
108
+ 1.1.1.2 99.98・・・・5 2023/01/01 01:01:01
109
+ 1.1.1.2 99.97・・・・5 2023/01/01 01:01:01
110
+ 1.1.1.2 99.96・・・・5 2023/01/01 01:01:01
72
- 99.99・・・・5
111
+ df_itertuples
112
+ deviceId system_ip・・・bdfSessionsUp bfdSessions
73
- 99.98・・・・5
113
+ 1.1.1.1 1.1.1.1 ・・・ 8 8
74
- 99.97・・・・5
114
+ 1.1.1.2 1.1.1.2 ・・・ 8 8
75
- 99.96・・・・5
115
+ 1.1.1.3 1.1.1.3 ・・・ 8 8
116
+ 1.1.1.4 1.1.1.4 ・・・ 8 8
76
117
 
77
118
  query_data
78
- {'query':(略),{'value':['1.1.1.3'],(略). 'type':'sum'}]}}
119
+ {'query':(略),{'value':['1.1.1.1'],(略). 'type':'sum'}]}}
79
- devide_url
80
- https://vmanage_ABC//dataservice/statistics/approute/fec/aggregation
81
120
  device_list
82
121
  <Response [200]>
83
122
  200
84
- df_json_data
85
- [{'loss_percentage': 99.99, (略), 'vqoe_score': 5}]
86
- df
123
+ df2
87
- loss_percentage・・・・vqoe_score
124
+ System_ip loss_percentage ・・・・ vqoe_score dt
125
+ 1.1.1.3 99.99・・・・5 2023/01/01 01:01:01
126
+ 1.1.1.3 99.98・・・・5 2023/01/01 01:01:01
127
+ 1.1.1.3 99.97・・・・5 2023/01/01 01:01:01
128
+ 1.1.1.3 99.96・・・・5 2023/01/01 01:01:01
88
- 99.99・・・・5
129
+ df_itertuples
130
+ deviceId system_ip・・・bdfSessionsUp bfdSessions
89
- 99.98・・・・5
131
+ 1.1.1.1 1.1.1.1 ・・・ 8 8
90
- 99.97・・・・5
132
+ 1.1.1.2 1.1.1.2 ・・・ 8 8
91
- 99.96・・・・5
133
+ 1.1.1.3 1.1.1.3 ・・・ 8 8
134
+ 1.1.1.4 1.1.1.4 ・・・ 8 8
92
135
 
93
136
  query_data
94
137
  {'query':(略),{'value':['1.1.1.1'],(略). 'type':'sum'}]}}
95
- devide_url
96
- https://vmanage_ABC//dataservice/statistics/approute/fec/aggregation
97
138
  device_list
98
139
  <Response [200]>
99
140
  200
100
- df_json_data
101
- [{'loss_percentage': 99.99, (略), 'vqoe_score': 5}]
102
- df
141
+ df2
103
- loss_percentage・・・・vqoe_score
104
- 99.99・・・・5
105
- 99.98・・・・5
106
- 99.97・・・・5
107
- 99.96・・・・5
108
-
109
142
  System_ip loss_percentage ・・・・ vqoe_score dt
110
143
  1.1.1.4 99.99・・・・5 2023/01/01 01:01:01
111
144
  1.1.1.4 99.98・・・・5 2023/01/01 01:01:01
112
145
  1.1.1.4 99.97・・・・5 2023/01/01 01:01:01
113
146
  1.1.1.4 99.96・・・・5 2023/01/01 01:01:01
147
+ df_itertuples
148
+ deviceId system_ip・・・bdfSessionsUp bfdSessions
149
+ 1.1.1.1 1.1.1.1 ・・・ 8 8
150
+ 1.1.1.2 1.1.1.2 ・・・ 8 8
151
+ 1.1.1.3 1.1.1.3 ・・・ 8 8
152
+ 1.1.1.4 1.1.1.4 ・・・ 8 8
114
153
  ```
115
154
 
116
155
  ### 該当のソースコード
@@ -224,23 +263,25 @@
224
263
 
225
264
  if device_list.status_code == 200:
226
265
  json_data = device_list.json()
266
+
267
+ df2 = pd.json_normalize(json_data["data"])
268
+
269
+ #先頭にsystemip
270
+ df2.insert(0, 'System_ip', data[1])
271
+ #末尾にdatetime
272
+ df2['df'] = datetime.now(),strftime('%Y%m%d% %H:%M:%S')
273
+ c = df2.pop('dt')
274
+ df.insert(df2.shape[1], c.name, c)
275
+
276
+ print("df2")
277
+ print(df2)
278
+
227
279
  else:
228
280
  print(device_list.status_code)
229
281
 
230
- df = pd.json_normalize(json_data["data"])
231
-
232
- print("df_json_data")
282
+ print("df_itertuples")
233
- print(json_data["data"])
234
-
235
- print("df")
236
283
  print(df)
237
-
238
- df.insert(0, 'System_ip', data[1])
284
+
239
- df['df'] = datetime.now(),strftime('%Y%m%d% %H:%M:%S')
240
- c = df.pop('dt')
241
- df.insert(df.shape[1], c.name, c)
242
-
243
- print(df)
244
285
  return df
245
286
  ```
246
287