source: genesis/tools/yaml2nag.py@ 8758

Last change on this file since 8758 was 8686, checked in by richardvm, 14 years ago

only allow nodes that are status=up

  • Property svn:executable set to *
File size: 2.6 KB
Line 
1#!/usr/bin/env python
2
3import os
4import glob
5import pprint
6
7try:
8 import yaml
9except ImportError, e:
10 print e
11 print "[ERROR] Please install the python-yaml or devel/py-yaml package"
12 exit(1)
13
14try:
15 from yaml import CLoader as Loader
16 from yaml import CDumper as Dumper
17except ImportError:
18 from yaml import Loader, Dumper
19
20NODE_DIR = os.getcwd()
21NODE_PREFIX = 'nodes-7-2,'
22
23def 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
33def 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
41def get_nodelist():
42 """ Get all available nodes - sorted """
43 os.chdir(NODE_DIR)
44 nodelist = sorted(glob.glob("CNode*"))
45 return nodelist
46
47def get_hostlist():
48 """ Combined hosts and proxy list"""
49 return get_nodelist()
50
51def 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
59def is_ileiden(node):
60 """ Is this a iLeiden node """
61 if (node['ileiden']):
62 return str(',nodes-ileiden')
63 else:
64 return str('')
65
66def 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
77def is_ap(node):
78 try:
79 if node['mode'] == 'ap-wds':
80 return 0
81 else:
82 return 1
83 except:
84 return 1
85
86def is_up(node):
87 """ Is node Up?"""
88 if node['status'] == 'up':
89 return 1
90 else:
91 return 0
92
93def get_hostname(node):
94 return node['nodename']
95
96def get_ip(node):
97 return node['masterip']
98
99def 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
106 return None
107 except:
108 print node['nodename']
109def main():
110 allconfigs = get_all_configs()
111# pprint.pprint(allconfigs)
112
113 for k,v in allconfigs.iteritems():
114 if is_iris(v) and is_up(v):
115 ileiden = is_ileiden(v)
116 interfaces = get_up_interfaces(v, 'iface-')
117 hostname = get_hostname(v)
118 ip = get_ip(v)
119
120 print """
121define host{
122 use host-pnp
123 host_name %s
124 alias %s
125 address %s
126 hostgroups %s%s%s
127}
128
129 """ % (hostname, hostname, ip, NODE_PREFIX, interfaces, ileiden)
130
131
132main()
Note: See TracBrowser for help on using the repository browser.