]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Adding vminfo script
authorMladen Uzunov <mladen.uzunov@digitaspixelpark.com>
Wed, 1 Jun 2022 09:06:56 +0000 (12:06 +0300)
committerMladen Uzunov <mladen.uzunov@digitaspixelpark.com>
Wed, 1 Jun 2022 09:06:56 +0000 (12:06 +0300)
bin/vminfo.py [new file with mode: 0755]
requirements.txt
setup.py

diff --git a/bin/vminfo.py b/bin/vminfo.py
new file mode 100755 (executable)
index 0000000..2428921
--- /dev/null
@@ -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 <mladen.uzunov@pixelpark.com>'
+__copyright__ = '(C) 2022 by Mladen Uzunov, Pixelpark GmbH, Berlin'
index 134e63937e4d812dfa1ed992d50824de3b02278b..07d2381aba1d1837245619583b466449c7817995 100644 (file)
@@ -12,3 +12,4 @@ setuptools
 fb_logging
 fb_tools
 fb_pdnstools
+tabulate
index b05f62786b20c2b7b400cc76a9b0dde73fc39bc0..1cf7587ff5d056dda686331d352aeb87abb3d56a 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -134,7 +134,9 @@ read_requirements()
 
 # -----------------------------------
 __scripts__ = [
-    'bin/create-terraform'
+    'bin/create-terraform',
+    'bin/pre-terraform',
+    'bin/vminfo'
 ]
 
 # -----------------------------------