Skip to content

Instantly share code, notes, and snippets.

@andysc
Last active April 6, 2022 03:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andysc/4753514 to your computer and use it in GitHub Desktop.
Save andysc/4753514 to your computer and use it in GitHub Desktop.
Blink(1) CPU monitor for linux. A little perl app which uses vmstat to see how busy the CPU is, and turn the Blink(1) blue through magenta through red, accordingly, using the command-line blink1-tool
# cpu.pl
# Andy Stanford-Clark
# Feb-13
# seconds between readings from vmstat
$sample_interval = 30;
# drive a blink(1) to represent the CPU usage
# blue is low, red is high
$green=0;
open (PIPE,"vmstat -n $sample_interval |") || die "can't open vmstat pipe";
$lines = 0;
while (<PIPE>)
{
print;
chomp;
$lines++;
next if $lines < 3;
@element = split(/ +/,$_);
$idle = $element[15];
print "idle: '$idle'\n";
$busy = 100-$idle;
if ($busy<=50)
{
$blue=255;
$red =int($busy/50*255+0.5);
}
else
{
$red=255;
$blue = int((100-$busy)/50*255+0.5);
}
print "($red,$green,$blue)\n";
if ($red != $last_red || $blue != $last_blue)
{
print "blink(1)\n";
system("./blink1-tool --rgb $red,$green,$blue");
$last_red = $red;
$last_blue = $blue;
}
}
run it in the background with something like "screen", or just nohup and & :)
as in (you may have to install screen first)
screen -dmS cpu perl cpu.pl
or
nohup perl cpu.pl >> /dev/null &
You can change how often it looks at the CPU by changing the $sample_interval in line 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment