Merge pull request from alerta/update-urlmon

Update urlmon for version 5
This commit is contained in:
Nick Satterly 2017-11-16 00:02:07 +00:00 committed by GitHub
commit 0691944915
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 35 deletions
integrations
mailer
urlmon

View file

@ -2,7 +2,7 @@
import setuptools
version = '5.0.2'
version = '5.0.3'
setuptools.setup(
name="alerta-mailer",

View file

@ -1,5 +1,5 @@
import os
import platform
import sys
import time
import urllib2
@ -9,9 +9,7 @@ import Queue
import re
import logging
from alertaclient.api import ApiClient
from alertaclient.alert import Alert
from alertaclient.heartbeat import Heartbeat
from alertaclient.api import Client
from BaseHTTPServer import BaseHTTPRequestHandler as BHRH
@ -180,13 +178,13 @@ class WorkerThread(threading.Thread):
if 'Content-type' in headers and headers['Content-type'] == 'application/json':
try:
body = json.loads(body)
except ValueError, e:
except ValueError as e:
LOG.error('Could not evaluate rule %s: %s', rule, e)
try:
eval(rule) # NOTE: assumes request body in variable called 'body'
except (SyntaxError, NameError, ZeroDivisionError), e:
except (SyntaxError, NameError, ZeroDivisionError) as e:
LOG.error('Could not evaluate rule %s: %s', rule, e)
except Exception, e:
except Exception as e:
LOG.error('Could not evaluate rule %s: %s', rule, e)
else:
if not eval(rule):
@ -207,26 +205,24 @@ class WorkerThread(threading.Thread):
tags = check.get('tags', list())
threshold_info = "%s : RT > %d RT > %d x %s" % (check['url'], warn_thold, crit_thold, check.get('count', 1))
urlmonAlert = Alert(
resource=resource,
event=event,
correlate=correlate,
group=group,
value=value,
severity=severity,
environment=environment,
service=service,
text=text,
event_type='serviceAlert',
tags=tags,
attributes={
'thresholdInfo': threshold_info
}
)
try:
self.api.send(urlmonAlert)
except Exception, e:
self.api.send_alert(
resource=resource,
event=event,
correlate=correlate,
group=group,
value=value,
severity=severity,
environment=environment,
service=service,
text=text,
event_type='serviceAlert',
tags=tags,
attributes={
'thresholdInfo': threshold_info
}
)
except Exception as e:
LOG.warning('Failed to send alert: %s', e)
self.queue.task_done()
@ -283,16 +279,16 @@ class WorkerThread(threading.Thread):
else:
req = urllib2.Request(url, headers=headers)
response = urllib2.urlopen(req, None, MAX_TIMEOUT)
except ValueError, e:
except ValueError as e:
LOG.error('Request failed: %s', e)
except urllib2.URLError, e:
except urllib2.URLError as e:
if hasattr(e, 'reason'):
reason = str(e.reason)
status = None
elif hasattr(e, 'code'):
reason = None
status = e.code
except Exception, e:
except Exception as e:
LOG.warning('Unexpected error: %s', e)
else:
status = response.getcode()
@ -321,7 +317,7 @@ class UrlmonDaemon(object):
self.running = True
self.queue = Queue.Queue()
self.api = self.api = ApiClient(endpoint=settings.ENDPOINT, key=settings.API_KEY)
self.api = Client(endpoint=settings.ENDPOINT, key=settings.API_KEY)
# Start worker threads
LOG.debug('Starting %s worker threads...', SERVER_THREADS)
@ -329,7 +325,7 @@ class UrlmonDaemon(object):
w = WorkerThread(self.queue, self.api)
try:
w.start()
except Exception, e:
except Exception as e:
LOG.error('Worker thread #%s did not start: %s', i, e)
continue
LOG.info('Started worker thread: %s', w.getName())
@ -340,10 +336,10 @@ class UrlmonDaemon(object):
self.queue.put((check, time.time()))
LOG.debug('Send heartbeat...')
heartbeat = Heartbeat(tags=[__version__])
try:
self.api.send(heartbeat)
except Exception, e:
origin = '{}/{}'.format('urlmon', platform.uname()[1])
self.api.heartbeat(origin, tags=[__version__])
except Exception as e:
LOG.warning('Failed to send heartbeat: %s', e)
time.sleep(LOOP_EVERY)