Tint2 CPU load, fan speed and memory usage

Overwiew

Note: I don't expect this script to run correctly on each possible machine. However, it's easy to customize and adjust to everyone's needs. Just take a look at python-psutil docs.

This script uses the python-psutil module to display the CPU average load, frequency, the temperature sensor reading, the fan speed and memory usage (used / total).

screenshot

Install python-psutil.

Save the script below as ~/scripts/cpu-fan-memory.py:

#!/usr/bin/env python

import psutil


def main():
    # CPU load

    # Display load per CPU as semi-graph:
    print(graph_per_cpu(), end=" ")

    # Unhash a line below to choose a textual display instead of semi-graphical bar:

    # CPU average load:
    # print("CPU: " + str(psutil.cpu_percent(interval=1)), end = "% ")

    # Load per CPU as percentage:
    # print("CPU%: " + per_cpu(), end = " ")

    # CPU frequency
    freq = psutil.cpu_freq(False)
    if freq is not None:
        print(str(round(freq[0] / 1000, 1)) + "/" + str(round(freq[2] / 1000, 1)), end = "GHz ")

    # Temperature sensor
    temp = psutil.sensors_temperatures(False)       # True for Fahrenheit
    if len(temp) > 0:                               # Temperature sensor found
        core_temp = temp["coretemp"]                # "acpitz" for ACPI Thermal Zone
        print(core_temp[0][1], end = "℃")

    # Fan speed
    fans = psutil.sensors_fans()
    if len(fans) > 0:                               # fan sensor found
        fan0 = next(iter(fans.values()))
        print(" " + str(fan0[0][1]), end = "/m ")
    else:
        print(" ", end = "")

    # Memory: used/total
    memory = psutil.virtual_memory()
    print(str(round((memory[0] - memory[1]) / 1073741824, 1)) + "/" + str(round(memory[0] / 1073741824, 1)) + "GB ")


def per_cpu():
    string = ""
    result = psutil.cpu_percent(interval=1, percpu=True)
    for val in result:
        proc = str(int(round(val, 1)))
        if len(proc) < 2:
            proc = "0" + proc
        string += proc + " "
    return string


def graph_per_cpu():

    # Unhash the pattern you like below or replace with your own:

    graph = "_▁▂▃▄▅▆▇███"
    # graph = " ⢀⣀⣠⣤⣴⣶⣾⣿⣿⣿"

    string = ""
    result = psutil.cpu_percent(interval=1, percpu=True)
    for val in result:
        proc = int(round(val / 10, 0))
        string += graph[proc]
    return string


if __name__ == "__main__":
    main()


Then add the executor in your Tint2 settings:

python ~/scripts/cpu-fan-memory.py

Use a fixed width font is recommendable. Example Tint2 executor may look as below:

#-------------------------------------
# Executor 1
execp = new
execp_command = python ~/scripts/cpu-fan-memory.py
execp_interval = 1
execp_has_icon = 0
execp_cache_icon = 1
execp_continuous = 0
execp_markup = 1
execp_lclick_command = 
execp_rclick_command = 
execp_mclick_command = 
execp_uwheel_command = 
execp_dwheel_command = 
execp_font = Monospace 11
execp_font_color = #aaaaff 100
execp_padding = 0 0
execp_background_id = 0
execp_centered = 0
execp_icon_w = 0
execp_icon_h = 0

Inspired by tknomanzr's script from forums.bunsenlabs.org. Thanks!