Advertisement
Guest User

Prediction path

a guest
May 28th, 2015
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.35 KB | None | 0 0
  1. 2a3
  2. >
  3. 10c11
  4. < # | Copyright Mathias Kettner 2014             mk@mathias-kettner.de |
  5. ---
  6. > # | Copyright Mathias Kettner 2013             mk@mathias-kettner.de |
  7. 112a114,122
  8. > def get_influx_data(hostname, service_description, varname, cf, fromtime, untiltime, influx_client):
  9. >     service_desc_influx = service_description.replace(" ", "")
  10. >     hostname_influx = hostname.replace(".", "_")
  11. >     data_raw = list(influx_client.query("select max(value) from %s where time > %is and time < %is and host='%s' and check='%s' group by time(2m);" % (service_desc_influx, fromtime, untiltime, hostname_influx, varname))[service_desc_influx])
  12. >     data_list = []
  13. >     for d in data_raw:
  14. >         data_list.append(d["max"])
  15. >     return 120, data_list
  16. >
  17. 116,126d125
  18. < # Check wether a certain time stamp lies with in daylight safing time (DST)
  19. < def is_dst(timestamp):
  20. <     return time.localtime(timestamp).tm_isdst
  21. <
  22. < # Returns the timezone *including* DST shift at a certain point of time
  23. < def timezone_at(timestamp):
  24. <     if is_dst(timestamp):
  25. <         return time.altzone
  26. <     else:
  27. <         return time.timezone
  28. <
  29. 129c128
  30. <     day_of_epoch, rel_time = divmod(t - timezone_at(t), 86400)
  31. ---
  32. >     day_of_epoch, rel_time = divmod(t - time.timezone, 86400)
  33. 133c132
  34. <     return "everyday", (t - timezone_at(t)) % 86400
  35. ---
  36. >     return "everyday", (t - time.timezone) % 86400
  37. 138c137
  38. <     return str(mday), (t - timezone_at(t)) % 86400
  39. ---
  40. >     return str(mday), (t - time.timezone) % 86400
  41. 141,142c140
  42. <     return "everyhour", (t - timezone_at(t)) % 3600
  43. <
  44. ---
  45. >     return "everyhour", (t - time.timezone) % 3600
  46. 146,148c144,146
  47. <         "slice"     : 86400, # 7 slices
  48. <         "groupby"   : group_by_wday,
  49. <         "valid"     : 7,
  50. ---
  51. >         "slice" : 86400, # 7 slices
  52. >         "groupby" : group_by_wday,
  53. >         "valid" : 7,
  54. 151,153c149,151
  55. <         "slice"     : 86400, # 31 slices
  56. <         "groupby"   : group_by_day_of_month,
  57. <         "valid"     : 28,
  58. ---
  59. >         "slice" : 86400, # 31 slices
  60. >         "groupby" : group_by_day_of_month,
  61. >         "valid" : 28,
  62. 156,158c154,156
  63. <         "slice"     : 86400, # 1 slice
  64. <         "groupby"   : group_by_day,
  65. <         "valid"     : 1,
  66. ---
  67. >         "slice" : 86400, # 1 slice
  68. >         "groupby" : group_by_day,
  69. >         "valid" : 1,
  70. 161,163c159,161
  71. <         "slice"     : 3600, # 1 slice
  72. <         "groupby"   : group_by_everyhour,
  73. <         "valid"     : 24,
  74. ---
  75. >         "slice" : 3600, # 1 slice
  76. >         "groupby" : group_by_everyhour,
  77. >         "valid" : 24,
  78. 174a173,219
  79. > def compute_prediction_influxdb(pred_file, timegroup, params, period_info, from_time, dsname, cf, ic):
  80. >     import math, json
  81. >
  82. >     begin = from_time
  83. >     slices = []
  84. >     absolute_begin = from_time - params["horizon"] * 86400
  85. >
  86. >     smallest_step=120
  87. >     while begin >= absolute_begin:
  88. >         tg, fr, un, rel = get_prediction_timegroup(begin, period_info)
  89. >         if tg == timegroup:
  90. >             step, data = get_influx_data(g_hostname, g_service_description,
  91. >                                       dsname, cf, fr, un-1, ic)
  92. >             slices.append((fr, data))
  93. >         begin -= period_info["slice"]
  94. >
  95. >
  96. >     num_points=len(slices[0][1])
  97. >     consolidated = []
  98. >     for i in xrange(num_points):
  99. >         point_line=[]
  100. >         for from_time, data in slices:
  101. >             if i < len(data):
  102. >                 d = data[i]
  103. >             if d != None:
  104. >                 point_line.append(d)
  105. >         if point_line:
  106. >             average = sum(point_line) / len(point_line)
  107. >             consolidated.append([
  108. >                  average,
  109. >                  min(point_line),
  110. >                  max(point_line),
  111. >                  stdev(point_line, average),
  112. >             ])
  113. >         else:
  114. >             consolidated.append([None, None, None, None])
  115. >
  116. >     result = {
  117. >         "num_points" : num_points,
  118. >         "step"       : smallest_step,
  119. >         "columns"    : [ "average", "min", "max", "stdev" ],
  120. >         "points"     : consolidated,
  121. >     }
  122. >     return result
  123. >
  124. >
  125. >
  126. 183,184c228
  127. <
  128. <     # The resolutions of the different time ranges differ. We interpolate
  129. ---
  130. >     # The resolution of the different time ranges differs. We interpolate
  131. 186c230
  132. <     # finest resolution. We also assume, that each step is always dividable
  133. ---
  134. >     # finest resolution. We also assume, that step step is always dividable
  135. 188,192d231
  136. <
  137. <     # Note: due to the f**king DST, we can have several shifts between
  138. <     # DST and non-DST during are computation. We need to compensate for
  139. <     # those. DST swaps within slices are being ignored. The DST flag
  140. <     # is checked against the beginning of the slice.
  141. 206c245
  142. <     num_points = len(slices[0][2])
  143. ---
  144. >     num_points = len(slices[0][2]) # The number of data points from the latest day
  145. 248a288,289
  146. >     from influxdb import InfluxDBClient
  147. >     influx_settings = read_influx_config('/etc/check_mk/influxdb.config')
  148. 291c332
  149. <         last_info = None
  150. ---
  151. >             last_info = None
  152. 315c356,360
  153. <         prediction = compute_prediction(pred_file, timegroup, params, period_info, from_time, dsname, cf)
  154. ---
  155. >         if influx_settings['use_influx']:
  156. >             client = InfluxDBClient(influx_settings['host'], influx_settings['port'], influx_settings['user'], influx_settings['password'], influx_settings['database'])
  157. >             prediction = compute_prediction_influxdb(pred_file, timegroup, params, period_info, from_time, dsname, cf, client)
  158. >         else:
  159. >             prediction = compute_prediction(pred_file, timegroup, params, period_info, from_time, dsname, cf)
  160. 361a407,422
  161. >
  162. > def read_influx_config(config_path):
  163. >     import ConfigParser as cp
  164. >     config = cp.SafeConfigParser({'host' : 'localhost', 'port' : '8086', 'user' : 'root', 'password' : 'root', 'database' : 'checkmk', 'use_influx' : 'False'})
  165. >     config.read(config_path)
  166. >     section = 'InfluxSettings'
  167. >     options = config.options(section)
  168. >     settings = {}
  169. >     for option in options:
  170. >         if option == 'use_influx':
  171. >             settings[option] = config.getboolean(section, option)
  172. >         elif option == 'port':
  173. >             settings[option] = config.getint(section, option)
  174. >         else:
  175. >             settings[option] = config.get(section, option)
  176. >     return settings
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement