邮件监控

邮件监控

监控读取AWS,6466磁带设备的事件通知邮件,进行过滤压制之后传送给ovou系统

Telmail.sh,telmail.pl为两个邮件发送例程


  1  #  -*- coding: gb2312 -*-
  2 
  3  '''
  4  summary:    
  5      1.6466磁带库报警处理
  6      2.AWS报警处理
  7      传输接口:    smtp
  8      过滤邮件内容,生成事件到ovo
  9  author :    zhangbin
 10  date:        2006.03.04
 11  company:    ultrapower.com.cn
 12  '''
 13 
 14 
 15  import  os
 16  import  sys
 17  import  socket
 18  import  time
 19  import  poplib
 20  import  base64
 21  import  string
 22  import  re
 23  from  conf  import   *
 24 
 25 
 26 
 27  aws_field_map = { ' Information ' : ' normal ' ,
 28               ' Warning ' : ' warning ' ,
 29               ' Degraded ' : ' major ' ,
 30               ' Critical ' : ' critical ' ,
 31               ' Fatal ' : ' critical '
 32              }
 33  ME = ' szmail '
 34  this  =  modlist[ME]
 35 
 36  host = this[ ' host ' ]
 37  user = this[ ' user ' ]
 38  passwd  = this[ ' passwd ' ]
 39 
 40 
 41  def  Print(msg):
 42      lp(ME,msg)
 43 
 44 
 45  def  process_AWS(title,msg):
 46       try :
 47          app = ' AWS '
 48          node = ''
 49          obj = ''
 50          severity = ''
 51          msg_text = ''
 52          
 53           print   ' >>> ' ,msg
 54          title.index( ' CSF Fault Detected ' )
 55          lines  =  msg.split( ' \n ' )
 56          
 57           for  l  in  lines:
 58              r  =  re.match( ' .*Fault id.*?:\s*(.*)/(.*) ' ,l)
 59               if  r:
 60                  node  =  r.group( 1 )
 61                  obj  =  r.group( 2 )
 62                   continue
 63              r  =  re.match( ' .*Severity.*?:\s*(\w+) ' ,l)
 64               if  r:
 65                  f =  r.group( 1 ).strip()
 66                   if  aws_field_map.has_key(f):
 67                      severity  =  aws_field_map[f]
 68                   continue
 69              r  =   re.match( ' .*Synopsis.*?:\s*([\w|\s]+) ' ,l)
 70               if  r :
 71                  msg_text = r.group( 1 )
 72          cmd  =   " opcmsg app=AWS  "
 73           if  node:
 74              cmd  =  cmd +   "  node=%s  " % node
 75           if  obj:
 76              cmd  =  cmd +   "  obj=%s  " % obj
 77           if  severity:
 78              cmd =  cmd  +   "  severity=%s  " % severity
 79           if  msg_text:
 80              cmd  =  cmd  +   '  msg_text="%s"  ' % msg_text
 81           print   " execute command:%s " % cmd
 82       except :
 83           return  False
 84       return  True
 85      
 86      
 87      
 88  def  process_TAPE(title,msg):
 89       '''  for 6466 tape monitor  '''
 90      
 91 
 92  def  mail_sender(body):
 93       pass
 94      
 95  def  mail_hdr_section(section,body):    
 96      title = ''
 97       try :
 98          n  =  body.index( '' )
 99           for  i  in  range(n):
100               try :
101                  occ  =  body[i].index(section + ' : ' )
102                  title =  body[i][ (occ + len(section + ' : ' )):]
103               except :
104                   continue
105       except :
106           pass
107       return  title
108      
109  def  mail_context(encode,body):
110       '''  extract mail body text from string-list
111          multiple charset will be encoded to base64,but plain text will not
112       '''
113      context  = ''
114       try :
115          n  =  body.index( '' )
116          lines  =  body[n:]
117           if  encode.find( ' base64 ' >=  0 :
118              context  =  base64.decodestring(context)
119           else  :
120              context  =  string.join(lines, ' \n ' # maybe 7bit
121       except :
122           pass
123       return  context
124 
125  def  process(c,m):
126       '''  c    -- mail number,
127          m    -- mail object
128          提取邮件标题和邮件内容
129       '''
130       for  i  in  range( 1 ):
131           print   " >>>>Process %d " % (i + 1 )
132          list  =  m.retr(i + 1 )
133          title  =  mail_hdr_section( ' Subject ' ,list[ 1 ])
134          encode  =  mail_hdr_section( ' Content-Transfer-Encoding ' ,list[ 1 ])
135           print  list[ 1 ]
136          context  =  mail_context(encode.strip(),list[ 1 ])
137           if   not  process_AWS(title,context):
138              process_TAPE(title,context)
139           # m.dele(i+1)
140 
141  def  main_entry():
142      Print( ' OK+,%s start up! ' % ME)
143       while  True:
144           try :
145              m  =  poplib.POP3(host)
146              m.user(user)
147              m.pass_(passwd)
148              c  =  m.stat()[0]
149               if   c :
150                   print   " >> Detected %s Letters! " % (c)
151                  process(c,m)
152              m.quit()
153           except :
154               pass
155          time.sleep(this[ ' wait ' ])    
156      
157  if   __name__ == ' __main__ ' :
158      main_entry()
159 
160 

你可能感兴趣的:(邮件监控)