]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Adding missing file
authorMladen Uzunov <mladen.uzunov@digitaspixelpark.com>
Thu, 2 Jun 2022 12:32:48 +0000 (15:32 +0300)
committerMladen Uzunov <mladen.uzunov@digitaspixelpark.com>
Thu, 2 Jun 2022 12:32:48 +0000 (15:32 +0300)
bin/vminfo [new file with mode: 0644]

diff --git a/bin/vminfo b/bin/vminfo
new file mode 100644 (file)
index 0000000..32767fa
--- /dev/null
@@ -0,0 +1,134 @@
+#!/usr/bin/env python
+import sys
+import requests as req
+from tabulate import tabulate
+import re
+from os import _exit
+from termcolor import colored
+
+
+class FactGetter:
+    def __init__(self, baseUrl, tableFormat="fancy_grid"):
+        self.facts = req.get(baseUrl).json()
+        self.tableFormat = tableFormat
+        self.data = self.getData()
+
+    def getData(self):
+        data = {}
+        fields = [
+            "fqdn",
+            "processorcount",
+            "memorysize",
+            "disks",
+            "customer",
+            "os",
+            "partitions",
+            "tier",
+            "pp_ip_cidr",
+            "ipaddress",
+            "pp_purpose",
+            "r10k_environment",
+        ]
+        for fact in self.facts:
+            try:
+                if fact["name"] in fields:
+                    data[fact["name"]] = fact
+            except TypeError:
+                print("[Error] The host is either incorrect or it does not exists in PuppetDB!")
+                _exit(1)
+        for key in data.keys():
+            del data[key]["certname"]
+            del data[key]["environment"]
+            del data[key]["name"]
+
+        return data
+
+    def generalInfo(self):
+        data = [["Customer", self.data["customer"]["value"]],
+                ["FQDN", self.data["fqdn"]["value"]],
+                ["OS", self.data["os"]["value"]["distro"]["description"]],
+                ["Purpose", self.data["pp_purpose"]["value"]],
+                ["Tier", self.data["tier"]["value"]],
+                ["R10k Env", self.data["r10k_environment"]["value"]]]
+        self.simpleTabulate([[colored("General info:", "green")]], "orgtbl")
+        self.simpleTabulate(data)
+
+    def resourceInfo(self):
+        data = [["Memory", self.data["memorysize"]["value"]],
+                ["CPU cores", self.data["processorcount"]["value"]]]
+        self.simpleTabulate([[colored("Resources:", "green")]], "orgtbl")
+        self.simpleTabulate(data)
+
+    def diskInfo(self):
+        serialNum = re.compile(r'^\d{20}$')
+        self.simpleTabulate([[colored("Disks:", "green")]], "orgtbl")
+        self.complexTabulate(self.data["disks"], excludeValue=serialNum, excludeHeader="serial")
+
+    def partitionInfo(self):
+        partuuid = re.compile(r"^[\d\w]{8}\-[\d\w]{2}$")
+        self.simpleTabulate([[colored("Partitions:", "green")]], "orgtbl")
+        self.complexTabulate(self.data["partitions"], additionalSpot={"size": 5, "location": 2},
+                             excludeHeader="partuuid", excludeValue=partuuid)
+
+    def networkInfo(self):
+        data = [[interface, ','.join(ip)] for interface, ip in self.data["pp_ip_cidr"]["value"].items()]
+        data.insert(0, ["Default IP", self.data["ipaddress"]["value"]])
+        self.simpleTabulate([[colored("Network:", "green")]], "orgtbl")
+        self.simpleTabulate(data)
+
+    def simpleTabulate(self, data, customTheme=None):
+        if customTheme:
+            print(tabulate(data, tablefmt=customTheme))
+        else:
+            print(tabulate(data, tablefmt=self.tableFormat))
+
+    def complexTabulate(self, data, excludeValue=None, excludeHeader=None, additionalSpot=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 excludeHeader is not None:
+                headers = [x for x in items[1].keys() if x !=
+                           excludeHeader and not re.match(excludeHeader, str(x))]
+            else:
+                headers = [x for x in items[1].keys()]
+            if additionalSpot is not None:
+                if len(values) < additionalSpot["size"]:
+                    values.insert(additionalSpot["location"], "--")
+            finalData.append(values)
+        print(tabulate(finalData, headers=headers, 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 IndexError:
+    print(_usage)
+    _exit(1)
+
+facter = FactGetter(f"https://puppetdb01.pixelpark.com/pdb/query/v4/nodes/{hostname}/facts", "psql")
+facter.generalInfo()
+facter.resourceInfo()
+facter.diskInfo()
+facter.partitionInfo()
+facter.networkInfo()
+
+
+__author__ = 'Mladen Uzunov <mladen.uzunov@pixelpark.com>'
+__copyright__ = '(C) 2022 by Mladen Uzunov, Pixelpark GmbH, Berlin'
\ No newline at end of file