Esempio n. 1
0
        def wrapped_function(*args, **kwargs):

            try:
                sys.stdout = StringIO()
                function_to_exec = function(*args, **kwargs)
                out = sys.stdout.getvalue()
                messager = HipChat(self.token)
                messager.message_room(message=out,
                                      room_id=self.room_id,
                                      message_from=self.message_from,
                                      **self.kwargs)
            finally:
                sys.stdout.close()
                sys.stdout = sys.__stdout__
            return function_to_exec
Esempio n. 2
0
def run():
    '''
    Worker runner that checks for alerts.
    '''

    global notifier_proxy, settings
    args = get_args_from_cli()
    alerts, settings = get_config(args.config)

    # setting up logging
    if not 'log_level' in settings:
        settings['log_level'] = logging.WARNING
    else:
        settings['log_level'] = settings['log_level'].upper()

    if not 'log_format' in settings:
        settings[
            'log_format'] = '%(asctime)s %(name)s %(levelname)s %(message)s'

    if not 'log_datefmt' in settings:
        settings['log_datefmt'] = '%Y-%m-%d %H:%M:%S'

    logging.basicConfig(filename=settings.get('log_file', None),
                        level=settings['log_level'],
                        format=settings['log_format'],
                        datefmt=settings['log_datefmt'])

    log.info('graphite-alerts started')
    log.debug('Command line arguments:')
    log.debug(args)

    log.debug('Initializing redis at %s', args.redisurl)
    STORAGE = RedisStorage(redis, args.redisurl)

    notifier_proxy.add_notifier(LogNotifier(STORAGE))
    notifier_proxy.add_notifier(ConsoleNotifier(STORAGE))

    settings['graphite_url'] = args.graphite_url or settings['graphite_url']
    if settings['graphite_url'].endswith('/'):
        settings['graphite_url'] = settings['graphite_url'][:-1]
    settings['pagerduty_key'] = args.pagerduty_key or settings['pagerduty_key']
    log.debug('graphite_url: %s', settings['graphite_url'])
    log.debug('pagerduty_key: %s', settings['pagerduty_key'])

    if settings['pagerduty_key']:
        pagerduty_client = PagerDuty(settings['pagerduty_key'])
        notifier_proxy.add_notifier(
            PagerdutyNotifier(pagerduty_client, STORAGE))

    if args.hipchat_key:
        hipchat = HipchatNotifier(HipChat(args.hipchat_key), STORAGE)
        hipchat.add_room(settings['hipchat_room'])
        notifier_proxy.add_notifier(hipchat)

    while True:
        start_time = time.time()
        seen_alert_targets = set()
        for alert in alerts:
            check_for_alert(alert)

        remove_old_seen_alerts()

        # what if cron should trigger us ?
        time_diff = time.time() - start_time
        sleep_for = 60 - time_diff
        if sleep_for > 0:
            sleep_for = 60 - time_diff
            log.info('Sleeping for %s seconds at %s', sleep_for,
                     datetime.utcnow())
            time.sleep(60 - time_diff)
Esempio n. 3
0
import time
from pins import Pins
from hipchat import HipChat

if __name__ == '__main__':
    pins = Pins()
    hipchat = HipChat()

    pins.setup()

    try:
        while True:
            status = hipchat.get_user_status()
            pins.set_color(status.get_color())
            time.sleep(5)
    except KeyboardInterrupt:
        pins.teardown()
Esempio n. 4
0
from notifier_proxy import NotifierProxy
from pagerduty_notifier import PagerdutyNotifier
from redis_storage import RedisStorage

STORAGE = RedisStorage(redis, os.getenv('REDISTOGO_URL'))

pg_key = os.getenv('PAGERDUTY_KEY')
pagerduty_client = PagerDuty(pg_key)

GRAPHITE_URL = os.getenv('GRAPHITE_URL')

notifier_proxy = NotifierProxy()
notifier_proxy.add_notifier(PagerdutyNotifier(pagerduty_client, STORAGE))

if 'HIPCHAT_KEY' in os.environ:
    hipchat = HipchatNotifier(HipChat(os.getenv('HIPCHAT_KEY')), STORAGE)
    hipchat.add_room(os.getenv('HIPCHAT_ROOM'))
    notifier_proxy.add_notifier(hipchat)

ALERT_TEMPLATE = r"""{{level}} alert for {{alert.name}} {{record.target}}.  The
current value is {{current_value}} which passes the {{threshold_level|lower}} value of
{{threshold_value}}. Go to {{graph_url}}.
{% if docs_url %}Documentation: {{docs_url}}{% endif %}.
"""
HTML_ALERT_TEMPLATE = r"""{{level}} alert for {{alert.name}} {{record.target}}.
The current value is {{current_value}} which passes the {{threshold_level|lower}} value of
{{threshold_value}}. Go to <a href="{{graph_url}}">the graph</a>.
{% if docs_url %}<a href="{{docs_url}}">Documentation</a>{% endif %}.
"""