Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigigraph / maj_bdd.py @ 2887f19e

History | View | Annotate | Download (8.62 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 0645820e Eric SIMORRE
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 ab5f0b58 Francois POIROTTE
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 f151053f Thomas BURGUIERE
24 0645820e Eric SIMORRE
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 f151053f Thomas BURGUIERE
    return g
34
35
# Hôte (Host)
36 b7c13398 Francois POIROTTE
#def create_Host(name, checkhostcmd, hosttpl, snmpcommunity, address, snmpport):
37 f158722a Francis LAHEUGUERE
def create_Host(name):
38 d9d07df6 Thomas BURGUIERE
    h = DBSession.query(Host).filter(Host.name == name).first()
39 f151053f Thomas BURGUIERE
    if not h:
40 f158722a Francis LAHEUGUERE
        h = Host(name=name,
41
                 checkhostcmd=u'dummy',
42
                 hosttpl=u'linux',
43 b7c13398 Francois POIROTTE
                 address=u"127.0.0.1",
44 f158722a Francis LAHEUGUERE
                 snmpcommunity=u"public",
45
                 snmpport=161,
46
                 weight=0)
47 f151053f Thomas BURGUIERE
        print "Ajout de l'hôte: ", name
48
        DBSession.add(h)
49
    return h
50
51 f158722a Francis LAHEUGUERE
#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 0645820e Eric SIMORRE
# 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 d9d07df6 Thomas BURGUIERE
        print "Ajout de l'hote: %(h)s dans le group: %(g)s" % \
62
                {'h': host.name,
63
                 'g': group.name}
64 0645820e Eric SIMORRE
        host.groups.append(group)
65 d9d07df6 Thomas BURGUIERE
66 a00a2345 Francois POIROTTE
def create_LowLevelService(hostname, servicename):
67
    s = DBSession.query(LowLevelService) \
68
            .join((Host, LowLevelService.idhost == Host.idhost)) \
69
            .filter(LowLevelService.servicename == servicename) \
70 059cc5de William MAISONMARCHEUX
            .filter(Host.name == hostname) \
71 d9d07df6 Thomas BURGUIERE
            .first()
72
    if not s:
73 a00a2345 Francois POIROTTE
        s = LowLevelService(idhost=get_host(hostname).idhost,
74 d9d07df6 Thomas BURGUIERE
                servicename=servicename,
75 f158722a Francis LAHEUGUERE
                weight = 42, 
76 d9d07df6 Thomas BURGUIERE
                op_dep=u"?")
77
        print "Ajout du service", servicename
78
        DBSession.add(s)
79 9a994732 Thomas BURGUIERE
    return s
80 d9d07df6 Thomas BURGUIERE
81
82 0645820e Eric SIMORRE
# 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 d9d07df6 Thomas BURGUIERE
        print "Ajout du service: %(s)s dans le group: %(g)s" % \
86 9a994732 Thomas BURGUIERE
                {'s': service.servicename,
87 d9d07df6 Thomas BURGUIERE
                 'g': group.name}
88 0645820e Eric SIMORRE
        service.groups.append(group)
89 d9d07df6 Thomas BURGUIERE
90 f158722a Francis LAHEUGUERE
def _get_service(hostname, servicename):
91
    """ Return Host object from hostname, None if not available"""
92 a00a2345 Francois POIROTTE
    return DBSession.query(LowLevelService) \
93
            .join((Host, Host.idhost == LowLevelService.idhost)) \
94 f158722a Francis LAHEUGUERE
            .filter(Host.name == hostname) \
95 a00a2345 Francois POIROTTE
            .filter(LowLevelService.servicename == servicename) \
96 9a994732 Thomas BURGUIERE
            .first()
97 cd60da72 Francis LAHEUGUERE
98 0645820e Eric SIMORRE
#Recherche de l'objet SupItemGroup à partir du name
99
def get_SupItemGroup(name):
100
    return DBSession.query(SupItemGroup).filter(SupItemGroup.name == name).first()
101 cd60da72 Francis LAHEUGUERE
102 a00a2345 Francois POIROTTE
#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 cd60da72 Francis LAHEUGUERE
            .filter(Host.name == hostname) \
108
            .first()
109
    return s
110
111 f158722a Francis LAHEUGUERE
# 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 f5c20e6e Francis LAHEUGUERE
# VigiloServer
135
def create_Server(name, description):
136 4a2d4c17 Francis LAHEUGUERE
    s = DBSession.query(VigiloServer).filter(VigiloServer.name == name).first()
137 f5c20e6e Francis LAHEUGUERE
    if not s:
138 0645820e Eric SIMORRE
        s = VigiloServer(name=name)
139 f5c20e6e Francis LAHEUGUERE
        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 cd60da72 Francis LAHEUGUERE
165 0645820e Eric SIMORRE
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 d9d07df6 Thomas BURGUIERE
171 f158722a Francis LAHEUGUERE
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 470f60ca Francis LAHEUGUERE
h5 = create_Host(u'par.linux0')
176 f151053f Thomas BURGUIERE
177 0645820e Eric SIMORRE
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 f151053f Thomas BURGUIERE
184 0645820e Eric SIMORRE
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 f151053f Thomas BURGUIERE
190 a00a2345 Francois POIROTTE
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 0645820e Eric SIMORRE
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 9a994732 Thomas BURGUIERE
203 f158722a Francis LAHEUGUERE
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 470f60ca Francis LAHEUGUERE
#gr5 = create_graph(u'graph5',u'Graph5', None)
208
gr5 = create_graph(u'IO',u'IO', None)
209 6a089374 Francis LAHEUGUERE
gr6 = create_graph(u'RAM',u'RAM', None)
210 e513c11c Francis LAHEUGUERE
gr7 = create_graph(u'TCP connections',u'TCP connections', None)
211 f2a0c70f Francis LAHEUGUERE
gr8 = create_graph(u'CPU usage (by type)',u'CPU usage (by type)', None)
212 f158722a Francis LAHEUGUERE
213
graphs = []
214
for g in DBSession.query(Graph).all():
215
    graphs.append(g)
216
217 f2a0c70f Francis LAHEUGUERE
218 f158722a Francis LAHEUGUERE
ds1 = create_ds(u'ineth0', u'GAUGE', s1 \
219 f2a0c70f Francis LAHEUGUERE
                , u'Données en entrée sur eth0', graphs[1:2])
220 f158722a Francis LAHEUGUERE
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 ad17826c Francis LAHEUGUERE
                , u'Données en entrée sur eth0', graphs[3:4])
226 e513c11c Francis LAHEUGUERE
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 6a089374 Francis LAHEUGUERE
ds6 = create_ds(u'RAM', u'GAUGE', s5 \
231 e513c11c Francis LAHEUGUERE
                , u'RAM', graphs[5:6])
232
ds7 = create_ds(u'TCP connections', u'GAUGE', s5 \
233
                , u'TCP connections', graphs[6:7])
234 f2a0c70f Francis LAHEUGUERE
ds8 = create_ds(u'CPU usage (by type)', u'GAUGE', s5 \
235
                , u'CPU usage (by type)', graphs[7:8])
236 f151053f Thomas BURGUIERE
237 f5c20e6e Francis LAHEUGUERE
# 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)