-
Notifications
You must be signed in to change notification settings - Fork 0
/
keepaneye
executable file
·94 lines (72 loc) · 3.16 KB
/
keepaneye
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/python3
#
# sd-keepaneye, keeping an eye on systemd.
# Copyright (C) 2021 Alexandre Rossi <alexandre.rossi@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import argparse
import logging
import os
import sys
import systemd.journal
import tomli
INSTALLED = os.path.split(os.path.dirname(__file__))[-1] in ('bin', 'sbin')
def load_conf():
config_name = 'keepaneye.conf'
if INSTALLED:
config_path = os.path.join('/etc', config_name)
else:
config_path = os.path.join(os.path.dirname(__file__), config_name)
with open(config_path, 'rb') as f:
return tomli.load(f)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Systemd Event Monitor.')
parser.add_argument('-d', '--debug',
action='store_true',
dest='debug',
help='Enable DEBUG level loggging and log to STDOUT instead of journald')
subparsers = parser.add_subparsers(dest='subcommand', help='sub-commands help')
parser_daemon = subparsers.add_parser('daemon', help='launch daemon watching systemd D-bus events')
parser_notify = subparsers.add_parser('notify', help='notify for the specified service')
parser_notify.add_argument('-s', '--service',
action='store',
dest='service',
required=True,
help='The service to notify for')
parser_notify.add_argument('-t', '--type',
action='store',
dest='notify_type',
default='stdout',
help='The type of notification to use (default is stdout)')
args = parser.parse_args()
conf = load_conf()
log = logging.getLogger()
if args.debug:
conf['keepaneye']['debug'] = True
if conf['keepaneye']['debug']:
log_level = logging.DEBUG
log_handler = logging.StreamHandler(sys.stdout)
log_handler.setFormatter(logging.Formatter('%(levelname)s:%(message)s'))
else:
log_level = logging.INFO
log_handler = systemd.journal.JournalHandler(SYSLOG_IDENTIFIER='keepaneyed')
log.setLevel(log_level)
log.addHandler(log_handler)
if args.subcommand == 'daemon':
import sdkeepaneye.daemon
sdkeepaneye.daemon.start(conf)
elif args.subcommand == 'notify':
import sdkeepaneye.notify
sdkeepaneye.notify.notify(args.notify_type, args.service)