I have just finished a simple python script which uses this module to call Zenoss API for getting live events. What the script does:
- connects to zenoss
- reads a file which has one phone number on each line
- sends a message to each number by
POST
ing some data(first of all it authenticates) to a specific URL with the following parameters:phonenr
,smstxt
,Submit
The script speaks for itself and it doesn't need comments. What I'm looking for is some logic in the script / cases that I didn't covered / coding standards that are wrong / design patterns and maybe some advices on how I can improve the code ( in a way that will make each number receive the message at the same time - maybe using multithreading will help )
The file that contains the phone numbers looks like this:
phone_no1 phone_no2 .. phone_non
The code is:
#!/usr/bin/env python # -*- coding: UTF-8 -*- import urllib2 as urllib from urllib import urlencode from os.path import join as joinPath from traceback import print_tb from os.path import isfile from sys import exc_info import myzenoss zenoss = myzenoss.Zenoss('ip_of_zenoss', 'admin', 'pass') hdrs = {'User-Agent': "Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11"} gh_url = 'http://ip-for-post/txsms.cgi' # basically this is an IP for a Mobilink which sends the SMS APPLICATION_PATH = '/srv/sms_alert/' ALERT_POINT_PATH = joinPath(APPLICATION_PATH, 'alert_contact') try: isInProduction = False for evt in zenoss.get_events(): if evt['prodState'] == 'Production': isInProduction = True if isInProduction and isfile(ALERT_POINT_PATH): alertContactContent = None with open(ALERT_POINT_PATH, 'r') as alertContactFile: alertContactContent = alertContactFile.read() alertContactContent = alertContactContent.splitlines() if alertContactContent: evt['summary'] = ':zenoss]\n\nError: {0}'.format(evt['summary']) evt['device']['text'] = '{0}'.format(evt['device']['text']) final_message = '{0}\nName: {1}\n\n'.format(evt['summary'], evt['device']['text']) for alertContactContentLine in alertContactContent: data = {'phonenr': alertContactContentLine, 'smstxt': str(final_message), 'Submit': 'SendSms'} data = urlencode(data) req = urllib.Request(gh_url, data, headers=hdrs) password_manager = urllib.HTTPPasswordMgrWithDefaultRealm() password_manager.add_password(None, gh_url, 'admin', 'password') auth_manager = urllib.HTTPBasicAuthHandler(password_manager) opener = urllib.build_opener(auth_manager) urllib.install_opener(opener) handler = urllib.urlopen(req) break # used this to only get the last event else: evt['summary'] = '#[ERROR: SMS ALERT NO CONTACT]# {0}'.format(evt['summary']) except Exception as e: ex_type, ex, tb = exc_info() print('\n #[ERROR]# TRANSFORM: exception: {ex}\n'.format(ex=e)) print('\n #[ERROR]# TRANSFORM: exception traceback: {trace}\n'.format(trace=print_tb(tb)))