temperature 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env perl
  2. # Copyright 2014 Pierre Mavro <deimos@deimos.fr>
  3. # Copyright 2014 Vivien Didelot <vivien@didelot.org>
  4. # Copyright 2014 Andreas Guldstrand <andreas.guldstrand@gmail.com>
  5. # Copyright 2014 Benjamin Chretien <chretien at lirmm dot fr>
  6. # Copyright 2019 Jesus E. <heckyel at hyperbola dot info>
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. use strict;
  18. use warnings;
  19. use utf8;
  20. use Getopt::Long;
  21. binmode(STDOUT, ":utf8");
  22. # default values
  23. my $t_warn = $ENV{T_WARN} || 70;
  24. my $t_crit = $ENV{T_CRIT} || 90;
  25. my $chip = $ENV{SENSOR_CHIP} || "";
  26. my $temperature = -9999;
  27. sub help {
  28. print "Usage: temperature [-w <warning>] [-c <critical>] [--chip <chip>]\n";
  29. print "-w <percent>: warning threshold to become yellow\n";
  30. print "-c <percent>: critical threshold to become red\n";
  31. print "--chip <chip>: sensor chip\n";
  32. exit 0;
  33. }
  34. GetOptions("help|h" => \&help,
  35. "w=i" => \$t_warn,
  36. "c=i" => \$t_crit,
  37. "chip=s" => \$chip);
  38. # Get chip temperature
  39. open (SENSORS, "sensors -u $chip |") or die;
  40. while (<SENSORS>) {
  41. if (/^\s+temp1_input:\s+[\+]*([\-]*\d+\.\d)/) {
  42. $temperature = $1;
  43. last;
  44. }
  45. }
  46. close(SENSORS);
  47. $temperature eq -9999 and die 'Cannot find temperature';
  48. # Print short_text, full_text
  49. print "$temperature°C\n" x2;
  50. # Print color, if needed
  51. if ($temperature >= $t_crit) {
  52. print "#FF0000\n";
  53. exit 33;
  54. } elsif ($temperature >= $t_warn) {
  55. print "#FFFC00\n";
  56. } elsif ($temperature < $t_warn) {
  57. print "#18FFFF\n";
  58. }
  59. exit 0;