Last active
March 5, 2016 16:17
-
-
Save Darkhogg/87370d7c6f6154d0a6c4 to your computer and use it in GitHub Desktop.
Generic Monitor script for Xfce that shows the normalized load average in color
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from __future__ import print_function, division | |
import multiprocessing as mp | |
import os | |
import sys | |
KEYPOINTS = ( | |
# lavg r g b | |
(0.15, 0.25, 0.25, 0.25), | |
(0.50, 0.40, 0.80, 0.40), | |
(0.75, 1.00, 0.80, 0.00), | |
(1.25, 1.00, 0.00, 0.00), | |
(1.68, 1.00, 0.00, 1.00), | |
(2.00, 1.00, 1.00, 1.00), | |
) | |
def find_keypoints(value): | |
ifrom, ito = 0, 0 | |
while (ito < len(KEYPOINTS)) and (value >= KEYPOINTS[ito][0]): | |
ito += 1 | |
ifrom = ito - 1 | |
if ito >= len(KEYPOINTS): | |
ito = ifrom | |
return KEYPOINTS[ifrom], KEYPOINTS[ito] | |
def interpolate(pos, left, right): | |
return left + pos * (right - left) | |
def interpolate_color_keypoints(value, kfrom, kto): | |
kdiff = kto[0] - kfrom[0] | |
pos = (value - kfrom[0]) / kdiff if kdiff else 0 | |
r = interpolate(pos, kfrom[1], kto[1]) | |
g = interpolate(pos, kfrom[2], kto[2]) | |
b = interpolate(pos, kfrom[3], kto[3]) | |
return r, g, b | |
def get_load_hex_color(load): | |
# Find the color values for the current load | |
kptfrom, kptto = find_keypoints(load) | |
r, g, b = interpolate_color_keypoints(load, kptfrom, kptto) | |
return '#{:02x}{:02x}{:02x}'.format(int(255*r), int(255*g), int(255*b)) | |
ncpus = mp.cpu_count() | |
loads = os.getloadavg() | |
hc1, hc2, hc3 = (get_load_hex_color(l / ncpus) for l in loads) | |
# Print in genmon forma | |
print('<txt><span foreground="{}">●</span> <span foreground="{}">●</span> <span foreground="{}">●</span></txt>'.format(hc1, hc2, hc3)) | |
print('<tool>{:.02f} {:.02f} {:.02f}</tool>'.format(*loads)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment