-
Notifications
You must be signed in to change notification settings - Fork 22
/
zensendevent
executable file
·199 lines (168 loc) · 5.91 KB
/
zensendevent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python
##############################################################################
#
# Copyright (C) Zenoss, Inc. 2012, all rights reserved.
#
# This content is made available according to terms specified in
# License.zenoss under the directory where your Zenoss product is installed.
#
##############################################################################
'''
Send events on a command line via XML-RPC.
This command can be put on any machine with Python installed, and
does not need Zope or Zenoss.
Sending events from a XML file requires either:
* called from Zenoss box that has $ZENHOME/Products/ZenEvents/XmlEvents.py
* a copy of XmlEvents.py in the same directory as this file
'''
import os
import socket
from xmlrpclib import ServerProxy
from optparse import OptionParser
# Map of human severity strings to Zenoss severity values.
sevconvert = {
'crit': 5,
'critical': 5,
'err': 4,
'error': 4,
'warn': 3,
'warning': 3,
'info': 2,
'debug': 1,
'clear': 0
}
# Default configuration options. May be overridden by environment variable,
# command line option, or configuration file.
DEFAULT_SERVER = 'localhost'
XML_RPC_PORT = 8081
DEFAULT_AUTH = 'admin:zenoss'
# Default ZENHOME in case we're on a Zenoss server, but not the zenoss user.
ZENHOME = os.environ.get('ZENHOME', '/opt/zenoss')
def first_of(*choices):
'''
Return the first choice that isn't None.
'''
for x in choices:
if x is not None:
return x
def value_from_conf(conf, name):
'''
Return value of named property from given standard configuration.
'''
conf_filename = os.path.join(ZENHOME, 'etc', '%s.conf' % conf)
try:
conf_file = open(conf_filename, 'r')
for line in conf_file:
line = line.strip()
if line.startswith(name):
return line.split()[1]
conf_file.close()
except Exception:
pass
def auth_from_conf():
'''
Return zenhub credentials from configuration file.
'''
conf_filename = os.path.join(ZENHOME, 'etc', 'hubpasswd')
try:
lines = open(conf_filename).read()
except Exception:
return
entries = [ x.strip() for x in lines.splitlines() if x.strip() ]
if entries:
return entries[0]
device = socket.getfqdn()
if device.endswith('.'):
device = device[:-1]
parser = OptionParser(usage="usage: %prog [options] summary")
parser.add_option("-d", "--device", dest="device", default=device,
help="device from which this event is sent, default: %default")
parser.add_option("-i", "--ipAddress", dest="ipAddress", default="",
help="Ip from which this event was sent, default: %default")
parser.add_option("-y", "--eventkey", dest="eventkey", default="",
help="eventKey to be used, default: %default")
parser.add_option("-p", "--component", dest="component", default="",
help="component from which this event is sent, default: ''")
parser.add_option("-k", "--eventclasskey", dest="eventClassKey", default="",
help="eventClassKey for this event, default: ''")
parser.add_option("-s", "--severity", dest="severity", default="Warn",
help="severity of this event: Critical, Error, Warn, Info, Debug, Clear")
parser.add_option("-c", "--eventclass", dest="eventClass", default=None,
help="event class for this event, default: ''")
parser.add_option("--monitor", dest="monitor", default="localhost",
help="monitor from which this event came")
parser.add_option("--port", dest="port", default=None,
help="xmlrpc server port, default: %s" % XML_RPC_PORT)
parser.add_option("--server", dest="server", default=None,
help="xmlrpc server, default: %s" % DEFAULT_SERVER)
parser.add_option("--auth", dest="auth", default=None,
help="xmlrpc server auth, default: %s" % DEFAULT_AUTH)
parser.add_option("-o", "--other", dest="other", default=[],
action='append',
help="Specify other event_field=value arguments. Can be specified"
" more than once.")
parser.add_option('-f', "--file", dest="input_file", default="",
help="Import events from XML file.")
parser.add_option('-v', dest="show_event", default=False,
action='store_true',
help="Show the event data sent to Zenoss.")
opts, args = parser.parse_args()
opts.server = first_of(
opts.server,
os.environ.get('ZENOSS_HUB_SERVER', None),
value_from_conf('global', 'hubhost'),
DEFAULT_SERVER)
opts.port = first_of(
opts.port,
os.environ.get('ZENOSS_HUB_XMLRPC_PORT', None),
value_from_conf('zenhub', 'xmlrpcport'),
XML_RPC_PORT)
opts.auth = first_of(
opts.auth,
os.environ.get('ZENOSS_HUB_AUTH', None),
auth_from_conf(),
DEFAULT_AUTH)
url = "http://%s@%s:%s" % (opts.auth, opts.server, opts.port)
serv = ServerProxy(url)
if opts.input_file:
import sys
try:
from Products.ZenEvents.XmlEvents import sendXMLEvents
except ImportError:
try:
from XmlEvents import sendXMLEvents
except ImportError:
print "Unable to import needed XmlEvents.py libray -- exiting!"
sys.exit(1)
sent, total = sendXMLEvents(serv, opts.input_file)
print "Sent %s of %s events" % (sent, total)
sys.exit(0)
evt = {}
if opts.severity.lower() in sevconvert:
evt['severity'] = sevconvert[opts.severity.lower()]
else:
parser.error('Unknown severity')
evt['summary'] = " ".join(args)
if not evt['summary']:
parser.error('no summary supplied')
evt['device'] = opts.device
evt['component'] = opts.component
if opts.ipAddress:
evt['ipAddress'] = opts.ipAddress
if opts.eventkey:
evt['eventKey'] = opts.eventkey
if opts.eventClassKey:
evt['eventClassKey'] = opts.eventClassKey
if opts.eventClass:
evt['eventClass'] = opts.eventClass
evt['monitor'] = opts.monitor
for line in opts.other:
try:
field, value = line.split('=', 1)
evt[field] = value
except:
pass
if opts.show_event:
from pprint import pprint
pprint(evt)
serv.sendEvent(evt)