2
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00

Allow also digest auth mechanism.

Currently only basic auth was supported. I added the ability of doing
digest auth (via 'env.auth_type').

Also, I fixed a bug where 'env.status_url' was ignored.
This commit is contained in:
René 'Necoro' Neumann 2013-10-01 17:23:32 +02:00
parent defc59e110
commit 1f04d10a6d

View file

@ -10,10 +10,11 @@
# #
# Configuration parameters: # Configuration parameters:
# env.status_url - url of lighty's server-status (optional, default is http://127.0.0.1/server-status) # env.status_url - url of lighty's server-status (optional, default is http://127.0.0.1/server-status)
# env.username - username to provide if status_url requires basic authentication (optional, default - no authentication) # env.username - username to provide if status_url requires authentication (optional, default - no authentication)
# env.password - password to provide if status_url requires basic authentication (optional, default - no authentication) # env.password - password to provide if status_url requires authentication (optional, default - no authentication)
# env.auth_type - the authentication mechanism to use -- either 'basic' (default) or 'digest'.
# #
# Note: If basic HTTP authentication is required you should specify both username and password. # Note: If HTTP authentication is required you should specify both username and password.
# #
# ## Installation # ## Installation
# Copy file to directory /usr/share/munin/plugins/ # Copy file to directory /usr/share/munin/plugins/
@ -23,7 +24,7 @@
#%# family=contrib #%# family=contrib
#%# capabilities=autoconf suggest #%# capabilities=autoconf suggest
import os, sys, urllib2, base64 import os, sys, urllib2
program = sys.argv[0] program = sys.argv[0]
graph_type = program[program.rfind("_")+1:] graph_type = program[program.rfind("_")+1:]
@ -79,19 +80,21 @@ elif len(sys.argv) == 2 and sys.argv[1] == "suggest":
for item in graph_types.keys(): for item in graph_types.keys():
print item print item
else: else:
if "status-url" not in os.environ: status_url = os.environ.get('status_url', 'http://127.0.0.1/server-status')
status_url = "http://127.0.0.1/server-status"
else:
status_url = os.environ["status_url"]
request = urllib2.Request("%s?auto" % status_url) request = urllib2.Request("%s?auto" % status_url)
if "username" in os.environ and "password" in os.environ: if "username" in os.environ and "password" in os.environ:
base64str = base64.encodestring("%s:%s" % (os.environ["username"], os.environ["password"])).replace("\r", "") mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
request.add_header("Authorization", "Basic %s" % base64str) mgr.add_password(None, status_url, os.environ["username"], os.environ["password"])
if os.environ.get("auth_type", "basic") == "digest":
auth = urllib2.HTTPDigestAuthHandler(mgr)
else:
auth = urllib2.HTTPBasicAuthHandler(mgr)
opener = urllib2.build_opener(auth)
urllib2.install_opener(opener)
info = urllib2.urlopen(request).read() info = urllib2.urlopen(request).read()
data = {} data = {}
lines = info.split("\n") for line in info.split("\n"):
for line in lines:
try: try:
(title, value) = line.split(": ") (title, value) = line.split(": ")
data[title] = value data[title] = value