Ejemplo n.º 1
0
def flush_pending_alarm():
    global alarm_count, alarm_subject, alarm_body

    if alarm_count <= 0:
        return;  # no alarm pending

    conf = CONF.alarm()

    # copy to local variables and clear global variables
    count = alarm_count
    subject = '[%s] %s' % (conf['site_name'], alarm_subject)
    if (count > 1):
        subject += ' (+ %d events)' % (count - 1)
    body = alarm_body
    alarm_count = 0
    alarm_subject = ''
    alarm_body = ''

    if conf['mail_alarm']:
        mail_from = conf['mail_user'] + '@' + conf['mail_server'].split(':')[0]

        # send to each mail_list entry for gmail smtp seems not handling mutiple To: addresses
        for mail_to in conf['mail_list']:
            msg = MIMEText(body)
            msg['Subject'] = subject
            msg['From'] = mail_from
            msg['To'] = mail_to

            LOG.info('Send Email Alarm: subject=%s to=%s body=%s', subject, mail_to, body)
            try:
                ms = smtplib.SMTP(conf['mail_server'])
                if conf['mail_tls']:
                    ms.starttls()
                ms.login(conf['mail_user'], conf['mail_password'])
                ms.sendmail(mail_from, mail_to, msg.as_string())
                ms.quit()
            except:
                LOG.exception()

    if conf['slack_alarm']:
        ch = conf['slack_channel'].strip()
        if ch[0] != '#':
            ch = '#' + ch

        LOG.info('Send Slack Alarm: channel=%s text=%s', ch, body)
        sc = SlackClient(conf['slack_token'])
        try:
            sc.api_call("chat.postMessage", channel=ch, text=body)
        except:
            LOG.exception()
Ejemplo n.º 2
0
    def sql_insert_nodes(cls, node_list, username):

        for node in node_list:
            name, ip = str(node).split(':')
            LOG.info('Insert node [%s %s %s]', name, ip, username)
            sql = 'INSERT INTO ' + cls.NODE_INFO_TBL + \
                  ' VALUES (\'' + name + '\',\'' + ip + '\',\'' + username + '\')'
            LOG.info('%s', sql)
            sql_rt = cls.sql_execute(sql)
            if sql_rt != 'SUCCESS':
                LOG.info(" [NODE TABLE] Node data insert fail \n%s", sql_rt)
                sys.exit(1)

            # set status tbl
            sql = 'INSERT INTO ' + cls.STATUS_TBL + \
                  ' VALUES (\'' + name + '\', \'none\', \'none\', \'none\', \'none\', \'none\', \'none\')'
            LOG.info('%s', sql)
            sql_rt = cls.sql_execute(sql)
            if sql_rt != 'SUCCESS':
                LOG.info(" [STATUS TABLE] Node data insert fail \n%s", sql_rt)
                sys.exit(1)

            # set resource tbl
            sql = 'INSERT INTO ' + cls.RESOURCE_TBL + ' VALUES (\'' + name + '\', -1, -1, -1)'
            LOG.info('%s', sql)
            sql_rt = cls.sql_execute(sql)
            if sql_rt != 'SUCCESS':
                LOG.info(" [RESOURCE TABLE] Node data insert fail \n%s",
                         sql_rt)
                sys.exit(1)

            # add Alarm Items
            for item in CONF.alarm()['item_list']:
                LOG.info('Insert item [%s %s]', name, item)
                sql = 'INSERT INTO ' + cls.EVENT_TBL + \
                      ' VALUES (\'' + name + '\',\'' + item + '\',\'none\', \'none\', \'none\')'
                LOG.info('%s', sql)
                sql_rt = cls.sql_execute(sql)
                if sql_rt != 'SUCCESS':
                    LOG.info(" [ITEM TABLE] Item data insert fail \n%s",
                             sql_rt)
                    sys.exit(1)