From: Mladen Uzunov Date: Thu, 2 Jun 2022 12:29:42 +0000 (+0300) Subject: Optimizing vminfo X-Git-Tag: 1.6.1~1^2~8^2~1 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=0fb46755ca9f4fd0ba502f67d5bf72cf834683ed;p=pixelpark%2Fcreate-terraform.git Optimizing vminfo --- diff --git a/bin/vminfo.py b/bin/vminfo.py deleted file mode 100755 index 4730e00..0000000 --- a/bin/vminfo.py +++ /dev/null @@ -1,117 +0,0 @@ -#!/usr/bin/env python3 -import sys -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/setup.py b/setup.py index 6a208fd..1cf7587 100644 --- a/setup.py +++ b/setup.py @@ -136,7 +136,7 @@ read_requirements() __scripts__ = [ 'bin/create-terraform', 'bin/pre-terraform', - 'bin/vminfo.py' + 'bin/vminfo' ] # -----------------------------------