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