Advertisement
adellam

Untitled

Apr 23rd, 2018
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 2.30 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. # Cycle on the list of Xen hypervisors
  4. # Get the list of the online hosts
  5. # Build the hosts file in a format that prometheus likes (json better than yaml)
  6. # One file for hypervisor?
  7.  
  8. # Input example (one node on one hypervisor):
  9. <map>
  10. <dom online="yes" domain="parthenos.d4science.org">
  11.  <name>themas.parthenos.d4science.org</name>
  12.  <mem>6000</mem>
  13.  <uptime>136d</uptime>
  14.  <fulluptime>136d22h5m</fulluptime>
  15.  <cpus>2</cpus>
  16.  <san>no</san>
  17.  <uuid>no</uuid>
  18.  <badconf>no</badconf>
  19.  <avgXenCpu>0</avgXenCpu>
  20. </dom>
  21. </map>
  22.  
  23. # Output example:
  24.  
  25. [
  26.  {
  27.    "targets": ["127.0.0.1:9100"],
  28.    "labels": {
  29.      "instance": "localhost"
  30.    }
  31.  }
  32. ]
  33. """
  34.  
  35.  
  36. import requests
  37. import lxml
  38. from lxml import etree
  39. import json
  40.  
  41. # hypervisors with hostname under 20 are either dismissed or do not export a xenmap
  42. # The latest available Xen hypervisor is 35
  43. hy_lower = 20
  44. hy_upper = 36
  45. prometheus_node_port = 9100
  46. dest_path_prefix = '/opt/prometheus/conf/sd_nodes'
  47. dest_path_prefix = '/tmp'
  48. host_data = {}
  49.  
  50. def get_hosts(xenmap_url):
  51.         hosts_list = []
  52.         xenmap = requests.get(xenmap_url)
  53.         hostmap = etree.fromstring(xenmap.text)
  54.  
  55.         for dom in hostmap.findall('dom'):
  56.                 host = dom.find('name').text
  57.                 status = dom.get('online')
  58.                 if status == 'yes':
  59.                         prometheus_target = host + ':' + str(prometheus_node_port)
  60.                         hosts_list.append(prometheus_target)
  61.         return hosts_list
  62.  
  63.  
  64. hypervisors = range(hy_lower, hy_upper)
  65. for i in 5, 7, 11:
  66.         hypervisors.append(i)
  67. for hyper_num in hypervisors:
  68.         hypervisor_host = 'dlib' + str(hyper_num) + 'x.dom0.research-infrastructures.eu'
  69.         xenmap_url = 'http://' + hypervisor_host + '/xen/map.xml'
  70.         dest_file = dest_path_prefix + '/' + hypervisor_host + '.json'
  71.         host_data['labels'] = dict(group='node')
  72.         host_data['targets'] = get_hosts(xenmap_url)
  73.         if host_data['targets']:
  74.                 json_data = json.dumps(host_data, ensure_ascii=False, sort_keys=False, indent=4)
  75.                 write_file = open(dest_file, 'w')
  76.                 write_file.write(json_data)
  77.                 write_file.close()
  78.  
  79.                 print hypervisor_host + (' VMs ') + json_data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement