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.

60 lines
1.4 KiB

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