Skip to content

Instantly share code, notes, and snippets.

@wneessen
Created February 22, 2021 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wneessen/8566d34da69dae8d5d286fe1d4004500 to your computer and use it in GitHub Desktop.
Save wneessen/8566d34da69dae8d5d286fe1d4004500 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import argparse
import json
import urllib.request
parser = argparse.ArgumentParser(
description="rspamd web interface statistic fetcher for InfluxDB usage")
parser.add_argument("--url", action="store",
help="URL to rspamd web interface installation")
parser.add_argument("--password", action="store",
help="Password for API authentication (same as for graphical login)")
parser.add_argument("--config", action="store",
help="Path to the configuration file for the application to use")
args = parser.parse_args()
# Perform some stupid basic validation on the arguments
if (args.url is None and args.password is None) and args.config is None:
parser.error(
"Please provide --url and --password arguments or the path to a configuration file using --config")
if (args.url is None and args.password is not None) or (args.url is not None and args.password is None):
parser.error("Please use --url with the --password argument and way round. You may provide the path to a "
"configuration file by only using the --config argument.")
if args.url is not None and args.password is not None and args.config is not None:
parser.error(
"Please provide whether --url and --password arguments *or* --config argument.")
if args.config is not None and (args.url is not None or args.password is not None):
parser.error(
"Please provide whether --url and --password arguments *or* --config argument.")
# Basic variable initialization
url = None
password = None
# Read variables from file if necessary
if args.config is not None:
data = None
try:
fh = open(args.config, "r")
data = json.load(fh)
except IOError as e:
parser.error(
"Could not read JSON config from file. Received IOError: " + str(e))
if data is not None and ("url" not in data or "password" not in data):
parser.error("Could not read URL and password from JSON configuration. Please see documentation for correct "
"configuration file formatting.")
elif data is None:
parser.error(
"Something went wrong during parsing of JSON configuration file. Please check your configuration!")
else:
url = data['url']
password = data['password']
else:
# Read variable from args
url = args.url
password = args.password
# Make sure we got the trailing slash at the URL
if str.endswith(url, "/"):
fetch_url = url + "stat?password=" + urllib.parse.quote_plus(password)
else:
fetch_url = url + "/stat?password=" + urllib.parse.quote_plus(password)
print(fetch_url)
try:
resp = urllib.request.urlopen(fetch_url)
except Exception as e:
print("Oops!", e, "occurred.")
print("Could not send GET request to given URL. Check url parameter!")
exit(1)
# Authorization failed
if resp.code == 403:
print("Authorization with rspamd web interface failed. Check password parameter!")
exit(1)
elif resp.code == 404:
print("HTTP server returned HTTP status code 404. Check url parameter!")
exit(1)
elif resp.code == 200:
print("All cool dude")
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment