forked from norcams/himlarcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_services.py
executable file
·48 lines (43 loc) · 1.48 KB
/
check_services.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
import utils
import http.client
import statsd
from himlarcli import utils as himutils
import socket
desc = 'Do a remote check of web services'
options = utils.get_options(desc, hosts=False, debug=True)
# Himmlar config
config = himutils.get_config(options.config)
region = config.get('openstack', 'region')
logger = himutils.get_logger(__name__, config, options.debug)
# statsd
statsd_server = config.get('statsd', 'server')
statsd_port = config.get('statsd', 'port')
prefix = 'uh-iaas.%s.checks' % region
statsd = statsd.StatsClient(statsd_server, statsd_port, prefix=prefix)
# Services to check
services = himutils.load_region_config('config/checks', region=region, log=logger)
for name, check in sorted(services['checks'].items()):
if 'timeout' in check:
timeout = check['timeout']
else:
timeout = 10
if check['ssl']:
c = http.client.HTTPSConnection(check['host'], timeout=timeout)
else:
c = http.client.HTTPConnection(check['host'], timeout=timeout)
try:
c.request("HEAD", check['url'])
response = c.getresponse()
status = response.status
except socket.error as e:
logger.debug("=> error on connect: %s" % e)
status = 0
statsd.gauge(name, status)
if status in check['code']:
statsd.gauge(name, 1)
print('%s -> ok' % check['host'])
else:
statsd.gauge(name, 0)
logger.debug('=> %s = %s' % (name, status))
print('%s -> failed' % check['host'])