This repository was archived by the owner on Feb 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu
More file actions
executable file
·68 lines (51 loc) · 3.03 KB
/
cpu
File metadata and controls
executable file
·68 lines (51 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/python
# Author: 3urobeat (https://github.com/HerrEurobeat)
# Description: Gets the utilization (load) & temperature from 'mpstat' & 'sensors' to be used with i3blocks. Supports warn and crit colors.
#
# Usage: Call file with -f flag and provide format in a string. The script will replace the keywords in your format string with the corresponding values.
# Provide the chip flag with the sensor of your CPU temp. Be sure to run 'sudo sensors-detect' in order to find and probe your CPU temp sensor. For me (AMD Ryzen) it is 'k10temp-pci-00c3'.
# Provide the lwarn and or lcrit flag with a number to change the color of the load output.
# Provide the twarn and or tcrit flag with a number to change the color of the temperature output.
#
# Disclaimer: Your i3block needs to include 'markup=pango' and the font you are using when calling the bar in your i3 config needs to be prefixed with 'pango:' for the colors to work!
#
# Example: ./cpu -chip k10temp-pci-00c3 -f "CPU: {load} {temp}" ...will result in: "CPU: 34% 39°C"
# Improvement idea: Always output 2 digits to keep length of output the same most of the time.
import os
import sys
# get flags and set defaults
if "-f" not in sys.argv: format = "LOAD {load} TEMP {temp}"
else: format = str(sys.argv[sys.argv.index("-f") + 1])
if "-chip" not in sys.argv: chip = ""
else: chip = str(sys.argv[sys.argv.index("-chip") + 1])
if "-lwarn" not in sys.argv: loadwarn = "9999"
else: loadwarn = str(sys.argv[sys.argv.index("-lwarn") + 1])
if "-lwarn" not in sys.argv: loadcrit = "9999"
else: loadcrit = str(sys.argv[sys.argv.index("-lcrit") + 1])
if "-twarn" not in sys.argv: tempwarn = "9999"
else: tempwarn = str(sys.argv[sys.argv.index("-twarn") + 1])
if "-tcrit" not in sys.argv: tempcrit = "9999"
else: tempcrit = str(sys.argv[sys.argv.index("-tcrit") + 1])
# get load and temperature
mpresultresult = os.popen("mpstat 2 1").read().split("\n") # output:
sensorsresult = os.popen(f"sensors -u {chip}").read().split("\n") # output:
# retrieve idle value from output, round the number, calculate load (100 - idle) and format string
cpuaverage = mpresultresult[len(mpresultresult) - 2].split(" ")
cpuidle = float(100 - float(cpuaverage[len(cpuaverage) - 1])).__round__(2)
cpuload = str(cpuidle) + "%"
# get chip temp
cputemp1in = [e for e in sensorsresult if 'temp1_input:' in e][0] # get temp1_input for this chip
cputemp = str(float(cputemp1in.replace(" temp1_input: ", "")).__round__(2)) + "°C"
# adjust colors
valueformat = '<span color="{}">{}</span>' # needs pango font prefix to display correctly
cwarn = "#FFFC00" # yellow
ccrit = "#FF0000" # red
if cpuload >= loadcrit: cpuload = valueformat.format(ccrit, cpuload)
elif cpuload >= loadwarn: cpuload = valueformat.format(cwarn, cpuload)
if cputemp >= tempcrit: cputemp = valueformat.format(ccrit, cputemp)
elif cputemp >= tempwarn: cputemp = valueformat.format(cwarn, cputemp)
# replace words in format with values
format = format.replace("{load}", cpuload)
format = format.replace("{temp}", cputemp)
# print result
print(format)