Dotfiles for my tiling window manager + terminal workflow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.3 KiB

  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. use strict;
  9. use warnings;
  10. use utf8;
  11. use Getopt::Long;
  12. # default values
  13. my $t_warn = 50;
  14. my $t_crit = 80;
  15. my $cpu_usage = -1;
  16. sub help {
  17. print "Usage: cpu_usage [-w <warning>] [-c <critical>]\n";
  18. print "-w <percent>: warning threshold to become yellow\n";
  19. print "-c <percent>: critical threshold to become red\n";
  20. exit 0;
  21. }
  22. GetOptions("help|h" => \&help,
  23. "w=i" => \$t_warn,
  24. "c=i" => \$t_crit);
  25. # Get CPU usage
  26. $ENV{LC_ALL}="en_US"; # if mpstat is not run under en_US locale, things may break, so make sure it is
  27. open (MPSTAT, 'mpstat |') or die;
  28. while (<MPSTAT>) {
  29. if (/^.*\s+(\d+\.\d+)\s+$/) {
  30. $cpu_usage = 100 - $1; # 100% - %idle
  31. last;
  32. }
  33. }
  34. close(MPSTAT);
  35. $cpu_usage eq -1 and die 'Can\'t find CPU information';
  36. # Print short_text, full_text
  37. printf "%.2f%%\n", $cpu_usage;
  38. printf "%.2f%%\n", $cpu_usage;
  39. # Print color, if needed
  40. if ($cpu_usage >= $t_crit) {
  41. print "#FF0000\n";
  42. exit 33;
  43. } elsif ($cpu_usage >= $t_warn) {
  44. print "#FFFC00\n";
  45. }
  46. exit 0;