]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - bin/temperature.pl
bin/temperature.pl: Support different sensor devices names.
[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 if (scalar @ARGV != 1 and (scalar @ARGV != 2 or not $ARGV[0] =~ /-[st]/)) {
24     print STDERR "Usage: $0 [-s | -t] <path/to/device>\n";
25     print STDERR "\n";
26     print STDERR "Normally this is /sys/devices/platform/*.\n";
27     exit 1;
28 }
29 if (scalar @ARGV == 1) {
30     @ARGV = ('', $ARGV[0]);
31 }
32
33 my $temperature_path = "$ARGV[1]/temp1_input";
34 my $critical_path    = "$ARGV[1]/temp1_crit";
35
36 # No temperature information available.
37 if (not -e $temperature_path or not -e $critical_path) {
38     print STDERR "'$temperature_path' or '$critical_path' not found.\n";
39     exit 1;
40 }
41
42 my $screen_mode = ($ARGV[0] eq '-s');
43 my $tmux_mode   = ($ARGV[0] eq '-t');
44
45 my $temperature;
46 my $critical;
47
48 my $file;
49 open $file, '<', $temperature_path or die $!;
50 $temperature = <$file>;
51 close $file or die $!;
52 open $file, '<', $critical_path or die $!;
53 $critical = <$file>;
54 close $file or die $!;
55
56 my $value = int($temperature / 1000);
57 my $risk  = ($critical - $temperature)/$critical;
58
59 # GNU screen mode with colors.
60 if ($screen_mode) {
61     my $color;
62     if ($risk < 0) {
63         $color = 'br r'; # bold reverse
64     } elsif ($risk < 0.1) {
65         $color = 'b r';
66     } elsif ($risk < 0.2) {
67         $color = 'b y';
68     } else {
69         $color = 'b g';
70     }
71
72     print "\005{+$color}$value\005{-}\n";
73
74 # Same in tmux mode.
75 } elsif ($tmux_mode) {
76     my $color;
77     my $style = 'bold';
78     if ($risk < 0) {
79         $color = 'red';
80         $style = 'reverse'; # blink doesn't work for me
81     } elsif ($risk < 0.1) {
82         $color = 'red';
83     } elsif ($risk < 0.2) {
84         $color = 'yellow';
85     } else {
86         $color = 'green';
87     }
88
89     print "#[fg=$color,$style]$value#[default]\n";
90
91 # Plain text output.
92 } else {
93     print "$value\n";
94 }