-
Notifications
You must be signed in to change notification settings - Fork 147
/
crashmailbatch.py
82 lines (60 loc) · 2.71 KB
/
crashmailbatch.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
#!/usr/bin/env python -u
##############################################################################
#
# Copyright (c) 2007 Agendaless Consulting and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the BSD-like license at
# http://www.repoze.org/LICENSE.txt. A copy of the license should accompany
# this distribution. THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL
# EXPRESS OR IMPLIED WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND
# FITNESS FOR A PARTICULAR PURPOSE
#
##############################################################################
# A event listener meant to be subscribed to PROCESS_STATE_CHANGE
# events. It will send mail when processes that are children of
# supervisord transition unexpectedly to the EXITED state.
# A supervisor config snippet that tells supervisor to use this script
# as a listener is below.
#
# [eventlistener:crashmailbatch]
# command=python crashmailbatch --toEmail=you@bar.com --fromEmail=me@bar.com
# events=PROCESS_STATE,TICK_60
doc = """\
crashmailbatch.py [--interval=<batch interval in minutes>]
[--toEmail=<email address>]
[--fromEmail=<email address>]
[--subject=<email subject>]
[--smtpHost=<hostname or address>]
Options:
--interval - batch cycle length (in minutes). The default is 1.0 minute.
This means that all events in each cycle are batched together
and sent as a single email
--toEmail - the email address to send alerts to
--fromEmail - the email address to send alerts from
--subject - the email subject line
--smtpHost - the SMTP server's hostname or address (defaults to 'localhost')
A sample invocation:
crashmailbatch.py --toEmail="you@bar.com" --fromEmail="me@bar.com"
"""
from supervisor import childutils
from superlance.process_state_email_monitor import ProcessStateEmailMonitor
class CrashMailBatch(ProcessStateEmailMonitor):
process_state_events = ['PROCESS_STATE_EXITED']
def __init__(self, **kwargs):
kwargs['subject'] = kwargs.get('subject', 'Crash alert from supervisord')
ProcessStateEmailMonitor.__init__(self, **kwargs)
self.now = kwargs.get('now', None)
def get_process_state_change_msg(self, headers, payload):
pheaders, pdata = childutils.eventdata(payload+'\n')
if int(pheaders['expected']):
return None
txt = 'Process %(groupname)s:%(processname)s (pid %(pid)s) died \
unexpectedly' % pheaders
return '%s -- %s' % (childutils.get_asctime(self.now), txt)
def main():
crash = CrashMailBatch.create_from_cmd_line()
crash.run()
if __name__ == '__main__':
main()