use configuration file for sensitive data default tip
authorMatthias Förste <foerste@schlittermann.de>
Tue, 27 Jan 2015 13:45:09 +0100
changeset 1 ec1f8cb59b7e
parent 0 9da56a166477
use configuration file for sensitive data
check_zarafa_login.py
zarafa_login.cfg
--- a/check_zarafa_login.py	Tue Jan 20 17:01:29 2015 +0100
+++ b/check_zarafa_login.py	Tue Jan 27 13:45:09 2015 +0100
@@ -1,19 +1,34 @@
 #!/usr/bin/python
+# vim: set fileencoding=utf-8 :
 
-import sys, traceback
+# argparse gibt es erst ab python 2.7
+import sys, getopt, traceback
+import ConfigParser
 import MAPI
 from MAPI.Util import *
 
-user = 'klaus'
-pw   = 'geheim!'
-url  = 'http://localhost:236/zarafa'
+configfile = '/etc/nagios-plugins/config/zarafa-login.cfg'
+exitcodes = { 'OK': 0, 'CRITICAL': 2 }
 
-exit = { 'OK': 0, 'CRITICAL': 2 }
-
+# backtrace abfangen damit wir den exit status setzen können
 try:
-	session = OpenECSession(user, pw, url)
+	opts, args = getopt.getopt(sys.argv[1:], "c:", ["config-file="])
+	for o, a in opts:
+		if o in ("-c", "--config-file"):
+			configfile = a
+		else:
+			assert False, "Unhandled option"
+	config = ConfigParser.RawConfigParser()
+	config.read(configfile)
+	p = [config.get('DEFAULT', k) for k in ['user', 'pw', 'url']]
+	session = OpenECSession(*p)
+	ec = 'OK'
+
 except:
+	ec = 'CRITICAL'
+
+print ec
+if ec != 'OK':
 	traceback.print_exc(file=sys.stdout)
-	sys.exit(exit['CRITICAL'])
 
-sys.exit(exit['OK']);
+sys.exit(exitcodes[ec]);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/zarafa_login.cfg	Tue Jan 27 13:45:09 2015 +0100
@@ -0,0 +1,4 @@
+[DEFAULT]
+user = klaus
+pw   = geheim!
+url  = http://localhost:236/zarafa