]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - bin/temperature.pl
0063bf4ffb510aebf23faa243c6313c69b834ff0
[config/dotfiles.git] / bin / temperature.pl
1 #!/usr/bin/perl
2
3 # Copyright (C) 2011-2012  Simon Ruderich
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
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
18
19 use strict;
20 use warnings;
21
22
23 my $temperature_path = '/sys/devices/virtual/hwmon/hwmon0/temp1_input';
24 my $critical_path    = '/sys/devices/virtual/hwmon/hwmon0/temp1_crit';
25
26 # No temperature information available.
27 if (! -e $temperature_path || ! -e $critical_path) {
28     exit 1;
29 }
30
31 my $screen_mode = (defined $ARGV[0] and $ARGV[0] eq '-s');
32 my $tmux_mode   = (defined $ARGV[0] and $ARGV[0] eq '-t');
33
34 my $temperature;
35 my $critical;
36
37 my $file;
38 open $file, '<', $temperature_path or die $!;
39 $temperature = <$file>;
40 close $file or die $!;
41 open $file, '<', $critical_path or die $!;
42 $critical = <$file>;
43 close $file or die $!;
44
45 my $value = int($temperature / 1000);
46 my $risk  = ($critical - $temperature)/$critical;
47
48 # GNU screen mode with colors.
49 if ($screen_mode) {
50     my $color;
51     if ($risk < 0) {
52         $color = 'br r'; # bold reverse
53     } elsif ($risk < 0.1) {
54         $color = 'b r';
55     } elsif ($risk < 0.2) {
56         $color = 'b y';
57     } else {
58         $color = 'b g';
59     }
60
61     print "\005{+$color}$value\005{-}\n";
62
63 # Same in tmux mode.
64 } elsif ($tmux_mode) {
65     my $color;
66     my $style = 'bold';
67     if ($risk < 0) {
68         $color = 'red';
69         $style = 'reverse'; # blink doesn't work for me
70     } elsif ($risk < 0.1) {
71         $color = 'red';
72     } elsif ($risk < 0.2) {
73         $color = 'yellow';
74     } else {
75         $color = 'green';
76     }
77
78     print "#[fg=$color,$style]$value#[default]\n";
79
80 # Plain text output.
81 } else {
82     print "$value\n";
83 }