예제 #1
0
class CertChecker( object ):

   def __init__( self ):
      self.ssh  = SSHConnector( )
      self.conf = Configurator( '/etc//cfg/cert-checker.cfg', 'cert-checker' )
      mailto    = self.conf.getValue('MAILTO').split(',')
      self.mail = Mailer( self.conf.getValue('MAILFROM'), mailto, self.conf.getValue('SMTPRELAY') )

   def __processDays( self ):
      files    = self.conf.getValue('REMOTE_FILES')
      files    = files.split(',')
      server   = self.conf.getValue('SERVER')
      server   = server.split(',')
      server   = Server( server[0], server[1], server[2], server[3], server[4] )
      tmp      = self.conf.getValue('TMP')
      contents = list()

      for file in files:
         self.ssh.getFile( server, file, "%s/" % tmp )
         cont_tmp = open("%s" % file, 'r' )
         expiring = list()

         for line in cont_tmp:
            line = line.strip()
            line = line.split( '|' )
            if int( line[1] ) < int( self.conf.getValue('DAYS') ):
               expiring.append( {'name':line[0], 'days':line[1]} )

         contents.append( { '%s' % (file): expiring } ) 

      return contents

   def startCheck( self ):
      expiring = self.__processDays()
      subject  = "Certificates expiration report"
      body     = "This is an automatic report of certificates that are about to expire\n\n"

      for exp in expiring:
         if len( exp[ exp.keys()[0] ] ) > 0:
            title = exp.keys()[0].split('-')[3].split('.')[0]
            body += "%s\n" % title
            for cert in exp[ exp.keys()[0] ]:
               if int( cert['days'] ) < 0:
                  body += " !! CERTIFICATE EXPIRED %s\n\n" % cert['name']
               else:
                  body += " W CERTIFICATE %s EXPIRES IN %s DAYS\n\n" % ( cert['name'], cert['days'] )

      body += "\n\n\n\nThis email will be sent every Monday at morning\n\n"
      body += "Recipients: %s\n" % self.conf.getValue('MAILTO')

      self.mail.sendMail( subject, body )
예제 #2
0
파일: sender.py 프로젝트: gaccardo/stuff
class Sender( object ):

   def __init__(self):
      self.logger = Logger(FILE_LOG)
      self.mailer = Mailer(MAIL_FROM, MAIL_TO, RELAY)

   def __hashDate(self):
      return base64.b64encode( time.asctime() )

   def __storeHashedDate(self, hash):
      file_pointer = open(HASH_FILE,'w')
      file_pointer.write(hash)
      file_pointer.close()

      self.logger.addLine('HASH: %s' % hash)

   def __sendEmail(self, hash):
      body = 'HASH: %s\n' % hash

      if self.mailer.sendMail('[CHECKER]', body):
         self.logger.addLine('Email sent')
      else:
         self.logger.addLine('Unable to sent email')

   def main(self):
      hash = self.__hashDate()
      self.__storeHashedDate(hash)
      self.__sendEmail(hash)
예제 #3
0
파일: acs.py 프로젝트: gaccardo/stuff
class ACSwitcher( object ):

   def __init__( self, acs, last ):
      self.acs  = acs
      self.last = last
      self.mail = Mailer( '', '', '')

   def __changeAC( self, new_ac ):
      f_pointer = open(self.last, 'w')
      f_pointer.write(new_ac)
      f_pointer.close()

   def __lastAC( self ):
      f_pointer = open(self.last, 'r')
      f_buffer  = f_pointer.read()
      f_pointer.close()

      last_ac = f_buffer.strip()

      for ac in self.acs:
         if ac.getHost() == last_ac:
            return last_ac

   def __newAC( self, last_ac ):
      for ac in self.acs:
         if ac.getHost() != last_ac:
            return ac.getHost()

   def __getName( self, host ):
      for ac in self.acs:
         if ac.getDeviceByHost( host ) is not None:
            return ac.getDeviceByHost( host )

   def __sendMail( self, result, last_ac, new_ac, errors ):
      subject = ""
      body    = ""

      if result == 0:
         subject = "AC Switch: Succesfull"
         body     = "###########################\n"
         body    += "#        AC Switch        #\n"
         body    += "###########################\n"
         body    += " * Result: Successfull\n"
         body    += " * Last AC: %s\n" % self.__getName(last_ac)
         body    += " * New AC: %s\n" % self.__getName(new_ac)
         body    += "###########################\n"
      else:
         subject = "AC Switch: Failed"
         body     = "###########################\n"
         body    += "#        AC Switch        #\n"
         body    += "###########################\n"
         body    += " * Result: Failed\n"
         body    += " * Last AC: %s\n" % self.__getName(last_ac)
         body    += " * New AC: %s\n" % self.__getName(new_ac)
         body    += "###########################\n"

         print 'ERRORS:' if len(errors) > 0 else ''
         for error in errors:
            body += " * %s\n" % error


      print subject
      print body

      self.mail.sendMail( subject, body )
      self.mall.sendMail( subject, body )

   def runCheck( self ):
      last_ac = self.__lastAC()
      new_ac  = self.__newAC( last_ac )
      bad     = 0
      errors  = list()

      for ac in self.acs:
         comp1 = True if ac.processOID(".1.3.6.1.4.1.9839.2.1.1.3.0")[0] == '1' else False
         comp2 = True if ac.processOID(".1.3.6.1.4.1.9839.2.1.1.2.0")[0] == '1' else False

         if ac.getHost() == last_ac:
            if comp1 == True or comp2 == True:
               bad += 1
               errors.append( "Prev AC shouldn't have any compressors working, but it have" )

         if ac.getHost() != last_ac:
            if not (comp1 == True or comp2 == True):
               errors.append( "New AC should have at least one of its compressors working, but it haven't" )
               bad += 1

      self.__sendMail( bad, last_ac, new_ac, errors )
      self.__changeAC( new_ac )
예제 #4
0
파일: ack.py 프로젝트: gaccardo/stuff
class ACK( object ):

   def __init__(self):
      self.c = Configurator('/etc//cfg/ack.conf', 'ack')
      self.l = Logger(self.c.getValue('FILE_LOG'), self.c.getValue('NAME'), self.c.getValue('DEBUG'))
      self.z = ZabbixDB(self.l, self.c.getValue('DB_HOST'), self.c.getValue('DB_USER'), self.c.getValue('DB_PASS'), self.c.getValue('DB_NAME'))

   def __checkUserPIN(self, pin, ffrom):
      users_raw = self.c.getValue('USERS')
      users_lines = users_raw.split(',')

      for user in users_lines:
         self.l.addInfoLine("%s :: %s" % ( user.split('|')[0], user.split('|')[1] ))
         if user.split('|')[0] == ffrom and user.split('|')[1] == pin:
             return True

      return False

   def __clearSubject(self, subject, pipes):
      if pipes:
          self.l.addInfoLine(subject)

          try:
             filtered = subject.split('|')
             self.l.addInfoLine(filtered[0])

             return filtered[1]
          except:
             self.l.addWarningLine('El subject esta mal formado')
         
          return "Mal formed subject"
      else:
          return subject

   def __sendEmail(self, user, subject, type):
      self.m = Mailer('[email protected]', self.c.getValue('MAIL_TO'), self.c.getValue('RELAY'))
      self.m.sendMail("ACK: %s" % self.__clearSubject(subject, type), "ACK Message: %s" % self.__clearSubject(subject, type))
      self.l.addInfoLine('Email Sended to itnetworking')

      self.m = Mailer('[email protected]', user, self.c.getValue('RELAY'))
      self.m.sendMail("ACK: %s" % self.__clearSubject(subject, type), "ACK Message: %s" % self.__clearSubject(subject, type))
      self.l.addInfoLine('Email Sended to %s' % user)

   def __isException(self, e_from):
      filtered = e_from.split('|')

      for email in filtered:
         if email == e_from:
            return True

      return False
      
   def main(self):
      stdin_raw   = sys.stdin.read()
      stdin_lines = stdin_raw.split('\n')
      KEY         = None
      PIN         = ""
      FROM        = ""
      ACK         = False

      self.l.addInfoLine('---------------------------------')
      for line in stdin_lines:

         if re.search('From ', line):
             FROM = line.split('From ')[1]
             FROM = FROM.split('  ')[0]
             self.l.addInfoLine("From: %s" % FROM)

         if re.search('Subject: ', line):
             SUBJECT = line.split('Subject: ')[1]
             self.l.addInfoLine("Subject: %s" % SUBJECT)
             ack_key = SUBJECT.split(' ')[0]

             if ack_key == 'ACK':
                PIN = SUBJECT.split(' ')[1]
                self.l.addInfoLine("ACK PIN: %s" % PIN)
                ACK = True

         if re.search('KEY: ', line):
             if KEY is None:
                 KEY = line.split('KEY: ')[1]
                 self.l.addInfoLine('KEY: %s' % KEY)

      if ACK:
         if not self.__checkUserPIN(PIN, FROM):
            self.l.addWarningLine('Invalid PIN/USER combination')
            self.l.addWarningLine('User %s has given an incorrect PIN %s' % (FROM, PIN))
            self.__sendEmail(FROM, "USER %s has given an incorrect PIN %s" % (FROM, PIN), False)
         else:
            self.l.addInfoLine('PIN/USER ACCEPTED')

            if self.z.ACK( KEY ):
               self.l.addInfoLine('ACK OK')
               self.__sendEmail(FROM, "ACK! %s" % SUBJECT, True)
            else:
               self.l.addErrorLine('ACK ERROR')

      else:
            if not self.__isException(FROM):
               self.__sendEmail(FROM, "WARNING USER %s MALFORMED EMAIL" % FROM, False)
         
      self.l.addInfoLine('---------------------------------')