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