From a1a842779aa0dc5a4749961f1148d8d684ed7c12 Mon Sep 17 00:00:00 2001 From: Mark Westerterp <1002588+westerterp@users.noreply.github.com> Date: Sun, 28 May 2023 20:24:18 +0200 Subject: [PATCH 1/2] Allow basic authentication for apache-stats.py --- snmp/apache-stats.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/snmp/apache-stats.py b/snmp/apache-stats.py index d55ae8d52..0d22ad264 100755 --- a/snmp/apache-stats.py +++ b/snmp/apache-stats.py @@ -17,6 +17,7 @@ # # # +import base64 import os import time import urllib.request @@ -24,6 +25,12 @@ cachetime = 30 cachefile = "/var/cache/librenms/apache-snmp" +url = "http://localhost/server-status?auto" + +# Leave fields below empty if no authentication is needed +username = "" +password = "" + # Check for a cache file newer than cachetime seconds ago if os.path.isfile(cachefile) and (time.time() - os.stat(cachefile)[8]) < cachetime: @@ -33,11 +40,19 @@ f.close() else: # Grab the status URL (fresh data), needs package urllib3 - data = ( - urllib.request.urlopen("http://localhost/server-status?auto") - .read() - .decode("UTF-8") - ) + if username == "" and password == "": + data = ( + urllib.request.urlopen(url) + .read() + .decode("UTF-8") + ) + else: + passman = urllib.request.HTTPPasswordMgrWithDefaultRealm() + passman.add_password(None, url, username, password) + authhandler = urllib.request.HTTPBasicAuthHandler(passman) + opener = urllib.request.build_opener(authhandler) + urllib.request.install_opener(opener) + data = urllib.request.urlopen(url).read().decode("UTF-8") # Write file f = open(cachefile + ".TMP." + str(os.getpid()), "w") f.write(data) From 8a23a3b3d96007cced515529c54acc9c0c346484 Mon Sep 17 00:00:00 2001 From: Mark Westerterp <1002588+westerterp@users.noreply.github.com> Date: Sun, 28 May 2023 20:32:16 +0200 Subject: [PATCH 2/2] Style fixes --- snmp/apache-stats.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/snmp/apache-stats.py b/snmp/apache-stats.py index 0d22ad264..7b97cba74 100755 --- a/snmp/apache-stats.py +++ b/snmp/apache-stats.py @@ -41,11 +41,7 @@ else: # Grab the status URL (fresh data), needs package urllib3 if username == "" and password == "": - data = ( - urllib.request.urlopen(url) - .read() - .decode("UTF-8") - ) + data = urllib.request.urlopen(url).read().decode("UTF-8") else: passman = urllib.request.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, url, username, password)