forked from jbohman/logsandra
-
Notifications
You must be signed in to change notification settings - Fork 9
/
logsandra-monitord.py
executable file
·63 lines (52 loc) · 2.31 KB
/
logsandra-monitord.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
#!/usr/bin/env python
"""
Logsandra monitor daemon
"""
import sys
import os
import optparse
import logging
from logsandra import utils
from logsandra.utils.daemon import Daemon
from logsandra.utils import config
from logsandra.monitor import monitor
class Application(Daemon):
def run(self):
self.logger.debug('Starting monitor daemon')
m = monitor.Monitor(self.config, False)
m.run()
if __name__ == '__main__':
default_working_dir = os.path.abspath(os.path.dirname(__file__))
default_config_file = os.path.join(default_working_dir, 'conf', 'logsandra.yaml')
usage = 'usage: %prog [options] start|stop|restart'
parser = optparse.OptionParser(usage=usage)
parser.add_option('--config-file', dest='config_file', metavar='FILE', default=default_config_file)
parser.add_option('--working-directory', dest='working_directory', metavar='DIRECTORY', default=default_working_dir)
parser.add_option('--pid', dest='pid_file', metavar='FILE', default=os.path.join(default_working_dir, 'logsandra-monitord.pid'))
parser.add_option('--application-data-directory', dest='application_data_directory', default=utils.application_data_directory('logsandra'))
(options, args) = parser.parse_args()
if not os.path.isdir(options.application_data_directory):
os.makedirs(options.application_data_directory)
logfile = os.path.join(options.application_data_directory, 'logsandra.log')
logging.basicConfig(filename=logfile, level=logging.DEBUG, format="%(asctime)s %(levelname)-5.5s [%(name)s] [%(threadName)s] %(message)s")
logger = logging.getLogger('logsandra.monitord')
application = Application(options.pid_file, working_directory=options.working_directory, stdout=logfile, stderr=logfile)
application.config = config.parse(options.config_file)
application.logger = logger
if len(args) == 1:
if args[0] == 'start':
application.start()
if args[0] == 'start-fg':
logger.addHandler(logging.StreamHandler())
application.run()
elif args[0] == 'stop':
application.stop()
elif args[0] == 'restart':
application.restart()
else:
print parser.get_usage()
sys.exit(2)
sys.exit(0)
else:
print parser.get_usage()
sys.exit(2)