Paste P6506

(An Untitled Masterwork)
ActivePublic

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