def run(args): config = get_config(args.config) alerts = config.alerts() notifier_proxy = create_notifier_proxy(config) graphite_url = config.get('GRAPHITE_URL') while True: start_time = time.time() seen_alert_targets = set() for alert in alerts: target = alert.get('target') try: records = get_records( graphite_url, requests.get, GraphiteDataRecord, target, from_=alert.get('from'), ) except requests.exceptions.RequestException: description, html_description = missing_target_descriptions( graphite_url, alert, target, Level.NO_DATA, None ) notifier_proxy.notify( alert, target, Level.NO_DATA, description, html_description ) records = [] for record in records: name = alert.get('name') target = record.target if (name, target) not in seen_alert_targets: # print 'Checking', (name, target) update_notifiers( notifier_proxy, alert, record, graphite_url ) seen_alert_targets.add((name, target)) # else: # print 'Seen', (name, target) time_diff = time.time() - start_time sleep_for = 60 - time_diff if sleep_for > 0: sleep_for = 60 - time_diff print 'Sleeping for {0} seconds at {1}'.format( sleep_for, datetime.datetime.utcnow() ) time.sleep(60 - time_diff)
def setUp(self): self.base_url = 'BASE_URL' self.record_class = MagicMock(GraphiteDataRecord) self.http_get = MagicMock(requests.get) self.http_get().content = SAMPLE_RESPONSE self.target = 'TARGET' self.records = get_records( self.base_url, self.http_get, self.record_class, self.target, )
def run(args): print 'graphite-pager {0}'.format(__version__) config = get_config(args.config) alerts = config.alerts() notifier_proxy = create_notifier_proxy(config) graphite_url = config.get('GRAPHITE_URL') while True: start_time = time.time() seen_alert_targets = set() for alert in alerts: target = alert.get('target') try: records = get_records( graphite_url, requests.get, GraphiteDataRecord, target, from_=alert.get('from'), ) except requests.exceptions.RequestException: if not alert.alert_data['allow_no_data']: print "Error, {0}".format(alert.alert_data) update_notifiers_missing(notifier_proxy, alert, config) records = [] for record in records: name = alert.get('name') if not record.target: continue target = record.target if (name, target) not in seen_alert_targets: update_notifiers( notifier_proxy, alert, record, graphite_url ) seen_alert_targets.add((name, target)) time_diff = time.time() - start_time sleep_for = 60 - time_diff if sleep_for > 0: sleep_for = 60 - time_diff print 'Sleeping for {0} seconds at {1}'.format( sleep_for, datetime.datetime.utcnow() ) time.sleep(60 - time_diff)
def run(args): config = get_config(args.config) alerts = config.alerts() notifier_proxy = create_notifier_proxy(config) graphite_url = config.get('GRAPHITE_URL') while True: start_time = time.time() seen_alert_targets = set() for alert in alerts: target = alert.get('target') try: records = get_records( graphite_url, requests.get, GraphiteDataRecord, target, from_=alert.get('from'), ) except requests.exceptions.RequestException: notification = 'Could not get target: {}'.format(target) print notification notifier_proxy.notify(alert, target, Level.CRITICAL, notification, notification) records = [] for record in records: name = alert.get('name') target = record.target if (name, target) not in seen_alert_targets: print 'Checking', (name, target) update_notifiers(notifier_proxy, alert, record, graphite_url) seen_alert_targets.add((name, target)) else: print 'Seen', (name, target) time_diff = time.time() - start_time sleep_for = 60 - time_diff if sleep_for > 0: sleep_for = 60 - time_diff print 'Sleeping for {0} seconds at {1}'.format( sleep_for, datetime.datetime.utcnow()) time.sleep(60 - time_diff)