source: ccsd/trunk/crcnetd/modules/ccs_graph.py @ 1338

Last change on this file since 1338 was 1338, checked in by ckb6, 6 years ago

Fixed a bug that prevented the duplicates from being removed due to different timestamps

  • Property svn:keywords set to Id
File size: 15.2 KB
Line 
1# Copyright (C) 2006  The University of Waikato
2#
3# This file is part of crcnetd - CRCnet Configuration System Daemon
4#
5# This file deals with graphs
6#
7# Author:       Chris Browning <ckb6@cs.waikato.ac.nz>
8# Version:      $Id$
9#
10# crcnetd is free software; you can redistribute it and/or modify it under the
11# terms of the GNU General Public License version 2 as published by the Free
12# Software Foundation.
13#
14# crcnetd is distributed in the hope that it will be useful, but WITHOUT ANY
15# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17# details.
18#
19# You should have received a copy of the GNU General Public License along with
20# crcnetd; if not, write to the Free Software Foundation, Inc.,
21# 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22from crcnetd._utils.ccsd_common import *
23from crcnetd._utils.ccsd_log import *
24from crcnetd._utils.ccsd_events import *
25from crcnetd._utils.ccsd_session import getSession, getSessionE
26from crcnetd._utils.ccsd_server import exportViaXMLRPC
27from crcnetd._utils.ccsd_service import getServiceInstance
28ccs_mod_type = CCSD_SERVER
29GRAPH_SERVER = "localhost:80"
30
31@exportViaXMLRPC(SESSION_RO, AUTH_USER)
32def getGraph(session_id, graph_id, time_id):
33    '''This returns everything the graph server needs to draw a graph'''
34
35    session = getSessionE(session_id)
36   
37    #Get the graph type
38    sql = "SELECT a.*, b.class_name AS filename2 FROM graph_type a, rrdbot_class b WHERE b.class_id=a.class_id AND graph_id=%s"
39    #sql = "SELECT * FROM graph_type where graph_id=%s"
40    res = session.query(sql, (graph_id))
41   
42    #Get the parts for the graph
43    sql = "SELECT * FROM graph_parts where graph_id=%s ORDER BY graph_order"
44    res2 = session.query(sql, (graph_id))
45
46    #Get the time class
47    sql = "SELECT * FROM graph_time where time_id=%s"
48    res3 = session.query(sql, (time_id))
49
50    return [res, res2, res3]
51
52@exportViaXMLRPC(SESSION_RO, AUTH_USER)
53def getSnmpLogs(session_id):
54    '''This returns a list of entries in the rrdbotlog view'''
55
56    session = getSessionE(session_id)
57
58    sql = "SELECT * FROM rrdbotlog" 
59    res = session.query(sql, ()) 
60    res.sort(key=lambda obj:obj["timestamp"]);
61    return res
62   
63@exportViaXMLRPC(SESSION_RW, AUTH_USER)
64def snmpDelete(session_id, host_id, class_id, num):
65    '''This deletes a row in the snmp_discovery table'''
66
67    session = getSessionE(session_id)
68
69    sql = "DELETE FROM snmp_discovery WHERE host_id=%s AND class_id=%s AND num=%s" 
70    session.execute(sql, (host_id,class_id, num)) 
71    return 1
72   
73@exportViaXMLRPC(SESSION_RW, AUTH_USER)
74def snmpDeleteHost(session_id, host_id):
75    '''This removes a host from the snmp_discovery table'''
76    session = getSessionE(session_id)
77
78    sql = "DELETE FROM snmp_discovery WHERE host_id=%s" 
79    session.execute(sql, (host_id)) 
80    return 1
81   
82@exportViaXMLRPC(SESSION_RW, AUTH_USER)
83def snmpFlush(session_id):
84    '''This flushes the entire snmp_discovery table'''
85    session = getSessionE(session_id)
86
87    sql = "DELETE FROM snmp_discovery" 
88    session.execute(sql, ()) 
89    return 1
90   
91@exportViaXMLRPC(SESSION_RO, AUTH_USER)
92def getHostGraphs(session_id, host_id):
93    '''This returns a list of graphs that are available to a host'''
94
95    session = getSessionE(session_id)
96
97    #-1 means all types of graphs
98    if int(host_id) == -1:
99        sql = "SELECT * FROM graph_group" 
100        res = session.query(sql, ()) 
101        res.sort(key=lambda obj:obj["group_name"].lower());
102        return res
103
104    sql = "SELECT DISTINCT b.group_id, b.group_name, a.host_id, a.host_name FROM graphview a LEFT JOIN graph_group b ON b.group_id=a.group_id WHERE host_id=%s" 
105    res = session.query(sql, (host_id))
106    res.sort(key=lambda obj:obj["group_name"].lower());
107    return res
108   
109@exportViaXMLRPC(SESSION_RO, AUTH_USER)
110def getLinkGraphs(session_id, link_id=-1, graph_id=-1, all=True):
111    global GRAPH_SERVER
112    '''This returns graph data for a link'''
113    session = getSessionE(session_id)
114    link_id = int(link_id)
115    graph_id = int(graph_id)
116   
117    if graph_id == link_id == -1:
118        #This case is for a menu so return limited data
119        sql = "SELECT DISTINCT link_id, description FROM graphview WHERE link_id!=0"
120        res = session.query(sql, ())
121    elif graph_id == -1:
122        sql = "SELECT * FROM graphview WHERE link_id=%s"
123        res = session.query(sql, (link_id))
124    elif link_id == -1:
125        sql = "SELECT * FROM graphview WHERE graph_id=%s AND link_id!=0"
126        res = session.query(sql, (graph_id))
127    else:
128        sql = "SELECT * FROM graphview WHERE link_id=%s AND graph_id=%s" 
129        res = session.query(sql, (link_id,graph_id))
130    res.sort(key=lambda obj:obj["description"].lower())
131    linkGraphs2 = []
132    for link in res:
133        newlink = {}
134        newlink["link_id"] = link["link_id"]
135        newlink["description"] = link["description"]
136        if all:
137            newlink ["host_id"] = link["host_id"]
138            newlink ["host_name"] = link["host_name"]
139            newlink ["graph_id"] = link["graph_id"]
140            newlink ["group_id"] = link["group_id"]
141            newlink ["title"] = link["title"]
142            newlink ["num"] = link["num"]
143            newlink ["description"] = link["description"]
144            newlink ["ip_address"] = link["ip_address"]
145       
146           
147        linkGraphs2.append(newlink)   
148    return GRAPH_SERVER, linkGraphs2     
149   
150@exportViaXMLRPC(SESSION_RO, AUTH_USER)
151def getGraphs(session_id, host_id=-1, group_id=-1, mirror='f'):
152    global GRAPH_SERVER
153    '''This returns data to draw graphs'''
154   
155    '''It also handles mirror in which both sides of a link are returned'''
156   
157    host_id = int(host_id)
158    group_id = int(group_id)
159    session = getSessionE(session_id)
160
161    #Select table based on mirror
162    if mirror == 't':
163        table = "mirrorview"
164    else:
165        table = "graphview"
166       
167    if group_id == host_id == -1:
168        sql = "SELECT * FROM %s" % table
169        res = session.query(sql, ())
170    elif group_id == -1:
171        sql = "SELECT * FROM %s WHERE host_id=%%s" % table
172        res = session.query(sql, (host_id))
173    elif host_id == -1:
174        sql = "SELECT * FROM %s WHERE group_id=%%s" % table
175        res = session.query(sql, (group_id))
176    else:
177        sql = "SELECT * FROM %s WHERE host_id=%%s AND group_id=%%s" % table
178        res = session.query(sql, (host_id,group_id))
179
180    #Prune of pythons double up of data but keep all data
181    #This loop saves time in php as it has to decode less
182    graphs2 = []
183    for graph in res:
184        newgraph = {}
185        newgraph ["host_id"] = graph["host_id"]
186        newgraph ["host_name"] = graph["host_name"]
187        newgraph ["graph_id"] = graph["graph_id"]
188        newgraph ["group_id"] = graph["group_id"]
189        newgraph ["title"] = graph["title"]
190        newgraph ["num"] = graph["num"]
191        newgraph ["description"] = graph["description"]
192        newgraph ["ip_address"] = graph["ip_address"]
193
194        if "num2" in graph.keys():
195             newgraph ["host_id2"] = graph["host_id2"]
196             newgraph ["host_name2"] = graph["host_name2"]
197             newgraph ["num2"] = graph["num2"]
198             newgraph ["ip_address2"] = graph["ip_address2"]
199               
200        graphs2.append(newgraph)   
201       
202    return GRAPH_SERVER, graphs2
203
204@exportViaXMLRPC(SESSION_RO, AUTH_USER)
205def getTimes(session_id):
206    '''Simple function to return all time classes'''
207
208    session = getSessionE(session_id)
209    sql = "SELECT * from graph_time"
210    res = session.query(sql, ())
211    res.sort()
212    return res
213
214   
215def getHostList(session_id):
216    ''' A custom getHostList that only returns hosts that there are graohs for'''
217    session = getSessionE(session_id)
218    sql = "SELECT DISTINCT host_id, host_name from graphview"
219    res = session.query(sql, ())
220    res.sort(key=lambda obj:obj["host_name"].lower())
221    return res
222
223@exportViaXMLRPC(SESSION_RO, AUTH_USER)
224def getEveryThing(session_id, host_id):
225    '''This function returns everything needed for the graph interface'''
226   
227    '''Its purpose is to reduce the number of xmlrpc calls required'''
228   
229    '''It also removes double up infomation for speed in php'''
230    x1 = time.time()
231
232   
233    hostList = getHostList(session_id)
234    hostList2 = []
235    for host in hostList:
236        newhost = {}
237        newhost["host_name"] = host["host_name"]
238        newhost["host_id"] = host["host_id"]
239        hostList2.append(newhost)
240    if host_id == '' and len(hostList2) > 0:
241        host_id = hostList2[0]["host_id"]
242    elif host_id == '':
243        host_id = 0
244
245    server, linkGraphs = getLinkGraphs(session_id,-1,-1,False)
246
247    hostGraphs = getHostGraphs(session_id, host_id)
248    hostGraphs2 = []
249    for graph in hostGraphs:
250        newgraph = {}
251        newgraph ["group_id"] = graph["group_id"]
252        newgraph ["group_name"] = graph["group_name"]
253        hostGraphs2.append(newgraph)   
254    server, graphs = getGraphs(session_id)
255    times = getTimes(session_id)
256    pack = [hostList2, linkGraphs, hostGraphs2, graphs, times]
257    return pack
258   
259@exportViaXMLRPC(SESSION_RO, AUTH_USER)
260def getGraphserver(session_id):
261    '''Simple function to return the graphserver'''
262    return GRAPH_SERVER
263   
264@exportViaXMLRPC(SESSION_RO, AUTH_USER)
265def getGraphGroups(session_id):
266    '''Simple function to return all graph groups'''
267
268    session = getSessionE(session_id)
269    sql = "SELECT * from graph_group"
270    res = session.query(sql, ())
271    return res
272
273@exportViaXMLRPC(SESSION_RO, AUTH_USER)
274def getGraphTypes(session_id, group_id):
275    '''Simple function to return all graph types in a group'''
276
277    session = getSessionE(session_id)
278    sql = "SELECT * from graph_type WHERE group_id=%s"
279    res = session.query(sql, (group_id))
280    return res
281
282@exportViaXMLRPC(SESSION_RO, AUTH_USER)
283def getGraphParts(session_id, graph_id):
284    '''Simple function to return all graph parts of a graph'''
285
286    session = getSessionE(session_id)
287    sql = "SELECT * from graph_parts WHERE graph_id=%s ORDER BY graph_order"
288    res = session.query(sql, (graph_id))
289    return res   
290
291@exportViaXMLRPC(SESSION_RO, AUTH_USER)
292def getGraphPart(session_id, part_id):
293    '''Simple function to return a graph part'''
294
295    session = getSessionE(session_id)
296    sql = "SELECT * from graph_parts WHERE part_id=%s"
297    res = session.query(sql, (part_id))
298    return res[0]     
299
300@exportViaXMLRPC(SESSION_RO, AUTH_USER)
301def getGraphGroup(session_id, group_id):
302    '''Simple function to return a group'''
303
304    session = getSessionE(session_id)
305    sql = "SELECT * from graph_group WHERE group_id=%s"
306    res = session.query(sql, (group_id))
307    return res[0]   
308   
309@exportViaXMLRPC(SESSION_RO, AUTH_USER)
310def getGraphType(session_id, graph_id):
311    '''Simple function to return a graph type'''
312
313    session = getSessionE(session_id)
314    sql = "SELECT * from graph_type WHERE graph_id=%s"
315    res = session.query(sql, (graph_id))
316    return res[0]   
317   
318@exportViaXMLRPC(SESSION_RO, AUTH_USER)
319def getGraphVars(session_id, graph_id):
320    '''Returns all vars that are defined'''
321
322    session = getSessionE(session_id)
323    sql = "SELECT varname from graph_parts WHERE graph_id=%s AND (type='DEF' OR type='CDEF')"
324    res = session.query(sql, (graph_id))
325    return res   
326
327@exportViaXMLRPC(SESSION_RW, AUTH_ADMINISTRATOR)
328def updateGraphPartDetails(session_id, part_id, newDetails):
329    """Updates the details of the Graph part.
330
331    newDetails should be a dictionary containing only these class paramters
332    that have changed and need to be updated in the database.
333    """
334    session = getSessionE(session_id)
335       
336    # Build SQL
337    props = ["type", "colour", "text", "cf", \
338        "filename", "varname", "graph_order"]
339
340    (sql, values) = buildUpdateFromDict("graph_parts", props, newDetails, \
341            "part_id", part_id)
342       
343    if values == None:
344        # No changes made... ?
345        return 1
346   
347    # Run the query
348    session.execute(sql, values)
349
350    return 1 
351   
352@exportViaXMLRPC(SESSION_RW, AUTH_ADMINISTRATOR)
353def updateGraphTypeDetails(session_id, graph_id, newDetails):
354    """Updates the details of the Graph type.
355
356    newDetails should be a dictionary containing only these class paramters
357    that have changed and need to be updated in the database.
358    """
359    session = getSessionE(session_id)
360       
361    # Build SQL
362    props = ["title", "class_id", "virtical_label"]
363
364    (sql, values) = buildUpdateFromDict("graph_type", props, newDetails, \
365            "graph_id", graph_id)
366       
367    if values == None:
368        # No changes made... ?
369        return 1
370   
371    # Run the query
372    session.execute(sql, values)
373
374    return 1   
375   
376@exportViaXMLRPC(SESSION_RW, AUTH_USER)
377def addGraphGroup(session_id, clas):
378    """Adds a new graph group to the database"""
379    session = getSessionE(session_id)
380   
381    # Build query
382    props = ["group_name"]
383    (sql, values) = buildInsertFromDict("graph_group", props, clas)
384   
385    # Run query
386    session.execute(sql, values)
387
388    return 1
389   
390@exportViaXMLRPC(SESSION_RW, AUTH_USER)
391def addGraphType(session_id, clas):
392    """Adds a new graph type to the database"""
393    session = getSessionE(session_id)
394   
395    # Build query
396    props = ["title", "class_id", "group_id"]
397    (sql, values) = buildInsertFromDict("graph_type", props, clas)
398   
399    # Run query
400    session.execute(sql, values)
401
402    return 1   
403   
404@exportViaXMLRPC(SESSION_RW, AUTH_USER)
405def addGraphPart(session_id, clas):
406    """Adds a new graph part to the database"""
407    session = getSessionE(session_id)
408   
409    # Build query
410    props = ["type", "colour", "text", "varname", "cf", "filename", "graph_id", "graph_order"]
411    (sql, values) = buildInsertFromDict("graph_parts", props, clas)
412   
413    # Run query
414    session.execute(sql, values)
415
416    return 1       
417   
418@exportViaXMLRPC(SESSION_RW, AUTH_ADMINISTRATOR)
419def removeGraphPart(session_id, part_id):
420    """Removes a graph part from the database."""
421   
422    session = getSessionE(session_id)
423
424    # Delete
425    sql = "DELETE FROM graph_parts WHERE part_id=%s"
426    res = session.execute(sql, (part_id))
427    return 1
428   
429@exportViaXMLRPC(SESSION_RW, AUTH_ADMINISTRATOR)
430def removeGraphType(session_id, graph_id):
431    """Removes a graph type from the database."""
432   
433    session = getSessionE(session_id)
434
435    # Delete
436    sql = "DELETE FROM graph_type WHERE graph_id=%s"
437    res = session.execute(sql, (graph_id))
438    return 1
439   
440@exportViaXMLRPC(SESSION_RW, AUTH_ADMINISTRATOR)
441def removeGraphGroup(session_id, group_id):
442    """Removes a graph group from the database."""
443   
444    session = getSessionE(session_id)
445
446    # Delete
447    sql = "DELETE FROM graph_group WHERE group_id=%s"
448    res = session.execute(sql, (group_id))
449    return 1   
450
451def ccs_init():
452    global GRAPH_SERVER
453    GRAPH_SERVER = "%s:%s" % (config_get("graphs", "host", "localhost"), \
454        config_get("graphs", "port", "80"))
455
456@catchEvent("interfaceModified")
457@catchEvent("linkModified")
458@catchEvent("hostModified")
459def eventcatcher(eventName, host_id, session_id, **params):
460    session = getSessionE(session_id)
461    str = "Caught event %s for host %s, session = %s ("
462    if not params:       
463        str += "%s"
464        params = ""
465    first = True
466    for param in params:
467        if first:
468            str += "%s"
469        else:
470            str += ", %s"
471    str += ")"
472    print str % (eventName, host_id, session_id, params)
473    print session.getChangesetDescription()
474   
Note: See TracBrowser for help on using the repository browser.