vigigraph / maj_bdd.py @ e2dbeada
History | View | Annotate | Download (9.13 KB)
1 | f151053f | Thomas BURGUIERE | # -*- coding: utf-8 -*-
|
---|---|---|---|
2 | ab5f0b58 | Francois POIROTTE | import os |
3 | import atexit |
||
4 | from datetime import datetime |
||
5 | f151053f | Thomas BURGUIERE | from sqlalchemy import and_ |
6 | ab5f0b58 | Francois POIROTTE | import paste.deploy |
7 | import tg |
||
8 | |||
9 | tg.config = paste.deploy.appconfig('config:%s/%s' % (os.getcwd(), 'development.ini')) |
||
10 | from vigilo.models.configure import DBSession, configure_db |
||
11 | configure_db(tg.config, 'sqlalchemy.')
|
||
12 | |||
13 | def commit_on_exit(): |
||
14 | """
|
||
15 | Effectue un COMMIT sur la transaction à la fin de l'exécution
|
||
16 | du script d'insertion des données de test.
|
||
17 | """
|
||
18 | import transaction |
||
19 | transaction.commit() |
||
20 | |||
21 | atexit.register(commit_on_exit) |
||
22 | f151053f | Thomas BURGUIERE | |
23 | f1b255cb | Francis LAHEUGUERE | from vigilo.models import Host, HostGroup |
24 | from vigilo.models import LowLevelService, ServiceGroup |
||
25 | from vigilo.models import PerfDataSource, Graph |
||
26 | f5c20e6e | Francis LAHEUGUERE | from vigilo.models import Ventilation, VigiloServer, Application |
27 | f151053f | Thomas BURGUIERE | |
28 | |||
29 | d9d07df6 | Thomas BURGUIERE | # Groupe d'hôtes (HostGroup)
|
30 | def create_HostGroup(name, parent=None): |
||
31 | g = DBSession.query(HostGroup).filter(HostGroup.name == name).first() |
||
32 | f151053f | Thomas BURGUIERE | if not g: |
33 | if parent:
|
||
34 | d9d07df6 | Thomas BURGUIERE | g = HostGroup(name=name, idparent=parent.idgroup) |
35 | f151053f | Thomas BURGUIERE | else:
|
36 | d9d07df6 | Thomas BURGUIERE | g = HostGroup(name=name) |
37 | f151053f | Thomas BURGUIERE | print "Ajout du Groupe: ", name |
38 | DBSession.add(g) |
||
39 | return g
|
||
40 | |||
41 | # Hôte (Host)
|
||
42 | f158722a | Francis LAHEUGUERE | #def create_Host(name, checkhostcmd, hosttpl, snmpcommunity, mainip, snmpport):
|
43 | def create_Host(name): |
||
44 | d9d07df6 | Thomas BURGUIERE | h = DBSession.query(Host).filter(Host.name == name).first() |
45 | f151053f | Thomas BURGUIERE | if not h: |
46 | f158722a | Francis LAHEUGUERE | h = Host(name=name, |
47 | checkhostcmd=u'dummy',
|
||
48 | hosttpl=u'linux',
|
||
49 | mainip=u"127.0.0.1",
|
||
50 | snmpcommunity=u"public",
|
||
51 | snmpport=161,
|
||
52 | weight=0)
|
||
53 | f151053f | Thomas BURGUIERE | print "Ajout de l'hôte: ", name |
54 | DBSession.add(h) |
||
55 | return h
|
||
56 | |||
57 | f158722a | Francis LAHEUGUERE | #Recherche de l'objet Host à partir du name
|
58 | def get_host(hostname): |
||
59 | """ Return Host object from hostname, None if not available"""
|
||
60 | return DBSession.query(Host) \
|
||
61 | .filter(Host.name == hostname) \ |
||
62 | .first() |
||
63 | |||
64 | d9d07df6 | Thomas BURGUIERE | # Ajout d'un hôte dans un groupe d'hôtes (Host -> HostGroup)
|
65 | def add_Host2HostGroup(host, group): |
||
66 | if host not in group.hosts: |
||
67 | print "Ajout de l'hote: %(h)s dans le group: %(g)s" % \ |
||
68 | {'h': host.name,
|
||
69 | 'g': group.name}
|
||
70 | group.hosts.append(host) |
||
71 | |||
72 | a00a2345 | Francois POIROTTE | def create_LowLevelService(hostname, servicename): |
73 | s = DBSession.query(LowLevelService) \ |
||
74 | .join((Host, LowLevelService.idhost == Host.idhost)) \ |
||
75 | .filter(LowLevelService.servicename == servicename) \ |
||
76 | 059cc5de | William MAISONMARCHEUX | .filter(Host.name == hostname) \ |
77 | d9d07df6 | Thomas BURGUIERE | .first() |
78 | if not s: |
||
79 | a00a2345 | Francois POIROTTE | s = LowLevelService(idhost=get_host(hostname).idhost, |
80 | d9d07df6 | Thomas BURGUIERE | servicename=servicename, |
81 | f158722a | Francis LAHEUGUERE | weight = 42,
|
82 | d9d07df6 | Thomas BURGUIERE | op_dep=u"?")
|
83 | print "Ajout du service", servicename |
||
84 | DBSession.add(s) |
||
85 | 9a994732 | Thomas BURGUIERE | return s
|
86 | d9d07df6 | Thomas BURGUIERE | |
87 | # Groupe de services (ServiceGroup)
|
||
88 | def create_ServiceGroup(name, parent=None): |
||
89 | g = DBSession.query(ServiceGroup).filter(ServiceGroup.name == name).first() |
||
90 | if not g: |
||
91 | if parent:
|
||
92 | g = ServiceGroup(name=name, idparent=parent.idgroup) |
||
93 | else:
|
||
94 | g = ServiceGroup(name=name) |
||
95 | print "Ajout du Groupe: ", name |
||
96 | DBSession.add(g) |
||
97 | return g
|
||
98 | |||
99 | # Ajout d'un hôte dans un groupe d'hôtes (Host -> HostGroup)
|
||
100 | a00a2345 | Francois POIROTTE | def add_LowLevelService2ServiceGroup(service, group): |
101 | 9a994732 | Thomas BURGUIERE | if service not in group.services: |
102 | d9d07df6 | Thomas BURGUIERE | print "Ajout du service: %(s)s dans le group: %(g)s" % \ |
103 | 9a994732 | Thomas BURGUIERE | {'s': service.servicename,
|
104 | d9d07df6 | Thomas BURGUIERE | 'g': group.name}
|
105 | 9a994732 | Thomas BURGUIERE | group.services.append(service) |
106 | d9d07df6 | Thomas BURGUIERE | |
107 | f158722a | Francis LAHEUGUERE | def _get_service(hostname, servicename): |
108 | """ Return Host object from hostname, None if not available"""
|
||
109 | a00a2345 | Francois POIROTTE | return DBSession.query(LowLevelService) \
|
110 | .join((Host, Host.idhost == LowLevelService.idhost)) \ |
||
111 | f158722a | Francis LAHEUGUERE | .filter(Host.name == hostname) \ |
112 | a00a2345 | Francois POIROTTE | .filter(LowLevelService.servicename == servicename) \ |
113 | 9a994732 | Thomas BURGUIERE | .first() |
114 | cd60da72 | Francis LAHEUGUERE | |
115 | #Recherche de l'objet ServiceGroup à partir du name
|
||
116 | def get_ServiceGroup(name): |
||
117 | return DBSession.query(ServiceGroup).filter(ServiceGroup.name == name).first()
|
||
118 | |||
119 | a00a2345 | Francois POIROTTE | #Recherche de l'objet LowLevelService à partir du name
|
120 | def get_LowLevelService(hostname, servicename): |
||
121 | s = DBSession.query(LowLevelService) \ |
||
122 | .join((Host, LowLevelService.idhost == Host.idhost)) \ |
||
123 | .filter(LowLevelService.servicename == servicename) \ |
||
124 | cd60da72 | Francis LAHEUGUERE | .filter(Host.name == hostname) \ |
125 | .first() |
||
126 | return s
|
||
127 | |||
128 | f158722a | Francis LAHEUGUERE | # DS (Graph)
|
129 | def create_graph(name, vlabel, perfdatasources): |
||
130 | gr = DBSession.query(Graph) \ |
||
131 | .filter(Graph.name == name) \ |
||
132 | .first() |
||
133 | if not gr: |
||
134 | gr = Graph(name=name, vlabel=vlabel) |
||
135 | print "Ajout du graph: ", vlabel |
||
136 | DBSession.add(gr) |
||
137 | return gr
|
||
138 | |||
139 | # DS (PerfDataSource)
|
||
140 | def create_ds(name, type, service, label, graphs): |
||
141 | ds = DBSession.query(PerfDataSource) \ |
||
142 | .filter(PerfDataSource.service == service) \ |
||
143 | .filter(PerfDataSource.name == name) \ |
||
144 | .first() |
||
145 | if not ds: |
||
146 | ds = PerfDataSource(name=name, type=type, service=service, label=label, graphs=graphs)
|
||
147 | print "Ajout de la datasource: ", label |
||
148 | DBSession.add(ds) |
||
149 | return ds
|
||
150 | |||
151 | f5c20e6e | Francis LAHEUGUERE | # VigiloServer
|
152 | def create_Server(name, description): |
||
153 | 4a2d4c17 | Francis LAHEUGUERE | s = DBSession.query(VigiloServer).filter(VigiloServer.name == name).first() |
154 | f5c20e6e | Francis LAHEUGUERE | if not s: |
155 | s = VigiloServer(name=name, description=description) |
||
156 | print "Ajout du Server Vigilo: %s - %s" % (name, description) |
||
157 | DBSession.add(s) |
||
158 | return s
|
||
159 | |||
160 | # VigiloApplication
|
||
161 | def create_Application(name): |
||
162 | a = DBSession.query(Application).filter(Application.name == name).first() |
||
163 | if not a: |
||
164 | a = Application(name=name) |
||
165 | print "Ajout de l'application Vigilo: %s" % name |
||
166 | DBSession.add(a) |
||
167 | return a
|
||
168 | |||
169 | # Ventilation
|
||
170 | def create_Ventilation(host, server, application): |
||
171 | v = None
|
||
172 | h = DBSession.query(Host).filter(Host.name == host).first() |
||
173 | s = DBSession.query(VigiloServer).filter(VigiloServer.name == server).first() |
||
174 | a = DBSession.query(Application).filter(Application.name == application).first() |
||
175 | if h and s: |
||
176 | v = Ventilation(idhost=h.idhost, idvigiloserver=s.idvigiloserver, idapp=a.idapp) |
||
177 | print "Ajout Ventilation - host %s - server %s - application %s" % (host, server, application) |
||
178 | DBSession.add(v) |
||
179 | return v
|
||
180 | |||
181 | cd60da72 | Francis LAHEUGUERE | |
182 | d9d07df6 | Thomas BURGUIERE | hg1 = create_HostGroup(u'Serveurs')
|
183 | hg2 = create_HostGroup(u'Telecoms')
|
||
184 | hg3 = create_HostGroup(u'Serveurs Linux', hg1)
|
||
185 | hg4 = create_HostGroup(u'NORTEL', hg2)
|
||
186 | hg5 = create_HostGroup(u'CISCO', hg2)
|
||
187 | |||
188 | f158722a | Francis LAHEUGUERE | h1 = create_Host(u'proto4.si.c-s.fr')
|
189 | h2 = create_Host(u'messagerie.si.c-s.fr')
|
||
190 | h3 = create_Host(u'testnortel.si.c-s.fr')
|
||
191 | h4 = create_Host(u'proto6.si.c-s.fr')
|
||
192 | 470f60ca | Francis LAHEUGUERE | h5 = create_Host(u'par.linux0')
|
193 | f151053f | Thomas BURGUIERE | |
194 | d9d07df6 | Thomas BURGUIERE | add_Host2HostGroup(h1, hg3) |
195 | add_Host2HostGroup(h2, hg3) |
||
196 | add_Host2HostGroup(h3, hg4) |
||
197 | add_Host2HostGroup(h4, hg5) |
||
198 | add_Host2HostGroup(h4, hg3) |
||
199 | 470f60ca | Francis LAHEUGUERE | add_Host2HostGroup(h5, hg3) |
200 | f151053f | Thomas BURGUIERE | |
201 | 9a994732 | Thomas BURGUIERE | sg1 = create_ServiceGroup(u'Général')
|
202 | sg2 = create_ServiceGroup(u'Interface Réseau')
|
||
203 | sg3 = create_ServiceGroup(u'Performance')
|
||
204 | sg4 = create_ServiceGroup(u'Partitions')
|
||
205 | sg5 = create_ServiceGroup(u'Processus')
|
||
206 | f151053f | Thomas BURGUIERE | |
207 | a00a2345 | Francois POIROTTE | s1 = create_LowLevelService(h1.name, u'Interface eth0')
|
208 | s2 = create_LowLevelService(h1.name, u'Interface eth1')
|
||
209 | s3 = create_LowLevelService(h1.name, u'Interface série')
|
||
210 | s4 = create_LowLevelService(h4.name, u'Interface')
|
||
211 | s5 = create_LowLevelService(h5.name, u'Interface Linux')
|
||
212 | |||
213 | add_LowLevelService2ServiceGroup(s1, sg2) |
||
214 | add_LowLevelService2ServiceGroup(s2, sg2) |
||
215 | add_LowLevelService2ServiceGroup(s3, sg1) |
||
216 | add_LowLevelService2ServiceGroup(s4, sg2) |
||
217 | add_LowLevelService2ServiceGroup(s5, sg2) |
||
218 | add_LowLevelService2ServiceGroup(s5, sg3) |
||
219 | 9a994732 | Thomas BURGUIERE | |
220 | f158722a | Francis LAHEUGUERE | gr1 = create_graph(u'graph1',u'Graph1', None) |
221 | gr2 = create_graph(u'graph2',u'Graph2', None) |
||
222 | gr3 = create_graph(u'graph3',u'Graph3', None) |
||
223 | gr4 = create_graph(u'graph4',u'Graph4', None) |
||
224 | 470f60ca | Francis LAHEUGUERE | #gr5 = create_graph(u'graph5',u'Graph5', None)
|
225 | gr5 = create_graph(u'IO',u'IO', None) |
||
226 | 6a089374 | Francis LAHEUGUERE | gr6 = create_graph(u'RAM',u'RAM', None) |
227 | e513c11c | Francis LAHEUGUERE | gr7 = create_graph(u'TCP connections',u'TCP connections', None) |
228 | f2a0c70f | Francis LAHEUGUERE | gr8 = create_graph(u'CPU usage (by type)',u'CPU usage (by type)', None) |
229 | f158722a | Francis LAHEUGUERE | |
230 | graphs = [] |
||
231 | for g in DBSession.query(Graph).all(): |
||
232 | graphs.append(g) |
||
233 | |||
234 | f2a0c70f | Francis LAHEUGUERE | |
235 | f158722a | Francis LAHEUGUERE | ds1 = create_ds(u'ineth0', u'GAUGE', s1 \ |
236 | f2a0c70f | Francis LAHEUGUERE | , u'Données en entrée sur eth0', graphs[1:2]) |
237 | f158722a | Francis LAHEUGUERE | ds2 = create_ds(u'outeth0', u'GAUGE', s2 \ |
238 | , u'Données en sortie sur eth0', graphs[1:3]) |
||
239 | ds3 = create_ds(u'outeth1', u'GAUGE', s3 \ |
||
240 | , u'Données en sortie sur eth1', graphs[2:4]) |
||
241 | ds4 = create_ds(u'ineth1', u'GAUGE', s4 \ |
||
242 | ad17826c | Francis LAHEUGUERE | , u'Données en entrée sur eth0', graphs[3:4]) |
243 | e513c11c | Francis LAHEUGUERE | ds5_1 = create_ds(u'IO Reads', u'GAUGE', s5 \ |
244 | , u'IO Reads', graphs[4:5]) |
||
245 | ds5_2 = create_ds(u'IO Writes', u'GAUGE', s5 \ |
||
246 | , u'IO Writes', graphs[4:5]) |
||
247 | 6a089374 | Francis LAHEUGUERE | ds6 = create_ds(u'RAM', u'GAUGE', s5 \ |
248 | e513c11c | Francis LAHEUGUERE | , u'RAM', graphs[5:6]) |
249 | ds7 = create_ds(u'TCP connections', u'GAUGE', s5 \ |
||
250 | , u'TCP connections', graphs[6:7]) |
||
251 | f2a0c70f | Francis LAHEUGUERE | ds8 = create_ds(u'CPU usage (by type)', u'GAUGE', s5 \ |
252 | , u'CPU usage (by type)', graphs[7:8]) |
||
253 | f151053f | Thomas BURGUIERE | |
254 | f5c20e6e | Francis LAHEUGUERE | # Serveurs Vigilo
|
255 | sv1 = create_Server(u'http://localhost', u'RRD+Nagios') |
||
256 | |||
257 | # Applications Vigilo
|
||
258 | ap1 = create_Application(u'rrdgraph')
|
||
259 | ap2 = create_Application(u'nagios')
|
||
260 | |||
261 | # Ventilation
|
||
262 | if sv1 is not None and ap1 is not None: |
||
263 | create_Ventilation(u'par.linux0', sv1.name, ap1.name)
|
||
264 | if sv1 is not None and ap2 is not None: |
||
265 | create_Ventilation(u'par.linux0', sv1.name, ap2.name)
|