1414from urllib2 import HTTPError
1515from urllib2 import URLError
1616import uuid
17+ import ssl
1718from xml .sax .saxutils import escape
1819from xml .sax .saxutils import quoteattr
1920import argparse
2021import io
2122
22- PLUGIN_VERSION = "1.4 "
23+ PLUGIN_VERSION = "1.6 "
2324
2425
2526def persist_event (api_key , directory , payload ):
@@ -51,20 +52,20 @@ def persist_event(api_key, directory, payload):
5152 exit (1 )
5253
5354
54- def lock_and_flush (endpoint , directory , port ):
55+ def lock_and_flush (endpoint , directory , port , insecure ):
5556 """Lock event directory and call flush"""
5657 lock_filename = "%s/lockfile" % directory
5758
5859 lockfile = open (lock_filename , "w" )
5960
6061 try :
6162 fcntl .flock (lockfile .fileno (), fcntl .LOCK_EX )
62- flush (endpoint , directory , port )
63+ flush (endpoint , directory , port , insecure )
6364 finally :
6465 lockfile .close ()
6566
6667
67- def flush (endpoint , directory , port ):
68+ def flush (endpoint , directory , port , insecure ):
6869 """Send all events in event directory to iLert"""
6970 headers = {"Content-type" : "application/xml" , "Accept" : "application/xml" }
7071 url = "%s:%s/api/v1/events/nagios" % (endpoint , port )
@@ -84,8 +85,12 @@ def flush(endpoint, directory, port):
8485 syslog .syslog ('sending event %s to iLert...' % event )
8586
8687 try :
88+ ctx = ssl .create_default_context ()
89+ if insecure == True :
90+ ctx .check_hostname = False
91+ ctx .verify_mode = ssl .CERT_NONE
8792 req = urllib2 .Request (url , xml_doc , headers )
88- urllib2 .urlopen (req , timeout = 60 )
93+ urllib2 .urlopen (req , timeout = 60 , context = ctx )
8994 except HTTPError as e :
9095 if e .code == 429 :
9196 syslog .syslog (syslog .LOG_WARNING , "too many requests, will try later. Server response: %s" % e .read ())
@@ -120,20 +125,33 @@ def create_xml(apikey, payload):
120125 return xml_doc
121126
122127
128+ def str_to_bool (v ):
129+ if isinstance (v , bool ):
130+ return v
131+ if v .lower () in ('yes' , 'true' , 't' , 'y' , '1' ):
132+ return True
133+ elif v .lower () in ('no' , 'false' , 'f' , 'n' , '0' ):
134+ return False
135+ else :
136+ raise argparse .ArgumentTypeError ('Boolean value expected.' )
137+
123138def main ():
124139 parser = argparse .ArgumentParser (description = 'send events from Nagios (and its forks, such as Icinga) to iLert' )
125140 parser .add_argument ('-m' , '--mode' , choices = ['nagios' , 'save' , 'cron' , 'send' ], required = True ,
126141 help = 'Execution mode: "save" persists an event to disk and "send" submits all saved events '
127142 'to iLert. Note that after every "save" "send" will also be called.' )
128143 parser .add_argument ('-a' , '--apikey' , help = 'API key for the alert source in iLert' )
129- parser .add_argument ('-e' , '--endpoint' , default = 'https://ilertnow .com' ,
144+ parser .add_argument ('-e' , '--endpoint' , default = 'https://api.ilert .com' ,
130145 help = 'iLert API endpoint (default: %(default)s)' )
131- parser .add_argument ('-p' , '--port' , type = int , default = 443 , help = 'endpoint port (default: %(default)s)' )
146+ parser .add_argument ('-p' , '--port' , type = int , default = 443 , help = 'Endpoint port (default: %(default)s)' )
132147 parser .add_argument ('-d' , '--dir' , default = '/tmp/ilert_nagios' ,
133- help = 'event directory where events are stored (default: %(default)s)' )
148+ help = 'Event directory where events are stored (default: %(default)s)' )
134149 parser .add_argument ('--version' , action = 'version' , version = PLUGIN_VERSION )
150+ parser .add_argument ('-k' , '--insecure' , type = str_to_bool , nargs = '?' , const = True , default = False ,
151+ help = 'Disable SSL certification validation (e.g. to use self-signed certificates) (default: %(default)s)' )
152+ parser .add_argument ('-x' , '--proxy' , help = 'Use proxy for the outbound traffic' )
135153 parser .add_argument ('payload' , nargs = argparse .REMAINDER ,
136- help = 'event payload as key value pairs in the format key1=value1 key2=value2 ...' )
154+ help = 'Event payload as key value pairs in the format key1=value1 key2=value2 ...' )
137155 args = parser .parse_args ()
138156
139157 # populate payload data from environment variables
@@ -159,16 +177,20 @@ def main():
159177 if not os .path .exists (args .dir ):
160178 os .makedirs (args .dir )
161179
180+ if args .proxy is not None :
181+ os .environ ['http_proxy' ] = args .proxy
182+ os .environ ['https_proxy' ] = args .proxy
183+
162184 if args .mode == "nagios" or args .mode == "save" :
163185 if apikey is None :
164186 error_msg = "parameter apikey is required in save mode and must be provided either via command line or in " \
165187 "the pager field of the contact definition in Nagios/Icinga"
166188 syslog .syslog (syslog .LOG_ERR , error_msg )
167189 parser .error (error_msg )
168190 persist_event (apikey , args .dir , payload )
169- lock_and_flush (args .endpoint , args .dir , args .port )
191+ lock_and_flush (args .endpoint , args .dir , args .port , args . insecure )
170192 elif args .mode == "cron" or args .mode == "send" :
171- lock_and_flush (args .endpoint , args .dir , args .port )
193+ lock_and_flush (args .endpoint , args .dir , args .port , args . insecure )
172194
173195 exit (0 )
174196
0 commit comments