From 941c510b3b457dd8da35e7bde41c3f8df2c9ce37 Mon Sep 17 00:00:00 2001 From: simon Date: Fri, 29 Jan 2021 19:26:19 +0700 Subject: [PATCH] added cpu loadavg script for i3blocks --- README.md | 4 ++++ i3block_py/cpu.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100755 i3block_py/cpu.py diff --git a/README.md b/README.md index 05b3bd5..5a87ae5 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,10 @@ A bunch of simple bash scripts to be called via i3blocks. ## i3block_py A collection of standalone python scripts for the slightly more complicated things. * **energy.py**: parses `acpi` to output current battery status. Uses `notify-send` to send messages on changes. +* **cpy.py**: parses `/proc/loadavg` to output load average values for last 1, last 5 and last 15 min. + * call `cpy.py 1` to get the avg from last min + * call `cpy.py 5` to get the avg from last 5 min + * call `cpy.py 15` to get the avg from last 15 min ## weather_applet Standalone script that pulls weather data from [openweathermap.org](https://openweathermap.org/) and prints out diff --git a/i3block_py/cpu.py b/i3block_py/cpu.py new file mode 100755 index 0000000..d8eb1f7 --- /dev/null +++ b/i3block_py/cpu.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 +""" parses /proc/loadavg for i3blocks """ + +import sys + +load_field_arg = sys.argv[1] +THREADS = 8 + +def get_load(): + """ returns list of load avg fields """ + with open('/proc/loadavg', 'r') as load_avg: + load = load_avg.readline().strip() + load_line = load.split()[:3] + load_line_float = [float(i) for i in load_line] + return load_line_float + + +def main(load_field): + """ main to run """ + print_main = "" + print_small = "" + print_color = "" + # read proc + load_line_float = get_load() + load_1, load_5, load_15 = load_line_float + # load avg + if load_field == str(1): + load = load_1 + load_string = format(load, '.2f') + print_small = f'{load_field}: {load_string}' + print_color = "#ffffff" + elif load_field == str(5): + load = load_5 + elif load_field == str(15): + load = load_15 + # colors + if 6 < load <= 10: + print_color = "#ffff00" + elif load > 10: + print_color = "#ff0000" + load_string = format(load, '.2f') + print_main = f'{load_field}: {load_string}' + print(print_main) + print(print_small) + print(print_color) + + +if __name__ == '__main__': + main(load_field_arg)