1
2
3
4
5
6
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
21 Agent.__init__(self, name, **params)
22 self.lastcolor = None
23 self.status = status.OK()
24
25
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
33 ('nb', 'Nombre de virus pour aujourd\'hui', {'align':'center'}, None)])
34 self.data = [title1, self.table, HTMLData('<br>'), self.table2]
35
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
44 self.status = status.OK()
45 fichier = "/var/log/syslog"
46
47
48 today = str(time.localtime().tm_mday)
49
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
63 for ligne in lignes:
64 if ligne.find("infected with virus") != -1 :
65
66 day = ligne[4:6]
67
68
69 ligne = ligne[ligne.find("'")+1:]
70 next = ligne.find("' ")
71
72 fichier = ligne[0:next]
73 ligne = ligne[next+23:]
74 next = ligne.find("'")
75
76 virus = ligne[0:next]
77
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
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
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
109 return { 'statistics' : [ {'vir' : 'Aucun',
110 'com' : '----',
111 'nb' : '----' } ],
112 'statistics2' : [ res2 ] }
113
114
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
123