-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchdog.py
executable file
·78 lines (59 loc) · 1.98 KB
/
watchdog.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
77
#!/usr/bin/python
#
# Copyright 2009, Robby Walker
#
# rss-shortener/server.py
# Watchdog for the RSS shortener server.
from email.mime.text import MIMEText
import optparse
import os
import smtplib
import subprocess
import time
COMMAND = ["/sbin/service", "rss-shortener"]
def call(command):
devnull = open(os.devnull, "w")
return not subprocess.call(COMMAND + [command], stdout=devnull, stderr=devnull)
def status():
return call("status")
def start():
return call("start")
class GmailServer(object):
def __init__(self, fromAddress, password):
self._fromAddress = fromAddress
self._mailServer = smtplib.SMTP('smtp.gmail.com', 587)
self._mailServer.ehlo()
self._mailServer.starttls()
self._mailServer.ehlo()
self._mailServer.login(fromAddress, password)
def send(self, to, subject, text):
msg = MIMEText(text)
msg['Subject'] = subject
msg['From'] = self._fromAddress
msg['To'] = to
self._mailServer.sendmail(self._fromAddress, [to], msg.as_string())
def close(self):
self._mailServer.close()
if __name__ == "__main__":
parser = optparse.OptionParser()
parser.add_option("-f", "--from", dest="fromAddress", type="string", default=None,
help="send email from FROM", metavar="FROM")
parser.add_option("-p", "--password", dest="password", type="string", default=None,
help="login to gmail with PASSWORD", metavar="PASSWORD")
parser.add_option("-t", "--to", dest="to", type="string", default=None,
help="send email to TO", metavar="TO")
(options, args) = parser.parse_args()
print "Running at %s with args %r" % (time.ctime(), options)
message = []
if not status():
message.append('Server is not running.')
if start():
message.append('Restarted successfully')
else:
message.append('Server would not restart')
message = '\n'.join(message)
if message:
print message
server = GmailServer(options.fromAddress, options.password)
server.send(options.to, 'rss-shortener watchdog', message)
print