/
site-alerter-daemon.py
executable file
·184 lines (158 loc) · 4.67 KB
/
site-alerter-daemon.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python
################################################################################
# site-alerter-daemon.py
#
#
# Created by Brian Baughman on 3/7/13.
# Copyright 2013 Brian Baughman. All rights reserved.
################################################################################
try:
import sys, re, time
from daemon import runner
from os import environ, path, _exit, makedirs, stat, devnull, access, X_OK
from subprocess import call, STDOUT, PIPE, Popen
pathname = path.dirname(sys.argv[0])
except:
print 'Failed to load base modules'
sys.exit(-1)
try:
try:
import xml.etree.ElementTree as ET
except:
try:
import elementtree.ElementTree as ET
except:
print 'Failed to load ElementTree'
_exit(-1)
import smtplib
from email import MIMEText
import matplotlib
matplotlib.use('agg')
from matplotlib import dates as mpldates
from matplotlib.cbook import is_numlike
from matplotlib import rcParams
from matplotlib.pyplot import draw, figure
from matplotlib.dates import datestr2num,date2num
import matplotlib.image as image
from numpy import deg2rad, rad2deg, arange, asarray, array, pi, where, iterable
import ephem
from bitly import shorten
# Home directory
homedir = environ['HOME']
# stop if something looks wrong
except:
print 'Failed to load modules'
_exit(-1)
# Check SSH
devnullfobj = open(devnull,'w')
if call(['rsync','--version'],stdout=devnullfobj,stderr=devnullfobj):
print 'rsync not available!'
_exit(-1)
devnullfobj.close()
if not path.isfile('%s/site-alerter.py'%pathname):
print 'Cannot file site-alerter.py!'
_exit(-1)
if not access('%s/site-alerter.py'%pathname,X_OK):
print 'Cannot execute site-alerter.py!'
_exit(-1)
################################################################################
# Environment Setup
################################################################################
try:
gcndaemonlog = environ['GCNDAEMONLOG']
except:
gcndaemonlog = '%s/logs/gcn-daemon.log'%homedir
try:
gcndaemonlock = environ['GCNDAEMONLOCK']
except:
gcndaemonlock = '/tmp/site-alerter-daemon.lock'
# Server hosting GCN DB and web content
try:
gcndbsrv = environ['GCNDBSRV']
except:
print 'GCNDBSRV is not defined!'
_exit(-2)
# Location of GCN database on GCNDBSRV
try:
gcndbosrv = environ['GCNDBOSRV']
except:
print 'GCNDBOSRV is not defined!'
_exit(-2)
gcndbfname = path.basename(gcndbosrv)
# Location of web content on GCNDBSRV
try:
gcnwebosrv = environ['GCNWEBOSRV']
except:
print 'GCNWEBOSRV is not defined!'
_exit(-2)
try:
gcnweb = environ['GCNWEB']
except:
gcnweb = '%s/public_html'%homedir
try:
gcninter = float(environ['GCNINTER'])
except:
gcninter = 60
################################################################################
# App to run
################################################################################
class App():
def __init__(self,log=devnull,inter=60,
pidfile='/tmp/site-alerter-daemon.lock'):
self.stdin_path = '/dev/null'
self.stdout_path = log
self.stderr_path = log
self.pidfile_path = pidfile
self.pidfile_timeout = inter+1
self.umask = 0022
self.inter = inter
self.modtime = None
self.buildsite = False
def run(self):
self.Check()
def UpdateDB(self):
'''
Updates the local copy of the GCN DB
'''
devnullfobj = open(devnull,'w')
call(['rsync','-azq','%s:%s'%(gcndbsrv,gcndbosrv),'%s/'%gcnweb] ) #,\
# stdout=devnullfobj,stderr=devnullfobj)
devnullfobj.close()
if path.isfile('%s/%s'%(gcnweb,gcndbfname)):
cmt = path.getmtime('%s/%s'%(gcnweb,gcndbfname))
if self.modtime == None or self.modtime != cmt:
self.modtime = cmt
self.buildsite = True
def SiteAlerter(self):
'''
Calls site-alerter.py
'''
call(['%s/site-alerter.py'%pathname])
def PushBack(self):
'''
Pushes generated web content back to DB server
'''
devnullfobj = open(devnull,'w')
call(['rsync','-azq','--exclude=%s'%gcndbfname,\
'%s/'%gcnweb,\
'%s:%s'%(gcndbsrv,gcnwebosrv)],\
stdout=devnullfobj,stderr=devnullfobj)
devnullfobj.close()
def Check(self):
'''
Function which calls itself every interval
'''
self.UpdateDB()
if self.buildsite:
self.SiteAlerter()
self.PushBack()
self.buildsite = False
# Wait for inter then call again
time.sleep(self.inter)
self.Check()
# Start daemon
if __name__ == "__main__":
app = App(log=gcndaemonlog,inter=gcninter,pidfile=gcndaemonlock)
daemon_runner = runner.DaemonRunner(app)
daemon_runner.daemon_context.files_preserve=[gcndaemonlog]
daemon_runner.do_action();