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

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

Reversed the direction of the sort of the snmp logs Fixes:#107

  • 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 ORDER BY timestamp DESC" 
59    res = session.query(sql, ()) 
60    return res
61   
62@exportViaXMLRPC(SESSION_RW, AUTH_USER)
63def snmpDelete(session_id, host_id, class_id, num):
64    '''This deletes a row in the snmp_discovery table'''
65
66    session = getSessionE(session_id)
67
68    sql = "DELETE FROM snmp_discovery WHERE host_id=%s AND class_id=%s AND num=%s" 
69    session.execute(sql, (host_id,class_id, num)) 
70    return 1
71   
72@exportViaXMLRPC(SESSION_RW, AUTH_USER)
73def snmpDeleteHost(session_id, host_id):
74    '''This removes a host from the snmp_discovery table'''
75    session = getSessionE(session_id)
76
77    sql = "DELETE FROM snmp_discovery WHERE host_id=%s" 
78    session.execute(sql, (host_id)) 
79    return 1
80   
81@exportViaXMLRPC(SESSION_RW, AUTH_USER)
82def snmpFlush(session_id):
83    '''This flushes the entire snmp_discovery table'''
84    session = getSessionE(session_id)
85
86    sql = "DELETE FROM snmp_discovery" 
87    session.execute(sql, ()) 
88    return 1
89   
90@exportViaXMLRPC(SESSION_RO, AUTH_USER)
91def getHostGraphs(session_id, host_id):
92    '''This returns a list of graphs that are available to a host'''
93
94    session = getSessionE(session_id)
95
96    #-1 means all types of graphs
97    if int(host_id) == -1:
98        sql = "SELECT * FROM graph_group" 
99        res = session.query(sql, ()) 
100        res.sort(key=lambda obj:obj["group_name"].lower());
101        return res
102
103    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" 
104    res = session.query(sql, (host_id))
105    res.sort(key=lambda obj:obj["group_name"].lower());
106    return res
107   
108@exportViaXMLRPC(SESSION_RO, AUTH_USER)
109def getLinkGraphs(session_id, link_id=-1, graph_id=-1, all=True):
110    global GRAPH_SERVER
111    '''This returns graph data for a link'''
112    session = getSessionE(session_id)
113    link_id = int(link_id)
114    graph_id = int(graph_id)
115   
116    if graph_id == link_id == -1:
117        #This case is for a menu so return limited data
118        sql = "SELECT DISTINCT link_id, description FROM graphview WHERE link_id!=0"
119        res = session.query(sql, ())
120    elif graph_id == -1:
121        sql = "SELECT * FROM graphview WHERE link_id=%s"
122        res = session.query(sql, (link_id))
123    elif link_id == -1:
124        sql = "SELECT * FROM graphview WHERE graph_id=%s AND link_id!=0"
125        res = session.query(sql, (graph_id))
126    else:
127        sql = "SELECT * FROM graphview WHERE link_id=%s AND graph_id=%s" 
128        res = session.query(sql, (link_id,graph_id))
129    res.sort(key=lambda obj:obj["description"].lower())
130    linkGraphs2 = []
131    for link in res:
132        newlink = {}
133        newlink["link_id"] = link["link_id"]
134        newlink["description"] = link["description"]
135        if all:
136            newlink ["host_id"] = link["host_id"]
137            newlink ["host_name"] = link["host_name"]
138            newlink ["graph_id"] = link["graph_id"]
139            newlink ["group_id"] = link["group_id"]
140            newlink ["title"] = link["title"]
141            newlink ["num"] = link["num"]
142            newlink ["description"] = link["description"]
143            newlink ["ip_address"] = link["ip_address"]
144       
145           
146        linkGraphs2.append(newlink)   
147    return GRAPH_SERVER, linkGraphs2     
148   
149@exportViaXMLRPC(SESSION_RO, AUTH_USER)
150def getGraphs(session_id, host_id=-1, group_id=-1, mirror='f'):
151    global GRAPH_SERVER
152    '''This returns data to draw graphs'''
153   
154    '''It also handles mirror in which both sides of a link are returned'''
155   
156    host_id = int(host_id)
157    group_id = int(group_id)
158    session = getSessionE(session_id)
159
160    #Select table based on mirror
161    if mirror == 't':
162        table = "mirrorview"
163    else:
164        table = "graphview"
165       
166    if group_id == host_id == -1:
167        sql = "SELECT * FROM %s" % table
168        res = session.query(sql, ())
169    elif group_id == -1:
170        sql = "SELECT * FROM %s WHERE host_id=%%s" % table
171        res = session.query(sql, (host_id))
172    elif host_id == -1:
173        sql = "SELECT * FROM %s WHERE group_id=%%s" % table
174        res = session.query(sql, (group_id))
175    else:
176        sql = "SELECT * FROM %s WHERE host_id=%%s AND group_id=%%s" % table
177        res = session.query(sql, (host_id,group_id))
178
179    #Prune of pythons double up of data but keep all data
180    #This loop saves time in php as it has to decode less
181    graphs2 = []
182    for graph in res:
183        newgraph = {}
184        newgraph ["host_id"] = graph["host_id"]
185        newgraph ["host_name"] = graph["host_name"]
186        newgraph ["graph_id"] = graph["graph_id"]
187        newgraph ["group_id"] = graph["group_id"]
188        newgraph ["title"] = graph["title"]
189        newgraph ["num"] = graph["num"]
190        newgraph ["description"] = graph["description"]
191        newgraph ["ip_address"] = graph["ip_address"]
192
193        if "num2" in graph.keys():
194             newgraph ["host_id2"] = graph["host_id2"]
195             newgraph ["host_name2"] = graph["host_name2"]
196             newgraph ["num2"] = graph["num2"]
197             newgraph ["ip_address2"] = graph["ip_address2"]
198               
199        graphs2.append(newgraph)   
200       
201    return GRAPH_SERVER, graphs2
202
203@exportViaXMLRPC(SESSION_RO, AUTH_USER)
204def getTimes(session_id):
205    '''Simple function to return all time classes'''
206
207    session = getSessionE(session_id)
208    sql = "SELECT * from graph_time"
209    res = session.query(sql, ())
210    res.sort()
211    return res
212
213   
214def getHostList(session_id):
215    ''' A custom getHostList that only returns hosts that there are graohs for'''
216    session = getSessionE(session_id)
217    sql = "SELECT DISTINCT host_id, host_name from graphview"
218    res = session.query(sql, ())
219    res.sort(key=lambda obj:obj["host_name"].lower())
220    return res
221
222@exportViaXMLRPC(SESSION_RO, AUTH_USER)
223def getEveryThing(session_id, host_id):
224    '''This function returns everything needed for the graph interface'''
225   
226    '''Its purpose is to reduce the number of xmlrpc calls required'''
227   
228    '''It also removes double up infomation for speed in php'''
229    x1 = time.time()
230
231   
232    hostList = getHostList(session_id)
233    hostList2 = []
234    for host in hostList:
235        newhost = {}
236        newhost["host_name"] = host["host_name"]
237        newhost["host_id"] = host["host_id"]
238        hostList2.append(newhost)
239    if host_id == '' and len(hostList2) > 0:
240        host_id = hostList2[0]["host_id"]
241    elif host_id == '':
242        host_id = 0
243
244    server, linkGraphs = getLinkGraphs(session_id,-1,-1,False)
245
246    hostGraphs = getHostGraphs(session_id, host_id)
247    hostGraphs2 = []
248    for graph in hostGraphs:
249        newgraph = {}
250        newgraph ["group_id"] = graph["group_id"]
251        newgraph ["group_name"] = graph["group_name"]
252        hostGraphs2.append(newgraph)   
253    server, graphs = getGraphs(session_id)
254    times = getTimes(session_id)
255    pack = [hostList2, linkGraphs, hostGraphs2, graphs, times]
256    return pack
257   
258@exportViaXMLRPC(SESSION_RO, AUTH_USER)
259def getGraphserver(session_id):
260    '''Simple function to return the graphserver'''
261    return GRAPH_SERVER
262   
263@exportViaXMLRPC(SESSION_RO, AUTH_USER)
264def getGraphGroups(session_id):
265    '''Simple function to return all graph groups'''
266
267    session = getSessionE(session_id)
268    sql = "SELECT * from graph_group"
269    res = session.query(sql, ())
270    return res
271
272@exportViaXMLRPC(SESSION_RO, AUTH_USER)
273def getGraphTypes(session_id, group_id):
274    '''Simple function to return all graph types in a group'''
275
276    session = getSessionE(session_id)
277    sql = "SELECT * from graph_type WHERE group_id=%s"
278    res = session.query(sql, (group_id))
279    return res
280
281@exportViaXMLRPC(SESSION_RO, AUTH_USER)
282def getGraphParts(session_id, graph_id):
283    '''Simple function to return all graph parts of a graph'''
284
285    session = getSessionE(session_id)
286    sql = "SELECT * from graph_parts WHERE graph_id=%s ORDER BY graph_order"
287    res = session.query(sql, (graph_id))
288    return res   
289
290@exportViaXMLRPC(SESSION_RO, AUTH_USER)
291def getGraphPart(session_id, part_id):
292    '''Simple function to return a graph part'''
293
294    session = getSessionE(session_id)
295    sql = "SELECT * from graph_parts WHERE part_id=%s"
296    res = session.query(sql, (part_id))
297    return res[0]     
298
299@exportViaXMLRPC(SESSION_RO, AUTH_USER)
300def getGraphGroup(session_id, group_id):
301    '''Simple function to return a group'''
302
303    session = getSessionE(session_id)
304    sql = "SELECT * from graph_group WHERE group_id=%s"
305    res = session.query(sql, (group_id))
306    return res[0]   
307   
308@exportViaXMLRPC(SESSION_RO, AUTH_USER)
309def getGraphType(session_id, graph_id):
310    '''Simple function to return a graph type'''
311
312    session = getSessionE(session_id)
313    sql = "SELECT * from graph_type WHERE graph_id=%s"
314    res = session.query(sql, (graph_id))
315    return res[0]   
316   
317@exportViaXMLRPC(SESSION_RO, AUTH_USER)
318def getGraphVars(session_id, graph_id):
319    '''Returns all vars that are defined'''
320
321    session = getSessionE(session_id)
322    sql = "SELECT varname from graph_parts WHERE graph_id=%s AND (type='DEF' OR type='CDEF')"
323    res = session.query(sql, (graph_id))
324    return res   
325
326@exportViaXMLRPC(SESSION_RW, AUTH_ADMINISTRATOR)
327def updateGraphPartDetails(session_id, part_id, newDetails):
328    """Updates the details of the Graph part.
329
330    newDetails should be a dictionary containing only these class paramters
331    that have changed and need to be updated in the database.
332    """
333    session = getSessionE(session_id)
334       
335    # Build SQL
336    props = ["type", "colour", "text", "cf", \
337        "filename", "varname", "graph_order"]
338
339    (sql, values) = buildUpdateFromDict("graph_parts", props, newDetails, \
340            "part_id", part_id)
341       
342    if values == None:
343        # No changes made... ?
344        return 1
345   
346    # Run the query
347    session.execute(sql, values)
348
349    return 1 
350   
351@exportViaXMLRPC(SESSION_RW, AUTH_ADMINISTRATOR)
352def updateGraphTypeDetails(session_id, graph_id, newDetails):
353    """Updates the details of the Graph type.
354
355    newDetails should be a dictionary containing only these class paramters
356    that have changed and need to be updated in the database.
357    """
358    session = getSessionE(session_id)
359       
360    # Build SQL
361    props = ["title", "class_id", "virtical_label"]
362
363    (sql, values) = buildUpdateFromDict("graph_type", props, newDetails, \
364            "graph_id", graph_id)
365       
366    if values == None:
367        # No changes made... ?
368        return 1
369   
370    # Run the query
371    session.execute(sql, values)
372
373    return 1   
374   
375@exportViaXMLRPC(SESSION_RW, AUTH_USER)
376def addGraphGroup(session_id, clas):
377    """Adds a new graph group to the database"""
378    session = getSessionE(session_id)
379   
380    # Build query
381    props = ["group_name"]
382    (sql, values) = buildInsertFromDict("graph_group", props, clas)
383   
384    # Run query
385    session.execute(sql, values)
386
387    return 1
388   
389@exportViaXMLRPC(SESSION_RW, AUTH_USER)
390def addGraphType(session_id, clas):
391    """Adds a new graph type to the database"""
392    session = getSessionE(session_id)
393   
394    # Build query
395    props = ["title", "class_id", "group_id"]
396    (sql, values) = buildInsertFromDict("graph_type", props, clas)
397   
398    # Run query
399    session.execute(sql, values)
400
401    return 1   
402   
403@exportViaXMLRPC(SESSION_RW, AUTH_USER)
404def addGraphPart(session_id, clas):
405    """Adds a new graph part to the database"""
406    session = getSessionE(session_id)
407   
408    # Build query
409    props = ["type", "colour", "text", "varname", "cf", "filename", "graph_id", "graph_order"]
410    (sql, values) = buildInsertFromDict("graph_parts", props, clas)
411   
412    # Run query
413    session.execute(sql, values)
414
415    return 1       
416   
417@exportViaXMLRPC(SESSION_RW, AUTH_ADMINISTRATOR)
418def removeGraphPart(session_id, part_id):
419    """Removes a graph part from the database."""
420   
421    session = getSessionE(session_id)
422
423    # Delete
424    sql = "DELETE FROM graph_parts WHERE part_id=%s"
425    res = session.execute(sql, (part_id))
426    return 1
427   
428@exportViaXMLRPC(SESSION_RW, AUTH_ADMINISTRATOR)
429def removeGraphType(session_id, graph_id):
430    """Removes a graph type from the database."""
431   
432    session = getSessionE(session_id)
433
434    # Delete
435    sql = "DELETE FROM graph_type WHERE graph_id=%s"
436    res = session.execute(sql, (graph_id))
437    return 1
438   
439@exportViaXMLRPC(SESSION_RW, AUTH_ADMINISTRATOR)
440def removeGraphGroup(session_id, group_id):
441    """Removes a graph group from the database."""
442   
443    session = getSessionE(session_id)
444
445    # Delete
446    sql = "DELETE FROM graph_group WHERE group_id=%s"
447    res = session.execute(sql, (group_id))
448    return 1   
449
450def ccs_init():
451    global GRAPH_SERVER
452    GRAPH_SERVER = "%s:%s" % (config_get("graphs", "host", "localhost"), \
453        config_get("graphs", "port", "80"))
454
455@catchEvent("interfaceModified")
456@catchEvent("linkModified")
457@catchEvent("hostModified")
458def eventcatcher(eventName, host_id, session_id, **params):
459    session = getSessionE(session_id)
460    str = "Caught event %s for host %s, session = %s ("
461    if not params:       
462        str += "%s"
463        params = ""
464    first = True
465    for param in params:
466        if first:
467            str += "%s"
468        else:
469            str += ", %s"
470    str += ")"
471    print str % (eventName, host_id, session_id, params)
472    print session.getChangesetDescription()
473   
Note: See TracBrowser for help on using the repository browser.