zephir.monitor.agents.clamav

Package zephir :: Package monitor :: Package agents :: Module clamav |
[frames] | no frames]

# Source Code for Module zephir.monitor.agents.clamav

      1  # -*- coding: UTF-8 -*-
      2  ###########################################################################
      3  # Eole NG - 2007
      4  # Copyright Pole de Competence Eole  (Ministere Education - Academie Dijon)
      5  # Licence CeCill  cf /root/LicenceEole.txt
      6  # eole@ac-dijon.fr
      7  ###########################################################################
      8  """
      9  Agent zephir pour l'étude des logs de scannedonly
     10  """
     11  import time
     12  import random
     13  import locale
     14  from os.path import isfile
     15  from calendar import month_abbr
     16  from zephir.monitor.agentmanager.agent import Agent
     17  from zephir.monitor.agentmanager.data import HTMLData, TableData
     18  from zephir.monitor.agentmanager import status
     19
     20  # FIXME: code sensible à l'environnement
     21  locale.setlocale(locale.LC_ALL, ('en_US', 'UTF8'))
     22  MONTH_ABBR = list(month_abbr)
     23
     24 -class ClamLog(Agent):
     25
     26 -    def __init__(self, name, **params):
     27          Agent.__init__(self, name, **params)
     28          self.lastcolor = None
     29          self.status = status.OK()
     30          # cas où on met direct le résultat :)
     31          #self.data=[HTMLData(self.result())]
     32          self.table = TableData([
     33              ('vir', 'Virus', {'align':'center'}, None),
     34  #            ('com', 'Poste client', {'align':'center'}, None),
     35              ('nb', 'Occurences', {'align':'center'}, None)])
     36          title1 = HTMLData("<h3>Derniers virus détectés<h3>")
     37          self.table2 = TableData([
     38              #('day', 'Jour', {'align':'center'}, None),
     39              ('nb', 'Nombre de virus pour aujourd\'hui',
     40                  {'align':'center'}, None)])
     41          self.data = [title1, self.table, HTMLData('<br>'), self.table2]
     42
     43 -    def _color(self, vir):
     44          """
     45          astuce pour réaliser un affichage sympa
     46          """
     47          color = self.lastcolor
     48          while color == self.lastcolor :
     49              color = random.choice(('red', 'green', 'blue', 'deeppink' ))
     50          self.lastcolor = color
     51          return "<font color=\"%s\">%s</font>" % (color, vir)
     52
     53 -    def measure(self):
     54          self.status = status.OK()
     55          fichier = '/var/log/rsyslog/local/scannedonlyd_clamav/scannedonlyd_clamav.warn.log'
     56          date = time.localtime()
     57          today = date.tm_mday
     58          mymonth = date.tm_mon
     59          if not isfile(fichier):
     60              lignes = []
     61          else:
     62              fp = open(fichier, 'r')
     63              # lecture limitée à 10Mo
     64              lignes = fp.readlines(10000000)
     65              fp.close()
     66          dico = {}
     67          totalday = 0
     68          #lignes.reverse() # si on veut trier par date
     69          for ligne in lignes:
     70              if ligne.find("contains virus") != -1:
     71                  data = ligne.split()
     72                  day = data[1]
     73                  month = data[0]
     74                  virus = data[-1][0:-1]
     75                  # FIXME: on ne connait plus la station avec scannedonly
     76                  client = 'unknown'
     77                  # tri par couple (virus, station)
     78                  if dico.has_key((virus, client)) :
     79                      dico[(virus, client)] += 1
     80                  else:
     81                      dico[(virus, client)] = 1
     82                  if int(day) == today and month == MONTH_ABBR[mymonth]:
     83                      totalday += 1
     84          # status (plus de 10 virus aujourd'hui ?)
     85          warninglevel = 1
     86          errorlevel = 10
     87          if totalday >= errorlevel :
     88              self.status = status.Error()
     89          elif totalday >= warninglevel :
     90              self.status = status.Warn()
     91          self.measure_data['nb'] = str(totalday)
     92
     93          res2 = { 'nb'  : str(totalday) }
     94
     95          if dico != {} :
     96              result = []
     97              cles = dico.keys()
     98              for cle in cles :
     99                  result.append({ 'vir' : self._color(cle[0]),
    100  #                                'com' : cle[1],
    101                                  'nb'  : dico[cle]
    102                                })
    103              return { 'statistics' : result,
    104                       'statistics2' : [ res2 ] }
    105          return { 'statistics' : [ {'vir' : 'Aucun',
    106  #                                   'com' : '----',
    107                                     'nb'  : '----'  } ],
    108                   'statistics2' : [ res2 ] }
    109
    110
    111 -    def write_data(self):
    112          Agent.write_data(self)
    113          if self.last_measure is not None:
    114              self.table.table_data = self.last_measure.value['statistics']
    115              self.table2.table_data = self.last_measure.value['statistics2']
    116
    117 -    def check_status(self):
    118          return self.status
    119