clearer names for actions, and infer actions better
[monitor.git] / nagios / plc_users_to_nagios.py
1 #!/usr/bin/python
2
3 from nagiosobjects import *
4 import plc
5 from generic import *
6 import sys
7
8
9 def getContactsAndContactGroupsFor(lb, type, email_list, testing=True):
10
11         if len(email_list) == 0:
12                 cg1 = ContactGroup(contactgroup_name="%s-%s" % (lb,type),
13                                                 alias="%s-%s" % (lb,type))
14                 return [cg1]
15
16         contact_list = []
17         person_list = []
18         count = 0
19         for person in email_list:
20                 # TODO: for testing!
21                 if testing:
22                         person="soltesz+%s%s%s@cs.princeton.edu" % ( lb, type, count )
23                 c1 = Contact(contact_name=person.replace("+", ""),
24                                                 host_notifications_enabled=1,
25                                                 service_notifications_enabled=1,
26                                                 host_notification_period="24x7",
27                                                 service_notification_period="24x7",
28                                                 host_notification_options="d,r,s",
29                                                 service_notification_options="c,w,r",
30                                                 host_notification_commands="monitor-notify-host-by-email",
31                                                 service_notification_commands="monitor-notify-service-by-email",
32                                                 email=person)
33                 count += 1
34                 contact_list.append(c1)
35                 person_list.append(person.replace("+",""))
36
37         cg1 = ContactGroup(contactgroup_name="%s-%s" % (lb,type),
38                                                 alias="%s-%s" % (lb,type),
39                                                 members=",".join(person_list))
40
41         contact_list.append(cg1)
42
43         return contact_list
44
45
46 print Command(command_name="monitor-notify-host-by-email",
47                                                  command_line="""/usr/share/monitor/nagios/actions/mail.py --host 1 --servicenotificationnumber $SERVICENOTIFICATIONNUMBER$ --hostnotificationnumber $HOSTNOTIFICATIONNUMBER$ --notificationtype $NOTIFICATIONTYPE$ --hostname $HOSTNAME$ --hoststate $HOSTSTATE$ --hostaddress $HOSTADDRESS$ --hostoutput "$HOSTOUTPUT$" --longdatetime "$LONGDATETIME$" --notificationitype $NOTIFICATIONTYPE$ --contactemail $CONTACTEMAIL$""").toString()
48
49 print Command(command_name="monitor-notify-service-by-email",
50                                                     command_line="""/usr/share/monitor/nagios/actions/mail.py --service 1 --servicenotificationnumber $SERVICENOTIFICATIONNUMBER$ --hostnotificationnumber $HOSTNOTIFICATIONNUMBER$ --notificationtype $NOTIFICATIONTYPE$ --hostname $HOSTNAME$ --hoststate $HOSTSTATE$ --hostaddress $HOSTADDRESS$ --hostoutput "$HOSTOUTPUT$" --longdatetime "$LONGDATETIME$" --notificationitype $NOTIFICATIONTYPE$ --servicedesc $SERVICEDESC$ --hostalias $HOSTALIAS$ --contactemail $CONTACTEMAIL$ --servicestate "$SERVICESTATE$" --serviceoutput "$SERVICEOUTPUT$" --contactgroupname $CONTACTGROUPNAME$ """).toString()
51
52
53 l_sites = plc.api.GetSites({'peer_id' : None})
54 #l_sites = plc.api.GetSites({'login_base' : ['asu', 'gmu', 'gt']})
55 #l_sites = plc.api.GetSites([10243, 22, 10247, 138, 139, 10050, 10257, 
56 #                            18, 20, 21, 10134, 24, 10138, 10141, 30, 31, 
57 #                            33, 10279, 41, 29, 10193, 10064, 81, 10194, 
58 #                            10067, 87, 10208, 10001, 233, 157, 10100, 10107])
59
60 test_emails = False
61 if len(sys.argv) > 1:
62     test_emails = True
63
64 for index,site in enumerate(l_sites):
65         shortname = site['abbreviated_name']
66         lb = site['login_base']
67         print >>sys.stderr, "Collecting emails for %s (%s/%s)" % (lb, index, len(l_sites))
68
69         # NOTE: do duplcate groups create duplicate emails?
70         cl1 = getContactsAndContactGroupsFor(lb, "techs", plc.getTechEmails(lb), test_emails)
71         cl2 = getContactsAndContactGroupsFor(lb, "pis", plc.getPIEmails(lb), test_emails)
72         # NOTE: slice users will change often.
73         cl3 = getContactsAndContactGroupsFor(lb, "sliceusers", plc.getSliceUserEmails(lb), test_emails)
74
75         for c in [cl1,cl2,cl3]:
76                 for i in c:
77                         print i.toString()
78