From 4fa3811c1225d15aa816e67653799521ef8e6d89 Mon Sep 17 00:00:00 2001 From: Gerald Turner Date: Fri, 20 Jan 2017 15:46:32 -0800 Subject: [PATCH] Add optional preemptive HTTP Basic Authentication to HTTP request in order to support monit configuration like "set httpd port 2812 allow munin:s3cr3t read-only" --- plugins/monit/monit_parser | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/plugins/monit/monit_parser b/plugins/monit/monit_parser index 67a2b767..31261a56 100755 --- a/plugins/monit/monit_parser +++ b/plugins/monit/monit_parser @@ -14,7 +14,10 @@ Monit needs to be configured with the httpd port enabled, e.g.: set httpd port 2812 +Optionally monit authentication can be configured, e.g.: + set httpd port 2812 + allow munin:s3cr3t read-only =head1 CONFIGURATION @@ -25,6 +28,12 @@ By default the monit instance at localhost is queried: env.port 2812 env.host localhost +Optionally monit authentication can be configured, e.g.: + + [monit_parser] + env.username munin + env.password s3cr3t + =head1 AUTHOR @@ -44,6 +53,7 @@ import xml.dom.minidom import os import sys import urllib.request +import base64 MONIT_XML_URL = ("http://{host}:{port}/_status?format=xml" .format(host=os.getenv("host", "localhost"), @@ -57,7 +67,15 @@ def sanitize(s): def get_monit_status_xml(): try: - conn = urllib.request.urlopen(MONIT_XML_URL) + req = urllib.request.Request(url=MONIT_XML_URL) + if os.getenv("password"): + auth_str = "%s:%s" % (os.getenv("username"), os.getenv("password")) + auth_bytes = bytes(auth_str, "utf-8") + auth_base64_bytes = base64.b64encode(auth_bytes) + auth_base64_str = str(auth_base64_bytes, "ascii") + auth_value = "Basic %s" % auth_base64_str + req.add_header("Authorization", auth_value) + conn = urllib.request.urlopen(req) except urllib.error.URLError as exc: conn = None if conn is None: