-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpmonit
executable file
·171 lines (128 loc) · 3.78 KB
/
pmonit
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
import getpass
import os
import os.path
import optparse
import stat
import string
##
# Templates of things to add.
#
DIRS = ['etc/monit.d', 'etc/logrotate.d',
'var/lib', 'var/lib/logrotate', 'var/log', 'var/run']
FILES = {
# ~/.monitrc
'.monitrc':
"""set mailserver {smtp_server}
set alert {email}
set logfile {home}/var/log/monit.log
include {home}/etc/monit.d/*
""",
# ~/.daemonrc
'.daemonrc':
"""* pidfiles={home}/var/run
* errlog={home}/var/log/daemon.log
* respawn,acceptable=60,delay=60,limit=3
""",
# ~/etc/logrotate.conf
'etc/logrotate.conf':
"""daily
rotate 4
copytruncate
compress
notifempty
include {home}/etc/logrotate.d
""",
# ~/etc/logrotate.d/monit
'etc/logrotate.d/monit':
"""{home}/var/log/monit.log {{
}}
""",
# ~/etc/logrotate.d/daemon
'etc/logrotate.d/daemon':
"""{home}/var/log/daemon.log {{
}}
""",
}
CRONTABS = [
"@reboot /usr/sbin/monit",
"*/5 * * * * /usr/sbin/monit",
"@daily /usr/sbin/logrotate -s {home}/var/lib/logrotate/status {home}/etc/logrotate.conf",
]
SERVICE_FILES = {
# etc/monit.d/{service}
'etc/monit.d/{service}':
"""check process {service}
with pidfile {home}/var/run/{service}.pid
start program = "/usr/bin/env /usr/bin/daemon --name {service} --output={home}/var/log/{service}.log -- sleep 15"
stop program = "/usr/bin/daemon --name {service} --stop"
""",
# etc/logrotate.d/{service}
'etc/logrotate.d/{service}':
"""{home}/var/log/{service}.log {{
}}
""",
}
##
# Functions that write things.
#
def build_dirs(home):
home = os.path.abspath(home)
for dir in DIRS:
fulldir = os.path.join(home, dir)
if not os.path.exists(fulldir):
os.makedirs(fulldir)
def build_files(home, email, smtp_server):
home = os.path.abspath(home)
for name, template in FILES.iteritems():
fullname = os.path.join(home, name)
f = open(fullname, 'wt')
f.write(template.format(home=home, email=email, smtp_server=smtp_server))
f.close()
os.chmod(fullname, stat.S_IRUSR | stat.S_IWUSR)
def add_crontabs(home):
home = os.path.abspath(home)
for crontab in CRONTABS:
print crontab.format(home=home)
def build_service_files(home, service_name):
home = os.path.abspath(home)
for name, template in SERVICE_FILES.iteritems():
name = name.format(service=service_name)
fullname = os.path.join(home, name)
f = open(fullname, 'wt')
f.write(template.format(home=home, service=service_name))
f.close()
##
# "Subcommand" handlers.
def init(options):
build_dirs(home=options.home)
build_files(options.home, email=options.email, smtp_server=options.smtp_server)
add_crontabs(options.home)
def service(options, service_name):
build_service_files(options.home, service_name)
##
# Command line handling.
def make_option_parser():
parser = optparse.OptionParser(usage="Usage: %prog [options] init|service")
parser.add_option('--home', dest='home', help='home direcory',
default=os.path.expanduser('~'))
parser.add_option('--email', dest='email', help='email address',
default=getpass.getuser()+'@localhost')
parser.add_option('--smtp-server', dest='smtp_server', help='SMTP server',
default='localhost')
return parser
if __name__ == '__main__':
option_parser = make_option_parser()
options, args = option_parser.parse_args()
if len(args) < 1:
option_parser.print_help()
elif args[0] == 'init':
if len(args) > 1:
option_parser.print_help()
else:
init(options)
elif args[0] == 'service':
if len(args) != 2:
option_parser.print_help()
else:
service(options, args[1])