def run(self): api = ApiClient(endpoint=OPTIONS['endpoint'], key=OPTIONS['key']) keep_alive = 0 while not self.should_stop: for alertid in on_hold.keys(): try: (alert, hold_time) = on_hold[alertid] except KeyError: continue if time.time() > hold_time: self.send_email(alert) try: del on_hold[alertid] except KeyError: continue if keep_alive >= 10: tag = OPTIONS['smtp_host'] or 'alerta-mailer' try: api.send(Heartbeat(tags=[tag])) except Exception as e: time.sleep(5) continue keep_alive = 0 keep_alive += 1 time.sleep(2)
class SnmpTrapHandler(object): def __init__(self): self.api = None def run(self): endpoint = os.environ.get('ALERTA_ENDPOINT', 'http://localhost:8080') key = os.environ.get('ALERTA_API_KEY', None) self.api = ApiClient(endpoint=endpoint, key=key) data = sys.stdin.read() LOG.info('snmptrapd -> %r', data) data = unicode(data, 'utf-8', errors='ignore') LOG.debug('unicoded -> %s', data) snmptrapAlert = SnmpTrapHandler.parse_snmptrap(data) if snmptrapAlert: try: self.api.send(snmptrapAlert) except Exception, e: LOG.warning('Failed to send alert: %s', e) LOG.debug('Send heartbeat...') heartbeat = Heartbeat(tags=[__version__]) try: self.api.send(heartbeat) except Exception, e: LOG.warning('Failed to send heartbeat: %s', e)
def main(): api = ApiClient() listener = Listener() while True: listener.send_cmd('READY\n') headers, body = listener.wait() event = headers['eventname'] if event.startswith('TICK'): supervisorAlert = Heartbeat( origin='supervisord', tags=[headers['ver'], event] ) else: if event.endswith('FATAL'): severity = 'critical' elif event.endswith('BACKOFF'): severity = 'warning' elif event.endswith('EXITED'): severity = 'minor' else: severity = 'normal' supervisorAlert = Alert( resource='%s:%s' % (platform.uname()[1], body['processname']), environment='Production', service=['supervisord'], event=event, correlate=[ 'PROCESS_STATE_STARTING', 'PROCESS_STATE_RUNNING', 'PROCESS_STATE_BACKOFF', 'PROCESS_STATE_STOPPING', 'PROCESS_STATE_EXITED', 'PROCESS_STATE_STOPPED', 'PROCESS_STATE_FATAL', 'PROCESS_STATE_UNKNOWN' ], value='serial=%s' % headers['serial'], severity=severity, origin=headers['server'], text='State changed from %s to %s.' % (body['from_state'], event), raw_data='%s\n\n%s' % (json.dumps(headers), json.dumps(body)) ) try: api.send(supervisorAlert) except Exception as e: listener.log_stderr(e) listener.send_cmd('RESULT 4\nFAIL') else: listener.send_cmd('RESULT 2\nOK')
def main(): api = ApiClient() listener = Listener() while True: listener.send_cmd('READY\n') headers, body = listener.wait() event = headers['eventname'] if event.startswith('TICK'): supervisorAlert = Heartbeat(origin='supervisord', tags=[headers['ver'], event]) else: if event.endswith('FATAL'): severity = 'critical' elif event.endswith('BACKOFF'): severity = 'warning' elif event.endswith('EXITED'): severity = 'minor' else: severity = 'normal' supervisorAlert = Alert( resource='%s:%s' % (platform.uname()[1], body['processname']), environment='Production', service=['supervisord'], event=event, correlate=[ 'PROCESS_STATE_STARTING', 'PROCESS_STATE_RUNNING', 'PROCESS_STATE_BACKOFF', 'PROCESS_STATE_STOPPING', 'PROCESS_STATE_EXITED', 'PROCESS_STATE_STOPPED', 'PROCESS_STATE_FATAL', 'PROCESS_STATE_UNKNOWN' ], value='serial=%s' % headers['serial'], severity=severity, origin=headers['server'], text='State changed from %s to %s.' % (body['from_state'], event), raw_data='%s\n\n%s' % (json.dumps(headers), json.dumps(body))) try: api.send(supervisorAlert) except Exception as e: listener.log_stderr(e) listener.send_cmd('RESULT 4\nFAIL') else: listener.send_cmd('RESULT 2\nOK')
def alert(self, matches): # Matches is a list of match dictionaries. # It contains more than one match when the alert has # the aggregation option set # for match in matches: api = ApiClient(endpoint=self.endpoint, key=self.api_key) alert = Alert(resource=self.resource, event=self.event, environment=self.environment, service=self.service, severity=self.severity, text=self.text, value=self.value) api.send(alert)
def main(): config_file = os.environ.get('ALERTA_CONF_FILE') or OPTIONS['config_file'] config = configparser.RawConfigParser(defaults=OPTIONS) try: config.read(os.path.expanduser(config_file)) except Exception: sys.exit("Problem reading configuration file %s - is this an ini file?" % config_file) parser = argparse.ArgumentParser( prog='zabbix-alerta', usage='zabbix-alerta SENDTO SUMMARY BODY', description='Zabbix-to-Alerta integration script', epilog=epilog, formatter_class=argparse.RawTextHelpFormatter ) parser.add_argument( 'sendto', help='config profile or alerta API endpoint and key' ) parser.add_argument( 'summary', help='alert summary' ) parser.add_argument( 'body', help='alert body (see format below)' ) args, left = parser.parse_known_args() # sendto=apiUrl[;key] if args.sendto.startswith('http://') or args.sendto.startswith('https://'): want_profile = None try: OPTIONS['endpoint'], OPTIONS['key'] = args.sendto.split(';', 1) except ValueError: OPTIONS['endpoint'] = args.sendto # sendto=profile else: want_profile = args.sendto or os.environ.get('ALERTA_DEFAULT_PROFILE') or config.defaults().get('profile') if want_profile and config.has_section('profile %s' % want_profile): for opt in OPTIONS: try: OPTIONS[opt] = config.getboolean('profile %s' % want_profile, opt) except (ValueError, AttributeError): OPTIONS[opt] = config.get('profile %s' % want_profile, opt) else: for opt in OPTIONS: try: OPTIONS[opt] = config.getboolean('DEFAULT', opt) except (ValueError, AttributeError): OPTIONS[opt] = config.get('DEFAULT', opt) parser.set_defaults(**OPTIONS) args = parser.parse_args() if args.debug: LOG.basicConfig(stream=sys.stderr, format=LOG_FORMAT, datefmt=LOG_DATE_FMT, level=LOG.DEBUG) else: LOG.basicConfig(filename=LOG_FILE, format=LOG_FORMAT, datefmt=LOG_DATE_FMT, level=LOG.INFO) LOG.info("[alerta] endpoint=%s key=%s", args.endpoint, args.key) api = ApiClient(endpoint=args.endpoint, key=args.key, ssl_verify=args.sslverify) LOG.debug("[alerta] sendto=%s, summary=%s, body=%s", args.sendto, args.summary, args.body) try: alert = parse_zabbix(args.summary, args.body) api.send(alert) except (SystemExit, KeyboardInterrupt): LOG.warning("Exiting zabbix-alerta.") sys.exit(0) except Exception as e: LOG.error(e, exc_info=1) sys.exit(1)
#!/usr/bin/env python from alertaclient.api import ApiClient from alertaclient.alert import Alert api = ApiClient() alert = Alert(resource='web-server-01', event='HttpError', correlate=['HttpOK'], group='Web', environment='Production', service=['theguardian.com'], severity='major', value='Bad Gateway (502)', text='Web server error.', tags=['web', 'dc1', 'london'], attributes={'customer': 'The Guardian'}) print alert try: print api.send(alert) except Exception as e: print e
class CloudWatch(object): def __init__(self): self.api = ApiClient() try: connection = boto.sqs.connect_to_region( AWS_REGION, aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY) except boto.exception.SQSError as e: LOG.error('SQS API call failed: %s', e) sys.exit(1) try: self.sqs = connection.create_queue(AWS_SQS_QUEUE) self.sqs.set_message_class(RawMessage) except boto.exception.SQSError as e: LOG.error('SQS queue error: %s', e) sys.exit(1) def run(self): while True: LOG.debug('Waiting for CloudWatch alarms on %s...', AWS_SQS_QUEUE) try: notification = self.sqs.read(wait_time_seconds=20) except boto.exception.SQSError as e: LOG.warning('Could not read from queue: %s', e) time.sleep(20) continue if notification: cloudwatchAlert = self.parse_notification(notification) try: self.api.send(cloudwatchAlert) except Exception as e: LOG.warning('Failed to send alert: %s', e) self.sqs.delete_message(notification) LOG.debug('Send heartbeat...') heartbeat = Heartbeat(tags=[__version__]) try: self.api.send(heartbeat) except Exception as e: LOG.warning('Failed to send heartbeat: %s', e) def parse_notification(self, notification): notification = json.loads(notification.get_body()) alarm = json.loads(notification['Message']) if 'Trigger' not in alarm: return # Defaults resource = '%s:%s' % (alarm['Trigger']['Dimensions'][0]['name'], alarm['Trigger']['Dimensions'][0]['value']) event = alarm['AlarmName'] severity = self.cw_state_to_severity(alarm['NewStateValue']) group = 'CloudWatch' value = alarm['Trigger']['MetricName'] text = alarm['AlarmDescription'] service = [ AWS_ACCOUNT_ID.get(alarm['AWSAccountId'], 'AWSAccountId:' + alarm['AWSAccountId']) ] tags = [alarm['Trigger']['Namespace']] correlate = list() origin = notification['TopicArn'] timeout = None create_time = datetime.datetime.strptime(notification['Timestamp'], '%Y-%m-%dT%H:%M:%S.%fZ') raw_data = notification['Message'] cloudwatchAlert = Alert( resource=resource, event=event, correlate=correlate, group=group, value=value, severity=severity, environment='Production', service=service, text=text, event_type='cloudwatchAlarm', tags=tags, attributes={ 'awsMessageId': notification['MessageId'], 'awsRegion': alarm['Region'], 'thresholdInfo': alarm['NewStateReason'] }, origin=origin, timeout=timeout, create_time=create_time, raw_data=raw_data, ) return cloudwatchAlert @staticmethod def cw_state_to_severity(state): if state == 'ALARM': return 'major' elif state == 'INSUFFICIENT_DATA': return 'warning' elif state == 'OK': return 'normal' else: return 'unknown'