add some service escalation templates
[monitor.git] / tools / plc_users_to_nagios.py
1 #!/usr/bin/python
2
3 class NagiosObject(object):
4         trans = {'d2_coords': '2d_coords'}
5
6         def __init__(self, id, **kwargs):
7                 self.id = id
8                 self.kwords = kwargs.keys()
9                 for key in self.kwords:
10                         self.__setattr__(key, kwargs[key])
11
12         def toString(self):
13                 ret = ""
14                 ret += "define %s {\n" % self.id
15                 for key in self.kwords:
16                         if key in self.trans:
17                                 ret += "    %s   %s\n" % (self.trans[key], self.__getattribute__(key))
18                         else:
19                                 ret += "    %s   %s\n" % (key, self.__getattribute__(key))
20                 ret += "}\n"
21                 return ret
22
23 class Host(NagiosObject):
24         def __init__(self, **kwargs):   
25                 NagiosObject.__init__(self, "host", **kwargs)
26
27 class HostGroup(NagiosObject):
28         def __init__(self, **kwargs):   
29                 NagiosObject.__init__(self, "hostgroup", **kwargs)
30
31 class HostEscalation(NagiosObject):
32         def __init__(self, **kwargs):   
33                 NagiosObject.__init__(self, "hostescalation", **kwargs)
34
35 class Contact(NagiosObject):
36         def __init__(self, **kwargs):   
37                 NagiosObject.__init__(self, "contact", **kwargs)
38
39 class ContactGroup(NagiosObject):
40         def __init__(self, **kwargs):   
41                 NagiosObject.__init__(self, "contactgroup", **kwargs)
42
43 class Service(NagiosObject):
44         def __init__(self, **kwargs):   
45                 NagiosObject.__init__(self, "service", **kwargs)
46
47 class ServiceDependency(NagiosObject):
48         def __init__(self, **kwargs):   
49                 NagiosObject.__init__(self, "servicedependency", **kwargs)
50
51 class ServiceEscalation(NagiosObject):
52         def __init__(self, **kwargs):   
53                 NagiosObject.__init__(self, "serviceescalation", **kwargs)
54
55 class ServiceGroup(NagiosObject):
56         def __init__(self, **kwargs):   
57                 NagiosObject.__init__(self, "servicegroup", **kwargs)
58
59 def getContactsAndContactGroupsFor(lb, type, email_list):
60
61         contact_list = []
62         for person in email_list:
63                 c1 = Contact(contact_name=person,
64                                                 host_notifications_enabled=1,
65                                                 service_notifications_enabled=1,
66                                                 host_notification_period="24x7",
67                                                 service_notification_period="24x7",
68                                                 host_notification_options="d,r,s",
69                                                 service_notification_options="c,r",
70                                                 host_notification_commands="notify-by-email",
71                                                 service_notification_commands="notify-by-email",
72                                                 email=person)
73                 contact_list.append(c1)
74
75         cg1 = ContactGroup(contactgroup_name="%s-%s" % (lb,type),
76                                                 alias="%s-%s" % (lb,type),
77                                                 members=",".join(email_list))
78
79         contact_list.append(cg1)
80
81         return contact_list
82
83
84 globalservices = []
85 for service in [('NET', "Network Services"),
86                                 ('SSH', "SSH Service"),
87                                 ('SSH806', "Auxiliary SSH Service"),
88                                 ('HTTP', "PlanetFlow HTTP"),
89                                 ('COTOP', "HTTP based COTOP"),
90                                 ]:
91                                 #('PLSOFT', "PlanetLab Software"),
92                                 #('MGMT',  "Remote Management")]:
93         globalservices.append(ServiceGroup(servicegroup_name=service[0], alias=service[1]))
94
95
96 globalhost = [Host(     name="planetlab-host",
97                                         use="generic-host",
98                                         check_period="24x7",
99                                         check_interval="120",
100                                         retry_interval="10",
101                                         max_check_attempts="6",
102                                         check_command="check-host-alive",
103                                         contact_groups="admins",
104                                         register="0")]
105
106 for obj in globalhost + globalservices:
107         print obj.toString()
108
109 from monitor.wrapper import plccache
110
111 plcdb = plccache.l_sites
112 netid2ip = plccache.d_from_l(plccache.plc.api.GetInterfaces(), 'interface_id')
113 lb2hn = plccache.plcdb_lb2hn
114
115 sites = plccache.plc.api.GetSites([10243, 22, 10247, 138, 139, 10050, 10257, 18, 20, 
116                                                         21, 10134, 24, 10138, 10141, 30, 31, 33, 10279, 41, 29, 10193, 10064, 81,
117                                                         10194, 10067, 87, 10208, 10001, 233, 157, 10100, 10107])
118
119 for site in sites:
120         shortname = site['abbreviated_name']
121         lb = site['login_base']
122         hg = HostGroup(hostgroup_name=lb, alias=shortname)
123         lat = site['latitude']
124         lon = site['longitude']
125         lon_x = -1
126         lat_y = -1
127         if lat is not None and lon is not None:
128                 scale = 5
129                 lon_x = int(180 + lon) * scale
130                 lat_y = int(180 - (lat + 90)) * scale
131
132         if site['login_base'] in lb2hn:
133                 nodes = lb2hn[site['login_base']]
134         else:
135                 continue
136
137         if len(nodes) == 0:
138                 continue
139
140         print hg.toString()
141
142         # NOTE: do duplcate groups create duplicate emails?
143         cl1 = getContactsAndContactGroupsFor(lb, "techs", plccache.plc.getTechEmails(lb))
144         cl2 = getContactsAndContactGroupsFor(lb, "pis", plccache.plc.getPIEmails(lb))
145         # NOTE: slice users will change often.
146         cl3 = getContactsAndContactGroupsFor(lb, "sliceusers", plccache.plc.getSliceUserEmails(lb))
147
148         for c in [cl1,cl2,cl3]:
149                 for i in c:
150                         print i.toString()
151
152         for node in nodes:
153                 hn = node['hostname']
154                 if len(node['interface_ids']) == 0:
155                         continue
156
157                 ip = netid2ip[str(node['interface_ids'][0])]['ip']
158
159                 if lon_x is not -1 and lat_y is not -1:
160                         coords="%s,%s" % (lon_x, lat_y)
161                 else:
162                         coords="0,0"
163                         
164                 h = Host(use="planetlab-host",
165                                 host_name=hn,
166                                 alias=hn,
167                                 address=ip,
168                                 d2_coords=coords,
169                                 statusmap_image="icon-system.png",
170                                 hostgroups=lb)
171
172                 print h.toString()
173
174                 s1 = Service(use="generic-service",
175                                         host_name=hn,
176                                         service_description="aSSH",
177                                         display_name="aSSH",
178                                         servicegroups="NET,SSH",
179                                         check_command="check_ssh!-t 120")
180                 s2 = Service(use="generic-service",
181                                         host_name=hn,
182                                         service_description="bSSH806",
183                                         display_name="bSSH806",
184                                         servicegroups="NET,SSH806",
185                                         check_command="check_ssh!-p 806 -t 120")
186                 s3 = Service(use="generic-service",
187                                         host_name=hn,
188                                         service_description="cHTTP",
189                                         display_name="cHTTP",
190                                         servicegroups="NET,HTTP",
191                                         check_command="check_http!-t 120")
192                 s4 = Service(use="generic-service",
193                                         host_name=hn,
194                                         service_description="dCOTOP",
195                                         display_name="dCOTOP",
196                                         servicegroups="NET,COTOP",
197                                         check_command="check_http!-p 3120 -t 120")
198
199                 se1 = ServiceEscalation( host_name=hn,
200                                                 service_description='aSSH',
201                                                 first_notification=0,
202                                                 last_notification=2,
203                                                 notification_interval=24*60*3.5,
204                                                 escalation_options="r,c",
205                                                 contact_groups="%s-techs" % lb)
206
207                 se2 = ServiceEscalation( host_name=hn,
208                                                 service_description='aSSH',
209                                                 first_notification=2,
210                                                 last_notification=4,
211                                                 notification_interval=24*60*3.5,
212                                                 escalation_options="r,c",
213                                                 contact_groups="%s-techs,%s-pis" % (lb,lb))
214
215                 se3 = ServiceEscalation( host_name=hn,
216                                                 service_description='aSSH',
217                                                 first_notification=4,
218                                                 last_notification=0,
219                                                 notification_interval=24*60*3.5,
220                                                 escalation_options="r,c",
221                                                 contact_groups="%s-techs,%s-pis,%s-sliceusers" % (lb,lb,lb))
222
223                 sd1 = ServiceDependency(host_name=hn,
224                                                                 service_description="aSSH",
225                                                                 dependent_host_name=hn,
226                                                                 dependent_service_description="bSSH806",
227                                                                 execution_failure_criteria="w,u,c,p",)
228
229                 sd2 = ServiceDependency(host_name=hn,
230                                                                 service_description="aSSH",
231                                                                 dependent_host_name=hn,
232                                                                 dependent_service_description="cHTTP",
233                                                                 execution_failure_criteria="w,u,c,p",)
234
235                 sd3 = ServiceDependency(host_name=hn,
236                                                                 service_description="aSSH",
237                                                                 dependent_host_name=hn,
238                                                                 dependent_service_description="dCOTOP",
239                                                                 execution_failure_criteria="w,u,c,p",)
240
241                 for service in [s1,s2,s3,s4,se1,se2,se3,sd1,sd2,sd3]:
242                         print service.toString()
243