Page MenuHomePhabricator
Paste P6506

(An Untitled Masterwork)

Authored by Dzahn on Dec 28 2017, 10:49 PM.
# send SMS to Icinga contacts
# 2017 - Daniel Zahn - Wikimedia Foundation
import sys
import argparse
import re
import smtplib
# full path to an Icinga contacts.cfg file
contacts_file = '/home/dzahn/contacts.cfg'
# mail sent from (icinga server) uses these
mail_from = ""
mail_server = ''
# get address for a user from an Icinga contacts file
# In the Wikimedia setup, 'address1' is used in Icinga contact
# definitions for an email address (containing a phone number)
# to be used with an Email2SMS gateway.
# Therefore e-mailing that address means sending SMS to users.
def get_address(contact_name):
with open(contacts_file, "rt") as contacts_data:
contact_data =
contact_data = contact_data.split("contact_name")
for contact in contact_data:
if contact_name in contact:
regex = re.compile("address1\s+\d+@.*(.*?)")
address1 ="address1")
return address1[1].strip()
# send an e-mail
def send_email(mail_to, mail_msg, mail_server = '', mail_from = ""):
server = smtplib.SMTP(mail_server)
server.sendmail(mail_from, mail_to, mail_msg)
# list all contact names found in the contacts file
def list_contacts_all(contacts_file):
with open(contacts_file, "rt") as contacts_data:
for line in contacts_data:
if "contact_name" in line:
contact_name = line.split("contact_name")
return sorted(contact_list, key=str.lower)
# list all contact names who can receive SMS (have an address1 set)
def list_contacts_sms(contacts_file):
with open(contacts_file, "rt") as contacts_data:
contact_data =
contact_data = contact_data.split("define contact")
for contact in contact_data:
regex = re.compile("address1\s+\d+@.*(.*?)")
contact_name = contact.split("contact_name")
contact_name = contact_name[1].split("\n")
return sorted(contact_list, key=str.lower)
# parse command-line arguments
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--list", action='store_true', help="List all contacts who can be sent SMS")
parser.add_argument("-lf", "--listfull", action='store_true', help="List all contacts who can be sent SMS along with their addresses.")
parser.add_argument("-la", "--listall", action='store_true', help="List all contacts found in contact file")
parser.add_argument("-a", "--address", nargs=1, help="Get the address for a contact name.")
parser.add_argument("-s", "--send", nargs=2, help="Send SMS to a contact name.")
if len(sys.argv)==1:
args = parser.parse_args()
# list all contacts in the Icinga contacts file who can be sent SMS (usage: -l | --list)
if args.list:
print ("\n".join(list_contacts_sms(contacts_file)))
# list all contacts in the Icinga contacts file (usage: -la | --listall)
if args.listall:
print ("\n".join(list_contacts_all(contacts_file)))
# list all possible (SMS'able) contacts along with their address1
if args.listfull:
contact_list = list_contacts_sms(contacts_file)
for contact_name in contact_list:
print "%s %s" % (contact_name, contact_addr)
# get the address for an Icinga contact name (usage: -a | --address <contact_name>)
if args.address:
contact_name = sys.argv[2]
# print "looking up address for '%s'" % contact_name
print contact_addr
# send SMS (via E-mail gateway) to an Icinga contact name
if args.send:
contact_name = sys.argv[2]
mail_msg = sys.argv[3]
contact_addr = get_address(contact_name)
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)
# send_email(contact_addr, mail_msg)

Event Timeline

Dzahn created this paste.Dec 28 2017, 10:49 PM