1
2
3
4
5
6
7
8
9
10
11
12 __doc__='''xtest.py
13
14 Sends test events to zenoss via xml-rpc.
15 Events can be specified on the command line or read from a file.
16 '''
17
18 import Globals
19 from Products.ZenUtils.CmdBase import CmdBase
20 import xmlrpclib
21 import time
22 import sys
23 from Products.ZenEvents.ZenEventClasses import Status_Perf
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
48
49
50 sampleEvent = dict(device='Sample device',
51 summary='Test event at %s' % time.time(),
52 eventClass=Status_Perf,
53 severity=4,
54 component='Sample component')
55 sampleClear = sampleEvent.copy()
56 sampleClear.update(dict(
57 severity=0,
58 summary='Clear event'))
59
60
64
65
67 """basic options setup sub classes can add more options here"""
68 CmdBase.buildOptions(self)
69 self.parser.add_option('--file',
70 dest="filepath",default=None,
71 help="file containing event details")
72 self.parser.add_option('--sample',
73 dest='dosample', default=False,
74 action='store_true',
75 help='Send sample event and clear event')
76 self.parser.add_option('-d', '--device',
77 dest="device",default='',
78 help="device to use for event")
79 self.parser.add_option('-s', '--summary',
80 dest="summary",default='',
81 help="summary to use for event")
82 self.parser.add_option('-c', '--component',
83 dest="component",default='',
84 help="component to use for event")
85 self.parser.add_option('-y', '--severity',
86 dest="severity",default=4,
87 type='int',
88 help="severity to use for event")
89 self.parser.add_option('--rpchost',
90 dest="rpchost",default='localhost',
91 help="host for xml-rpc request")
92 self.parser.add_option('--rpcport',
93 dest="rpcport",default='8081',
94 help="port for xml-rpc request")
95
96
98 ''' Not much actual parsing going on here, just importing
99 the given file.
100 '''
101 if not filepath:
102 filepath = self.options.filepath
103 args = {}
104 execfile(filepath, {}, args)
105 if 'events' in args:
106 events = args['events']
107 else:
108 events = []
109 sys.stderr.write('%s has no value for events\n' % filepath)
110 return events
111
112
114 ''' Returns xmlrpc proxy, creating on if the instance doesn't
115 already have one.
116 '''
117 if not self.proxy:
118 self.proxy = xmlrpclib.ServerProxy(
119 'http://%s:%s/' % (self.options.rpchost, self.options.rpcport),
120
121 encoding='iso-8859-1')
122 return self.proxy
123
124
131
132
134 ''' Sends the sample event and corresponding clear event.
135 Repeats this as many times as specified by repeat.
136 '''
137 for i in range(repeat):
138 self.sendEvents([self.sampleEvent, self.sampleClear])
139
140
148
149
156
157
159 "performance test"
160 xt = XTest()
161 xt.sendSampleEvents(repeat=100)
162
171
173 xt = XTest()
174 xt.sendSmapleWithDelayedClear()
175
176 if __name__ == '__main__':
177 xt = XTest()
178 if xt.options.dosample:
179 xt.sendSampleEvents()
180 elif xt.options.filepath:
181 xt.sendEventsFromFile()
182 elif xt.options.device:
183 event = dict(device=xt.options.device,
184 summary=xt.options.summary,
185 component=xt.options.component,
186 severity=xt.options.severity)
187 xt.sendEvents([event])
188 else:
189 xt.parser.print_help()
190