#!/usr/bin/perl # # Copyright 2014 Pierre Mavro # Copyright 2014 Vivien Didelot # Copyright 2014 Andreas Guldstrand # # Licensed under the terms of the GNU GPL v3, or any later version. use strict; use utf8; use Getopt::Long; # default values my $cpu_usage = -1; my $decimals = $ENV{DECIMALS} // 2; my $label ="C "; # Get CPU usage $ENV{LC_ALL}="en_US"; # if mpstat is not run under en_US locale, things may break, so make sure it is open (MPSTAT, 'mpstat 1 1 |') or die; while () { if (/^.*\s+(\d+\.\d+)[\s\x00]?$/) { $cpu_usage = 100 - $1; # 100% - %idle last; } } close(MPSTAT); $cpu_usage eq -1 and die 'Can\'t find CPU information'; # Print short_text, full_text printf "${label}"; printf "%.${decimals}f%%\n", $cpu_usage; exit 0;