Skip to content

Commit

Permalink
implement --program option in crashmail
Browse files Browse the repository at this point in the history
  • Loading branch information
welelay committed Sep 7, 2015
1 parent cd19fca commit 55fc542
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 6 deletions.
9 changes: 9 additions & 0 deletions conftest/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
To test
- copy crashin fakemail to /R/bin
- copy crashmail.conf testcrash.conf testcrash2.conf to /etc/supervisor/conf.d

monitor log files in /L/
- You'll get an 'Ignoring hostname: testcrash' message every 10s in /L/crashmail.log (testcrash)

- You'll get 2 'unexpected exit, mailing' message every 30s in /L/crashmail.log (notifycrash:testcrash2, notifycrash:testcrash3)
and the message in /L/fakemail.log
7 changes: 7 additions & 0 deletions conftest/crashin
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
# test program: non 0 exit code after timeout
timeout="$1"
echo $(date) "Crashing in $timeout seconds"
sleep "$timeout"
echo $(date) "CRASHING NOW"
exit 123
10 changes: 10 additions & 0 deletions conftest/crashmail.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# config to use the fakemail mail sender
# and monitor only testcrash2 (crashes every 30s)
[eventlistener:crashmail]
command =
/usr/local/bin/crashmail
-p notifycrash:*
-o hostname -m [email protected]
-s '/R/bin/fakemail -t -i -f [email protected]'
events=PROCESS_STATE_EXITED
stderr_logfile=/L/crashmail.log
6 changes: 6 additions & 0 deletions conftest/fakemail
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
# don't send any mail: only write the args and stdin to the logfile
logfile=/L/fakemail.log
echo $(date) "$*" >> "$logfile"
cat >> "$logfile"
echo >> "$logfile"
9 changes: 9 additions & 0 deletions conftest/testcrash.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# test program: crashes every 10 seconds
[program:testcrash]
command=/R/bin/crashin 10

autostart=true
autorestart=true

stdout_logfile=/L/testcrash.log
redirect_stderr=true
21 changes: 21 additions & 0 deletions conftest/testcrash2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[group:notifycrash]
programs=testcrash2,testcrash3

# test program: crashes every 30 seconds
[program:testcrash2]
command=/R/bin/crashin 30

autostart=true
autorestart=true

stdout_logfile=/L/testcrash2.log
redirect_stderr=true

[program:testcrash3]
command=/R/bin/crashin 33

autostart=true
autorestart=true

stdout_logfile=/L/testcrash3.log
redirect_stderr=true
34 changes: 28 additions & 6 deletions superlance/crashmail.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
-p -- specify a supervisor process_name. Send mail when this process
transitions to the EXITED state unexpectedly. If this process is
part of a group, it can be specified using the
part of a group, it must be specified using the
'process_name:group_name' syntax.
-a -- Send mail when any child of the supervisord transitions
Expand Down Expand Up @@ -66,6 +66,7 @@
"""

import os
import re
import sys

from supervisor import childutils
Expand Down Expand Up @@ -124,10 +125,25 @@ def runforever(self, test=False):
if self.optionalheader:
subject = self.optionalheader + ':' + subject

self.stderr.write('unexpected exit, mailing\n')
self.stderr.flush()

self.mail(self.email, subject, msg)
ident = pheaders['processname']
if pheaders['groupname'] != ident:
ident = pheaders['groupname'] + ":" + ident

if self.any or \
not self.programs or \
any(prog.match(ident) for prog in self.programs):

self.stderr.write('\nunexpected exit, mailing\n')
self.stderr.flush()

self.mail(self.email, subject, msg)

else:

self.stderr.write('\nignoring %s\n' % subject)
self.stderr.flush()


childutils.listener.ok(self.stdout)
if test:
Expand All @@ -140,7 +156,7 @@ def mail(self, email, subject, msg):
body += msg
with os.popen(self.sendmail, 'w') as m:
m.write(body)
self.stderr.write('Mailed:\n\n%s' % body)
self.stderr.write('Mailed:\n\n%s\n' % body)
self.mailed = body


Expand All @@ -167,13 +183,19 @@ def main(argv=sys.argv):
email = None
optionalheader = None

progGroupRE = re.compile(r"(\w+):\*")

for option, value in opts:

if option in ('-h', '--help'):
usage()

if option in ('-p', '--program'):
programs.append(value)
pg = progGroupRE.match(value)
if pg:
programs.append(re.compile(pg.group(1)+":.*"))
else:
programs.append(re.compile(re.escape(value)))

if option in ('-a', '--any'):
any = True
Expand Down

0 comments on commit 55fc542

Please sign in to comment.