1 | #!/usr/bin/python |
---|---|
2 | # icinga_sms.py |
3 | # |
4 | # send SMS to Icinga contacts |
5 | # |
6 | # https://phabricator.wikimedia.org/T82937 |
7 | # 2017 - Daniel Zahn - Wikimedia Foundation |
8 | # |
9 | import sys |
10 | import argparse |
11 | import re |
12 | import smtplib |
13 | |
14 | # full path to an Icinga contacts.cfg file |
15 | contacts_file = '/home/dzahn/contacts.cfg' |
16 | |
17 | # mail sent from einsteinium.wikimedia.org (icinga server) uses these |
18 | mail_from = "icinga@wikimedia.org" |
19 | mail_server = 'mx1001.wikimedia.org' |
20 | |
21 | # get address for a user from an Icinga contacts file |
22 | # |
23 | # In the Wikimedia setup, 'address1' is used in Icinga contact |
24 | # definitions for an email address (containing a phone number) |
25 | # to be used with an Email2SMS gateway. |
26 | # Therefore e-mailing that address means sending SMS to users. |
27 | def get_address(contact_name): |
28 | with open(contacts_file, "rt") as contacts_data: |
29 | contact_data = contacts_data.read() |
30 | contact_data = contact_data.split("contact_name") |
31 | for contact in contact_data: |
32 | if contact_name in contact: |
33 | regex = re.compile("address1\s+\d+@.*(.*?)") |
34 | regex.search(contact) |
35 | address1 = regex.search(contact).group(0).split("address1") |
36 | return address1[1].strip() |
37 | |
38 | # send an e-mail |
39 | def send_email(mail_to, mail_msg, mail_server = 'mx1001.wikimedia.org', mail_from = "icinga@wikimedia.org"): |
40 | server = smtplib.SMTP(mail_server) |
41 | server.sendmail(mail_from, mail_to, mail_msg) |
42 | server.quit() |
43 | |
44 | # list all contact names found in the contacts file |
45 | def list_contacts_all(contacts_file): |
46 | contact_list=[] |
47 | with open(contacts_file, "rt") as contacts_data: |
48 | for line in contacts_data: |
49 | if "contact_name" in line: |
50 | contact_name = line.split("contact_name") |
51 | contact_list.append(contact_name[1].strip()) |
52 | return sorted(contact_list, key=str.lower) |
53 | |
54 | # list all contact names who can receive SMS (have an address1 set) |
55 | def list_contacts_sms(contacts_file): |
56 | contact_list=[] |
57 | with open(contacts_file, "rt") as contacts_data: |
58 | contact_data = contacts_data.read() |
59 | contact_data = contact_data.split("define contact") |
60 | for contact in contact_data: |
61 | regex = re.compile("address1\s+\d+@.*(.*?)") |
62 | if regex.search(contact): |
63 | contact_name = contact.split("contact_name") |
64 | contact_name = contact_name[1].split("\n") |
65 | contact_list.append(contact_name[0].strip()) |
66 | |
67 | return sorted(contact_list, key=str.lower) |
68 | |
69 | # parse command-line arguments |
70 | parser = argparse.ArgumentParser() |
71 | parser.add_argument("-l", "--list", action='store_true', help="List all contacts who can be sent SMS") |
72 | parser.add_argument("-lf", "--listfull", action='store_true', help="List all contacts who can be sent SMS along with their addresses.") |
73 | parser.add_argument("-la", "--listall", action='store_true', help="List all contacts found in contact file") |
74 | parser.add_argument("-a", "--address", nargs=1, help="Get the address for a contact name.") |
75 | parser.add_argument("-s", "--send", nargs=2, help="Send SMS to a contact name.") |
76 | |
77 | if len(sys.argv)==1: |
78 | parser.print_help() |
79 | sys.exit(1) |
80 | |
81 | args = parser.parse_args() |
82 | |
83 | # list all contacts in the Icinga contacts file who can be sent SMS (usage: -l | --list) |
84 | if args.list: |
85 | print ("\n".join(list_contacts_sms(contacts_file))) |
86 | |
87 | # list all contacts in the Icinga contacts file (usage: -la | --listall) |
88 | if args.listall: |
89 | print ("\n".join(list_contacts_all(contacts_file))) |
90 | |
91 | # list all possible (SMS'able) contacts along with their address1 |
92 | if args.listfull: |
93 | contact_list = list_contacts_sms(contacts_file) |
94 | for contact_name in contact_list: |
95 | contact_addr=get_address(contact_name) |
96 | print "%s %s" % (contact_name, contact_addr) |
97 | |
98 | # get the address for an Icinga contact name (usage: -a | --address <contact_name>) |
99 | if args.address: |
100 | contact_name = sys.argv[2] |
101 | # print "looking up address for '%s'" % contact_name |
102 | contact_addr=get_address(contact_name) |
103 | print contact_addr |
104 | |
105 | # send SMS (via E-mail gateway) to an Icinga contact name |
106 | if args.send: |
107 | contact_name = sys.argv[2] |
108 | mail_msg = sys.argv[3] |
109 | contact_addr = get_address(contact_name) |
110 | print "sending email to SMS gateway for %s at %s | content: '%s' (from: %s via %s)" % (contact_name, contact_addr, mail_msg, mail_from, mail_server) |
111 | # send_email(contact_addr, mail_msg) |
112 | |
Paste P6506
(An Untitled Masterwork)
(An Untitled Masterwork)
Authored by Dzahn on Dec 28 2017, 10:49 PM.
Tags
None
Subscribers
None