Changeset 10388 in genesis
- Timestamp:
- Apr 8, 2012, 9:52:11 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/yaml2nag.py
r9953 r10388 1 1 #!/usr/bin/env python 2 # 3 # Script to create nagios output files from gformat configurtion files 4 # 5 # Original: Richard van Mansom <richardvm@wirelessleiden.nl> 6 # Rick van der Zwet <rick@wirelessleiden.nl> 2 7 3 import os 4 import glob 5 import pprint 8 import gformat 6 9 7 try: 8 import yaml 9 except ImportError, e: 10 print e 11 print "[ERROR] Please install the python-yaml or devel/py-yaml package" 12 exit(1) 10 NODE_PREFIX = 'nodes-7-2' 13 11 14 try: 15 from yaml import CLoader as Loader 16 from yaml import CDumper as Dumper 17 except ImportError: 18 from yaml import Loader, Dumper 19 20 NODE_DIR = os.getcwd() 21 NODE_PREFIX = 'nodes-7-2,' 22 23 def get_yaml(item): 24 """ Get configuration yaml for 'item'""" 25 gfile = NODE_DIR + '/%s/wleiden.yaml' % item 26 27 f = open(gfile, 'r') 28 datadump = yaml.load(f,Loader=Loader) 29 f.close() 30 31 return datadump 32 33 def get_all_configs(): 34 """ Get dict with key 'host' with all configs present """ 35 configs = dict() 36 for host in get_hostlist(): 37 datadump = get_yaml(host) 38 configs[host] = datadump 39 return configs 40 41 def get_nodelist(): 42 """ Get all available nodes - sorted """ 43 os.chdir(NODE_DIR) 44 nodelist = sorted(glob.glob("CNode*")) 45 return nodelist 46 47 def get_hostlist(): 48 """ Combined hosts and proxy list""" 49 return get_nodelist() 50 51 def get_all_configs(): 52 """ Get dict with key 'host' with all configs present """ 53 configs = dict() 54 for host in get_hostlist(): 55 datadump = get_yaml(host) 56 configs[host] = datadump 57 return configs 58 59 def is_ileiden(node): 60 """ Is this a iLeiden node """ 61 if (node['ileiden']): 62 return str(',nodes-ileiden') 63 else: 64 return str('') 65 66 def get_up_interfaces(node, prefix): 67 """ Get nagios syntax for up interlinks """ 68 iface_up = list() 69 70 for inf in node['interfaces'].split(','): 71 iface = 'iface_%s' % inf 72 if (node[iface]['status'] == 'up' and is_ap(node[iface])): 73 iface_up.append('iface-%s' % inf) 74 75 return ','.join(iface_up) 76 77 def is_ap(node): 78 try: 79 if node['dhcp'] == 'no': 80 return 1 81 else: 82 return 0 83 except: 84 return 0 85 86 def is_up(node): 87 """ Is node Up?""" 88 if node['status'] == 'up': 89 return 1 90 else: 91 return 0 92 93 def get_hostname(node): 94 return node['nodename'] 95 96 def get_ip(node): 97 return node['masterip'] 12 def is_ap(ifacedump): 13 return ifacedump.has_key('dhcp') and not ifacedump['dhcp'] == 'no' 98 14 99 15 def is_iris(node): 100 try: 101 if node['release'] == '8.0-RELEASE': 102 return 1 103 elif node['release'] == '7.2-RELEASE': 104 return 1 105 elif node['release'] == '8.1-RELEASE': 106 return 1 107 elif node['release'] == '8.2-RELEASE': 108 return 1 16 return node.has_key('release') and node['release'] in ['8.0-RELEASE', '7.2-RELEASE', '8.1-RELEASE', '8.2-RELEASE'] 109 17 110 return None 111 except:112 return 018 if __name__ == '__main__': 19 for host in gformat.get_hostlist(): 20 datadump = gformat.get_yaml(host) 113 21 114 def main(): 115 allconfigs = get_all_configs()116 # pprint.pprint(allconfigs)22 # Make sure to process only active IRIS nodes 23 if not is_iris(datadump) or not datadump['status'] == 'up': 24 continue 117 25 118 for k,v in allconfigs.iteritems(): 119 120 if is_iris(v) and is_up(v): 121 ileiden = is_ileiden(v) 122 interfaces = get_up_interfaces(v, 'iface-') 123 hostname = get_hostname(v) 124 ip = get_ip(v) 125 126 print """ 26 # Default hostgroup 27 hostgroups = [NODE_PREFIX,] 28 29 # Interface groups 30 for iface in datadump['autogen_iface_keys']: 31 ifacedump = datadump[iface] 32 if (ifacedump['status'] == 'up' and not is_ap(ifacedump)): 33 key = 'iface-%s' % ifacedump['autogen_ifname'] 34 if not key in hostgroups: 35 hostgroups.append(key) 36 37 if datadump['ileiden']: 38 hostgroups.append('node-ileiden') 39 40 datadump['hostgroups'] = ','.join(hostgroups) 41 print """ 127 42 define host{ 128 43 use host-pnp 129 host_name % s130 alias % s131 address % s132 hostgroups % s%s%s44 host_name %(nodename)s 45 alias %(nodename)s 46 address %(masterip)s 47 hostgroups %(hostgroups)s 133 48 } 134 49 135 """ % (hostname, hostname, ip, NODE_PREFIX, interfaces, ileiden) 136 137 138 main() 50 """ % datadump
Note:
See TracChangeset
for help on using the changeset viewer.