summaryrefslogtreecommitdiff
path: root/python-status-exporter/modules/nginx_vod.py
blob: c4547cfa2b0aaeb400113c4ab0f3b08514297e3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/env python2.6

import sys
import os
import inspect
sys.path.insert(0, os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) + "/client_python")

import httplib
import xml.etree.ElementTree as XET

from prometheus_client import start_http_server, Summary, Gauge

"""
vod_status_con = httplib.HTTPConnection('127.0.0.1', 87, timeout=2)
vod_status_con.request("GET", "/vod_status")

resp = vod_status_con.getresponse()
"""

def rec_xml_loop(xml, data, path=""):
  children = xml.getchildren()
  if len(children) < 1:
    data[path + xml.tag] = xml.text
  else:
    for c in children:
      if path == "":
        rec_xml_loop(c, data, xml.tag)
      else:
        rec_xml_loop(c, data, path + "_" + xml.tag)
    
  if path == "":
    return data

VOD_HOST = "127.0.0.1"
VOD_PORT = 87
VOD_PATH = "/vod_status"

# REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')
# TEST_SEC = Gauge('time_now_sec', 'Current value for det second hand on our clock')

def get_xml(host=VOD_HOST, port=VOD_PORT, path=VOD_PATH):
  vod_status_con = httplib.HTTPConnection(host, port, timeout=2)
  vod_status_con.request("GET", path)
  resp = vod_status_con.getresponse()

  if resp.status == 200:
    data = resp.read()
    vod_status_con.close()
    return data
  else:
    vod_status_con.close()
    raise Exception("Unable to fetch info from http server {}:{}/{}".format(host, port, path))
  
def init_datapoints(data_dict, labels):
  from_xml_data = dict()
  xml_obj = XET.fromstring(get_xml())
  xml_data_dict = rec_xml_loop(xml_obj, from_xml_data)
  for key in xml_data_dict:
    data_dict[key] = Gauge(key, key, labels)

def update_datapoints(data_dict, label_vals):
  from_xml_data = dict()
  xml_obj = XET.fromstring(get_xml())
  xml_data_dict = rec_xml_loop(xml_obj, from_xml_data)
  for key in data_dict:
    if key in xml_data_dict:
      try:
        float(xml_data_dict[key])
      except ValueError:
        pass
      else:
        data_dict[key].labels(*label_vals).set(xml_data_dict[key])