diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 16c92f8..0000000 --- a/.gitignore +++ /dev/null @@ -1,6 +0,0 @@ -TODO -sysmon.log -*/__pycache__/ -__pycache__/ -*.so -dump/ diff --git a/DOCS.md b/DOCS.md deleted file mode 100644 index ec3396c..0000000 --- a/DOCS.md +++ /dev/null @@ -1,189 +0,0 @@ -# Documentation -This document outlines how to integrate sysmon into other programs or scripts. It serves as a guide for developers looking to utilize sysmon's functionality within their projects. - -## Plugins -Sysmon is divided into five distinct plugins, each catering to different system monitoring aspects: - -1. [cpuinfo](#cpuinfo-plugin) [(source)](/plugins/cpuinfo.py) -2. [meminfo](#meminfo-plugin) [(source)](/plugins/meminfo.py) -3. [loadavg](#loadavg-plugin) [(source)](/plugins/loadavg.py) -4. [procpid](#procpid-plugin) [(source)](/plugins/procpid.py) -5. [netstats](#netstats-plugin) [(source)](/plugins/netstats.py) - -## Utils -The utils file offers configurations to customize the output data from plugins. For instance, in meminfo, you can disable swap stats, and in netstats, you can hide the local IP address. Below is an example: - -```python -from util import util - -# Disable showing local IP -util.SHOW_LOCAL_IP = False - -# Override interface -util.INTERFACE = "wlp4s0" - -from plugins import netstats - -netstats_class = netstats.Netstats() -print(netstats_class.get_data()) -netstats_class.close_files() # Always close files!! -``` - -Output: - -``` -{'interface': 'wlp4s0', - 'local_ip': 'Hidden', - 'statistics': {'received': 721839959, - 'speeds': {'received': 721839959, 'transferred': 23420738}, - 'transferred': 23420738}} -``` - -## Usage -The usage pattern for all plugins remains consistent. Follow these steps to integrate a plugin into your script: - -1. Import the desired plugin: -```python -from plugins import cpuinfo -``` - -2. Create an object of the plugin: -```python -cpuinfo_class = cpuinfo.Cpuinfo() -``` - -3. Execute the `get_data` function to retrieve system information: -```python -cpuinfo_class.get_data() -``` - -4. Ensure to close any open files if the plugin maintains them: -```python -cpuinfo_class.close_files() -``` - -Format the retrieved data as required; the output is in JSON format. You can also check the [formatters](/formatters) directory, to get an idea of how to use these plugins. - -## cpuinfo Plugin -The `cpuinfo` plugin provides detailed information about the CPU, including: - -- `architecture`: CPU architecture -- `cache`: - 1. `size`: size (in bytes) - 2. `level`: level -- `cores`: - 1. `physical`: physical cores count - 2. `logical`: logical cores count -- `frequency`: CPU frequency -- `model`: CPU model -- `uses_smt`: Whether CPU uses simultaneous multithreading (SMT) -- `temperature`: CPU temperature in Celsius -- `usage`: CPU usage - -### Output -``` -{'architecture': 'x86_64', - 'cache': {'level': 'L2', 'size': 3145728}, - 'cores': {'logical': 2, 'physical': 2}, - 'frequency': 1064.03, - 'model': 'Intel Core2 Duo P7450', - 'temperature': 45.0, - 'usage': 94.2} -``` - -## meminfo Plugin -The `meminfo` plugin offers details about physical (RAM) and virtual (ZRAM/SWAP) memory usage. It includes: - -Under the `physical` and `virtual` sections: -- `percentage`: Memory usage percentage -- `values`: Raw memory values in bytes - -### Output -``` -{'physical': {'percentage': {'actual_used': 69.7, - 'available': 23.8, - 'free': 3.7, - 'used': 76.2}, - 'values': {'actual_used': 2667823104, - 'available': 911327232, - 'cached': 1017966592, - 'free': 141074432, - 'total': 3826864128, - 'used': 2915536896}}, - 'virtual': {'percentage': {'free': 27.1, 'used': 72.9}, - 'values': {'free': 874192896, - 'cached': 39645184, - 'total': 3221221376, - 'used': 2347028480}}} -``` - -Note: there are two "used" fields, due to different calculation. "actual_used" is close to how htop calculates it, and used is: MemTotal - MemAvailable - -## loadavg Plugin -The `loadavg` plugin provides load times, uptime, and boot time information. It includes: - -- `entities`: Active and total running processes -- `load_times`: System load for 1, 5, and 15 minutes -- `uptime`: - 1. `seconds`: uptime, in seconds - 2. `timestamp`: unix timestamp. use strftime to convert it to human readable format - -### Output -``` -{'entities': {'active': 4, 'total': 557}, - 'load_times': {'1': 1.26, '15': 1.0, '5': 1.11}, - 'uptime': {'seconds': 166860, 'timestamp': 1719750035.6097515}} -``` - -## procpid Plugin -The `procpid` plugin displays information about the most VmRSS-consuming processes. It includes: - -- `name`: process name -- `pid`: process id (int) -- `state`: process state -- `vmrss`: process vmrss usage (int) - -### Output -``` -{'processes': [{'name': 'firefox', - 'pid': 3759, - 'state': 'Sleeping', - 'vmrss': 462802944}, - {'name': 'e.android.music', - 'pid': 5864, - 'state': 'Sleeping', - 'vmrss': 422006784}, - {'name': 'system_server', - 'pid': 4895, - 'state': 'Sleeping', - 'vmrss': 306163712}, - {'name': 'ndroid.systemui', - 'pid': 5062, - 'state': 'Sleeping', - 'vmrss': 268738560}, - {'name': 'Isolated', - 'pid': 11112, - 'state': 'Sleeping', - 'vmrss': 268210176}, - {'name': 'AndroidUI', - 'pid': 6421, - 'state': 'Sleeping', - 'vmrss': 249049088}]} -``` - -## netstats Plugin -The `netstats` plugin shows network status such as the network adapter name and data transfer statistics. It includes: - -- `interface`: Network interface name -- `local_ip`: Local IP address -- `statistics`: Data transfer statistics since boot time - - `speeds`: Network speed in bytes per second - -### Output -``` -{'interface': 'waydroid0', - 'local_ip': '192.168.240.1', - 'statistics': {'received': 9024248, - 'speeds': {'received': 9024248, 'transferred': 556928103}, - 'transferred': 556928103}} -``` diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 15a590b..0000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2022 devlocalhost - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/README.md b/README.md deleted file mode 100644 index 6eedbf0..0000000 --- a/README.md +++ /dev/null @@ -1,66 +0,0 @@ -# Sysmon: A Ready-to-Use System Monitor - -[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) -[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff) - -## Preview -For a preview, see the [PREVIEW.md file](screens/PREVIEW.md). - -## What is Sysmon? -Sysmon is a system monitor that is ready to use and easy to understand. It utilizes Linux's /proc pseudo filesystem to read information and curses to display it. All you need to do is clone this repository and run the sysmon file. - -## What Can Sysmon Do? -- Display CPU information including model, temperature (1), frequency (2), cores (1), threads count, and cache memory size (1). -- Show memory information such as total, available, used (3), cached RAM, and swap information. -- Provide system load, entities, and uptime. -- Present the processes (6 by default) consuming the most VmRSS, including their state and name. -- Present network information such as device, transferred and received data, and speed. - -1. Information reported might not be correct. - -2. Frequency information may depend on the kernel and might not match the maximum frequency reported on the manufacturer's website. - -3. There are two "used" columns because the calculations differ. "Actual used" represents a report similar to htop, while "used" equals MemTotal - MemAvailable. - -## Using Sysmon -Clone the repository: -```sh -git clone https://github.com/devlocalhost/sysmon -``` - -Navigate to the Sysmon directory: -```sh -cd sysmon -``` - -If Sysmon isn't executable, make it so: -```sh -chmod +x sysmon -``` - -Finally, run Sysmon: -```sh -./sysmon -``` - -To use Sysmon anywhere, you can create a symlink: -```sh -echo $(pwd)/sysmon | sudo tee /usr/local/bin/sysmon && sudo chmod +x /usr/local/bin/sysmon -``` -This command will echo the current working directory + `/sysmon`, create a new file named `sysmon` in `/usr/local/bin` with executable permissions. If `/usr/local/bin` is in your PATH, then typing `sysmon` anywhere will execute Sysmon. - -## Help and Usage -Run `sysmon --help` to display the help menu. - -## Usage in Other Scripts -Please refer to the [documentation](DOCS.md). - -## Reporting Bugs, Suggestions, or Corrections -Please open an issue, including the traceback and log file (`sysmon -d`). A file named "sysmon.log" will be created in the current directory. - -If you want to suggest a new feature or report incorrect data (e.g., CPU temperature or RAM usage), feel free to open an issue. - -## Credits -Many thanks to [skyblueborb](https://github.com/skyblueborb) for helping to test, fix, and improve the CPU temperature feature. - -Also, many thanks to [ari](https://ari-web.xyz/gh) for assistance with padding and text formatting. diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..d33ef70 --- /dev/null +++ b/config.toml @@ -0,0 +1,29 @@ +# order matters +plugins_enabled = [ + "cpuinfo", + "meminfo", + "loadavg", + "netstats", + "procs", +] + +# show swap statistics in meminfo? +[plugins_config] + [plugins_config.meminfo] + show_swap = true + + # how many processes to show in procs + [plugins_config.procs] + processes_count = 6 + + [plugins_config.netstats] + show_local_ip = true + # useful when sysmon picks the wrong one, or you want to use a custom interface + custom_interface = "" + +[misc] +# binary: 1 mebibyte = 1024 kibibyes. if set to false, +# will use metric instead (1 megabyte = 1000 kilobytes) +use_binary_conversion = true +debugging = true +refresh_sec = 1 diff --git a/__init__.py b/formatters/__init__.py similarity index 100% rename from __init__.py rename to formatters/__init__.py diff --git a/formatters/cpuinfo.py b/formatters/cpuinfo.py index 3f6279a..bc6a448 100644 --- a/formatters/cpuinfo.py +++ b/formatters/cpuinfo.py @@ -1,71 +1,24 @@ -#!/usr/bin/env python3 +from plugins.cpuinfo import Plugin +from misc.utils import to_bytes, convert_bytes -""" -formatter for cpuinfo plugin -""" -from util.util import SHOW_TEMPERATURE, convert_bytes -from plugins import cpuinfo +cpuinfo_plugin = Plugin() -cpuinfo_class = cpuinfo.Cpuinfo() +def get_lines(): + data = cpuinfo_plugin.get_data() + lines = [] -def main(): - """ - returns the data, but formatted. - """ - - data = cpuinfo_class.get_data() - - cpu_temperature = data["temperature"] - - if SHOW_TEMPERATURE and cpu_temperature: - cpu_temperature = str(cpu_temperature) - cpu_temperature += " °C" - - arch_model_temp_line = ( - f"{cpu_temperature:>8} | CPU: {data['architecture']} {data['model']}" - ) - - else: - arch_model_temp_line = f"| CPU: {data['architecture']} {data['model']}" - - cores_count_text = ( - f"Cores: {data['cores']['physical']}C/{data['cores']['logical']}T" - if data["cores"]["physical"] != 0 - else f"Cores: {data['cores']['logical']}" + lines.append(f" --- /proc/cpuinfo {'-' * 47}") + lines.append( + f" Usage: {data.utilization:>3}% {data.temperature:>5} °C | {data.architecture} {data.model} @ {round(data.average_frequency / 1000):>4} MHz" + ) + lines.append( + f" Cores: {data.physical_cores}C/{data.logical_cores}T | Freq range: {round(data.frequency_min / 1000)}..{round(data.frequency_max / 1000)} MHz | Cache: {data.cache_level} {convert_bytes(to_bytes(data.cache_size))}" ) - # this fuckery can only happen on arm platforms since for some - # fucking reason some retard thought it would be a good idea for - # the cpuinfo file to be different on x86 and arm platforms - # fuck you. - - cache_line = " | Cache: No data\n" - - if data["cache"]["level"]: - cache_line = f" | Cache: {data['cache']['level']}" - if data["cache"]["size"]: - cache_line += f" {convert_bytes(data['cache']['size'])}\n" - - output_text = [ - f" --- /proc/cpuinfo {'-' * 47}\n", - f" Usage: {data['usage']:>5}% {arch_model_temp_line}" + "\n", - f" {cores_count_text} | Frequency: {data['frequency']:>7} MHz", - ] - - output_text[2] += cache_line - # output_text[2] = output_text[2] + cache_line - - return "".join(output_text) - - -def end(): - """ - clean up when the program exits - """ - cpuinfo_class.close_files() + return lines -if __name__ == "__main__": - main() +def close_files(): + cpuinfo_plugin.close_files() diff --git a/formatters/loadavg.py b/formatters/loadavg.py index bca1c35..c2b387c 100644 --- a/formatters/loadavg.py +++ b/formatters/loadavg.py @@ -1,70 +1,61 @@ -#!/usr/bin/env python3 - -""" -formatter for loadavg plugin -""" - from datetime import datetime -from plugins import loadavg +from plugins.loadavg import Plugin -loadavg_class = loadavg.Loadavg() +loadavg_plugin = Plugin() -def main(): - """ - returns the data, but formatted. - """ +# crazy name, i know. +def _dynamicallyAddTheLetterSForTheFormatSecondsFunction(value, unit): + return f"{value} {unit}" if value == 1 else f"{value} {unit}s" - data = loadavg_class.get_data() - intervals = (("week", 604800), ("day", 86400), ("hour", 3600), ("minute", 60)) - result = [] +def _format_seconds(seconds): + wk, remainder = divmod(seconds, 7 * 24 * 3600) + ds, remainder = divmod(remainder, 24 * 3600) + hr, remainder = divmod(remainder, 3600) + mn, _ = divmod(remainder, 60) - seconds = data["uptime"]["seconds"] - original_seconds = seconds + fmted = [] # formatted output - if seconds < 60: - result.append(f"{seconds} seconds") + if wk: + fmted.append(_dynamicallyAddTheLetterSForTheFormatSecondsFunction(wk, "week")) - else: - for time_type, count in intervals: - value = seconds // count + if ds: + fmted.append(_dynamicallyAddTheLetterSForTheFormatSecondsFunction(ds, "day")) - if value: - seconds -= value * count - result.append(f"{value} {time_type if value == 1 else time_type + 's'}") + if hr: + fmted.append(_dynamicallyAddTheLetterSForTheFormatSecondsFunction(hr, "hour")) - if len(result) > 1: - result[-1] = "and " + result[-1] + if mn: + fmted.append(_dynamicallyAddTheLetterSForTheFormatSecondsFunction(mn, "minute")) - uptime_formatted = ", ".join(result) - uptime_since = datetime.fromtimestamp(data["uptime"]["timestamp"]).strftime( - "%A, %B %d %Y, %I:%M:%S %p" - ) + # if sec or not fmted: + # fmted.append(_dynamicallyAddTheLetterSForTheFormatSecondsFunction(sec, "second")) - output_list = [ - f" --- /proc/loadavg {'-' * 47}\n", - f" Load: {data['load_times']['1']}, {data['load_times']['5']}, {data['load_times']['15']}", - f"\n Uptime: {uptime_formatted}\n Booted: {uptime_since}\n", - ] + if seconds < 60: + return _dynamicallyAddTheLetterSForTheFormatSecondsFunction(seconds, "second") + + else: + return ", ".join(fmted) - output_list[1] = output_list[1] = ( - output_list[1] - + " " * max(0, 35 - len(output_list[1])) - + f"| Procs: {data['entities']['active']} active, {data['entities']['total']} total" - ) - return "".join(output_list) +def get_lines(): + data = loadavg_plugin.get_data() + lines = [f" --- /proc/loadavg {'-' * 47}"] -def end(): - """ - clean up when the program exits - """ + lines.append(f" Load: {data.load_times.OneMin} {data.load_times.FiveMin} {data.load_times.FifteenMin}") + lines.append(f" Uptime: {_format_seconds(data.uptime.Seconds)}") + lines.append(f" Booted: {datetime.fromtimestamp(data.uptime.Timestamp).strftime('%A, %B %d %Y, %I:%M:%S %p')}") + + lines[1] = ( + lines[1] + " " * max(0, 35 - len(lines[1])) + + f"| Procs: {data.entities.Active} active, {data.entities.Total} total" + ) - loadavg_class.close_files() + return lines -if __name__ == "__main__": - main() +def close_files(): + loadavg_plugin.close_files() diff --git a/formatters/meminfo.py b/formatters/meminfo.py index fed6889..86cf68c 100644 --- a/formatters/meminfo.py +++ b/formatters/meminfo.py @@ -1,154 +1,32 @@ -#!/usr/bin/env python3 +from plugins.meminfo import Plugin +from misc.utils import to_bytes, convert_bytes -""" -formatter for meminfo plugin -""" +meminfo_plugin = Plugin() -from util.util import convert_bytes, SHOW_SWAP -from plugins import meminfo -meminfo_class = meminfo.Meminfo() +def get_lines(): + data = meminfo_plugin.get_data() - -def main(): - """ - returns the data, but formatted. - """ - - data = meminfo_class.get_data() - - if data["virtual"]["values"]["total"] != 0 and SHOW_SWAP is not False: - combined_total_memory = ( - data["physical"]["values"]["total"] + data["virtual"]["values"]["total"] - ) - combined_total_actual_used = ( - data["physical"]["values"]["actual_used"] - + data["virtual"]["values"]["used"] - ) - combined_total_used = ( - data["physical"]["values"]["used"] + data["virtual"]["values"]["used"] - ) - combined_total_available = ( - data["physical"]["values"]["available"] + data["virtual"]["values"]["free"] - ) - - combined_used_percent = round( - ( - data["physical"]["percentage"]["used"] - + data["virtual"]["percentage"]["used"] - ) - / 2, - 1, - ) - combined_available_percent = round( - ( - data["physical"]["percentage"]["available"] - + data["virtual"]["percentage"]["free"] - ) - / 2, - 1, - ) - - output_list_phys_virt = [ - f" --- /proc/meminfo {'-' * 47}\n", - f" RAM: {' ' * 25}Swap:\n", - f" Total: {convert_bytes(data['physical']['values']['total'])}", - f" Used: {convert_bytes(data['physical']['values']['used'])} ({data['physical']['percentage']['used']}%)", - f" Ac. Used: {convert_bytes(data['physical']['values']['actual_used'])} ({data['physical']['percentage']['actual_used']}%)\n", - f" Available: {convert_bytes(data['physical']['values']['available'])} ({data['physical']['percentage']['available']}%)\n", - f" Free: {convert_bytes(data['physical']['values']['free'])} ({data['physical']['percentage']['free']}%)", - f" Cached: {convert_bytes(data['physical']['values']['cached'])} ({data['physical']['percentage']['cached']}%)", - f" - Combined: {'- ' * 26}\n", - f" Total: {convert_bytes(combined_total_memory)}", - f" Available: {convert_bytes(combined_total_available)} ({combined_available_percent}%)", - ] - # 0. plugin name - # 1. table row - # 2. Total - # 3. Used - # 4. Actual Used - # 5. Available - # 6. Free - # 7. Cached - - # line = line + spaces * 40 [len line] + other_line - # this is what basically happens over the next lines. i choose to do this - # instead of using a library because i do not want to depend on external libraries - # and i "cba" to write a different solution. for now, this is the best solution. - output_list_phys_virt[2] = ( - output_list_phys_virt[2] - + " " * max(0, 40 - len(output_list_phys_virt[2])) - + f"Total: {convert_bytes(data['virtual']['values']['total'])}\n" - ) - output_list_phys_virt[3] = ( - output_list_phys_virt[3] - + " " * max(0, 41 - len(output_list_phys_virt[3])) - + f"Used: {convert_bytes(data['virtual']['values']['used'])} ({data['virtual']['percentage']['used']}%)\n" - ) - output_list_phys_virt[6] = ( - output_list_phys_virt[6] - + " " * max(0, 36 - len(output_list_phys_virt[6])) - + f" Free: {convert_bytes(data['virtual']['values']['free'])} ({data['virtual']['percentage']['free']}%)\n" - ) - output_list_phys_virt[7] = ( - output_list_phys_virt[7] - + " " * max(0, 39 - len(output_list_phys_virt[7])) - + f"Cached: {convert_bytes(data['virtual']['values']['cached'])}\n" - ) - output_list_phys_virt[9] = ( - output_list_phys_virt[9] - + " " * max(0, 41 - len(output_list_phys_virt[9])) - + f"Used: {convert_bytes(combined_total_used)} ({combined_used_percent}%)\n" - ) - output_list_phys_virt[10] = ( - output_list_phys_virt[10] - + " " * max(0, 37 - len(output_list_phys_virt[10])) - + f"Ac. Used: {convert_bytes(combined_total_actual_used)}\n" - ) - - return "".join(output_list_phys_virt) - - output_list_phys = [ - f" --- /proc/meminfo {'-' * 47}\n", - f" RAM: {' ' * 25}\n", - f" Total: {convert_bytes(data['physical']['values']['total'])}", - f" Used: {convert_bytes(data['physical']['values']['used'])} ({data['physical']['percentage']['used']}%)", - f" Available: {convert_bytes(data['physical']['values']['available'])} ({data['physical']['percentage']['available']}%)", + left = [ + " RAM:", + f" Total: {convert_bytes(data.physical_values.MemTotal)}", + f" Used: {convert_bytes(data.physical_values.Used)} ({data.physical_percentages.Used}%)", + f" Available: {convert_bytes(data.physical_values.MemAvailable)} ({data.physical_percentages.Available}%)", + ] + right = [ + "", + f" Free: {convert_bytes(data.physical_values.MemFree)} ({data.physical_percentages.Free}%)", + f"Ac. Used: {convert_bytes(data.physical_values.ActualUsed)} ({data.physical_percentages.ActualUsed}%)", + f" Cached: {convert_bytes(data.physical_values.Cached)} ({data.physical_percentages.Cached}%)", ] - # 0. plugin name - # 1. table row - # 2. Total - # 3. Used - # 4. Free - - # avail, actual, cached - - output_list_phys[2] = ( - output_list_phys[2] - + " " * max(0, 41 - len(output_list_phys[2])) - + f"Free: {convert_bytes(data['physical']['values']['free'])} ({data['physical']['percentage']['free']}%)\n" - ) - output_list_phys[3] = ( - output_list_phys[3] - + " " * max(0, 37 - len(output_list_phys[3])) - + f"Ac. Used: {convert_bytes(data['physical']['values']['actual_used'])} ({data['physical']['percentage']['actual_used']}%)\n" - ) - output_list_phys[4] = ( - output_list_phys[4] - + " " * max(0, 39 - len(output_list_phys[4])) - + f"Cached: {convert_bytes(data['physical']['values']['cached'])} ({data['physical']['percentage']['cached']}%)\n" - ) - - return "".join(output_list_phys) + lines = [f" --- /proc/meminfo {'-' * 47}"] -def end(): - """ - clean up when the program exits - """ + for l, r in zip(left, right): + lines.append(f"{l:<38}{r}") - meminfo_class.close_files() + return lines -if __name__ == "__main__": - main() +def close_files(): + meminfo_plugin.close_files() diff --git a/formatters/netstats.py b/formatters/netstats.py index 4798c62..d228e33 100644 --- a/formatters/netstats.py +++ b/formatters/netstats.py @@ -1,66 +1,19 @@ -#!/usr/bin/env python3 +from plugins.netstats import Plugin +from misc.utils import to_bytes, convert_bytes -""" -formatter for netstats plugin -""" +netstats_plugin = Plugin() -from util.util import convert_bytes -from plugins import netstats -netstats_class = netstats.Netstats() +def get_lines(): + data = netstats_plugin.get_data() + lines = [f" --- /sys/class/net {'-' * 46}"] + + lines.append(f" Local IP: {data.ip:<15} | Interface: {data.name}") + lines.append(f" Total: Down {convert_bytes(data.transfer_statistics.total_received):<13} | Up {convert_bytes(data.transfer_statistics.total_transferred)}") + lines.append(f" Rate: Down {convert_bytes(data.transfer_statistics.speeds.received):<13} | Up {convert_bytes(data.transfer_statistics.speeds.transferred)}") + + return lines -def main(): - """ - returns the data, but formatted. - """ - - data = netstats_class.get_data() - - device_name = data["interface"] - - if device_name and data["local_ip"] != "!?!?": - # the above local_ip check is a workaround so netstats updates the output - # when youre using sysmon, and your internet connection goes down or up - - local_ip = data["local_ip"] - raw_received = data["statistics"]["received"] - raw_transferred = data["statistics"]["transferred"] - - human_received = convert_bytes(raw_received) - human_transferred = convert_bytes(raw_transferred) - - human_received_speed = convert_bytes(data["statistics"]["speeds"]["received"]) - human_transferred_speed = convert_bytes( - data["statistics"]["speeds"]["transferred"] - ) - - return ( - f" --- /sys/class/net {'-' * 46}\n" - f" Local IP: {local_ip}{' ' * max(15 - len(local_ip), 0)} | Interface: {device_name}\n" - f" Received: {human_received}" - + " " - * (14 - len(human_received)) - + f"({raw_received} bytes)\n" - f" Transferred: {human_transferred}" - + " " - * (14 - len(human_transferred)) - + f"({raw_transferred} bytes)\n" - f" Speed: Down {human_received_speed}" - + " " * (14 - len(human_received_speed)) - + f"| Up {human_transferred_speed}\n" - ) - - return f" --- /sys/class/net/!?!? {'-' * 41}\n" - - -def end(): - """ - clean up when the program exits - """ - - netstats_class.close_files() - - -if __name__ == "__main__": - main() +def close_files(): + netstats_plugin.close_files() diff --git a/formatters/procpid.py b/formatters/procpid.py deleted file mode 100644 index dd7ae57..0000000 --- a/formatters/procpid.py +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env python3 - -""" -formatter for procpid plugin -""" - -from util.util import convert_bytes -from plugins import procpid - -procpid_class = procpid.Procpid() - - -def main(): - """ - returns the data, but formatted. - """ - - data = procpid_class.get_data() - - formatted_data = [ - f" --- /proc/pid/status {'-' * 44}\n Name PID RSS State" - ] - - for proc in data["processes"]: - vmrss_human_format = convert_bytes(proc["vmrss"]) - formatted_data.append( - f" {proc['name'] or '!?!?'}{' ' * (28 - len(proc['name'] or '!?!?'))}" - f" {proc['pid']}{' ' * (7 - len(str(proc['pid'])))}" - f" {vmrss_human_format}{' ' * (14 - len(vmrss_human_format))} {proc['state'] or '!?!?'}" - ) - - return "\n".join(formatted_data) + "\n" - - -if __name__ == "__main__": - main() diff --git a/formatters/procs.py b/formatters/procs.py new file mode 100644 index 0000000..dc55099 --- /dev/null +++ b/formatters/procs.py @@ -0,0 +1,33 @@ +from plugins.procs import Plugin +from misc.utils import to_bytes, convert_bytes + +procs_plugin = Plugin() + + +def get_lines(): + data = procs_plugin.get_data() + + pnl = [] # save length of proc names + for proc in data: + pnl.append(len(proc.Name)) + + lpnl = int(max(pnl) + 1) # longest proc name length, the length of the longest proc name + lpidl = 7 # longest pid length, this is a default fallback value + # THIS WILL LOOK VERY UGLY IF PROCS ARE SORTED BY CPU USAGE + # the text will keep constantly jumping left and right. naybe this shiuld be + # hardcoded like before? + + with open("/proc/sys/kernel/pid_max", encoding="utf-8") as pid_max: + lpidl = len(pid_max.read().strip()) + + lines = [f" --- /proc/pid/status {'-' * 44}"] + lines.append(f"{' ' * lpidl}PID Name {' ' * (lpnl - 5)}VmRSS CPU% State") + + for process in data: + lines.append(f" {process.PID:>{lpidl}} {process.Name:<{lpnl}}{convert_bytes(process.VmRSS):<14}{process.Utilization:<9}{process.State}") + + return lines + + +def close_files(): + procs_plugin.close_files() diff --git a/util/__init__.py b/misc/__init__.py similarity index 100% rename from util/__init__.py rename to misc/__init__.py diff --git a/misc/config.py b/misc/config.py new file mode 100644 index 0000000..8890214 --- /dev/null +++ b/misc/config.py @@ -0,0 +1,56 @@ +import os +import tomllib + + +class ConfigError(Exception): + """ + triggered when something goes wrong + """ + + +class Config: + """ + config related functions + """ + + def __init__(self, config_path=None): + self._config_file = config_path or self._get_config_file() + # self._loaded_config = self.load_config() # used in get_value + + def _get_config_file(self): + config_file_path = os.path.expanduser("~/.config/sysmon/config.toml") + + if not os.path.exists(config_file_path): + raise ConfigError( + "[_get_config_file] Config file does not exist. Did you copy 'config.toml' to '~/.config/sysmon/'?" + ) + + return config_file_path + + def load_config(self): + """ + load the config nd return the dict + """ + + try: + with open(self._config_file, mode="rb") as config_file: + return tomllib.load(config_file) + + except FileNotFoundError as exc: + raise ConfigError( + f"[load_config] Config file '{self._config_file}' not found." + ) from exc + + # def get_value(self, section, key): + # """ + # get value from config file + # """ + + # # is this even useful or needed? developer can just do this + # # their own way, idk + + # try: + # return self._loaded_config[section][key] + + # except KeyError as exc: + # raise ConfigError(f"[get_value] Failed reading value: {exc}") from exc diff --git a/misc/logger.py b/misc/logger.py new file mode 100644 index 0000000..67fff76 --- /dev/null +++ b/misc/logger.py @@ -0,0 +1,20 @@ +import logging + +from .config import Config, ConfigError + +config = Config().load_config() +DEBUGGING = config["misc"]["debugging"] + + +def create_logger(plugin_name): + logging.basicConfig( + filename="sysmon.log" if DEBUGGING else "/dev/null", + level=logging.DEBUG if DEBUGGING else logging.INFO, + datefmt="%X", + format="%(asctime)s.%(msecs)03d - %(filename)-16s %(funcName)-30s -> %(message)s", + filemode="w", + ) + + logger = logging.getLogger(plugin_name) + + return logger diff --git a/misc/utils.py b/misc/utils.py new file mode 100644 index 0000000..0af4a53 --- /dev/null +++ b/misc/utils.py @@ -0,0 +1,20 @@ +# TODO: this needs to be loaded using the config file +CONVERSION_TYPE = 1024 + + +def to_bytes(kilobytes): + """convert kilobytes to bytes""" + + return kilobytes * 1024 + + +def convert_bytes(fsize, units=("bytes", "KiB", "MiB", "GiB", "TiB")): + """convert bytes to human readable format""" + + for unit in units: + if fsize < CONVERSION_TYPE: + return f"{fsize:.2f} {unit if CONVERSION_TYPE == 1024 else unit.replace('i', '')}" + + fsize /= CONVERSION_TYPE + + return f"{fsize:.2f} {units[-1]}" diff --git a/plugins/__init__.py b/plugins/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/plugins/base.py b/plugins/base.py new file mode 100644 index 0000000..58f10c0 --- /dev/null +++ b/plugins/base.py @@ -0,0 +1,48 @@ +from misc.logger import create_logger + + +class BasePlugin: + """base plugin, which all others are based on""" + + def __init__(self): + """init function, defining basics""" + + self.logger = create_logger(self.__class__.__name__) + self._opened_files = [] + + def _seek_files(self): + for file in self._opened_files: + self.logger.debug(f"seeking {file.name}") + + try: + file.seek(0) + + except Exception as exc: + self.logger.debug(f"error seeking file: {exc}") + + def _open_file(self, file, mode="r", encoding="utf-8"): + """modifying the default open method so i dont have to define encoding every time""" + + return open(file, mode=mode, encoding=encoding) + + def close_files(self): + """ + closing any opened files. must be called + when exiting + """ + + for file in self._opened_files: + try: + file.close() + self.logger.debug(f"closing file {file.name}") + + except Exception as exc: + self.logger.debug(f"error closing file: {exc}") + + def get_data(self): + """ + where the magic happens. this function + gets then returns data + """ + + pass diff --git a/plugins/cpuinfo.py b/plugins/cpuinfo.py index ba68cff..86744d1 100644 --- a/plugins/cpuinfo.py +++ b/plugins/cpuinfo.py @@ -1,29 +1,30 @@ -#!/usr/bin/env python3 - -"""cpuinfo plugin for sysmon""" - import os import re -import sys import glob import platform -from util.util import ( - en_open, - convert_bytes, - to_bytes, -) +from dataclasses import dataclass -from util.logger import setup_logger +from plugins.base import BasePlugin -import re +@dataclass +class ProcessorDetails: + model: str = "!?!?" + utilization: int = 0 + average_frequency: int = 0 + frequency_min: float = 0.0 + frequency_max: float = 0.0 + physical_cores: int = 0 + logical_cores: int = 0 + temperature: float = 0 + cache_level: str = "!?!?" + cache_size: float = 0.0 + # is cpu cache information even needed + architecture: str = "!?!?" -def clean_cpu_model(model): - """ - clean and remove garbage from cpu model string - """ +def _clean_processor_model_string(model_string): replace_stuff = [ r"\(R\)", r"\(TM\)", @@ -36,291 +37,223 @@ def clean_cpu_model(model): r"CPU", r"with Radeon HD Graphics", r"with Radeon Graphics", + r"with Radeon Vega Mobile Gfx", + r"\d+-Core", + r"\b\d{1,2}(?:st|nd|rd|th)?\s*(?:Gen|Generation)\b", ] - model = re.sub(r"\b\d{1,2}(?:st|nd|rd|th)?\s*(?:Gen|Generation)\b", "", model, flags=re.IGNORECASE) - for pattern in replace_stuff: - model = re.sub(pattern, "", model, flags=re.IGNORECASE) + model_string = re.sub(pattern, "", model_string, flags=re.IGNORECASE) + + return " ".join(model_string.split()).split("@", maxsplit=1)[0].strip() - return " ".join(model.split()).split("@", maxsplit=1)[0].rstrip() +def _get_processor_cache_data(): + cache_index = "" + highest_level = 0 -class Cpuinfo: - """ - Cpuinfo class - get cpu details + for index in glob.glob("/sys/devices/system/cpu/cpu*/cache/index*/"): + level = int(open(f"{index}/level").read().strip()) + type = open(f"{index}/type").read().strip() - Usage: - call get_data() to get data - returns dict + if level > highest_level and type == "Unified": + highest_level = level + cache_index = index - DO: - NOT CALL print_data(). That function - is intended to be used by sysmon. (might change in the future...?) - CALL close_files() when your program ends - to avoid opened files - """ + return (str(highest_level), cache_index) +class Plugin(BasePlugin): def __init__(self): - self.logger = setup_logger(__name__) + super().__init__() - self.logger.debug("[init] initializing") + self.logger.debug("initialize plugin") + self._old_user_time, self._old_system_time, self._old_idle = 0, 0, 0 + self._temperature_sensor = self._get_temperature_file() - self.hwmon_dirs = glob.glob("/sys/class/hwmon/*") - self.thermal_dirs = glob.glob("/sys/class/thermal/*") + self._cache_data = _get_processor_cache_data() + self._cache_level = "L" + self._cache_data[0] - self.temperature_file = self.set_temperature_file() + with self._open_file(os.path.join(self._cache_data[1], "size")) as f: + self._cache_size = int(f.read().strip().replace("K", "")) try: - self.core_file = en_open( - "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq" - ) - self.logger.debug(f"[init] {self.core_file}") - - except FileNotFoundError: - self.core_file = None - - self.stat_file = en_open("/proc/stat") - self.cpu_usage_data = [ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - ] - self.static_data = self.get_static_info() - - self.files_opened = [self.core_file, self.stat_file, self.temperature_file] - - def close_files(self): - """ - closing the opened files. always call this - when ending the program - """ - - for file in self.files_opened: - try: - self.logger.debug(f"[close_files] {file.name}") - file.close() - - except: - pass - - def get_static_info(self): - """ - get 'static' information, like cpu cache, cores count, etc - """ - - data_dict = { - "frequency": None, - "cores": { - "physical": 0, - "logical": 0, - }, - "model": None, - "architecture": None, - "cache": { - "level": None, - "size": None, - }, - } - - data_dict["architecture"] = platform.machine() - - with open("/sys/devices/system/cpu/present") as present_cores: - data_dict["cores"]["logical"] = ( - int(present_cores.read().strip().split("-")[1]) + 1 - ) + self._stat_file = self._open_file("/proc/stat") + self._opened_files.append(self._stat_file) + + self.logger.debug(f"opened file /proc/stat") + + except Exception as exc: + self.logger.error(f"error opening file: {exc}") + + frequency_ranges = sorted(self._get_frequency_ranges()) + cores_count = self._get_cores_count() # physical,logical + + self._processor_details = ProcessorDetails( + model=self._get_processor_model(), + frequency_min=frequency_ranges[0], + frequency_max=frequency_ranges[1], + physical_cores=cores_count[0], + logical_cores=cores_count[1], + architecture=platform.machine(), + cache_level=self._cache_level, + cache_size=self._cache_size, + ) - core_last_index = sorted( - glob.glob("/sys/devices/system/cpu/cpu0/cache/index*/") - )[-1] + self.logger.debug( + f"initialize static processor details: {self._processor_details}" + ) - try: - with open(os.path.join(core_last_index, "size")) as cache_size: - data_dict["cache"]["size"] = to_bytes( - int(cache_size.read().lower().replace("k", "").strip()) - ) + def _get_temperature_file(self): + """Get the CPU temperature from /sys/class/hwmon and /sys/class/thermal""" - with open(os.path.join(core_last_index, "level")) as cache_level: - data_dict["cache"]["level"] = "L" + cache_level.read().strip() + allowed_types = ("coretemp", "k10temp", "acpitz", "cpu_1_0_usr", "cpu-1-0-usr", "cpu_thermal", "cpu-thermal") + combined_dirs = [ + *glob.glob("/sys/class/hwmon/*"), + *glob.glob("/sys/class/thermal/*"), + ] - except FileNotFoundError: - self.logger.debug( - f"[get_static_info]: 'size' or 'level' files not found? available files in core_last_index: {os.listdir(core_last_index)}" + for temp_dir in combined_dirs: + sensor_type_file = ( + os.path.join(temp_dir, "type") + if os.path.isfile(os.path.join(temp_dir, "type")) + and os.path.exists(os.path.join(temp_dir, "type")) + else os.path.join(temp_dir, "name") ) - try: # reading from cpuinfo file - with en_open("/proc/cpuinfo") as cpuinfo_file: - cpuinfo_file_data = {} - - for line in cpuinfo_file: - if line.strip(): - key, value = [s.strip() for s in line.split(":", 1)] - cpuinfo_file_data[key.replace(" ", "_").lower()] = value - - if ( - "arm" not in data_dict["architecture"] - and "aarch" not in data_dict["architecture"] - ): - # this code applies only for desktop platforms - - cpuinfo_file.seek(0) - data_dict["cores"]["physical"] = sum( - 1 - for item in list(set(cpuinfo_file.readlines())) - if "core id" in item - ) + try: + with self._open_file(sensor_type_file) as temp_type_file: + sensor_type = temp_type_file.read().strip() - cache = cpuinfo_file_data.get("cache_size") - frequency = cpuinfo_file_data.get("cpu_mhz") + self.logger.debug(f"[set_temperature_file] {temp_dir}: {sensor_type}") - if data_dict["cache"]["size"] == "Unknown" and cache: - self.logger.debug( - "[get_static_info] fallback to /proc/cpuinfo cache" - ) + if sensor_type in allowed_types: + temperature_files = glob.glob( + os.path.join(temp_dir, "temp*_input*") + ) or glob.glob(os.path.join(temp_dir, "temp")) - data_dict["cache"]["size"] = to_bytes(int(cache.split()[0])) + if temperature_files: + self.logger.debug(f"[set_temperature_file] using {temperature_files[-1]} as sensor file") + return temperature_files[-1] - if frequency: - data_dict["frequency"] = round(float(frequency), 2) + except FileNotFoundError: + self.logger.debug(f"[set_temperature_file] FileNotFoundError, does {sensor_type_file} exist?") - model = clean_cpu_model(cpuinfo_file_data.get("model_name")) + return None - else: # in case this is an arm system, grab hardware - model = clean_cpu_model(cpuinfo_file_data.get("hardware")) + def _get_processor_utilization(self): + # credit: https://beta.stackoverflow.com/q/58257596 - data_dict["model"] = model if len(model) < 25 else model[:22] + "..." + stat_file = self._stat_file.readlines() + # cpuN user-time nice-time system-time idle-time io-wait ireq softirq steal guest guest_nice + # cpu 2432102 96139 671184 40452630 28234 141491 43214 0 0 0 - return data_dict + f_cpu = stat_file[0].strip().split(" ") + # calculating the overall cpu utilization - except FileNotFoundError: - sys.exit("Couldnt find /proc/cpuinfo file") + current_user_time = int(f_cpu[2]) + current_system_time = int(f_cpu[4]) + current_idle = int(f_cpu[5]) - except PermissionError: - sys.exit( - "Couldnt read the file. Do you have read permissions for /proc/cpuinfo file?" - ) + calculation = (current_user_time + current_system_time) - (self._old_user_time + self._old_system_time) - def cpu_freq(self): - """get cpu frequency""" + try: + utilization = (calculation / (calculation + (current_idle - self._old_idle)) * 100) - if self.core_file: - self.core_file.seek(0) + except ZeroDivisionError: + utilization = 0 - return round(int(self.core_file.read().strip()) / 1000, 2) + # updating old_vars + self._old_user_time, self._old_system_time, self._old_idle = ( + current_user_time, + current_system_time, + current_idle, + ) - return self.static_data["frequency"] + return round(utilization) - def cpu_usage(self): - """/proc/stat - cpu usage of the system""" + def _get_cores_frequency(self, pattern="m*"): + # credit: https://beta.stackoverflow.com/q/12483399 - old_cpu_usage_data = self.cpu_usage_data - previous_data = ( - self.cpu_usage_data[0] - + self.cpu_usage_data[1] - + self.cpu_usage_data[2] - + self.cpu_usage_data[5] - + self.cpu_usage_data[6] - ) + frequencies = [] - self.stat_file.seek(0) + for scaling_file in glob.glob( f"/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_{pattern}_freq"): + try: + with self._open_file(scaling_file) as f: + frequencies.append(int(f.read())) - new_cpu_usage_data = [ - int(num) - for num in self.stat_file.readline().strip("cpu").strip().split(" ") - ] + except FileNotFoundError as exc: + self.logger.debug(f"failed to open file: {exc}") - current_data = ( - int(new_cpu_usage_data[0]) - + int(new_cpu_usage_data[1]) - + int(new_cpu_usage_data[2]) - + int(new_cpu_usage_data[5]) - + int(new_cpu_usage_data[6]) - ) + return frequencies - total = sum(map(int, old_cpu_usage_data)) - sum(map(int, new_cpu_usage_data)) + def _get_frequency_ranges(self): + freqs_sorted = sorted(self._get_cores_frequency()) - self.cpu_usage_data = new_cpu_usage_data + return (freqs_sorted[0], freqs_sorted[-1]) - try: - return abs(round(100 * ((previous_data - current_data) / total), 1)) + def _get_average_frequency(self): + freqs = self._get_cores_frequency("cur") - except ZeroDivisionError: - return 0 + return sum(freqs) / len(freqs) - except FileNotFoundError: - sys.exit("Couldnt find /proc/stat file") + def _get_cores_count(self): + # this might be highly inaccurate, testing needed + # credit: https://beta.stackoverflow.com/q/73489422 - except PermissionError: - sys.exit( - "Couldnt read the file. Do you have read permissions for /proc/stat file?" - ) + # physical cores part + siblings = [] - def set_temperature_file(self): - """Get the CPU temperature from /sys/class/hwmon and /sys/class/thermal""" + for file in glob.glob("/sys/devices/system/cpu/cpu[0-9]*/topology/thread_siblings_list" ): + try: + with self._open_file(file) as f: + siblings.append(f.read().strip()) - allowed_types = ("coretemp", "k10temp", "acpitz", "cpu_1_0_usr", "cpu-1-0-usr") + except FileNotFoundError as exc: + self.logger.debug(f"failed to open file: {exc}") + return 0 - combined_dirs = [*self.hwmon_dirs, *self.thermal_dirs] + physical_cores = len(list(set(siblings))) - self.logger.debug(f"[set_temperature_file] {combined_dirs}") + # logical cores part + with self._open_file("/sys/devices/system/cpu/present") as _present_cores: + logical_cores = int(_present_cores.read().strip().split("-")[1]) + 1 - for temp_dir in combined_dirs: - sensor_type_file = ( - os.path.join(temp_dir, "type") - if os.path.isfile(os.path.join(temp_dir, "type")) - and os.path.exists(os.path.join(temp_dir, "type")) - else os.path.join(temp_dir, "name") - ) + return (physical_cores, logical_cores) - try: - with en_open(sensor_type_file) as temp_type_file: - sensor_type = temp_type_file.read().strip() + def _get_processor_model(self): + model_name = None - self.logger.debug( - f"[set_temperature_file] {temp_dir}: {sensor_type}" - ) + try: + if platform.machine() in ("aarch64", "aarch", "arm", "arm64"): + # we need to read a different file on arm platforms + with self._open_file("/proc/device-tree/compatible", "rb", encoding=None) as f: + return f.read().replace(b"\x00", b"").decode().split(",")[-1].upper() + # yes, return it instead of model_name = blabla + # because the string is usally clean - if sensor_type in allowed_types: - temperature_files = glob.glob( - os.path.join(temp_dir, "temp*_input*") - ) or glob.glob(os.path.join(temp_dir, "temp")) + else: # we are not on arm, proceed "normally" + with self._open_file("/proc/cpuinfo") as f: + lines = f.readlines() - if temperature_files: - self.logger.debug( - f"[set_temperature_file] using {temperature_files[-1]} as sensor file" - ) - return en_open(temperature_files[-1]) + for line in lines: + if line.startswith("model name"): + model_name = line + break - except FileNotFoundError: - self.logger.debug( - f"[set_temperature_file] COULD NOT OPEN FILE. NO SENSOR SET" - ) + except Exception as exc: + self.logger.debug(f"could not open file: {exc}") - return None + return _clean_processor_model_string("".join(model_name.split(":")[1:])) def get_data(self): - """/proc/cpuinfo - cpu information""" + self._seek_files() - data = { - "temperature": None, - "usage": self.cpu_usage(), - } + self._processor_details.utilization = self._get_processor_utilization() + self._processor_details.average_frequency = self._get_average_frequency() - data = data | self.get_static_info() # merge the two dicts into one + if self._temperature_sensor: + self._processor_details.temperature = round(float(self._open_file(self._temperature_sensor).read().strip()) / 1000, 1) - data["frequency"] = self.cpu_freq() - - if self.temperature_file: - self.temperature_file.seek(0) - data["temperature"] = float( - int(self.temperature_file.read().strip()) // 1000 - ) + self.logger.debug("data out") - return data + return self._processor_details diff --git a/plugins/loadavg.py b/plugins/loadavg.py index afa9fba..df2f17d 100644 --- a/plugins/loadavg.py +++ b/plugins/loadavg.py @@ -4,87 +4,78 @@ import time +from decimal import Decimal from datetime import datetime -from util.util import en_open -from util.logger import setup_logger +from dataclasses import dataclass +from plugins.base import BasePlugin -class Loadavg: - """ - Loadavg class - get load times and uptime - Usage: - call get_data() to get data - returns dict +@dataclass +class LoadTimes: + OneMin: Decimal = 0 + FiveMin: Decimal = 0 + FifteenMin: Decimal = 0 - DO: - NOT CALL print_data(). That function - is intended to be used by sysmon. (might change in the future...?) - CALL close_files() when your program ends - to avoid opened files - """ - def __init__(self): - """ - initializing important stuff - """ +@dataclass +class Entities: + Active: int = 0 + Total: int = 0 - self.logger = setup_logger(__name__) - self.logger.debug("[init] initializing") +@dataclass +class Uptime: + Timestamp: int = 0 + Seconds: int = 0 - self.loadavg_file = en_open("/proc/loadavg") - self.logger.debug("[init] /proc/loadavg") - self.uptime_file = en_open("/proc/uptime") - self.logger.debug("[init] /proc/uptime") +@dataclass +class LoadavgData: + load_times: LoadTimes = None + entities: Entities = None + uptime: Uptime = None - self.files_opened = [self.loadavg_file, self.uptime_file] - def close_files(self): - """ - closing the opened files. always call this - when ending the program - """ +class Plugin(BasePlugin): + def __init__(self): + super().__init__() - for file in self.files_opened: - try: - self.logger.debug(f"[close_files] {file.name}") - file.close() + self.logger.debug("initialize plugin") - except: - pass + try: + self._loadavg_file = self._open_file("/proc/loadavg") + self.logger.debug("opened file /proc/loadavg") + self._opened_files.append(self._loadavg_file) - def get_data(self): - """ - returns a json dict with data - """ + self._uptime_file = self._open_file("/proc/uptime") + self.logger.debug("opened file /proc/uptime") + self._opened_files.append(self._uptime_file) - for file in self.files_opened: - self.logger.debug(f"[get_data] {file.name}") - file.seek(0) + except Exception as exc: + self.logger.error(f"error opening file: {exc}") + + def get_data(self): + self._seek_files() - loadavg_data = self.loadavg_file.read().split() + loadavg_file_data = self._loadavg_file.read().split() - uptime_seconds = int(float(self.uptime_file.readline().split()[0])) + uptime_seconds = int(float(self._uptime_file.readline().split()[0])) uptime_timestamp = time.time() - uptime_seconds - data = { - "load_times": { - "1": float(loadavg_data[0]), - "5": float(loadavg_data[1]), - "15": float(loadavg_data[2]), - }, - "entities": { - "active": int(loadavg_data[3].split("/")[0]), - "total": int(loadavg_data[3].split("/")[1]), - }, - "uptime": { - "timestamp": uptime_timestamp, - "seconds": uptime_seconds, - }, - } - - self.logger.debug("[get_data] return data") + data = LoadavgData( + load_times=LoadTimes( + OneMin=Decimal(loadavg_file_data[0]), + FiveMin=Decimal(loadavg_file_data[1]), + FifteenMin=Decimal(loadavg_file_data[2]), + ), + entities=Entities( + Active=int(loadavg_file_data[3].split("/")[0]), + Total=int(loadavg_file_data[3].split("/")[1]), + ), + uptime=Uptime(Timestamp=uptime_timestamp, Seconds=uptime_seconds), + ) + + self.logger.debug("data out") return data diff --git a/plugins/meminfo.py b/plugins/meminfo.py index 2e6af8c..e9fed38 100644 --- a/plugins/meminfo.py +++ b/plugins/meminfo.py @@ -1,80 +1,89 @@ -#!/usr/bin/env python3 +from dataclasses import dataclass -"""meminfo plugin for sysmon""" +from plugins.base import BasePlugin -from util.util import en_open -from util.logger import setup_logger +@dataclass +class PhysicalValues: + MemTotal: int = 0 + MemAvailable: int = 0 + MemFree: int = 0 + Cached: int = 0 + AltCached: int = 0 + SReclaimable: int = 0 + Buffers: int = 0 + Used: int = 0 + ActualUsed: int = 0 -class Meminfo: - """ - Meminfo class - get physical and swap memory usage - Usage: - call get_data() to get data - returns dict +@dataclass +class VirtualValues: + SwapTotal: int = 0 + SwapFree: int = 0 + SwapCached: int = 0 + Used: int = 0 - DO: - NOT CALL print_data(). That function - is intended to be used by sysmon. (might change in the future...?) - CALL close_files() when your program ends - to avoid opened files - """ - def __init__(self): - """ - initializing important stuff - """ +@dataclass +class PhysicalPercentages: + Used: int = 0 + ActualUsed: int = 0 + Available: int = 0 + Free: int = 0 + Cached: int = 0 - self.logger = setup_logger(__name__) - self.logger.debug("[init] initializing") +@dataclass +class VirtualPercentages: + Used: int = 0 + Free: int = 0 - self.meminfo_file = en_open("/proc/meminfo") - self.logger.debug("[init] /proc/meminfo") - self.files_opened = [self.meminfo_file] +@dataclass +class MeminfoData: + physical_values: PhysicalValues = None + virtual_values: VirtualValues = None + physical_percentages: PhysicalPercentages = None + virtual_percentages: VirtualPercentages = None - def close_files(self): - """ - closing the opened files. always call this - when ending the program - """ - for file in self.files_opened: - try: - self.logger.debug(f"[close_files] {file.name}") - file.close() +class Plugin(BasePlugin): + def __init__(self, config=None): + super().__init__() - except: - pass + if config: + self.config = config - def get_data(self): - """ - returns a json dict with data - """ + self.logger.debug("initialize plugin") - for file in self.files_opened: - self.logger.debug(f"[get_data] {file.name}") - file.seek(0) + try: + self._meminfo_file = self._open_file("/proc/meminfo") + self._opened_files.append(self._meminfo_file) - # thanks to https://stackoverflow.com/a/28161352 - meminfo_data = dict( - (i.split()[0].rstrip(":"), int(i.split()[1]) * 1024) - for i in self.meminfo_file.readlines() - ) + self.logger.debug("opened file /proc/meminfo") - phy_memory_total = meminfo_data.get("MemTotal", 0) - phy_memory_available = meminfo_data.get("MemAvailable", 0) - phy_memory_free = meminfo_data.get("MemFree", 0) + except Exception as exc: + self.logger.error(f"error opening file: {exc}") - phy_memory_raw_cached = meminfo_data.get("Cached", 0) - phy_memory_sreclaimable = meminfo_data.get("SReclaimable", 0) - phy_memory_buffers = meminfo_data.get("Buffers", 0) + def get_data(self): + self._seek_files() - phy_memory_cached = ( - phy_memory_raw_cached + phy_memory_buffers + phy_memory_sreclaimable + meminfo_file_data = dict( + (i.split()[0].rstrip(":"), int(i.split()[1]) * 1024) + for i in self._meminfo_file.readlines() ) + + # raw values section: physical + phy_memory_total = meminfo_file_data.get("MemTotal", 0) + phy_memory_available = meminfo_file_data.get("MemAvailable", 0) + phy_memory_free = meminfo_file_data.get("MemFree", 0) + phy_memory_used = round(phy_memory_total - phy_memory_available) + + phy_memory_raw_cached = meminfo_file_data.get("Cached", 0) + phy_memory_sreclaimable = meminfo_file_data.get("SReclaimable", 0) + phy_memory_buffers = meminfo_file_data.get("Buffers", 0) + phy_memory_cached = (phy_memory_raw_cached + phy_memory_buffers + phy_memory_sreclaimable) + phy_memory_actual_used = round( phy_memory_total - phy_memory_free @@ -82,59 +91,66 @@ def get_data(self): - phy_memory_raw_cached - phy_memory_sreclaimable ) - phy_memory_used = round(phy_memory_total - phy_memory_available) - phy_memory_used_percent = round( - (int(phy_memory_used) / int(phy_memory_total)) * 100, 1 - ) + + # raw values sectio: virtual + # TODO: send (and get) swap data only if enabled in config + swap_memory_total = meminfo_file_data.get("SwapTotal", 0) + swap_memory_free = meminfo_file_data.get("SwapFree", 0) - swap_memory_total = meminfo_data.get("SwapTotal", 0) - swap_memory_free = meminfo_data.get("SwapFree", 0) - - swap_memory_cached = meminfo_data.get("SwapCached", 0) + swap_memory_cached = meminfo_file_data.get("SwapCached", 0) swap_memory_used = round(swap_memory_total - swap_memory_free) - try: - swap_memory_used_percent = round( - (int(swap_memory_used) / int(swap_memory_total)) * 100, 1 - ) # TODO: fix ZeroDivisionError - - except ZeroDivisionError: - swap_memory_used_percent = 0 - - data = { - "physical": { - "values": { - "total": phy_memory_total, - "used": phy_memory_used, - "actual_used": phy_memory_actual_used, - "available": phy_memory_available, - "free": phy_memory_free, - "cached": phy_memory_cached, - }, - "percentage": { - "used": phy_memory_used_percent, - "actual_used": round( - (int(phy_memory_actual_used) / int(phy_memory_total)) * 100, 1 - ), - "available": round(100 - phy_memory_used_percent, 1), - "free": round( - (int(phy_memory_free) / int(phy_memory_total)) * 100, 1 - ), - "cached": round((phy_memory_cached / phy_memory_total) * 100, 1), - }, - }, - "virtual": { - "values": { - "total": swap_memory_total, - "used": swap_memory_used, - "free": swap_memory_free, - "cached": swap_memory_cached, - }, - "percentage": { - "used": swap_memory_used_percent, - "free": round(100 - swap_memory_used_percent, 1), - }, - }, - } + # TODO: check once again if the percentage calculations are correct. + # percentages value section: physical + phy_memory_used_percent = round((int(phy_memory_used) / int(phy_memory_total)) * 100, 1) + phy_memory_actual_used_percent = round((int(phy_memory_actual_used) / int(phy_memory_total)) * 100, 1) + phy_memory_available_percent = round(100 - phy_memory_used_percent, 1) + phy_memory_free_percent = round((int(phy_memory_free) / int(phy_memory_total)) * 100, 1) + phy_memory_cached_percent = round((phy_memory_cached / phy_memory_total) * 100, 1) + + # TODO: check once again if the percentage calculations are correct. + # TODO: send (and get) swap data only if enabled in config + # percentages value section: virtual + swap_memory_used_percent = ( + round((int(swap_memory_used) / int(swap_memory_total)) * 100, 1) + if swap_memory_total > 0 + else 0 + ) + + swap_memory_free_percent = round(100 - swap_memory_used_percent, 1) + + # TODO: send (and get) swap data only if enabled in config + data = MeminfoData( + physical_values=PhysicalValues( + MemTotal=phy_memory_total, + MemAvailable=phy_memory_available, + MemFree=phy_memory_free, + Cached=phy_memory_raw_cached, + AltCached=phy_memory_cached, + SReclaimable=phy_memory_sreclaimable, + Buffers=phy_memory_buffers, + Used=phy_memory_used, + ActualUsed=phy_memory_actual_used, + ), + virtual_values=VirtualValues( + SwapTotal=swap_memory_total, + SwapFree=swap_memory_free, + SwapCached=swap_memory_cached, + Used=swap_memory_used, + ), + physical_percentages=PhysicalPercentages( + Used=phy_memory_used_percent, + ActualUsed=phy_memory_actual_used_percent, + Available=phy_memory_available_percent, + Free=phy_memory_free_percent, + Cached=phy_memory_cached_percent, + ), + virtual_percentages=VirtualPercentages( + Used=swap_memory_used_percent, + Free=swap_memory_free_percent, + ), + ) + + self.logger.debug("data out") return data diff --git a/plugins/netstats.py b/plugins/netstats.py index 9c5cb91..e05279c 100644 --- a/plugins/netstats.py +++ b/plugins/netstats.py @@ -1,204 +1,221 @@ -#!/usr/bin/env python3 - -"""netstats plugin for sysmon""" - import os import fcntl -import glob import socket import struct -from util.util import ( - en_open, - INTERFACE, - SHOW_LOCAL_IP, -) -from util.logger import setup_logger +from dataclasses import dataclass +from plugins.base import BasePlugin -def interface_is_not_blacklisted(iface): - """ - checks if a interface is not 'valid' - """ - blacklist = [768, 769, 770, 771, 772, 777, 778, 779, 783, 65534] +@dataclass +class InterfaceDetails: + name: str | None = None + directory: str | None = None + rx_bytes_file: str = None + tx_bytes_file: str = None - with en_open(iface + "/type") as device_type: - return int(device_type.read()) not in blacklist +@dataclass +class TransferSpeeds: + received: int = 0 + transferred: int = 0 -def interface_is_up(iface): - """ - check if interface is up - """ - with en_open(iface + "/operstate") as status: - return status.read().strip() == "up" +@dataclass +class TransferStatistics: + total_received: int = 0 + total_transferred: int = 0 + speeds: TransferSpeeds = None -def find_active_interface(): - """ - get active interface - """ +@dataclass +class NetstatsData: + name: str = "!?!?" + ip: str = "!?!?" + transfer_statistics: TransferStatistics = None - for iface in glob.glob("/sys/class/net/*"): - if ( - os.path.isdir(iface) - and interface_is_not_blacklisted(iface) - and interface_is_up(iface) - ): - return ( - f"{iface}/statistics/rx_bytes", - f"{iface}/statistics/tx_bytes", - iface.split("/")[4], - ) - return None +class _TrackTranferSpeeds: + def __init__(self): + self.rx = 0 + self.tx = 0 + def update_values(self, rx, tx): + self.rx = rx + self.tx = tx -def get_network_interface(): - """ - Detect an active network interface and return its directory - """ - if INTERFACE is None: - result = find_active_interface() +class Plugin(BasePlugin): + # def __init__(self, config=None): + def __init__(self, config=None): + super().__init__() - if result: - return result + # if config: + # self.config = config - return None + self.logger.debug("initialize plugin") - return ( - f"/sys/class/net/{INTERFACE}/statistics/rx_bytes", - f"/sys/class/net/{INTERFACE}/statistics/tx_bytes", - INTERFACE, - ) + # if self.config: + # self._custom_interface = self.config.get("custom_interface") + # if self._custom_interface: + # self.logger.debug(f"using custom interface {self._custom_interface}") + # self.interface_data = self.get_interface_data(self._custom_interface) -class Speed: - """ - calculate internet speed - """ + # else: + # self.logger.debug("detecting interface automatically") + # self.interface_data = self._get_current_interface() - def __init__(self): - self.rx = 0 - self.tx = 0 + self.logger.debug("detecting interface automatically") + self.interface_data = self._get_current_interface() + + try: + self._rx_file = self._open_file(self.interface_data.rx_bytes_file) + self._tx_file = self._open_file(self.interface_data.tx_bytes_file) + + self._opened_files.append(self._rx_file) + self._opened_files.append(self._tx_file) + self.logger.debug("opened interface rx and tx files") + + except Exception as exc: + self.logger.debug(f"could not open files: {exc}") + + self._transfer_speed_track = _TrackTranferSpeeds() + + def _interface_is_not_blacklisted(self, interface_name): + """ + checks if a interface is not 'valid' + """ + + blacklist = [768, 769, 770, 771, 772, 777, 778, 779, 783, 65534] + + with self._open_file(f"/sys/class/net/{interface_name}/type") as device_type: + device_type_int = int(device_type.read()) + + self.logger.debug(f"interface {interface_name} type {device_type_int}") + return device_type_int not in blacklist - def set_values(self, rx, tx): + def _interface_is_up(self, interface_name): """ - set rx and tx values + check if interface is up """ - self.rx = int(rx) - self.tx = int(tx) + with self._open_file(f"/sys/class/net/{interface_name}/operstate") as device_status: + device_status_str = device_status.read().strip() + self.logger.debug(f"interface {interface_name} operstate {device_status_str}") + + return device_status_str == "up" -class Netstats: - """ - Netstats class - get network stats and speed + def _get_interface_ip(self, interface_name): + # https://stackoverflow.com/a/27494105 + create_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - Usage: - call get_data() to get data - returns dict + try: + local_ip = socket.inet_ntoa( + fcntl.ioctl( + create_socket.fileno(), + 0x8915, + struct.pack("256s", interface_name[:15].encode("UTF-8")), + )[20:24] + ) - DO: - NOT CALL print_data(). That function - is intended to be used by sysmon. (might change in the future...?) - CALL close_files() when your program ends - to avoid opened files - """ + except OSError: + pass + + return local_ip + + def get_interface_data(self, interface_name): + interface_dir = f"/sys/class/net/{interface_name}" + interface_statistics_dir = f"{interface_dir}/statistics" + interface_rx_bytes = f"{interface_statistics_dir}/rx_bytes" + interface_tx_bytes = f"{interface_statistics_dir}/tx_bytes" + + try: + if ( + self._interface_is_not_blacklisted(interface_name) + and self._interface_is_up(interface_name) + and os.listdir(interface_dir) + and os.listdir(interface_statistics_dir) + and os.stat(interface_rx_bytes) + and os.stat(interface_tx_bytes) + ): + self.logger.debug(f"interface {interface_name} passes all checks") + return InterfaceDetails( + name=interface_name, + directory=interface_dir, + rx_bytes_file=interface_rx_bytes, + tx_bytes_file=interface_tx_bytes, + ) + + else: + return InterfaceDetails() + + except FileNotFoundError as exc: + self.logger.debug( + f"one of the checks has failed. check if statistics dir and rx/tx_bytes files exist for {interface_name}. {exc}" + ) - def __init__(self): + def _get_current_interface(self): """ - initializing important stuff + detect which interface is being used right now """ - self.logger = setup_logger(__name__) + interfaces = [] + + # first check: kernel routing + with self._open_file("/proc/net/route") as proc_net: + for iface in proc_net.readlines()[1:]: + # Iface Destination Gateway Flags + # 'wlan0', '00000000', 'FE01A8C0', '0003', '0', '0', '600', '00000000', '0', '0', '0' + interface_data = iface.strip().split("\t")[:4] - self.logger.debug("[init] initializing") + self.logger.debug(f"found interface: {interface_data}") - self.interface = get_network_interface() + if interface_data[1] == "00000000": + # this means default route, which is what we want + if int(interface_data[3], 16) >= 2: + # more than 2 bits means destination is a gateway, we want that + # !! BUT !! what if its more than 2? if it was 3, that would be fine + # !! BUT !! what if its more than 4? 5? 6? is it still valid? + interfaces.append(interface_data[0]) - if self.interface: - self.rx_file = en_open(self.interface[0]) - self.tx_file = en_open(self.interface[1]) + # additional check: sysfs + for interface in interfaces: + interface_data = self.get_interface_data(interface) - self.files_opened = [self.rx_file, self.tx_file] + if interface_data.name: + return interface_data - self.logger.debug("[init] net_save") + self.logger.debug("nothing found?") + return None - self.speed_track = Speed() + def get_data(self): + self._seek_files() - def close_files(self): - """ - closing the opened files. always call this - when ending the program - """ + if not self.interface_data: + return NetstatsData() - for file in self.files_opened: - try: - self.logger.debug(f"[close_files] {file.name}") - file.close() + current_rx_bytes = int(self._rx_file.read().strip()) + current_tx_bytes = int(self._tx_file.read().strip()) - except: - pass + rx_speed = abs(self._transfer_speed_track.rx - current_rx_bytes) + tx_speed = abs(self._transfer_speed_track.tx - current_tx_bytes) - def get_data(self): - """ - returns a json dict with data - """ + self._transfer_speed_track.update_values(current_rx_bytes, current_tx_bytes) + + self.logger.debug("data out") - data = { - "interface": None, - "local_ip": None, - "statistics": { - "received": None, - "transferred": None, - "speeds": { - "received": None, - "transferred": None, - }, - }, - } - - if self.interface is not None: - interface_name = self.interface[2] - - self.rx_file.seek(0) - self.tx_file.seek(0) - - rx = int(self.rx_file.read().strip()) - tx = int(self.tx_file.read().strip()) - - rx_speed = abs(self.speed_track.rx - rx) - tx_speed = abs(self.speed_track.tx - tx) - - self.speed_track.set_values(rx, tx) - - # https://stackoverflow.com/a/27494105 - create_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - - local_ip = "Hidden" - - if SHOW_LOCAL_IP: - try: - local_ip = socket.inet_ntoa( - fcntl.ioctl( - create_socket.fileno(), - 0x8915, - struct.pack("256s", interface_name[:15].encode("UTF-8")), - )[20:24] - ) - - except OSError: - local_ip = "!?!?" - - data["interface"] = interface_name - data["local_ip"] = local_ip - data["statistics"]["received"] = rx - data["statistics"]["transferred"] = tx - data["statistics"]["speeds"]["received"] = rx_speed - data["statistics"]["speeds"]["transferred"] = tx_speed - - return data + return NetstatsData( + name=self.interface_data.name, + ip=self._get_interface_ip(self.interface_data.name), + transfer_statistics=TransferStatistics( + total_received=current_rx_bytes, + total_transferred=current_tx_bytes, + speeds=TransferSpeeds( + received=rx_speed, + transferred=tx_speed, + ), + ), + ) diff --git a/plugins/procpid.py b/plugins/procpid.py deleted file mode 100644 index 05ef7e0..0000000 --- a/plugins/procpid.py +++ /dev/null @@ -1,122 +0,0 @@ -#!/usr/bin/env python3 - -"""procpid plugin for sysmon""" - -import os -import sys - - -from util.util import ( - en_open, - to_bytes, - PROCS, -) - -from util.logger import setup_logger - - -def read_process_status(pid): - """get pid data, like name, state, vmrss""" - - pid_data = {} - - try: - with en_open(f"/proc/{pid}/status") as pid_status_file: - pid_file_lines = {} - pid_data["pid"] = pid - - for line in pid_status_file: - line = line.split() - - key = line[0].rstrip(":").lower() - - try: - value = ( - " ".join(line[1:][1:]).strip("(").strip(")").title() - if key == "state" - else line[1:][0] - ) - - except IndexError: - value = "!?!?" - - pid_file_lines[key] = value - - with open(f"/proc/{pid}/cmdline") as pid_cmdline: - exec_name = ( - pid_cmdline.read() - .replace("\x00", " ") - .strip() - .split("/")[-1] - .split(" ")[0] - ) - - if len(exec_name) > 28: - exec_name = exec_name[:25] + "..." - - pid_file_lines["name"] = exec_name - - return pid_file_lines - - except FileNotFoundError: - pass - - -class Procpid: - """ - Procpid class - get processes and sort from highest to lowest - based on VmRSS usage - - Usage: - call get_data() to get data - returns dict - - DO: - NOT CALL print_data(). That function - is intended to be used by sysmon. (might change in the future...?) - CALL close_files() when your program ends - to avoid opened files - """ - - def __init__(self): - """ - initializing important stuff - """ - - self.logger = setup_logger(__name__) - - self.logger.debug("[init] initializing") - - def get_data(self): - """ - returns a json dict with data - """ - - data = {"processes": []} - - process_dirs = [pid for pid in os.listdir("/proc") if pid.isdigit()] - processes = [] - - for pid in process_dirs: - process_info = read_process_status(pid) - - if process_info: - processes.append(process_info) - - self.logger.debug("[get_data] sorting") - - processes = sorted( - processes, key=lambda x: int(x.get("vmrss", 0)), reverse=True - ) - - for proc_data in processes[:PROCS]: - data["processes"].append( - { - "name": proc_data["name"], - "pid": int(proc_data["pid"]), - "vmrss": to_bytes(int(proc_data["vmrss"])), - "state": proc_data["state"], - } - ) - - return data diff --git a/plugins/procs.py b/plugins/procs.py new file mode 100644 index 0000000..1cda123 --- /dev/null +++ b/plugins/procs.py @@ -0,0 +1,136 @@ +#!/usr/bin/env python3 + +"""procpid plugin for sysmon""" + +import os + +from dataclasses import dataclass + +from plugins.base import BasePlugin + + +@dataclass +class ProcessData: + Name: str = "!?!?" + PID: int = 0 + VmRSS: float = 0.0 + State: str = "!?!?" + Utilization: int = 0 + + +class Plugin(BasePlugin): + def __init__(self): + super().__init__() + + self.logger.debug("initialize plugin") + self.processes_to_show = 8 + + self.logger.debug(f"showing only {self.processes_to_show} processes") + + self._procstat_file = self._open_file("/proc/stat") + self._opened_files.append(self._procstat_file) + + self._old_time_total = 0 + self._proc_times = {} + + self.logger.debug("opened file /proc/stat") + + def _get_process_data(self, pid, time_total): + """get pid data, like name, state, vmrss""" + + # should this function be outside of the ProcsPlugin? + + try: + with open(f"/proc/{pid}/status") as process_status_file: + process_file_lines = {} + + for line in process_status_file: + line = line.split() + key = line[0].rstrip(":").lower() + + try: + value = ( + " ".join(line[1:][1:]).strip("(").strip(")").title() + if key == "state" + else line[1:][0] + ) + + except IndexError: + value = "!?!?" + + # how accurate is this? does every line consist of key: value ? + process_file_lines[key] = value + + with open(f"/proc/{pid}/cmdline") as pid_cmdline: + exec_name = ( + pid_cmdline.read() + .split("\x00")[0] + .split("/")[-1] + ) + + if len(exec_name) > 25: + exec_name = exec_name[:22] + "..." + + # reading the cmdline file can give us a more "accurate"/better + # name for the process, compared to the status file + # BUT, sometimes it can be iaccurate or blank. + + process_name = ( + process_file_lines.get("name") + if process_file_lines.get("name") + else exec_name + ) + + return ProcessData( + Name=process_name, + PID=process_file_lines.get("pid", 0), + VmRSS=int(process_file_lines.get("vmrss", 0)) * 1024, # or maybe not? give raw value instead? + State=process_file_lines.get("state", "!?!?"), + Utilization=self._get_process_utilization(pid, time_total), + ) + + except (FileNotFoundError, ProcessLookupError): + return ProcessData() + + def _get_process_utilization(self, pid, time_total): + # credit: https://stackoverflow.com/a/1424556 + + with self._open_file(f"/proc/{pid}/stat") as f: + data = f.read() + + after_comm = data[data.rindex(")") + 1 :].split() + utime = int(after_comm[11]) + stime = int(after_comm[12]) + + old_utime, old_stime = self._proc_times.get(pid, (utime, stime)) + + # HARDCODED VALUES BELOW + # DONT HARDCODE THE * 20 PART + # but then, how am i supposed to get total logical cores? maybe i can import cpuinfo + # plugin, or maybe another method? idfk + user_util = 100 * (utime - old_utime) / (time_total - self._old_time_total) * 20 + sys_util = 100 * (stime - old_stime) / (time_total - self._old_time_total) * 20 + + self._proc_times[pid] = (utime, stime) + + return round(user_util + sys_util, 1) + + def get_data(self): + self._procstat_file.seek(0) + + time_total = sum(int(x) for x in self._procstat_file.readline().split()[1:]) + process_data = [] + + for process_id in [pid for pid in os.listdir("/proc") if pid.isdigit()]: + process_data.append(self._get_process_data(process_id, time_total)) + + self._old_time_total = time_total + sorted_processes = sorted( + process_data, key=lambda x: int(x.VmRSS), reverse=True + ) + # int(x.VmRSS): this part can be swapped with x.Utilization btw, which will sort + # by cpu usage. this should definitely be a feature, and config too + + self.logger.debug("data out") + + return sorted_processes[:self.processes_to_show] diff --git a/screens/PREVIEW.md b/screens/PREVIEW.md deleted file mode 100644 index 5610aeb..0000000 --- a/screens/PREVIEW.md +++ /dev/null @@ -1,13 +0,0 @@ -# preview - -## laptop -![sysmon preview 1](preview-1.png) - -## desktop pc -![sysmon preview 2](preview-2.png) - -## rooted android phone -![sysmon preview 3](preview-3.png) - -## laptop -![sysmon_preview_4](preview-4.png) diff --git a/screens/preview-1.png b/screens/preview-1.png deleted file mode 100644 index cdb5c3e..0000000 Binary files a/screens/preview-1.png and /dev/null differ diff --git a/screens/preview-2.png b/screens/preview-2.png deleted file mode 100644 index 4a4c292..0000000 Binary files a/screens/preview-2.png and /dev/null differ diff --git a/screens/preview-3.png b/screens/preview-3.png deleted file mode 100644 index fee131d..0000000 Binary files a/screens/preview-3.png and /dev/null differ diff --git a/screens/preview-4.png b/screens/preview-4.png deleted file mode 100644 index 15177ee..0000000 Binary files a/screens/preview-4.png and /dev/null differ diff --git a/sysmon b/sysmon index 7560eab..c5398ca 100755 --- a/sysmon +++ b/sysmon @@ -1,241 +1,39 @@ #!/usr/bin/env python3 -""" -sysmon - a SYStem MONitor written in python, designed to be easy -to understand, and "ready to use", without installing dependecies (except curses) - -made by dev64 (also known as .dev, devlocalhost) -""" - -import sys import curses -import select -import argparse -import importlib - from datetime import datetime -from util import util - -arguments = [ - { - "options": ["-nc", "--nocpuinfo"], - "kwargs": { - "action": "store_true", - "help": "disable cpuinfo plugin (cpu information, like usage, model, and more)", - }, - }, - { - "options": ["-nm", "--nomeminfo"], - "kwargs": { - "action": "store_true", - "help": "disable meminfo plugin (memory information, like total, used and more)", - }, - }, - { - "options": ["-nl", "--noloadavg"], - "kwargs": { - "action": "store_true", - "help": "disable loadavg plugin (load times information, including uptime, and more)", - }, - }, - { - "options": ["-np", "--noprocpid"], - "kwargs": { - "action": "store_true", - "help": "disable procpid plugin (shows the most vmrss consuming processes)", - }, - }, - { - "options": ["-nn", "--nonetstats"], - "kwargs": { - "action": "store_true", - "help": "disable netstats plugin (network stats, like received/transfered " - "bytes, and more)", - }, - }, - { - "options": ["-nt", "--temp"], - "kwargs": { - "action": "store_true", - "help": "hide cpu temperature in cpuinfo plugin", - }, - }, - { - "options": ["-ns", "--swap"], - "kwargs": { - "action": "store_true", - "help": "hide swap information in meminfo plugin", - }, - }, - { - "options": ["-ni", "--ip"], - "kwargs": { - "action": "store_true", - "help": "hide IP in netstats plugin", - }, - }, - { - "options": ["-m", "--metric"], - "kwargs": { - "action": "store_true", - "help": "use metric (1000) instead of IEC (1024) unit for data convertion", - }, - }, - { - "options": ["-d", "--debug"], - "kwargs": { - "action": "store_true", - "help": "enable debugging/logging", - }, - }, - { - "options": ["-p", "--procs"], - "kwargs": { - "type": int, - "metavar": "INT", - "help": "show INT processes in procpid instead of default value. Default: 6", - }, - }, - { - "options": ["-s", "--sleep"], - "kwargs": { - "type": float, - "default": 1, - "metavar": "FLOAT", - "help": "refresh data every FLOAT seconds instead of the default value. Default: 1.0", - }, - }, - { - "options": ["-i", "--interface"], - "kwargs": { - "type": str, - "metavar": "INTERFACE", - "help": "use INTERFACE instead of detected interface in netstats plugin", - }, - }, -] - - -def custom_help_formatter(prog): - """fixing the ugly looking help menu""" - - return argparse.HelpFormatter(prog, max_help_position=46) - - -def parse_arguments(): - """create arguments""" - - parser = argparse.ArgumentParser( - description="sysmon, a system monitor made in python | github.com/devlocalhost/sysmon", - formatter_class=custom_help_formatter, - ) - for arg in arguments: - parser.add_argument(*arg["options"], **arg["kwargs"]) +from formatters import cpuinfo, meminfo, loadavg, procs, netstats # dont hardcode, use the config. - return parser.parse_args() - - -# formatting and arguments - -# sysmon printing part - - -def start_sysmon(stdscr, plugins, sleep): - """main printing method, what you see when you run sysmon""" - - stdscr.nodelay(True) +def main(stdscr): curses.curs_set(0) - curses.use_default_colors() + stdscr.nodelay(True) + stdscr.timeout(1000) # dont hardcode, use the config. - formatters = [importlib.import_module(func) for func in plugins] + formatters = [cpuinfo, meminfo, loadavg, procs, netstats] # dont hardcode, use the config. try: while True: - stdscr.erase() - - stdscr.addstr( - f" --- Sysmon | {datetime.now().strftime('%I:%M:%S %p')} " - + "-" * 42 - + "\n" - ) + stdscr.clear() + lines = [f" --- Sysmon | {datetime.now().strftime('%I:%M:%S %p')} " + "-" * 42 + "\n"] for formatter in formatters: - stdscr.addstr(formatter.main()) + lines += formatter.get_lines() - stdscr.addstr(f" {'-' * 67}") - stdscr.refresh() + lines.append(f" {'-' * 67}") # this is the last line in the index - rlist = select.select([sys.stdin], [], [], sleep) + for i, line in enumerate(lines): + stdscr.addstr(i, 0, line) - if rlist: - kinput = stdscr.getch() - - if kinput == ord("q"): - for formatter in formatters: - try: - formatter.end() - - except AttributeError: - pass - - curses.curs_set(1) - - break - - except curses.error: - sys.exit("Not enough terminal space. Please resize your terminal") - - except KeyboardInterrupt: - curses.endwin() - curses.curs_set(1) - - curses.endwin() - - -def main(): - """execute sysmon""" - - plugins_array = [ - "formatters.cpuinfo", - "formatters.meminfo", - "formatters.loadavg", - "formatters.procpid", - "formatters.netstats", - ] - - for argument in args.__dict__: - if argument.startswith("no"): - if getattr(args, argument): - argument = f"formatters.{argument.replace('no', '')}" - plugins_array.pop(plugins_array.index(argument)) - - curses.wrapper(start_sysmon, plugins_array, args.sleep) - - -if __name__ == "__main__": - args = parse_arguments() - - if args.temp: - util.SHOW_TEMPERATURE = False - - if args.swap: - util.SHOW_SWAP = False - - if args.procs: - util.PROCS = args.procs - - if args.metric: - util.CONVERSION_TYPE = 1000 + stdscr.refresh() - if args.interface: - util.INTERFACE = args.interface + if stdscr.getch() == ord("q"): + break - if args.ip: - util.SHOW_LOCAL_IP = False + finally: + for formatter in formatters: + formatter.close_files() - if args.debug: - util.DEBUGGING = True - main() +curses.wrapper(main) diff --git a/util/logger.py b/util/logger.py deleted file mode 100644 index 08d2340..0000000 --- a/util/logger.py +++ /dev/null @@ -1,17 +0,0 @@ -import logging - -from . import util - - -def setup_logger(plugin_name): - logging.basicConfig( - filename="sysmon.log", - level=logging.DEBUG if util.DEBUGGING else logging.INFO, - datefmt="%X", - format="%(asctime)s.%(msecs)03d - %(name)s -> %(message)s", - filemode="w", - ) - - logger = logging.getLogger(plugin_name) - - return logger diff --git a/util/util.py b/util/util.py deleted file mode 100644 index 9a40ec4..0000000 --- a/util/util.py +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/env python3 - -""" -extra plugin for sysmon - basic functions -needed for sysmon to function -""" - -import os - - -# START OF IMPORTANT PART FOR THE PLUGINS - -# general vars, save data directory and IEC or metric conversion (size) -SAVE_DIR = None -CONVERSION_TYPE = 1024 - -# cpuinfo.py vars - control whenever to show cpu temperature -SHOW_TEMPERATURE = True - -# meminfo.py vars - show swap statsor no -SHOW_SWAP = True - -# procpid.py vars - how many processes to display -PROCS = 6 - -# netstats vars - custom interface, from /sys/class/net/ -# check /sys/class/net/ for more -INTERFACE = None -SHOW_LOCAL_IP = True - -# debugging - for logger.py -DEBUGGING = False - -# END OF IMPORTANT PART FOR THE PLUGINS - - -def convert_bytes(fsize, units=("bytes", "KiB", "MiB", "GiB", "TiB")): - """convert bytes to human readable format""" - - for unit in units: - if fsize < CONVERSION_TYPE: - return f"{fsize:.2f} {unit if CONVERSION_TYPE == 1024 else unit.replace('i', '')}" - - fsize /= CONVERSION_TYPE - - return f"{fsize:.2f} {units[-1]}" - - -def en_open(file, method="r"): - """modifying the default open method so i dont have to define encoding every time""" - - return open(file, mode=method, encoding="utf-8") - - -def to_bytes(kilobytes): - """convert kilobytes to bytes""" - - return kilobytes * 1024