Package zephir :: Package monitor :: Package agents :: Module clamav
[hide private]
[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  """ 
 10  Agent zephir pour l'étude des logs de clamav 
 11  """ 
 12   
 13  from zephir.monitor.agentmanager.agent import Agent 
 14  from zephir.monitor.agentmanager.data import HTMLData, TableData 
 15  from zephir.monitor.agentmanager import status 
 16  import random 
 17  import time 
 18   
19 -class ClamLog(Agent):
20 - def __init__(self, name, **params):
21 Agent.__init__(self, name, **params) 22 self.lastcolor = None 23 self.status = status.OK() 24 # cas où on met direct le résultat :) 25 #self.data=[HTMLData(self.result())] 26 self.table = TableData([ 27 ('vir', 'Virus', {'align':'center'}, None), 28 ('com', 'Poste client', {'align':'center'}, None), 29 ('nb', 'Occurences', {'align':'center'}, None)]) 30 title1 = HTMLData("<h3>Derniers virus détectés<h3>") 31 self.table2 = TableData([ 32 #('day', 'Jour', {'align':'center'}, None), 33 ('nb', 'Nombre de virus pour aujourd\'hui', {'align':'center'}, None)]) 34 self.data = [title1, self.table, HTMLData('<br>'), self.table2]
35
36 - def _color(self,vir):
37 color = self.lastcolor 38 while color == self.lastcolor : 39 color = random.choice(('red', 'green', 'blue', 'deeppink' )) 40 self.lastcolor = color 41 return "<font color=\"%s\">%s</font>" % (color, vir)
42
43 - def measure(self):
44 self.status = status.OK() 45 fichier = "/var/log/syslog" 46 # les logs ont max une semaine 47 # on peut donc se contenter du numéro dans le mois :) 48 today = str(time.localtime().tm_mday) 49 #mymonth = str(time.localtime().tm_mon) 50 try : 51 fp = open(fichier,'r') 52 except: 53 self.status = status.Erreur() 54 return { 'statistics' : [ {'vir' : 'ERREUR', 55 'com' : '----', 56 'nb' : '----' }], 57 'statistics2' : [] } 58 lignes = fp.readlines() 59 fp.close() 60 dico = {} 61 totalday = 0 62 #lignes.reverse() # si on veut trier par date 63 for ligne in lignes: 64 if ligne.find("infected with virus") != -1 : 65 ## mois (ang) et jour (num) 66 day = ligne[4:6] 67 #month = ligne[0:3] 68 #date = ligne[4:6]+" "+ligne[0:3] 69 ligne = ligne[ligne.find("'")+1:] 70 next = ligne.find("' ") 71 ## fichier 72 fichier = ligne[0:next] 73 ligne = ligne[next+23:] 74 next = ligne.find("'") 75 ## virus 76 virus = ligne[0:next] 77 ## client 78 client = ligne[next+12:-2] 79 if dico.has_key((virus,client)) : 80 dico[(virus,client)] += 1 81 else: 82 dico[(virus,client)] = 1 83 from twisted.python import log 84 if int(day) == int(today) : 85 totalday += 1 86 # status (plus de 5 virus aujourd'hui ? 87 warninglevel = 1 88 errorlevel = 10 89 if totalday >= errorlevel : 90 self.status = status.Error() 91 elif totalday >= warninglevel : 92 self.status = status.Warn() 93 self.measure_data['nb'] = str(totalday) 94 95 res2 = { 'nb' : str(totalday) } 96 #'day' : today+"/"+mymonth, 97 98 if dico != {} : 99 result = [] 100 cles = dico.keys() 101 for cle in cles : 102 result.append({ 'vir' : self._color(cle[0]), 103 'com' : cle[1], 104 'nb' : dico[cle] 105 }) 106 return { 'statistics' : result, 107 'statistics2' : [ res2 ] } 108 #return "<font color=red>"+virus+"</font> sur le client"+" "+client+" ("+date+")" 109 return { 'statistics' : [ {'vir' : 'Aucun', 110 'com' : '----', 111 'nb' : '----' } ], 112 'statistics2' : [ res2 ] }
113 114
115 - def write_data(self):
116 Agent.write_data(self) 117 if self.last_measure is not None: 118 self.table.table_data = self.last_measure.value['statistics'] 119 self.table2.table_data = self.last_measure.value['statistics2']
120
121 - def check_status(self):
122 return self.status
123