1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-21 18:41:03 +00:00

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"
This commit is contained in:
Gerald Turner 2017-01-20 15:46:32 -08:00
parent b417a72020
commit 4fa3811c12
No known key found for this signature in database
GPG key ID: BCA01F0771A96FCD

View file

@ -14,7 +14,10 @@ Monit needs to be configured with the httpd port enabled, e.g.:
set httpd port 2812 set httpd port 2812
Optionally monit authentication can be configured, e.g.:
set httpd port 2812
allow munin:s3cr3t read-only
=head1 CONFIGURATION =head1 CONFIGURATION
@ -25,6 +28,12 @@ By default the monit instance at localhost is queried:
env.port 2812 env.port 2812
env.host localhost env.host localhost
Optionally monit authentication can be configured, e.g.:
[monit_parser]
env.username munin
env.password s3cr3t
=head1 AUTHOR =head1 AUTHOR
@ -44,6 +53,7 @@ import xml.dom.minidom
import os import os
import sys import sys
import urllib.request import urllib.request
import base64
MONIT_XML_URL = ("http://{host}:{port}/_status?format=xml" MONIT_XML_URL = ("http://{host}:{port}/_status?format=xml"
.format(host=os.getenv("host", "localhost"), .format(host=os.getenv("host", "localhost"),
@ -57,7 +67,15 @@ def sanitize(s):
def get_monit_status_xml(): def get_monit_status_xml():
try: 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: except urllib.error.URLError as exc:
conn = None conn = None
if conn is None: if conn is None: