forked from reddit/monitors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalerts.py
76 lines (64 loc) · 2.18 KB
/
alerts.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/python
import ConfigParser
import logging
import logging.handlers
import os
import sys
import time
import wessex
__all__ = ["harold", "config"]
harold = None
config = None
def init(config_path='production.ini'):
global config, harold
config = load_config(path=config_path)
if config.has_section('logging'):
configure_logging(config)
harold = get_harold(config)
def load_config(path='production.ini'):
config = ConfigParser.RawConfigParser()
config.read([path])
return config
def get_harold(config):
harold_host = config.get('harold', 'host')
harold_port = config.getint('harold', 'port')
harold_secret = config.get('harold', 'secret')
return wessex.Harold(
host=harold_host, port=harold_port, secret=harold_secret)
class StreamLoggingFormatter(logging.Formatter):
def formatTime(self, record, datefmt=None):
timestamp = time.strftime(datefmt)
return timestamp % dict(ms=(1000 * record.created) % 1000)
def _get_logging_handler(config):
mode = config.get('logging', 'mode')
if mode == 'file':
return logging.FileHandler(config.get('logging', 'file'))
elif mode == 'stderr':
return logging.StreamHandler()
elif mode == 'syslog':
return logging.handlers.SysLogHandler(
config.get('logging', 'syslog_addr'))
else:
raise ValueError('unsupported logging mode: %r' % mode)
def _get_logging_formatter(config):
mode = config.get('logging', 'mode')
if mode == 'syslog':
app_name = os.path.basename(sys.argv[0])
return logging.Formatter(
'%s: [%%(levelname)s] %%(message)s' % app_name)
else:
return StreamLoggingFormatter(
'%(levelname).1s%(asctime)s: %(message)s',
'%m%d %H:%M:%S.%%(ms)03d')
def _get_logging_level(config):
if config.has_option('logging', 'level'):
return config.get('logging', 'level')
else:
return logging.INFO
def configure_logging(config):
ch = _get_logging_handler(config)
ch.setFormatter(_get_logging_formatter(config))
logger = logging.getLogger()
logger.setLevel(_get_logging_level(config))
logger.addHandler(ch)
return logger