Package ZenEvents :: Module zenmail
[hide private]
[frames] | no frames]

Source Code for Module ZenEvents.zenmail

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13  #! /usr/bin/env python  
 14  # Notes: database wants events in UTC time 
 15  # Events page shows local time, as determined on the server where zenoss runs 
 16   
 17  __doc__='''zenmail 
 18   
 19  Turn email messages into events. 
 20   
 21  $Id$ 
 22  ''' 
 23   
 24  from EventServer import EventServer 
 25   
 26  from twisted.mail import smtp 
 27  from twisted.internet import reactor, protocol, defer 
 28  from zope.interface import implements 
 29  from Products.ZenRRD.RRDDaemon import RRDDaemon 
 30   
 31  from email.Header import Header 
 32  import email 
 33   
 34  from MailProcessor import MailProcessor 
 35   
 36  import logging 
 37  log = logging.getLogger("zen.mail") 
 38   
 39   
40 -class ZenossEventPoster(object):
41 implements(smtp.IMessage) 42
43 - def __init__(self, processor):
44 self.lines = [] 45 self.processor = processor
46 47
48 - def lineReceived(self, line):
49 self.lines.append(line)
50 51
52 - def postEvent(self, messageStr):
53 message = email.message_from_string(messageStr) 54 self.processor.process(messageStr)
55 56
57 - def eomReceived(self):
58 log.info('message data completed.') 59 self.lines.append('') 60 messageData = '\n'.join(self.lines) 61 62 self.postEvent(messageData) 63 64 if True: 65 return defer.succeed(None) 66 else: 67 return defer.failure(None)
68 69
70 - def connectionLost(self):
71 log.info('connection lost unexpectedly') 72 del(self.lines)
73 74
75 -class ZenossDelivery(object):
76 implements(smtp.IMessageDelivery) 77
78 - def __init__(self, processor):
79 self.processor = processor
80 81
82 - def receivedHeader(self, helo, origin, recipients):
83 myHostname, self.clientIP = helo 84 date = smtp.rfc822date() 85 86 headerValue = 'by %s from %s with ESMTP ; %s' % ( 87 myHostname, self.clientIP, date) 88 89 log.info('relayed (or sent directly) from: %s' % self.clientIP) 90 91 header = 'Received: %s' % Header(headerValue) 92 return header
93 94
95 - def validateTo(self, user):
96 log.info('to: %s' % user.dest) 97 return self.makePoster
98 99
100 - def makePoster(self):
101 return ZenossEventPoster(self.processor)
102 103
104 - def validateFrom(self, helo, originAddress):
105 log.info("from: %s" % originAddress) 106 return originAddress
107 108
109 -class SMTPFactory(protocol.ServerFactory):
110 - def __init__(self, processor):
111 self.processor = processor
112
113 - def buildProtocol(self, addr):
114 delivery = ZenossDelivery(self.processor) 115 smtpProtocol = smtp.SMTP(delivery) 116 smtpProtocol.factory = self 117 return smtpProtocol
118 119
120 -class ZenMail(EventServer, RRDDaemon):
121 name = 'zenmail' 122
123 - def __init__(self):
124 EventServer.__init__(self) 125 RRDDaemon.__init__(self, ZenMail.name) 126 127 self.changeUser() 128 self.processor = MailProcessor(self.dmd.ZenEventManager) 129 130 self.factory = SMTPFactory(self.processor) 131 132 if self.options.useFileDescriptor != -1: 133 self.useTcpFileDescriptor(int(self.options.useFileDescriptor), 134 self.factory) 135 else: 136 log.info("listening on port: %d" % self.options.listenPort) 137 reactor.listenTCP(self.options.listenPort, self.factory) 138 reactor.run()
139 140
141 - def handleError(self, error):
142 log.error(error) 143 log.error(error.getErrorMessage()) 144 145 self.finish()
146 147
148 - def _finish(self):
149 self.finish()
150 151
152 - def buildOptions(self):
153 EventServer.buildOptions(self) 154 self.parser.add_option('--useFileDescriptor', 155 dest='useFileDescriptor', 156 default="-1", 157 type="int", 158 help="File descriptor to use for listening") 159 self.parser.add_option('--listenPort', 160 dest='listenPort', 161 default="25", 162 type="int", 163 help="Alternative listen port to use")
164 165 166 if __name__ == '__main__': 167 ZenMail().main() 168