def eventMonitor(self,dbcon):
        mydate = str(datetime.datetime.now().date())

        if self.last_event_log_time is None:
            email_vars = ['agent_id','date_id','last_event_log_time','email_sent_time']
            email_result = cassandraDB.customQuery('SELECT {0} from {1} where date_id=%s ORDER BY last_event_log_time DESC LIMIT 1'.format(', '.join(email_vars), notification_table), (mydate,))
            if len(email_result) > 0:
                self.last_event_log_time = email_result[0].last_event_log_time
            else:
                self.last_event_log_time = datetime.datetime.now(tz=pytz.UTC)

        offline_vars = EVENTS_TABLE_VARS.keys()

        if self.last_event_log_time is None:
            result = cassandraDB.customQuery('SELECT {0} from {1} where date_id=%s ORDER BY logged_time'.format(', '.join(offline_vars), EVENTS_TABLE_NAME), (mydate,))
        else:
            result = cassandraDB.customQuery('SELECT {0} from {1} where date_id=%s and logged_time>%s ORDER BY logged_time'.format(', '.join(offline_vars), EVENTS_TABLE_NAME), (mydate, self.last_event_log_time))

        event_count = 0
        result = list(result)


        if len(result) > 0:

            #create a map from all possible events to a list of notification addresses and their priority
            notificationchannel_map = dict()
            dbcon.execute("select id from possible_events") #get all possible event ids
            event_ids = dbcon.fetchall()
            for event_id in event_ids:
                #get all possible alert types associated with those events
                event_id = event_id[0]
                dbcon.execute("select alerttypes_id from alert_types_associated_events where possibleevents_id=%s",
                                    (event_id,))
                if dbcon.rowcount:
                    alert_types = dbcon.fetchall()
                    for alert_type in alert_types:
                        # get the alert and its priority assigned to that alert_type. If two alerts are tried to assign to same alert_type,
                        # they will be merged and hence, there will still be one-to-one relation
                        dbcon.execute("select id, priority_id from alerts where alert_type_id=%s", (alert_type,))
                        if dbcon.rowcount:
                            alert_and_priority_list = dbcon.fetchall()
                            #get all the notification addresses assigned to that alert
                            for alert_id, priority_id in alert_and_priority_list:
                                dbcon.execute(
                                    "select notificationchanneladdresses_id from alerts_alert_channels where alerts_id=%s",
                                    (alert_id,))
                                if dbcon.rowcount:
                                    notificationchannels = dbcon.fetchall()
                                    if event_id not in notificationchannel_map:
                                        notificationchannel_map[event_id] = dict()
                                    if priority_id not in notificationchannel_map[event_id]:
                                        notificationchannel_map[event_id][priority_id] = set()

                                    notificationchannel_map[event_id][priority_id].update(notificationchannels)

                #if there are two entries for the same channel for the same event with different priority, drop the lower priority
                if event_id in notificationchannel_map:
                    priorities = sorted(notificationchannel_map[event_id].keys())
                    for i in range(0,len(priorities)-1):
                        for j in range(i+1,len(priorities)):
                            overlap = notificationchannel_map[event_id][priorities[i]].intersection(notificationchannel_map[event_id][priorities[j]])
                            notificationchannel_map[event_id][priorities[i]] -= overlap

            # create a map of priority, notification_channel --> to list of events to send to that channel
            notification_map = dict()
            for event in result:

                # get event_id from the possible events table for current event
                dbcon.execute("select id from possible_events where event_name=%s", (event.event,))
                if dbcon.rowcount:
                    event_id = dbcon.fetchone()[0]
                    # get a dict of notification_priority, notification address) for this type of event from the map previously created
                    if event_id not in notificationchannel_map:
                        continue
                    prioritized_channels = notificationchannel_map[event_id]
                    for priority_id, notification_channels in prioritized_channels.items():
                        if priority_id not in notification_map:
                            notification_map[priority_id] = dict()
                        for (notification_channel,) in notification_channels:
                            if notification_channel not in notification_map[priority_id]:
                                notification_map[priority_id][notification_channel] = list()
                            # if there are two notification channel with two different priorities for the same event, the second priority takes over
                            notification_map[priority_id][notification_channel].append(event)

                self.last_event_log_time = event.logged_time
            dbcon.commit()
            notification_variables['date_id'] = mydate
            notification_variables['last_event_log_time'] = self.last_event_log_time
            notification_variables['email_sent_time'] = datetime.datetime.utcnow()
            self.TSDCustomInsert(notification_variables, notification_log_variables, notification_table)

            for priority in notification_map.keys():

                for notification_channel,eventlist in notification_map[priority].items():
                    dbcon.execute("select notification_channel.notification_channel, alerts_notificationchanneladdress.notify_address from alerts_notificationchanneladdress INNER JOIN notification_channel ON notification_channel.id=alerts_notificationchanneladdress.notification_channel_id  where alerts_notificationchanneladdress.id=%s",(notification_channel,))
                    channeltype, address = dbcon.fetchone()
                    if channeltype.lower()=="email":
                        report_lines = """
                            <html>
                            <head></head>
                            <body>
                            <p><b>Event Report</b></p>
                            """

                        for event in eventlist:
                            event_count += 1
                            report_lines += 'On '+str(date_converter.UTCToLocal(event.time).date()) + ', at '\
                                            +datetime.datetime.strftime(date_converter.UTCToLocal(event.time),'%H:%M:%S') + '<b> '+str(event.source) + ' : ' + str(event.event)+'</b> because of ' + str(event.reason)
                            report_lines += '<br/>'
                            self.last_event_log_time = event.logged_time

                        if event_count == 0:
                            return

                        report_lines += """
                        </body>
                        </html>
                        """
                        print report_lines
                        dbcon.execute("select priority_level from priority where id=%s",(priority,))
                        priority_text = dbcon.fetchone()[0] if dbcon.rowcount else ""
                        self.send_email(address,priority_text,report_lines)
                    elif channeltype.lower() =='text':
                        sms_text = ''
                        for event in eventlist:
                            sms_text += event.source + ' ' + event.event
                        self.send_sms(address,sms_text)
Exemple #2
0
    "icm1_bgqbyppscakd_1": "T-3 46 Children, Youth, & Fam",
    "icm1_bgqbzonzdfsq_1": "T-12 Potomac room",
    "icm1_bgqbvednmeuj_1": "T-1 8-12 Area Agency On Aging",
    "icm1_bgqbzjeasozn_1": "T-7 27 Mental Health & Disabl",
    "wnc_401370_1": "power_5_Agency_Aging_Power",
    "icm1_bgqbyancdjdz_1": "T-4 47 Dept of Family SVCS EX",
    "icm1_bgqbybnamlhj_1": "T-5 39A Veterans Affairs",
    "icm1_bgqbzhruhouv_1": "T-6 43 Office of the Director",
    "icm1_bgqbzlzjhmbk_1": "T-9 77 CSEP",
    "icm1_bgqbzkcoyfvf_1": "T-8 25 MGMT Services",
    "icm1_bgqbznkgojde_1": "T-11 Blue Crab Dining room",
    "wnc_381662_1": "power_12_Agency_Aging",
    "icm1_bgqbzmfdwycr_1": "t10-multipurpose",
    "icm1_bgqbynxedgjs_1": "T-2 4-5 Area Agency On Aging"
}

folder = today.replace('-', '')
os.makedirs('/home/bemoss/BEMOSS_DATA/PGCOUNTY/' + folder + '/')
makeConnection()

for agent_id in downloads.keys():
    table = 'b' + agent_id.lower()
    qstring = "select * from bemossspace." + table + " where agent_id='" + agent_id.upper(
    ) + "' and date_id='" + today + "';"
    result = customQuery(qstring)
    filename = downloads[agent_id] + '-' + today + '.csv'
    resultFile = open(
        '/home/bemoss/BEMOSS_DATA/PGCOUNTY/' + folder + '/' + filename, 'wb')
    wr = csv.writer(resultFile, dialect='excel')
    wr.writerows(result)
    def setup(self,dbcon):
        #Create offline_event table
        retry = True
        while retry:
            try:
                retry = False
                cassandraDB.createCustomTable(columns=EVENTS_TABLE_VARS, partition_keys=EVENTS_TABLE_PARTITION_KEYS,
                                              clustering_keys=EVENTS_TABLE_CLUSTERING_KEYS, tablename=EVENTS_TABLE_NAME)
            except cassandraDB.AlreadyExists as er:
                pass
            except (cluster.NoHostAvailable, cluster.OperationTimedOut) as er:
                print er
                print 'Retrying ...'
                retry = True
                time.sleep(10)
            except Exception as er:
                print er
                print 'Cannot create offline table'
                raise

        #Create platform_event table
        try:
            cassandraDB.createCustomTable(columns=platform_log_variables, partition_keys=platform_log_partition_keys,
                                          clustering_keys=platform_log_clustering_keys,tablename=platform_table)
        except cassandraDB.AlreadyExists as er:
            pass
        except Exception as er:
            print er
            print 'Cannot create platform table'
            raise

        #Create email_event table
        try:
            cassandraDB.createCustomTable(columns=notification_log_variables, partition_keys=notification_log_partition_keys,
                                          clustering_keys=notification_log_clustering_keys, tablename=notification_table)
        except cassandraDB.AlreadyExists as er:
            pass
        except Exception as er:
            print er
            print 'Cannot create email table'
            raise

        try:
            retrieve_vars = ['agent_id','start_time','end_time','event_id']
            result = cassandraDB.customQuery('SELECT {0} from {1} where agent_id=%s ORDER BY start_time DESC LIMIT 1'.\
                        format(', '.join(retrieve_vars),platform_table,self.agent_id),(platform_variables['agent_id'],))
        except Exception as er:
            print er
            print 'Error querrying platform table'
        else:
            if len(result)>0:
                last_entry = result[0]
                #Platformmonitor just started. Save last failure and current restart info
                #crash
                self.EventRegister(dbcon, 'shutdown',source=self.node_name,event_time=last_entry.end_time,notify=False)
                #restart
                self.EventRegister(dbcon, 'restart',source=self.node_name,notify=False)
            else:
                print 'Fresh boy'

            #Platform monitor agent fresh start. Save the event on platform_event table.
            #for reference: platform_log_variables = {'agent_id':'text','date_id':'text','start_time':'TIMESTAMP','end_time':'TIMESTAMP','event_id':'UUID'}
            platform_variables['start_time']=datetime.datetime.utcnow()
            platform_variables['date_id']=str(datetime.datetime.now().date())
            platform_variables['event_id']=uuid.uuid4()
            platform_variables['end_time']=datetime.datetime.utcnow()
            platform_variables['logged_time']=datetime.datetime.utcnow()
            self.TSDCustomInsert(platform_variables,platform_log_variables,platform_table)