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