Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. from influxdb import InfluxDBClient
  2. import json
  3. from datetime import datetime, timedelta
  4. import time
  5. import sys
  6. from scrut_api import *
  7.  
  8. config = json.load(open('/home/brian/influx/config.json'))
  9.  
  10.  
  11.  
  12.  
  13. influx_client = InfluxDBClient(host=config['influxHost'], port=8086)
  14. influx_client.switch_database(config['influxDB'])
  15.  
  16. # initiate Scurtinizer client.
  17. client = scrut_api_client(
  18. hostname=config['hostname'],
  19. authToken=config['authToken'])
  20.  
  21.  
  22.  
  23.  
  24.  
  25. def backfill_by_hours(number_of_hours):
  26. pattern = '%Y-%m-%d %H:%M:%S'
  27. print("starting to backfill data for {} hours".format(number_of_hours))
  28. #go into interface_list and loop for each exporter.
  29. if sys.argv[1] == "backfill":
  30. for exporter in config['interfaceList']:
  31. #for each interface_ID, go find time time data
  32. for interface_id in exporter['interfaces']:
  33. #set the time increments, this makes sure we move back in time and hour each loop.
  34. end_time_start = 0
  35. start_time_start = 60
  36. #loops back for specified number of hours.
  37. for x in range(0, int(number_of_hours)):
  38.  
  39. Start_Time = int(time.mktime(time.strptime((str(datetime.now().replace(
  40. microsecond=0, second=0) - timedelta(minutes=start_time_start))), pattern)))
  41. End_Time = int(time.mktime(time.strptime((str(datetime.now().replace(
  42. microsecond=0, second=0) - timedelta(minutes=end_time_start))), pattern)))
  43.  
  44. report_object = scrut_json(filters={'sdfDips_0': 'in_{}_{}-{}'.format(exporter['exporter'],exporter['exporter'],interface_id)},
  45. reportTypeLang="interfaces",
  46. times={
  47. "dateRange": "Custom",
  48. "start": "{}".format(Start_Time),
  49. "end": "{}".format(End_Time),
  50. "clientTimezone": "America/New_York"
  51. })
  52.  
  53. report_format = scrut_data_requested()
  54. params = scrut_params(
  55. client=client,
  56. json_data=report_object.report_json,
  57. data_requested=report_format.format)
  58.  
  59. response = scrut_request(params)
  60. graph_data = response.data["report"]["graph"]["timeseries"]["inbound"]
  61. label_data = response.data["report"]["graph"]["pie"]["inbound"]
  62.  
  63. #moves time back 1 hour.
  64. end_time_start = end_time_start + 60
  65. start_time_start = start_time_start + 60
  66.  
  67.  
  68. #creates a list to be written into influx
  69. json_body = []
  70. for value in range (0,len(label_data)):
  71. label = label_data[value]['label_dns']
  72. for time_stamp in graph_data[value]:
  73. original = datetime.fromtimestamp(time_stamp[0])
  74. new = original + timedelta(hours=4)
  75. time_data = datetime.fromtimestamp(new.timestamp()).strftime('%Y-%m-%dT%H:%M:%S')
  76. data = {
  77. "measurement": "interfaces",
  78. "time":time_data,
  79. "fields":{
  80. "bits":int((time_stamp[1] * 8/60))
  81. },
  82. "tags":{
  83. "application_label":label
  84. }
  85. }
  86. json_body.append(data)
  87.  
  88. print("backfilled data for {}".format(time_data))
  89. influx_client.write_points(json_body)
  90. elif sys.argv[1] == "foward":
  91. Start_Time = int(time.mktime(time.strptime((str(datetime.now().replace(microsecond=0, second=0) - timedelta(minutes=5))), pattern)))
  92. End_Time = int(time.mktime(time.strptime((str(datetime.now().replace(microsecond=0, second=0) - timedelta(minutes=0))), pattern)))
  93. for exporter in config['interfaceList']:
  94. #for each interface_ID, go find time time data
  95. for interface_id in exporter['interfaces']:
  96. report_object = scrut_json(filters={'sdfDips_0': 'in_{}_{}-{}'.format(exporter['exporter'],exporter['exporter'],interface_id)},
  97. reportTypeLang="interfaces",
  98. times={
  99. "dateRange": "Custom",
  100. "start": "{}".format(Start_Time),
  101. "end": "{}".format(End_Time),
  102. "clientTimezone": "America/New_York"
  103. })
  104. report_format = scrut_data_requested()
  105. params = scrut_params(
  106. client=client,
  107. json_data=report_object.report_json,
  108. data_requested=report_format.format)
  109. response = scrut_request(params)
  110. graph_data = response.data["report"]["graph"]["timeseries"]["inbound"]
  111. label_data = response.data["report"]["graph"]["pie"]["inbound"]
  112.  
  113. json_body = []
  114.  
  115. for value in range (0,len(label_data)):
  116. label = label_data[value]['label_dns']
  117. for time_stamp in graph_data[value]:
  118. original = datetime.fromtimestamp(time_stamp[0])
  119. new = original + timedelta(hours=4)
  120. time_data = datetime.fromtimestamp(new.timestamp()).strftime('%Y-%m-%dT%H:%M:%S')
  121. data = {
  122. "measurement": "interfaces",
  123. "time":time_data,
  124. "fields":{
  125. "bits":int((time_stamp[1] * 8/60))
  126. },
  127. "tags":{
  128. "application_label":label
  129. }
  130. }
  131. json_body.append(data)
  132. print("backfilled data for {}".format(time_data))
  133. influx_client.write_points(json_body)
  134.  
  135.  
  136.  
  137.  
  138. backfill_by_hours(sys.argv[2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement