Last active
December 29, 2015 10:49
-
-
Save jrha/7659868 to your computer and use it in GitHub Desktop.
Quattor Report Stats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
"""Gather and submit some operating data about a Quattor server""" | |
import os | |
import platform | |
import urllib | |
import ConfigParser | |
import argparse | |
from datetime import date | |
CONFIGFILE = "/etc/quattor/dial_home.cfg" | |
parser = argparse.ArgumentParser(description='Gather and submit some operating data about a Quattor server.') | |
parser.add_argument("--submit", action="store_true", help="Really submit data back home, instead of just displaying the report.") | |
args = parser.parse_args() | |
if os.path.exists(CONFIGFILE): | |
config = ConfigParser.ConfigParser() | |
config.read([CONFIGFILE]) | |
profiledir = config.get("main", "profiledir") | |
homeurl = config.get("main", "homeurl") | |
# Report Structure | |
report = { | |
"platform" : {}, | |
"profiles" : {}, | |
"deploycount" : 0, | |
"server" : { | |
"type" : "scdb", | |
}, | |
} | |
# Identify platform | |
report["platform"]["architecture"] = platform.machine() | |
report["platform"]["system"] = platform.system() | |
report["platform"]["distro"] = list(platform.dist()) | |
# Count profile types | |
for profile in os.listdir(profiledir): | |
extension = profile.split(".")[-1] | |
if extension not in report["profiles"]: | |
report["profiles"][extension] = 0 | |
report["profiles"][extension] += 1 | |
# Check if this is an Aquilon server | |
if os.path.exists("/etc/aqd.conf"): | |
report["server"]["type"] = "aquilon" | |
version = urllib.urlopen("http://localhost:6901/").read() | |
if version: | |
report["server"]["version"] = version.splitlines()[0] | |
# Count SCDB deploys today | |
if report["server"]["type"] == "scdb": | |
f = open("/tmp/quattor-post-commit.log") | |
for l in f: | |
if l.startswith(date.today().isoformat()): | |
if "Switching to tag" in l: | |
report["deploycount"] += 1 | |
f.close() | |
report = str(report).replace("'", '"') #Quick and dirty JSON | |
if args.submit: | |
f = urllib.urlopen(homeurl, "report="+report) | |
print f.read() | |
else: | |
print report | |
else: | |
print("ERROR: %s not found") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
print "Content-Type: text/plain" | |
from os import environ | |
import cgi | |
import json | |
from datetime import datetime | |
form = cgi.FieldStorage() | |
try: | |
report = json.loads(form['report'].value) | |
except ValueError: | |
report = '' | |
hostname = '' | |
if 'REMOTE_HOST' in environ: | |
hostname = environ["REMOTE_HOST"] | |
timestamp = datetime.now().isoformat() | |
record = { | |
"hostname" : hostname, | |
"timestamp" : timestamp, | |
"report" : report, | |
} | |
if report and hostname and timestamp: | |
report_file = open('/tmp/quattor-report-stats.json', 'a') | |
json.dump(report, report_file) | |
report_file.write("\n") | |
report_file.close() | |
print "Thanks %s!" % (hostname) | |
else: | |
print "Invalid report, sorry %s." % (hostname) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment