diff --git a/snmp/apache-stats.py b/snmp/apache-stats.py index d55ae8d52..7b97cba74 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,15 @@ 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)