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

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

This commit contains the ccsd side of minipulating snmp instructions and snmp state. This is used as a link between ccsd and the polling system (rrdbot-script)

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