]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - bin/temperature.pl
e342dfdf2e31be052a53c172ccfc815a6aee8f91
[config/dotfiles.git] / bin / temperature.pl
1 #!/usr/bin/perl
2
3
4 use strict;
5 use warnings;
6
7
8 my $temperature_path = '/sys/devices/virtual/hwmon/hwmon0/temp1_input';
9 my $critical_path    = '/sys/devices/virtual/hwmon/hwmon0/temp1_crit';
10
11 # No temperature information available.
12 if (! -e $temperature_path || ! -e $critical_path) {
13     exit 1;
14 }
15
16 my $screen_mode = (defined $ARGV[0] and $ARGV[0] eq '-s');
17 my $tmux_mode   = (defined $ARGV[0] and $ARGV[0] eq '-t');
18
19 my $temperature;
20 my $critical;
21
22 my $file;
23 open $file, '<', $temperature_path or die $!;
24 $temperature = <$file>;
25 close $file;
26 open $file, '<', $critical_path or die $!;
27 $critical = <$file>;
28 close $file;
29
30 my $value = int($temperature / 1000);
31 my $risk  = ($critical - $temperature)/$critical;
32
33 # GNU screen mode with colors.
34 if ($screen_mode) {
35     my $color;
36     if ($risk < 0) {
37         $color = 'br r'; # bold reverse
38     } elsif ($risk < 0.1) {
39         $color = 'b r';
40     } elsif ($risk < 0.2) {
41         $color = 'b y';
42     } else {
43         $color = 'b g';
44     }
45
46     print "\005{+$color}$value\005{-}\n";
47
48 # Same in tmux mode.
49 } elsif ($tmux_mode) {
50     my $color;
51     my $style = 'bold';
52     if ($risk < 0) {
53         $color = 'red';
54         $style = 'reverse'; # blink doesn't work for me
55     } elsif ($risk < 0.1) {
56         $color = 'red';
57     } elsif ($risk < 0.2) {
58         $color = 'yellow';
59     } else {
60         $color = 'green';
61     }
62
63     print "#[fg=$color,$style]$value#[default]\n";
64
65 # Plain text output.
66 } else {
67     print "$value\n";
68 }