diff --git a/src/example-decoded.json b/src/example-decoded.json new file mode 100644 index 0000000..8cbaa73 --- /dev/null +++ b/src/example-decoded.json @@ -0,0 +1,45 @@ +{ + "version": "2.0.5", + "SID": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "agent": "0", + "user": "hetrixtools", + "os": "Debian GNU/Linux 12 (bookworm)", + "kernel": "6.1.0-10-amd64", + "hostname": "fedi", + "time": "2023-09-27 14:21:01 EDT", + "reqreboot": "0", + "uptime": "4308590", + "cpumodel": "Intel(R) Xeon(R) CPU E5-2690 0 @ 2.90GHz", + "cpusockets": "4", + "cpucores": "4", + "cputhreads": "1", + "cpuspeed": "2900", + "cpu": "5.26316", + "wa": "3.10526", + "st": "0.105263", + "us": "1.57895", + "sy": "0.157895", + "load1": "0.209474", + "load5": "0.267895", + "load15": "0.228421", + "ramsize": "8088644", + "ram": "15.7722", + "ramswapsize": "2097148", + "ramswap": "42.8114", + "rambuff": "0.499311", + "ramcache": "81.95", + "disks": "/,ext4,105360293888,51153186816,49385013248;", + "inodes": "/,6540800,69604,6471196;", + "iops": "/,846711,116053;", + "raid": "", + "dh": "", + "nics": "enX0,10300,8627;", + "ipv4": "enX0,172.30.2.31;", + "ipv6": "enX0,;", + "conn": "", + "temp": "", + "serv": "", + "cust": "", + "rps1": "", + "rps2": "" +} diff --git a/src/example-encoded.json b/src/example-encoded.json new file mode 100644 index 0000000..79d77f6 --- /dev/null +++ b/src/example-encoded.json @@ -0,0 +1,45 @@ +{ + "version": "2.0.5", + "SID": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "agent": "0", + "user": "hetrixtools", + "os": "RGViaWFuIEdOVS9MaW51eCAxMiAoYm9va3dvcm0p", + "kernel": "Ni4xLjAtMTAtYW1kNjQK", + "hostname": "ZmVkaQo=", + "time": "MjAyMy0wOS0yNyAxNDoyMTowMSBFRFQK", + "reqreboot": "0", + "uptime": "4308590", + "cpumodel": "SW50ZWwoUikgWGVvbihSKSBDUFUgRTUtMjY5MCAwIEAgMi45MEdIeg==", + "cpusockets": "4", + "cpucores": "4", + "cputhreads": "1", + "cpuspeed": "2900", + "cpu": "5.26316", + "wa": "3.10526", + "st": "0.105263", + "us": "1.57895", + "sy": "0.157895", + "load1": "0.209474", + "load5": "0.267895", + "load15": "0.228421", + "ramsize": "8088644", + "ram": "15.7722", + "ramswapsize": "2097148", + "ramswap": "42.8114", + "rambuff": "0.499311", + "ramcache": "81.95", + "disks": "LyxleHQ0LDEwNTM2MDI5Mzg4OCw1MTE1MzE4NjgxNiw0OTM4NTAxMzI0ODsK", + "inodes": "Lyw2NTQwODAwLDY5NjA0LDY0NzExOTY7Cg==", + "iops": "Lyw4NDY3MTEsMTE2MDUzOw==", + "raid": "", + "dh": "", + "nics": "ZW5YMCwxMDMwMCw4NjI3Ow==", + "ipv4": "ZW5YMCwxNzIuMzAuMi4zMTs=", + "ipv6": "ZW5YMCw7", + "conn": "", + "temp": "", + "serv": "", + "cust": "", + "rps1": "", + "rps2": "" +} diff --git a/src/hetrixtools_freebsd/__init__.py b/src/hetrixtools_freebsd/__init__.py new file mode 100644 index 0000000..c808e82 --- /dev/null +++ b/src/hetrixtools_freebsd/__init__.py @@ -0,0 +1,12 @@ +import sys + +from importlib.metadata import PackageNotFoundError, version # pragma: no cover + +try: + # Change here if project is renamed and does not equal the package name + dist_name = "hetrixtools-freebsd" + __version__ = version(dist_name) +except PackageNotFoundError: # pragma: no cover + __version__ = "unknown" +finally: + del version, PackageNotFoundError diff --git a/src/hetrixtools_freebsd/cli.py b/src/hetrixtools_freebsd/cli.py new file mode 100644 index 0000000..2fda482 --- /dev/null +++ b/src/hetrixtools_freebsd/cli.py @@ -0,0 +1,166 @@ +""" +This is the main cli file. + +To test, run ``pip install -e .`` which will install the command ``hetrixtools`` inside your current environment. + +""" + +import argparse +import logging +import sys +import subprocess + +from hetrixtools_freebsd import __version__ + +__author__ = "Paco Hope" +__copyright__ = "Copyright 2023 (c) Paco Hope" +__license__ = "BSD-3-Clause" + +_logger = logging.getLogger(__name__) + +class Update: + def __init__(self): + self.version = "" + self.SID = "" + self.agent = "" + self.user = "" + self.os = "" + self.kernel = "" + self.hostname = "" + self.time = "" + self.reqreboot = "" + self.uptime = "" + self.cpumodel = "" + self.cpusockets = "" + self.cpucores = "" + self.cputhreads = "" + self.cpuspeed = "" + self.cpu = "" + self.wa = "" + self.st = "" + self.us = "" + self.sy = "" + self.load1 = "" + self.load5 = "" + self.load15 = "" + self.ramsize = "" + self.ram = "" + self.ramswapsize = "" + self.ramswap = "" + self.rambuff = "" + self.ramcache = "" + self.disks = "" + self.inodes = "" + self.iops = "" + self.raid = "" + self.dh = "" + self.nics = "" + self.ipv4 = "" + self.ipv6 = "" + self.conn = "" + self.temp = "" + self.serv = "" + self.cust = "" + self.rps1 = "" + self.rps2 = "" + + def callCommand(self, command=[]): + """Given a command and args in a list, run the command and return its + output as a UTF-8 string""" + + result = subprocess.run(command, stdout=subprocess.PIPE) + return result.stdout.decode(encoding="utf-8") + + def getCPUInfo(self): + """ Calls commands and sets + self.cpumodel self.cpusockets self.cpucores self.cputhreads self.cpuspeed + """ + ncpu = self.callCommand(['sysctl', 'hw.ncpu']).split("\n")[0].split(" ")[1] + cpuspeed = self.callCommand(['sysctl', 'hw.clockrate']).split("\n")[0].split(" ")[1] + print(f"ncpu = {ncpu}, cpuspeed = {cpuspeed}") + +# ---- Python API ---- + +def go(): + """Run the command.""" + update = Update() + update.getCPUInfo() + +def parse_args(args): + """Parse command line parameters + + Args: + args (List[str]): command line parameters as list of strings + (for example ``["--help"]``). + + Returns: + :obj:`argparse.Namespace`: command line parameters namespace + """ + parser = argparse.ArgumentParser(description="Just a Fibonacci demonstration") + parser.add_argument( + "--version", + action="version", + version=f"hetrixtools-freebsd {__version__}", + ) + parser.add_argument( + "-v", + "--verbose", + dest="loglevel", + help="set loglevel to INFO", + action="store_const", + const=logging.INFO, + ) + parser.add_argument( + "-vv", + "--very-verbose", + dest="loglevel", + help="set loglevel to DEBUG", + action="store_const", + const=logging.DEBUG, + ) + return parser.parse_args(args) + + +def setup_logging(loglevel): + """Setup basic logging + + Args: + loglevel (int): minimum loglevel for emitting messages + """ + logformat = "[%(asctime)s] %(levelname)s:%(name)s:%(message)s" + logging.basicConfig( + level=loglevel, stream=sys.stdout, format=logformat, datefmt="%Y-%m-%d %H:%M:%S" + ) + + +def main(args): + """Wrapper allowing :func:`go` to be called in a CLI fashion + """ + args = parse_args(args) + setup_logging(args.loglevel) + _logger.debug("Starting crazy calculations...") + go() + print("Done") + _logger.info("Script ends here") + + +def run(): + """Calls :func:`main` passing the CLI arguments extracted from :obj:`sys.argv` + + This function can be used as entry point to create console scripts with setuptools. + """ + main(sys.argv[1:]) + + +if __name__ == "__main__": + # ^ This is a guard statement that will prevent the following code from + # being executed in the case someone imports this file instead of + # executing it as a script. + # https://docs.python.org/3/library/__main__.html + + # After installing your project with pip, users can also run your Python + # modules as scripts via the ``-m`` flag, as defined in PEP 338:: + # + # python -m hetrixtools_freebsd.cli 42 + # + run()