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