<commit_before>#!/usr/bin/env python
########################################
#
# alert-ganglia.py - Alert Ganglia module
#
########################################

# TODO
# 1. query for cluster metrics so can alert off them too

import os
import sys
import optparse
import time
import urllib2
try:
    import json
except ImportError:
    import simplejson as json
import yaml
import stomp
import time
import datetime
import logging
import copy
import uuid
import re

__version__ = '1.5'

BROKER_LIST  = [('devmonsvr01',61613), ('localhost', 61613)] # list of brokers for failover
ALERT_QUEUE  = '/queue/alerts'
EXPIRATION_TIME = 600 # seconds = 10 minutes

API_SERVER = 'ganglia.gul3.gnl:80'
API_VERSION = 'latest'
# METRIC_API = 'http://%s/ganglia/api/%s/metric-data' % (API_SERVER, API_VERSION)
METRIC_API = 'http://%s/ganglia/api/snapshot.py' % (API_SERVER)

RULESFILE = '/opt/alerta/conf/alert-ganglia.yaml'
LOGFILE = '/var/log/alerta/alert-ganglia.log'
PIDFILE = '/var/run/alerta/alert-ganglia.pid'

SEVERITY_CODE = {
    # ITU RFC5674 -> Syslog RFC5424
    'CRITICAL':       1, # Alert
    'MAJOR':          2, # Crtical
    'MINOR':          3, # Error
    'WARNING':        4, # Warning
    'NORMAL':         5, # Notice
    'INFORM':         6, # Informational
    'DEBUG':          7, # Debug
}

_check_rate   = 120             # Check rate of alerts

host_info = dict()
host_metrics = dict()
rules = dict()

currentCount  = dict()
currentState  = dict()
previousSeverity = dict()

# Convert string to number
def num(value):
    try:
        return int(value)
    except ValueError:
        pass
    try:
        return float(value)
    except:
        return value

# Query Ganglia Snapshot API for metric data
def get_metrics():
    global _refresh_rate, environment, host_info, host_metrics

    now = time.time()

    url = "%s?environment=%s" % (METRIC_API, environment)
    logging.info('Getting metrics from %s', url)

    try:
        output = urllib2.urlopen(url).read()
        response = json.loads(output)
    except urllib2.URLError, e:
        logging.error('Could not retrieve data from %s - %s', url, e)
        return

    logging.info('Snapshot taken at %s', response['response']['localTime'])

    hosts = [host for host in response['response']['hosts']]
    for h in hosts:
        host_info[h['host']] = dict()
        host_info[h['host']] = {
            'id':          h['id'],
            'environment': h['environment'],
            'grid':        h['grid'],
            'cluster':     h['cluster'],
            'ipAddress':   h['ipAddress'],
            'location':    h['location'],
            'uptime':      h['gmondStarted'],
            'graphUrl':    h.get('graphUrl', 'none')<commit_msg>Clear host info and metric dicts on each iteration<commit_after>#!/usr/bin/env python
########################################
#
# alert-ganglia.py - Alert Ganglia module
#
########################################

# TODO
# 1. query for cluster metrics so can alert off them too

import os
import sys
import optparse
import time
import urllib2
try:
    import json
except ImportError:
    import simplejson as json
import yaml
import stomp
import time
import datetime
import logging
import copy
import uuid
import re

__version__ = '1.5'

BROKER_LIST  = [('devmonsvr01',61613), ('localhost', 61613)] # list of brokers for failover
ALERT_QUEUE  = '/queue/alerts'
EXPIRATION_TIME = 600 # seconds = 10 minutes

API_SERVER = 'ganglia.gul3.gnl:80'
API_VERSION = 'latest'
# METRIC_API = 'http://%s/ganglia/api/%s/metric-data' % (API_SERVER, API_VERSION)
METRIC_API = 'http://%s/ganglia/api/snapshot.py' % (API_SERVER)

RULESFILE = '/opt/alerta/conf/alert-ganglia.yaml'
LOGFILE = '/var/log/alerta/alert-ganglia.log'
PIDFILE = '/var/run/alerta/alert-ganglia.pid'

SEVERITY_CODE = {
    # ITU RFC5674 -> Syslog RFC5424
    'CRITICAL':       1, # Alert
    'MAJOR':          2, # Crtical
    'MINOR':          3, # Error
    'WARNING':        4, # Warning
    'NORMAL':         5, # Notice
    'INFORM':         6, # Informational
    'DEBUG':          7, # Debug
}

_check_rate   = 120             # Check rate of alerts

host_info = dict()
host_metrics = dict()
rules = dict()

currentCount  = dict()
currentState  = dict()
previousSeverity = dict()

# Convert string to number
def num(value):
    try:
        return int(value)
    except ValueError:
        pass
    try:
        return float(value)
    except:
        return value

# Query Ganglia Snapshot API for metric data
def get_metrics():
    global _refresh_rate, environment, host_info, host_metrics

    now = time.time()

    url = "%s?environment=%s" % (METRIC_API, environment)
    logging.info('Getting metrics from %s', url)

    try:
        output = urllib2.urlopen(url).read()
        response = json.loads(output)
    except urllib2.URLError, e:
        logging.error('Could not retrieve data from %s - %s', url, e)
        return

    logging.info('Snapshot taken at %s', response['response']['localTime'])

    host_info = {}
    host_metrics = {}

    hosts = [host for host in response['response']['hosts']]
    for h in hosts:
        host_info[h['host']] = dict()
        host_info[h['host']] = {
            'id':          h['id'],
            'environment': h['environment'],
            'grid':        h['grid'],
            'cluster':     h['cluster'],
            'ipAddress':   h['ipAddress'],
            'location':    h['location'],
            'uptime':      h['gmondStarted'],
            'graphUrl':    h.get('graphUrl', 'none')
{'fraction_non_alphanumeric': 0.15695471766191593, 'fraction_numerical': 0.01587587948764207, 'mean_word_length': 2.7358490566037736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6232726', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1759, 'n_words': 465}