--- /dev/null
+#!/usr/bin/env python3
+import sys
+
+if sys.version_info[0] != 3:
+ print("This script is intended to use with Python3.", file=sys.stderr)
+ print("You are using Python: {0}.{1}.{2}-{3}-{4}.\n".format(
+ *sys.version_info), file=sys.stderr)
+ sys.exit(1)
+
+if sys.version_info[1] < 4:
+ print("A minimal Python version of 3.4 is necessary to execute this script.", file=sys.stderr)
+ print("You are using Python: {0}.{1}.{2}-{3}-{4}.\n".format(
+ *sys.version_info), file=sys.stderr)
+ sys.exit(1)
+
+import requests
+from tabulate import tabulate
+import re
+from os import _exit
+
+
+class FactGetter:
+ def __init__(self, baseUrl, tableFormat):
+ self.baseUrl = baseUrl
+ self.tableFormat = tableFormat
+
+ def complexTabulate(self, data, excludeValue=None, exludeHeader=None, addtionalSpot=None):
+ finalData = []
+ for items in data["value"].items():
+ if excludeValue is not None:
+ values = [x for x in items[1].values() if x !=
+ excludeValue and not re.match(excludeValue, str(x))]
+ else:
+ values = [x for x in items[1].values()]
+
+ if exludeHeader is not None:
+ headers = [x for x in items[1].keys() if x !=
+ exludeHeader and not re.match(exludeHeader, str(x))]
+ else:
+ headers = [x for x in items[1].keys()]
+ if addtionalSpot is not None:
+ if len(values) < addtionalSpot["size"]:
+ values.insert(addtionalSpot["location"], "-x-")
+ finalData.append(values)
+ print(tabulate(finalData, headers=headers, tablefmt=self.tableFormat))
+
+ def generalInfo(self):
+ result = []
+ URL = f"{self.baseUrl}/customer"
+ data = requests.get(URL).json()[0]
+ for k, v in data.items():
+ result.append([k, v])
+ print("\n", " GENERAL INFO ".center(60, "#"))
+ print(tabulate(result, tablefmt=self.tableFormat))
+
+ def cpuInfo(self):
+ URL = f"{self.baseUrl}/processorcount"
+ data = requests.get(URL).json()[0]
+ print("\n", " CPU ".center(60, "#"))
+ print(tabulate([["CPU cores", str(data['value'])]],
+ tablefmt=self.tableFormat))
+
+ def memInfo(self):
+ URL = f"{self.baseUrl}/memory"
+ data = requests.get(URL).json()[0]
+ print("\n", " MEMORY (RAM) ".center(60, "#"))
+ self.complexTabulate(data)
+
+ def diskInfo(self):
+ serialNum = re.compile(r'^[\d]{20}$')
+ URL = f"{self.baseUrl}/disks"
+ data = requests.get(URL).json()[0]
+ print("\n", " DISKS ".center(60, "#"))
+ self.complexTabulate(data, excludeValue=serialNum,
+ exludeHeader="serial")
+
+ def partitionInfo(self):
+ partuuid = re.compile(r"^[\d\w]{8}\-[\d\w]{2}$")
+ URL = f"{self.baseUrl}/partitions"
+ data = requests.get(URL).json()[0]
+ print("\n", " PARTITIONS ".center(60, "#"))
+ self.complexTabulate(data, addtionalSpot={
+ "size": 5, "location": 2}, exludeHeader="partuuid", excludeValue=partuuid)
+
+ def networkInfo(self):
+ data = []
+ URL = f"{self.baseUrl}/interfaces"
+ interfaces = requests.get(URL).json()[0]["value"].split(',')
+ print("\n", " NETWORK ".center(60, "#"))
+ for interface in interfaces:
+ URL = f"{self.baseUrl}/ipaddress_{interface}"
+ data.append([interface, requests.get(URL).json()[0]["value"]])
+ data.insert(0, ["default", requests.get(
+ f"{self.baseUrl}/ipaddress").json()[0]["value"]])
+ print(tabulate(data, headers=["Interface",
+ "IPaddr"], tablefmt=self.tableFormat))
+
+
+_usage = f"""
+ usage: {sys.argv[0]} [hostname]
+
+ Description:
+ The script will display the following information about the host:
+ - General information
+ - CPU information
+ - Memory information
+ - Disk information
+ - Partition information
+ - Network information
+ """
+
+
+try:
+ hostname = sys.argv[1]
+except:
+ print(_usage)
+ _exit(1)
+
+factGetter = FactGetter(
+ f"https://puppetdb01.pixelpark.com/pdb/query/v4/nodes/{hostname}/facts", tableFormat="psql")
+factGetter.generalInfo()
+factGetter.cpuInfo()
+factGetter.memInfo()
+factGetter.diskInfo()
+factGetter.partitionInfo()
+factGetter.networkInfo()
+
+
+__author__ = 'Mladen Uzunov <mladen.uzunov@pixelpark.com>'
+__copyright__ = '(C) 2022 by Mladen Uzunov, Pixelpark GmbH, Berlin'