1
2
3
4
5
6
7
8
9 """
10 Agent zephir listant le contenu de l'annaire LDAP
11 """
12 import time
13 from scribe import ldapconf
14 from scribe import eoleldap
15 from zephir.monitor.agentmanager.agent import Agent
16 from zephir.monitor.agentmanager.data import HTMLData, TableData
17 from zephir.monitor.agentmanager import status
18 from zephir.monitor.agentmanager.util import log
19
21 """
22 colorie la chaine avec en HTML
23 """
24 return "<font color=\"red\">%s</font>" % string
25
26 -def add(num1, num2) :
27 """
28 addition qui renvoit ??? si erreur
29 """
30 if '???' in (num1, num2):
31 return '???'
32 else:
33 return(num1 + num2)
34
36
38 """
39 Gestion du status :
40 par défaut : OK
41 Erreur si pas de connexion
42 Warning si erreur lors d'une ou plusieurs requêtes
43 """
44 Agent.__init__(self, name, **params)
45
46 self.status = status.OK()
47 self.fail = 0
48 self.lastwarn = ''
49
50 title1 = HTMLData("<h3>Nombre d'utilisateurs<h3>")
51 self.table1 = TableData([
52 ('name', 'Utilisateurs', {'align':'center'}, None),
53 ('nb', 'Nombre' , {'align':'center'}, None)
54 ])
55
56 title2 = HTMLData("<h3>Nombre de groupes<h3>")
57 self.table2 = TableData([
58 ('name', 'Groupes', {'align':'center'}, None),
59 ('nb', 'Nombre' , {'align':'center'}, None)
60 ])
61
62 title3 = HTMLData("<h3>Nombre de partages<h3>")
63 self.table3 = TableData([
64 ('name', 'Partages', {'align':'center'}, None),
65 ('nb', 'Nombre' , {'align':'center'}, None)
66 ])
67
68 title4 = HTMLData("<h3>Nombre de Postes<h3>")
69 self.table4 = TableData([
70 ('name', 'Postes', {'align':'center'}, None),
71 ('nb', 'Nombre' , {'align':'center'}, None)
72 ])
73
74 self.data = [title1, self.table1, title2, self.table2,
75 title3, self.table3, title4, self.table4]
76
78 """
79 exécute toutes les requêtes nécessaires
80 """
81 conn = eoleldap.Ldap()
82 try:
83 conn.connect()
84 except:
85 time.sleep(0.1)
86 reload(eoleldap)
87 conn = eoleldap.Ldap()
88 try:
89 conn.connect()
90 except Exception, err:
91 log.msg('erreur connexion ldap : %s' % str(err))
92 self.status = status.Warn(str(err))
93 return {'statistics1' : [], 'statistics2' : [],
94 'statistics3' : [], 'statistics4' : []}
95
96
97 self.status = status.OK()
98 self.fail = 0
99 self.lastwarn = ''
100
101
102
103 statistics1 = []
104 total1 = 0
105
106 for label, filtre in [('Eleves', ldapconf.ELEVE_FILTER),
107 ('Enseignants', ldapconf.PROF_FILTER),
108 ('Responsables', ldapconf.RESPONSABLE_FILTER),
109 ('Administratifs', ldapconf.ADMINISTRATIF_FILTER),
110 ('Invités', ldapconf.AUTRE_FILTER),]:
111 num = self._requete(conn, '(&%s)' % filtre, 'uid')
112 total1 = add(total1, num)
113 statistics1.append (
114 { 'name' : label,
115 'nb' : num
116 })
117
118 statistics1.append (
119 { 'name' : color('Total'),
120 'nb' : color(total1)
121 })
122
123
124
125
126 statistics2 = []
127 total2 = 0
128
129 for label, _type in [('Niveaux', 'Niveau'), ('Classes', 'Classe'),
130 ('Options', 'Option'), ('Equipes pédagogiques', 'Equipe'),
131 ('Matières', 'Matiere'), ('Services administratifs', 'Service'),
132 ('Groupes de travail', 'Groupe'),]:
133 num = self._requete(conn, '(&%s(type=%s))' % (ldapconf.GROUP_FILTER, _type), 'cn')
134 total2 = add(total2, num)
135 statistics2.append (
136 { 'name' : label,
137 'nb' : num
138 })
139
140 try:
141 num = self._requete(conn, '(&%s)' % (ldapconf.GROUP_FILTER), 'cn') - total2
142 except:
143 num = '???'
144 total2 = add(total2, num)
145
146
147 statistics2.append (
148 { 'name' : 'Groupes spéciaux',
149 'nb' : num
150 })
151
152 statistics2.append (
153 { 'name' : color('Total'),
154 'nb' : color(total2)
155 })
156
157
158
159
160 statistics3 = []
161 statistics3.append (
162 { 'name' : color('Total'),
163 'nb' : color(self._requete(conn, '(objectclass=sambaFileShare)', 'cn'))
164 })
165
166
167
168
169 statistics4 = []
170 statistics4.append (
171 { 'name' : color('Total'),
172 'nb' : color(self._requete(conn, "(&(objectclass=posixAccount)(description=Computer))", 'cn'))
173 })
174
175 if self.fail >= 2 :
176 self.status = status.Warn(self.lastwarn)
177
178 return {'statistics1' : statistics1, 'statistics2' : statistics2,
179 'statistics3' : statistics3, 'statistics4' : statistics4}
180
181 - def _requete(self, conn, filtre, attr):
182 time.sleep(0.05)
183 try:
184 return len(conn._search(filtre, attr))
185 except:
186 try:
187
188 time.sleep(0.1)
189 return len(conn._search(filtre, attr))
190 except Exception, err:
191 log.msg('erreur requête ldap : %s' % str(err))
192 self.lastwarn = str(err)
193 self.fail += 1
194 return '???'
195
197 Agent.write_data(self)
198 if self.last_measure is not None:
199 self.table2.table_data = self.last_measure.value['statistics2']
200 self.table3.table_data = self.last_measure.value['statistics3']
201 self.table1.table_data = self.last_measure.value['statistics1']
202 self.table4.table_data = self.last_measure.value['statistics4']
203
206