From: Mladen Uzunov Date: Wed, 1 Jun 2022 09:06:56 +0000 (+0300) Subject: Adding vminfo script X-Git-Tag: 1.6.1~1^2~13^2~2 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=d7c6f308f043b51a5d2f2e37ce50fac818f2635d;p=pixelpark%2Fcreate-terraform.git Adding vminfo script --- diff --git a/bin/vminfo.py b/bin/vminfo.py new file mode 100755 index 0000000..2428921 --- /dev/null +++ b/bin/vminfo.py @@ -0,0 +1,130 @@ +#!/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 ' +__copyright__ = '(C) 2022 by Mladen Uzunov, Pixelpark GmbH, Berlin' diff --git a/requirements.txt b/requirements.txt index 134e639..07d2381 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,3 +12,4 @@ setuptools fb_logging fb_tools fb_pdnstools +tabulate diff --git a/setup.py b/setup.py index b05f627..1cf7587 100644 --- a/setup.py +++ b/setup.py @@ -134,7 +134,9 @@ read_requirements() # ----------------------------------- __scripts__ = [ - 'bin/create-terraform' + 'bin/create-terraform', + 'bin/pre-terraform', + 'bin/vminfo' ] # -----------------------------------