Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- """
- # Cycle on the list of Xen hypervisors
- # Get the list of the online hosts
- # Build the hosts file in a format that prometheus likes (json better than yaml)
- # One file for hypervisor?
- # Input example (one node on one hypervisor):
- <map>
- <dom online="yes" domain="parthenos.d4science.org">
- <name>themas.parthenos.d4science.org</name>
- <mem>6000</mem>
- <uptime>136d</uptime>
- <fulluptime>136d22h5m</fulluptime>
- <cpus>2</cpus>
- <san>no</san>
- <uuid>no</uuid>
- <badconf>no</badconf>
- <avgXenCpu>0</avgXenCpu>
- </dom>
- </map>
- # Output example:
- [
- {
- "targets": ["127.0.0.1:9100"],
- "labels": {
- "instance": "localhost"
- }
- }
- ]
- """
- import requests
- import lxml
- from lxml import etree
- import json
- # hypervisors with hostname under 20 are either dismissed or do not export a xenmap
- # The latest available Xen hypervisor is 35
- hy_lower = 20
- hy_upper = 36
- prometheus_node_port = 9100
- dest_path_prefix = '/opt/prometheus/conf/sd_nodes'
- dest_path_prefix = '/tmp'
- host_data = {}
- def get_hosts(xenmap_url):
- hosts_list = []
- xenmap = requests.get(xenmap_url)
- hostmap = etree.fromstring(xenmap.text)
- for dom in hostmap.findall('dom'):
- host = dom.find('name').text
- status = dom.get('online')
- if status == 'yes':
- prometheus_target = host + ':' + str(prometheus_node_port)
- hosts_list.append(prometheus_target)
- return hosts_list
- hypervisors = range(hy_lower, hy_upper)
- for i in 5, 7, 11:
- hypervisors.append(i)
- for hyper_num in hypervisors:
- hypervisor_host = 'dlib' + str(hyper_num) + 'x.dom0.research-infrastructures.eu'
- xenmap_url = 'http://' + hypervisor_host + '/xen/map.xml'
- dest_file = dest_path_prefix + '/' + hypervisor_host + '.json'
- host_data['labels'] = dict(group='node')
- host_data['targets'] = get_hosts(xenmap_url)
- if host_data['targets']:
- json_data = json.dumps(host_data, ensure_ascii=False, sort_keys=False, indent=4)
- write_file = open(dest_file, 'w')
- write_file.write(json_data)
- write_file.close()
- print hypervisor_host + (' VMs ') + json_data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement