def POST(self, **post_params): params = cherrypy.request.body.params print params #return if its a deployment message alert = params.get('alert', None) if not alert: return('Deployment alert') alert = json.loads(alert) app_name = alert.get("application_name", None) or alert["alert_policy_name"] severity = alert['severity'] alert_url = alert['alert_url'] description = alert['long_description'] #some messages are filtered out #update settings.filters with more filters for filter in settings.filters: if filter in description.lower(): return('Alert filtered out') user_details = NewrelicHelper().fetch_users(app_name) short_url = Bitly().shorten_url(alert_url) #users_channels subscribed to an application for user_detail in user_details: user_email = user_detail[2] greeting = user_detail[0] and "Hi %s, " % user_detail[0] or "Hi, " sms_msg = greeting + description + ". It is %s. Alert url: %s" % (severity, short_url) phone_number = settings.users.get(user_email, None) if phone_number: Sms().send(phone_number, sms_msg) #all the alerts are sent to default person for (default_user, ph_no) in settings.default.items(): sms_msg = "Hi, " + description + ". It is %s. Alert url: %s" % (severity, short_url) Sms().send(ph_no, sms_msg) #plugin alerts (its a different path all together) all_plugins = self.plugins() all_users = self.get_all_users() matched_plugins = [p for p in all_plugins if p.lower() in app_name.lower() or p.lower() in description] if matched_plugins: matched_plugins = matched_plugins[0] people_subscribed_to_plugin = self.plugin_users_map(matched_plugins) for user_email in people_subscribed_to_plugin: u_d = all_users.get(user_email, None) if u_d: greeting = u_d[1] and "Hi %s, " % u_d[1] or "Hi, " sms_msg = greeting + description + ". It is %s. Alert url: %s" % (severity, short_url) print sms_msg if u_d[0] != "new": Sms().send(u_d[0], sms_msg) return('Message sent')
from helper.newrelic_helper import NewrelicHelper print "connected to db" a = sqlite3.connect("users.db") print "creating user_plugins table" a.execute("create table user_plugins(user varchar, plugins varchar(1000), ph_num varchar(20), first_name varchar(50))") print "creating plugin_users table" a.execute("create table plugin_users(plugin varchar, users varchar(1000), type varchar(10))") print "filling in plugins" for plugin in settings.plugins: a.execute("insert into plugin_users values(?,?,?)", (plugin, json.dumps([]), "plugin")) newrelic = NewrelicHelper() print "filling apps" for app in newrelic.all_alert_policies(): a.execute("insert into plugin_users values(?,?,?)", (app, json.dumps([]), "app")) print "filling users" old_users = settings.users all_users = newrelic.all_users() new_users = [u for u in all_users.keys() if u not in old_users.keys()] for (user,p_num) in old_users.items(): a.execute("insert into user_plugins values(?,?,?,?)", (user,json.dumps([]), p_num, all_users[user])) for nu in new_users: a.execute("insert into user_plugins values(?,?,?,?)", (nu,json.dumps([]), "new", all_users[nu]))
import sqlite3 import settings import json from helper.newrelic_helper import NewrelicHelper print "connecting to db" a = sqlite3.connect("users.db") newrelic = NewrelicHelper() print "fetching new users" old_users = a.execute('select user from user_plugins').fetchall() old_users = [i[0] for i in old_users] all_users = newrelic.all_users() new_users = [u for u in all_users.keys() if u not in old_users] print "inserting %d new users" % len(new_users) for nu in new_users: a.execute("insert into user_plugins values(?,?,?,?)", (nu,json.dumps([]), "new", all_users[nu])) a.commit() print "done :^)"
import sqlite3 import settings import json from helper.newrelic_helper import NewrelicHelper a = sqlite3.connect("users.db") print "connected to db" newrelic = NewrelicHelper() apps = newrelic.all_alert_policies() print "pulled all policies" plugins = a.execute("select plugin from plugin_users").fetchall() plugins = [i[0] for i in plugins] print "pulled old plugins" new_policies = [i for i in apps if not i in plugins] print new_policies for app in new_policies: a.execute("insert into plugin_users values(?,?,?)", (app, json.dumps([]), "app")) print "inserting new policies %s" % str(new_policies) a.commit()