vigigraph / maj_bdd.py @ b8b576c5
History | View | Annotate | Download (7.57 KB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
import os |
3 |
import atexit |
4 |
from datetime import datetime |
5 |
from sqlalchemy import and_ |
6 |
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 |
|
23 |
from vigilo.models import Host, HostGroup |
24 |
from vigilo.models import LowLevelService, ServiceGroup |
25 |
from vigilo.models import PerfDataSource, Graph |
26 |
|
27 |
|
28 |
# Groupe d'hôtes (HostGroup)
|
29 |
def create_HostGroup(name, parent=None): |
30 |
g = DBSession.query(HostGroup).filter(HostGroup.name == name).first() |
31 |
if not g: |
32 |
if parent:
|
33 |
g = HostGroup(name=name, idparent=parent.idgroup) |
34 |
else:
|
35 |
g = HostGroup(name=name) |
36 |
print "Ajout du Groupe: ", name |
37 |
DBSession.add(g) |
38 |
return g
|
39 |
|
40 |
# Hôte (Host)
|
41 |
#def create_Host(name, checkhostcmd, hosttpl, snmpcommunity, mainip, snmpport):
|
42 |
def create_Host(name): |
43 |
h = DBSession.query(Host).filter(Host.name == name).first() |
44 |
if not h: |
45 |
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 |
print "Ajout de l'hôte: ", name |
53 |
DBSession.add(h) |
54 |
return h
|
55 |
|
56 |
#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 |
# 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 |
def create_LowLevelService(hostname, servicename): |
72 |
s = DBSession.query(LowLevelService) \ |
73 |
.join((Host, LowLevelService.idhost == Host.idhost)) \ |
74 |
.filter(LowLevelService.servicename == servicename) \ |
75 |
.filter(Host.name == hostname) \ |
76 |
.first() |
77 |
if not s: |
78 |
s = LowLevelService(idhost=get_host(hostname).idhost, |
79 |
servicename=servicename, |
80 |
weight = 42,
|
81 |
op_dep=u"?")
|
82 |
print "Ajout du service", servicename |
83 |
DBSession.add(s) |
84 |
return s
|
85 |
|
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 |
def add_LowLevelService2ServiceGroup(service, group): |
100 |
if service not in group.services: |
101 |
print "Ajout du service: %(s)s dans le group: %(g)s" % \ |
102 |
{'s': service.servicename,
|
103 |
'g': group.name}
|
104 |
group.services.append(service) |
105 |
|
106 |
def _get_service(hostname, servicename): |
107 |
""" Return Host object from hostname, None if not available"""
|
108 |
return DBSession.query(LowLevelService) \
|
109 |
.join((Host, Host.idhost == LowLevelService.idhost)) \ |
110 |
.filter(Host.name == hostname) \ |
111 |
.filter(LowLevelService.servicename == servicename) \ |
112 |
.first() |
113 |
|
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 |
#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 |
.filter(Host.name == hostname) \ |
124 |
.first() |
125 |
return s
|
126 |
|
127 |
# 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 |
|
151 |
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 |
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 |
h5 = create_Host(u'par.linux0')
|
162 |
|
163 |
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 |
add_Host2HostGroup(h5, hg3) |
169 |
|
170 |
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 |
|
176 |
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 |
|
189 |
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 |
#gr5 = create_graph(u'graph5',u'Graph5', None)
|
194 |
gr5 = create_graph(u'IO',u'IO', None) |
195 |
gr6 = create_graph(u'RAM',u'RAM', None) |
196 |
gr7 = create_graph(u'TCP connections',u'TCP connections', None) |
197 |
gr8 = create_graph(u'CPU usage (by type)',u'CPU usage (by type)', None) |
198 |
|
199 |
graphs = [] |
200 |
for g in DBSession.query(Graph).all(): |
201 |
graphs.append(g) |
202 |
|
203 |
|
204 |
ds1 = create_ds(u'ineth0', u'GAUGE', s1 \ |
205 |
, u'Données en entrée sur eth0', graphs[1:2]) |
206 |
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 |
, u'Données en entrée sur eth0', graphs[3:4]) |
212 |
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 |
ds6 = create_ds(u'RAM', u'GAUGE', s5 \ |
217 |
, u'RAM', graphs[5:6]) |
218 |
ds7 = create_ds(u'TCP connections', u'GAUGE', s5 \ |
219 |
, u'TCP connections', graphs[6:7]) |
220 |
ds8 = create_ds(u'CPU usage (by type)', u'GAUGE', s5 \ |
221 |
, u'CPU usage (by type)', graphs[7:8]) |
222 |
|