b5023f26425a1a455948572646dcf82742a4f315
[monitor.git] / nagios / plc2nagios.py
1 #!/usr/bin/python
2
3 import soltesz
4 import plc
5
6 class NagiosObject(object):
7         trans = {'d2_coords': '2d_coords'}
8
9         def __init__(self, id, **kwargs):
10                 self.id = id
11                 self.kwords = kwargs.keys()
12                 for key in self.kwords:
13                         self.__setattr__(key, kwargs[key])
14
15         def toString(self):
16                 ret = ""
17                 ret += "define %s {\n" % self.id
18                 for key in self.kwords:
19                         if key in self.trans:
20                                 ret += "    %s   %s\n" % (self.trans[key], self.__getattribute__(key))
21                         else:
22                                 ret += "    %s   %s\n" % (key, self.__getattribute__(key))
23                 ret += "}\n"
24                 return ret
25
26 class Host(NagiosObject):
27         def __init__(self, **kwargs):   
28                 NagiosObject.__init__(self, "host", **kwargs)
29
30 class HostGroup(NagiosObject):
31         def __init__(self, **kwargs):   
32                 NagiosObject.__init__(self, "hostgroup", **kwargs)
33
34 class Service(NagiosObject):
35         def __init__(self, **kwargs):   
36                 NagiosObject.__init__(self, "service", **kwargs)
37
38 class ServiceDependency(NagiosObject):
39         def __init__(self, **kwargs):   
40                 NagiosObject.__init__(self, "servicedependency", **kwargs)
41
42 class ServiceGroup(NagiosObject):
43         def __init__(self, **kwargs):   
44                 NagiosObject.__init__(self, "servicegroup", **kwargs)
45
46
47 globalservices = []
48 for service in [('NET', "Network Services"),
49                                 ('SSH', "SSH Service"),
50                                 ('SSH806', "Auxiliary SSH Service"),
51                                 ('HTTP', "PlanetFlow HTTP"),
52                                 ('COTOP', "HTTP based COTOP"),
53                                 ]:
54                                 #('PLSOFT', "PlanetLab Software"),
55                                 #('MGMT',  "Remote Management")]:
56         globalservices.append(ServiceGroup(servicegroup_name=service[0], alias=service[1]))
57
58
59 globalhost = [Host(     name="planetlab-host",
60                                         use="generic-host",
61                                         check_period="24x7",
62                                         check_interval="60",
63                                         retry_interval="5",
64                                         max_check_attempts="10",
65                                         check_command="check-host-alive",
66                                         contact_groups="admins",
67                                         register="0")]
68
69 for obj in globalhost + globalservices:
70         print obj.toString()
71
72 plcdb = soltesz.dbLoad("l_plcsites")
73 netid2ip = soltesz.dbLoad("plcdb_netid2ip")
74 lb2hn = soltesz.dbLoad("plcdb_lb2hn")
75
76 for site in plcdb:
77         shortname = site['abbreviated_name']
78         lb = site['login_base']
79         hg = HostGroup(hostgroup_name=lb, alias=shortname)
80         lat = site['latitude']
81         lon = site['longitude']
82         lon_x = -1
83         lat_y = -1
84         if lat is not None and lon is not None:
85                 scale = 5
86                 lon_x = int(180 + lon) * scale
87                 lat_y = int(180 - (lat + 90)) * scale
88
89         if site['login_base'] in lb2hn:
90                 nodes = lb2hn[site['login_base']] # plc.getSiteNodes2(site['login_base'])
91         else:
92                 continue
93
94         if len(nodes) == 0:
95                 continue
96
97         print hg.toString()
98
99         for node in nodes:
100                 hn = node['hostname']
101                 if len(node['nodenetwork_ids']) == 0:
102                         continue
103
104                 ip = netid2ip[node['nodenetwork_ids'][0]]
105
106                 if lon_x is not -1 and lat_y is not -1:
107                         coords="%s,%s" % (lon_x, lat_y)
108                 else:
109                         coords="0,0"
110                         
111                 h = Host(use="planetlab-host",
112                                 host_name=hn,
113                                 alias=hn,
114                                 address=ip,
115                                 d2_coords=coords,
116                                 statusmap_image="icon-system.png",
117                                 hostgroups=lb)
118
119                 print h.toString()
120
121                 s1 = Service(use="generic-service",
122                                         host_name=hn,
123                                         service_description="aSSH",
124                                         display_name="aSSH",
125                                         servicegroups="NET,SSH",
126                                         check_command="check_ssh!-t 120")
127                 s2 = Service(use="generic-service",
128                                         host_name=hn,
129                                         service_description="bSSH806",
130                                         display_name="bSSH806",
131                                         servicegroups="NET,SSH806",
132                                         check_command="check_ssh!-p 806 -t 120")
133                 s3 = Service(use="generic-service",
134                                         host_name=hn,
135                                         service_description="cHTTP",
136                                         display_name="cHTTP",
137                                         servicegroups="NET,HTTP",
138                                         check_command="check_http!-t 120")
139                 s4 = Service(use="generic-service",
140                                         host_name=hn,
141                                         service_description="dCOTOP",
142                                         display_name="dCOTOP",
143                                         servicegroups="NET,COTOP",
144                                         check_command="check_http!-p 3120 -t 120")
145
146                 sd1 = ServiceDependency(host_name=hn,
147                                                                 service_description="aSSH",
148                                                                 dependent_host_name=hn,
149                                                                 dependent_service_description="bSSH806",
150                                                                 execution_failure_criteria="w,u,c,p",)
151
152                 sd2 = ServiceDependency(host_name=hn,
153                                                                 service_description="aSSH",
154                                                                 dependent_host_name=hn,
155                                                                 dependent_service_description="cHTTP",
156                                                                 execution_failure_criteria="w,u,c,p",)
157
158                 sd3 = ServiceDependency(host_name=hn,
159                                                                 service_description="aSSH",
160                                                                 dependent_host_name=hn,
161                                                                 dependent_service_description="dCOTOP",
162                                                                 execution_failure_criteria="w,u,c,p",)
163
164                 for service in [s1,s2,s3,s4,sd1,sd2,sd3]:
165                         print service.toString()
166