vigigraph / maj_bdd.py @ b8b576c5
History | View | Annotate | Download (7.57 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 | f151053f | Thomas BURGUIERE | |
27 | |||
28 | d9d07df6 | Thomas BURGUIERE | # Groupe d'hôtes (HostGroup)
|
29 | def create_HostGroup(name, parent=None): |
||
30 | g = DBSession.query(HostGroup).filter(HostGroup.name == name).first() |
||
31 | f151053f | Thomas BURGUIERE | if not g: |
32 | if parent:
|
||
33 | d9d07df6 | Thomas BURGUIERE | g = HostGroup(name=name, idparent=parent.idgroup) |
34 | f151053f | Thomas BURGUIERE | else:
|
35 | d9d07df6 | Thomas BURGUIERE | g = HostGroup(name=name) |
36 | f151053f | Thomas BURGUIERE | print "Ajout du Groupe: ", name |
37 | DBSession.add(g) |
||
38 | return g
|
||
39 | |||
40 | # Hôte (Host)
|
||
41 | f158722a | Francis LAHEUGUERE | #def create_Host(name, checkhostcmd, hosttpl, snmpcommunity, mainip, snmpport):
|
42 | def create_Host(name): |
||
43 | d9d07df6 | Thomas BURGUIERE | h = DBSession.query(Host).filter(Host.name == name).first() |
44 | f151053f | Thomas BURGUIERE | if not h: |
45 | f158722a | Francis LAHEUGUERE | h = Host(name=name, |
46 | checkhostcmd=u'dummy',
|
||
47 | hosttpl=u'linux',
|
||
48 | mainip=u"127.0.0.1",
|
||
49 | snmpcommunity=u"public",
|
||
50 | snmpport=161,
|
||
51 | weight=0)
|
||
52 | f151053f | Thomas BURGUIERE | print "Ajout de l'hôte: ", name |
53 | DBSession.add(h) |
||
54 | return h
|
||
55 | |||
56 | f158722a | Francis LAHEUGUERE | #Recherche de l'objet Host à partir du name
|
57 | def get_host(hostname): |
||
58 | """ Return Host object from hostname, None if not available"""
|
||
59 | return DBSession.query(Host) \
|
||
60 | .filter(Host.name == hostname) \ |
||
61 | .first() |
||
62 | |||
63 | d9d07df6 | Thomas BURGUIERE | # Ajout d'un hôte dans un groupe d'hôtes (Host -> HostGroup)
|
64 | def add_Host2HostGroup(host, group): |
||
65 | if host not in group.hosts: |
||
66 | print "Ajout de l'hote: %(h)s dans le group: %(g)s" % \ |
||
67 | {'h': host.name,
|
||
68 | 'g': group.name}
|
||
69 | group.hosts.append(host) |
||
70 | |||
71 | a00a2345 | Francois POIROTTE | def create_LowLevelService(hostname, servicename): |
72 | s = DBSession.query(LowLevelService) \ |
||
73 | .join((Host, LowLevelService.idhost == Host.idhost)) \ |
||
74 | .filter(LowLevelService.servicename == servicename) \ |
||
75 | 059cc5de | William MAISONMARCHEUX | .filter(Host.name == hostname) \ |
76 | d9d07df6 | Thomas BURGUIERE | .first() |
77 | if not s: |
||
78 | a00a2345 | Francois POIROTTE | s = LowLevelService(idhost=get_host(hostname).idhost, |
79 | d9d07df6 | Thomas BURGUIERE | servicename=servicename, |
80 | f158722a | Francis LAHEUGUERE | weight = 42,
|
81 | d9d07df6 | Thomas BURGUIERE | op_dep=u"?")
|
82 | print "Ajout du service", servicename |
||
83 | DBSession.add(s) |
||
84 | 9a994732 | Thomas BURGUIERE | return s
|
85 | d9d07df6 | Thomas BURGUIERE | |
86 | # Groupe de services (ServiceGroup)
|
||
87 | def create_ServiceGroup(name, parent=None): |
||
88 | g = DBSession.query(ServiceGroup).filter(ServiceGroup.name == name).first() |
||
89 | if not g: |
||
90 | if parent:
|
||
91 | g = ServiceGroup(name=name, idparent=parent.idgroup) |
||
92 | else:
|
||
93 | g = ServiceGroup(name=name) |
||
94 | print "Ajout du Groupe: ", name |
||
95 | DBSession.add(g) |
||
96 | return g
|
||
97 | |||
98 | # Ajout d'un hôte dans un groupe d'hôtes (Host -> HostGroup)
|
||
99 | a00a2345 | Francois POIROTTE | def add_LowLevelService2ServiceGroup(service, group): |
100 | 9a994732 | Thomas BURGUIERE | if service not in group.services: |
101 | d9d07df6 | Thomas BURGUIERE | print "Ajout du service: %(s)s dans le group: %(g)s" % \ |
102 | 9a994732 | Thomas BURGUIERE | {'s': service.servicename,
|
103 | d9d07df6 | Thomas BURGUIERE | 'g': group.name}
|
104 | 9a994732 | Thomas BURGUIERE | group.services.append(service) |
105 | d9d07df6 | Thomas BURGUIERE | |
106 | f158722a | Francis LAHEUGUERE | def _get_service(hostname, servicename): |
107 | """ Return Host object from hostname, None if not available"""
|
||
108 | a00a2345 | Francois POIROTTE | return DBSession.query(LowLevelService) \
|
109 | .join((Host, Host.idhost == LowLevelService.idhost)) \ |
||
110 | f158722a | Francis LAHEUGUERE | .filter(Host.name == hostname) \ |
111 | a00a2345 | Francois POIROTTE | .filter(LowLevelService.servicename == servicename) \ |
112 | 9a994732 | Thomas BURGUIERE | .first() |
113 | cd60da72 | Francis LAHEUGUERE | |
114 | #Recherche de l'objet ServiceGroup à partir du name
|
||
115 | def get_ServiceGroup(name): |
||
116 | return DBSession.query(ServiceGroup).filter(ServiceGroup.name == name).first()
|
||
117 | |||
118 | a00a2345 | Francois POIROTTE | #Recherche de l'objet LowLevelService à partir du name
|
119 | def get_LowLevelService(hostname, servicename): |
||
120 | s = DBSession.query(LowLevelService) \ |
||
121 | .join((Host, LowLevelService.idhost == Host.idhost)) \ |
||
122 | .filter(LowLevelService.servicename == servicename) \ |
||
123 | cd60da72 | Francis LAHEUGUERE | .filter(Host.name == hostname) \ |
124 | .first() |
||
125 | return s
|
||
126 | |||
127 | f158722a | Francis LAHEUGUERE | # DS (Graph)
|
128 | def create_graph(name, vlabel, perfdatasources): |
||
129 | gr = DBSession.query(Graph) \ |
||
130 | .filter(Graph.name == name) \ |
||
131 | .first() |
||
132 | if not gr: |
||
133 | gr = Graph(name=name, vlabel=vlabel) |
||
134 | print "Ajout du graph: ", vlabel |
||
135 | DBSession.add(gr) |
||
136 | return gr
|
||
137 | |||
138 | # DS (PerfDataSource)
|
||
139 | def create_ds(name, type, service, label, graphs): |
||
140 | ds = DBSession.query(PerfDataSource) \ |
||
141 | .filter(PerfDataSource.service == service) \ |
||
142 | .filter(PerfDataSource.name == name) \ |
||
143 | .first() |
||
144 | if not ds: |
||
145 | ds = PerfDataSource(name=name, type=type, service=service, label=label, graphs=graphs)
|
||
146 | print "Ajout de la datasource: ", label |
||
147 | DBSession.add(ds) |
||
148 | return ds
|
||
149 | |||
150 | cd60da72 | Francis LAHEUGUERE | |
151 | d9d07df6 | Thomas BURGUIERE | hg1 = create_HostGroup(u'Serveurs')
|
152 | hg2 = create_HostGroup(u'Telecoms')
|
||
153 | hg3 = create_HostGroup(u'Serveurs Linux', hg1)
|
||
154 | hg4 = create_HostGroup(u'NORTEL', hg2)
|
||
155 | hg5 = create_HostGroup(u'CISCO', hg2)
|
||
156 | |||
157 | f158722a | Francis LAHEUGUERE | h1 = create_Host(u'proto4.si.c-s.fr')
|
158 | h2 = create_Host(u'messagerie.si.c-s.fr')
|
||
159 | h3 = create_Host(u'testnortel.si.c-s.fr')
|
||
160 | h4 = create_Host(u'proto6.si.c-s.fr')
|
||
161 | 470f60ca | Francis LAHEUGUERE | h5 = create_Host(u'par.linux0')
|
162 | f151053f | Thomas BURGUIERE | |
163 | d9d07df6 | Thomas BURGUIERE | add_Host2HostGroup(h1, hg3) |
164 | add_Host2HostGroup(h2, hg3) |
||
165 | add_Host2HostGroup(h3, hg4) |
||
166 | add_Host2HostGroup(h4, hg5) |
||
167 | add_Host2HostGroup(h4, hg3) |
||
168 | 470f60ca | Francis LAHEUGUERE | add_Host2HostGroup(h5, hg3) |
169 | f151053f | Thomas BURGUIERE | |
170 | 9a994732 | Thomas BURGUIERE | sg1 = create_ServiceGroup(u'Général')
|
171 | sg2 = create_ServiceGroup(u'Interface Réseau')
|
||
172 | sg3 = create_ServiceGroup(u'Performance')
|
||
173 | sg4 = create_ServiceGroup(u'Partitions')
|
||
174 | sg5 = create_ServiceGroup(u'Processus')
|
||
175 | f151053f | Thomas BURGUIERE | |
176 | a00a2345 | Francois POIROTTE | s1 = create_LowLevelService(h1.name, u'Interface eth0')
|
177 | s2 = create_LowLevelService(h1.name, u'Interface eth1')
|
||
178 | s3 = create_LowLevelService(h1.name, u'Interface série')
|
||
179 | s4 = create_LowLevelService(h4.name, u'Interface')
|
||
180 | s5 = create_LowLevelService(h5.name, u'Interface Linux')
|
||
181 | |||
182 | add_LowLevelService2ServiceGroup(s1, sg2) |
||
183 | add_LowLevelService2ServiceGroup(s2, sg2) |
||
184 | add_LowLevelService2ServiceGroup(s3, sg1) |
||
185 | add_LowLevelService2ServiceGroup(s4, sg2) |
||
186 | add_LowLevelService2ServiceGroup(s5, sg2) |
||
187 | add_LowLevelService2ServiceGroup(s5, sg3) |
||
188 | 9a994732 | Thomas BURGUIERE | |
189 | f158722a | Francis LAHEUGUERE | gr1 = create_graph(u'graph1',u'Graph1', None) |
190 | gr2 = create_graph(u'graph2',u'Graph2', None) |
||
191 | gr3 = create_graph(u'graph3',u'Graph3', None) |
||
192 | gr4 = create_graph(u'graph4',u'Graph4', None) |
||
193 | 470f60ca | Francis LAHEUGUERE | #gr5 = create_graph(u'graph5',u'Graph5', None)
|
194 | gr5 = create_graph(u'IO',u'IO', None) |
||
195 | 6a089374 | Francis LAHEUGUERE | gr6 = create_graph(u'RAM',u'RAM', None) |
196 | e513c11c | Francis LAHEUGUERE | gr7 = create_graph(u'TCP connections',u'TCP connections', None) |
197 | f2a0c70f | Francis LAHEUGUERE | gr8 = create_graph(u'CPU usage (by type)',u'CPU usage (by type)', None) |
198 | f158722a | Francis LAHEUGUERE | |
199 | graphs = [] |
||
200 | for g in DBSession.query(Graph).all(): |
||
201 | graphs.append(g) |
||
202 | |||
203 | f2a0c70f | Francis LAHEUGUERE | |
204 | f158722a | Francis LAHEUGUERE | ds1 = create_ds(u'ineth0', u'GAUGE', s1 \ |
205 | f2a0c70f | Francis LAHEUGUERE | , u'Données en entrée sur eth0', graphs[1:2]) |
206 | f158722a | Francis LAHEUGUERE | ds2 = create_ds(u'outeth0', u'GAUGE', s2 \ |
207 | , u'Données en sortie sur eth0', graphs[1:3]) |
||
208 | ds3 = create_ds(u'outeth1', u'GAUGE', s3 \ |
||
209 | , u'Données en sortie sur eth1', graphs[2:4]) |
||
210 | ds4 = create_ds(u'ineth1', u'GAUGE', s4 \ |
||
211 | ad17826c | Francis LAHEUGUERE | , u'Données en entrée sur eth0', graphs[3:4]) |
212 | e513c11c | Francis LAHEUGUERE | ds5_1 = create_ds(u'IO Reads', u'GAUGE', s5 \ |
213 | , u'IO Reads', graphs[4:5]) |
||
214 | ds5_2 = create_ds(u'IO Writes', u'GAUGE', s5 \ |
||
215 | , u'IO Writes', graphs[4:5]) |
||
216 | 6a089374 | Francis LAHEUGUERE | ds6 = create_ds(u'RAM', u'GAUGE', s5 \ |
217 | e513c11c | Francis LAHEUGUERE | , u'RAM', graphs[5:6]) |
218 | ds7 = create_ds(u'TCP connections', u'GAUGE', s5 \ |
||
219 | , u'TCP connections', graphs[6:7]) |
||
220 | f2a0c70f | Francis LAHEUGUERE | ds8 = create_ds(u'CPU usage (by type)', u'GAUGE', s5 \ |
221 | , u'CPU usage (by type)', graphs[7:8]) |