Debian Codegraph @

gmond/python_modules/example/spfexample.py

raw
1 #/******************************************************************************
2 #* Copyright (C) 2007 Novell, Inc. All rights reserved.
3 #*
4 #* Redistribution and use in source and binary forms, with or without
5 #* modification, are permitted provided that the following conditions are met:
6 #*
7 #* - Redistributions of source code must retain the above copyright notice,
8 #* this list of conditions and the following disclaimer.
9 #*
10 #* - Redistributions in binary form must reproduce the above copyright notice,
11 #* this list of conditions and the following disclaimer in the documentation
12 #* and/or other materials provided with the distribution.
13 #*
14 #* - Neither the name of Novell, Inc. nor the names of its
15 #* contributors may be used to endorse or promote products derived from this
16 #* software without specific prior written permission.
17 #*
18 #* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
19 #* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 #* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 #* ARE DISCLAIMED. IN NO EVENT SHALL Novell, Inc. OR THE CONTRIBUTORS
22 #* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 #* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 #* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 #* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 #* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 #* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 #* POSSIBILITY OF SUCH DAMAGE.
29 #*
30 #* Author: Brad Nicholes (bnicholes novell.com)
31 #******************************************************************************/
32
33 import random
34 import time
35
36 descriptors = list()
37
38 # This is a list of bogus hosts that this module will report
39 # through the spoofing functionality. Normally a spoofing
40 # module would be expected to discover spoofed hosts through
41 # some sort of query mechanism or by reading some type of
42 # input file.
43 spoofHosts = [['spf1.myhost.org', '10.10.0.1'], ['spf2.myhost.org', '10.10.0.2'], ['spf3.myhost.org', '10.10.0.3']]
44
45 # Default hardcoded metric values
46 location = 'unspecified'
47 osname = 'SuSE Linux Ultimate'
48 started = int(time.time())
49 bootTime = {}
50
51
52 def spf_heartbeat(name):
53 '''Return heartbeat.'''
54 return started
55
56
57 def spf_location(name):
58 '''Return location.'''
59 return location
60
61
62 def spf_boottime(name):
63 '''Return boottime.'''
64 return started
65
66
67 def spf_osname(name):
68 '''Return OS Name.'''
69 return osname
70
71
72 def Random_Numbers(name):
73 '''Return a random number.'''
74 return int(random.uniform(0, 100))
75
76
77 def Init_Metric(spfHost, name, tmax, metrictype, units, slope, fmt, desc, metricAlias, ipAddr, handler):
78 '''Create a metric definition dictionary object for an imaginary box.'''
79 metric_name = name + ':' + spfHost
80 spoofHost = ipAddr + ':' + spfHost
81
82 # name - Should result in a unique metric indentifier. Attaching the spoofed host name
83 # allows the same metric name to be reused while the resulting name is unique.
84 # spoof_host - Must follow the same format as gmetric. The format should be the IP and
85 # host name separated by a colon.
86 # spoof_name - Must be the original name of the metric that this definition will be spoofing.
87 # In other words, if this metric name is spf_boottime, the spoof_name would be
88 # boottime if this metric is meant to spoof the original boottime metric.
89
90 d = {'name': metric_name,
91 'call_back': handler,
92 'time_max': tmax,
93 'value_type': metrictype,
94 'units': units,
95 'slope': slope,
96 'format': fmt,
97 'description': desc,
98 'spoof_host': spoofHost,
99 'spoof_name': metricAlias}
100
101 return d
102
103
104 def metric_init(params):
105 '''Initialize the random number generator and create the
106 metric definition dictionary object for each metric.'''
107 global descriptors
108 random.seed()
109
110 for metric_name, ipaddr in spoofHosts:
111 descriptors.append(Init_Metric(metric_name, 'spf_random_cpu_util', int(90),
112 'uint', '%', 'both', '%u', 'Spoofed CPU Utilization', 'cpu_util', ipaddr, Random_Numbers))
113 descriptors.append(Init_Metric(metric_name, 'spf_heartbeat', int(20),
114 'uint', '', '', '%u', 'Spoofed Heartbeat', 'heartbeat', ipaddr, spf_heartbeat))
115 descriptors.append(Init_Metric(metric_name, 'spf_location', int(1200),
116 'string', '(x,y,z)', '', '%s', 'Spoofed Location', 'location', ipaddr, spf_location))
117 descriptors.append(Init_Metric(metric_name, 'spf_boottime', int(1200),
118 'uint', 's', 'zero', '%u', 'Spoofed Boot Time', 'boottime', ipaddr, spf_boottime))
119 descriptors.append(Init_Metric(metric_name, 'spf_osname', int(1200),
120 'string', '', 'zero', '%s', 'Spoofed Operating System Name', 'os_name', ipaddr, spf_osname))
121
122 return descriptors
123
124
125 def metric_cleanup():
126 '''Clean up the metric module.'''
127 pass
128
129
130 #This code is for debugging and unit testing
131 if __name__ == '__main__':
132 params = {}
133 d = metric_init(params)
134 for d in descriptors:
135 v = d['call_back'](d['name'])
136 print 'value for %s is %s' % (d['name'], str(v))
137 print d