| 1 |
#/****************************************************************************** |
| 2 |
#* Portions 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 |
descriptors = list() |
| 35 |
Random_Max = 50 |
| 36 |
Constant_Value = 50 |
| 37 |
|
| 38 |
|
| 39 |
def Random_Numbers(name): |
| 40 |
'''Return a random number.''' |
| 41 |
return int(random.uniform(0, Random_Max)) |
| 42 |
|
| 43 |
|
| 44 |
def Constant_Number(name): |
| 45 |
'''Return a constant number.''' |
| 46 |
return int(Constant_Value) |
| 47 |
|
| 48 |
|
| 49 |
def metric_init(params): |
| 50 |
'''Initialize the random number generator and create the |
| 51 |
metric definition dictionary object for each metric.''' |
| 52 |
global descriptors |
| 53 |
global Random_Max |
| 54 |
global Constant_Value |
| 55 |
random.seed() |
| 56 |
|
| 57 |
print '[pyexample] Received the following parameters' |
| 58 |
print params |
| 59 |
|
| 60 |
if 'RandomMax' in params: |
| 61 |
Random_Max = int(params['RandomMax']) |
| 62 |
if 'ConstantValue' in params: |
| 63 |
Constant_Value = int(params['ConstantValue']) |
| 64 |
|
| 65 |
d1 = {'name': 'PyRandom_Numbers', |
| 66 |
'call_back': Random_Numbers, |
| 67 |
'time_max': 90, |
| 68 |
'value_type': 'uint', |
| 69 |
'units': 'N', |
| 70 |
'slope': 'both', |
| 71 |
'format': '%u', |
| 72 |
'description': 'Example module metric (random numbers)', |
| 73 |
'groups': 'example,random'} |
| 74 |
|
| 75 |
d2 = {'name': 'PyConstant_Number', |
| 76 |
'call_back': Constant_Number, |
| 77 |
'time_max': 90, |
| 78 |
'value_type': 'uint', |
| 79 |
'units': 'N', |
| 80 |
'slope': 'zero', |
| 81 |
'format': '%hu', |
| 82 |
'description': 'Example module metric (constant number)'} |
| 83 |
|
| 84 |
descriptors = [d1, d2] |
| 85 |
return descriptors |
| 86 |
|
| 87 |
|
| 88 |
def metric_cleanup(): |
| 89 |
'''Clean up the metric module.''' |
| 90 |
pass |
| 91 |
|
| 92 |
|
| 93 |
#This code is for debugging and unit testing |
| 94 |
if __name__ == '__main__': |
| 95 |
params = {'RandomMax': '500', |
| 96 |
'ConstantValue': '322'} |
| 97 |
metric_init(params) |
| 98 |
for d in descriptors: |
| 99 |
v = d['call_back'](d['name']) |
| 100 |
print 'value for %s is %u' % (d['name'], v) |