i3blocks-cpu.plon commit improve webp-convert.sh (b8ae487)
   1#!/usr/bin/perl
   2#
   3# Copyright 2014 Pierre Mavro <deimos@deimos.fr>
   4# Copyright 2014 Vivien Didelot <vivien@didelot.org>
   5# Copyright 2014 Andreas Guldstrand <andreas.guldstrand@gmail.com>
   6#
   7# Licensed under the terms of the GNU GPL v3, or any later version.
   8
   9use strict;
  10use warnings;
  11use utf8;
  12use Getopt::Long;
  13
  14# default values
  15my $t_warn = 50;
  16my $t_crit = 80;
  17my $cpu_usage = -1;
  18my $decimals = 2;
  19
  20sub help {
  21    print "Usage: cpu_usage [-w <warning>] [-c <critical>] [-d <decimals>]\n";
  22    print "-w <percent>: warning threshold to become yellow\n";
  23    print "-c <percent>: critical threshold to become red\n";
  24    print "-d <decimals>:  Use <decimals> decimals for percentage (default is $decimals) \n"; 
  25    exit 0;
  26}
  27
  28GetOptions("help|h" => \&help,
  29           "w=i"    => \$t_warn,
  30           "c=i"    => \$t_crit,
  31           "d=i"    => \$decimals,
  32);
  33
  34# Get CPU usage
  35$ENV{LC_ALL}="en_US"; # if mpstat is not run under en_US locale, things may break, so make sure it is
  36open (MPSTAT, 'mpstat 1 1 |') or die;
  37while (<MPSTAT>) {
  38    if (/^.*\s+(\d+\.\d+)\s+$/) {
  39        $cpu_usage = 100 - $1; # 100% - %idle
  40        last;
  41    }
  42}
  43close(MPSTAT);
  44
  45$cpu_usage eq -1 and die 'Can\'t find CPU information';
  46
  47# Print short_text, full_text
  48printf "%02d.${decimals}%%\n", $cpu_usage;
  49#printf "%.${decimals}f%%\n", $cpu_usage;
  50
  51# Print color, if needed
  52if ($cpu_usage >= $t_crit) {
  53    print "#FF0000\n";
  54    exit 33;
  55} elsif ($cpu_usage >= $t_warn) {
  56    print "#FFFC00\n";
  57}
  58
  59exit 0;