html/hddtemp.pl
1 ########
2 # INFO
3 #
4 # Type this to add the item:
5 #
6 # /statusbar window add hddtemp
7 #
8 # See
9 #
10 # /help statusbar
11 #
12 #
13 # If you want to use this script install the hddtemp daemon on the host
14 # you like to monitor and set it up, You can use multiple hosts aswell.
15 #
16 # Example:
17 # /set hddtemp_hosts host1 host2 host3
18 # /set hddtemp_ports 7634 7635 7553
19 #
20 # If all the daemons run all on the same port you can set a single port,
21 # It will be used for all hosts.
22 #
23 # Example:
24 # /set hddtemp_ports 7634
25 #
26 # There are 2 coloring threshold settings hddtemp_threshold_green
27 # and hddtemp_threshold_red. If the temperature is higher than
28 # green and lower then red the color will be yellow.
29 #
30 # Example:
31 # /set hddtemp_threshold_green 35
32 # /set hddtemp_threshold_red 45
33 #
34 # (I don't know if the unit retured by the daemon depends on the
35 # locale. I've Celsius values here.)
36 #
37 #
38 # There is a setting for the degree sign.
39 # Since there is a difference between 8bit and utf-8
40 # you can set it to what you prefer
41 #
42 ########
43 # CHANGES:
44 # 0.13 - added forking (not blocking irrsi while fetching the temperatures)
45 #######
46 # TODO: themeing support
47 #
48
49 use strict;
50 use Irssi;
51 use IO::Socket::INET;
52 use POSIX;
53
54 use vars qw($VERSION %IRSSI);
55 $VERSION = "0.14";
56 %IRSSI = (
57 authors => "Valentin Batz",
58 contact => "vb\@g-23.org",
59 name => "hddtemp",
60 description => "adds a statusbar item which shows temperatures of harddisks (with multiple hddtemp-hosts support)",
61 license => "GPLv2",
62 changed => "2004-06-21",
63 url => "http://hurzelgnom.bei.t-online.de/irssi/scripts/hddtemp.pl",
64 sbitems => "hddtemp"
65 );
66
67 my $forked;
68 my $pipe_tag;
69 my $outstring = 'hddtemp...';
70
71 sub get_data {
72 my $lines;
73 my @hosts = split(/ /, Irssi::settings_get_str("hddtemp_hosts"));
74 my @ports = split(/ /, Irssi::settings_get_str("hddtemp_ports"));
75 print "hi";
76 while(scalar(@hosts) > scalar(@ports)){
77 push @ports, @ports[0];
78 }
79 my $i=0;
80 for ($i;$i<scalar(@hosts);$i++) {
81 my $sock = IO::Socket::INET->new(PeerAddr => @hosts[$i],
82 PeerPort => @ports[$i],
83 Proto => 'tcp',
84 Timeout => 10);
85 #skip dead hosts
86 next unless $sock;
87 while( $_ = $sock->getline()) {
88 $lines .= $_.';';
89 }
90 }
91 return $lines;
92 }
93
94 sub get_temp {
95 my ($rh, $wh);
96 pipe($rh, $wh);
97 return if $forked;
98 my $pid = fork();
99 if (!defined($pid)) {
100 Irssi::print("Can't fork() - aborting");
101 close($rh); close($wh);
102 return;
103 }
104 $forked = 1;
105 if ($pid > 0) {
106 #parent
107 close($wh);
108 Irssi::pidwait_add($pid);
109 $pipe_tag = Irssi::input_add(fileno($rh), INPUT_READ, \&pipe_input, $rh);
110 return;
111 }
112
113 my $lines;
114 eval {
115 #child
116 $lines = get_data();
117 #write the reply
118 print ($wh $lines);
119 close($wh);
120 };
121 POSIX::_exit(1);
122 }
123
124 sub pipe_input {
125 my $rh=shift;
126 my $linesx=<$rh>;
127 close($rh);
128 Irssi::input_remove($pipe_tag);
129 $forked = 0;
130 my %temps;
131 my $green = Irssi::settings_get_int("hddtemp_threshold_green");
132 my $red = Irssi::settings_get_int("hddtemp_threshold_red");
133 my $degree = Irssi::settings_get_str("hddtemp_degree_sign");
134 unless ($linesx) {
135 return(0);
136 }
137 my ($hdd, $model, $temp, $unit);
138 my $i=0;
139 foreach my $lines (split(';', $linesx)) {
140 foreach my $line (split(/\|\|/, $lines)) {
141 #remove heading/traling |
142 $line =~ s/^\|//;
143 $line =~ s/\|$//;
144
145 ($hdd, $model, $temp, $unit) = split(/\|/,$line,4);
146
147 $hdd =~ s/(.*)\/(.*)$/$2/;
148 #different colors for different termperatures
149 if ($temp <= $green) {
150 $temps{$i.':'.$hdd} = '%g'.$temp.'%n'.$degree.$unit;
151 } elsif ($temp > $green && $temp < $red) {
152 $temps{$i.':'.$hdd} = '%y'.$temp.'%n'.$degree.$unit;
153 } elsif ($temp >= $red) {
154 $temps{$i.':'.$hdd} = '%r'.$temp.'%n'.$degree.$unit;
155 }
156 }
157 $i++;
158 }
159 my $out='';
160 foreach (sort keys %temps) {
161 $out .= "$_: $temps{$_} ";
162 }
163 $out=~s/\s+$//;
164 # temporal use of $out to prevent statusbar drawing errors
165 $outstring=$out;
166 Irssi::statusbar_items_redraw('hddtemp');
167 }
168
169 sub sb_hddtemp() {
170 my ($item, $get_size_only) = @_;
171 $item->default_handler($get_size_only, "{sb $outstring}", undef, 1);
172 }
173
174 Irssi::timeout_add(15000, \&get_temp, undef);
175 Irssi::statusbar_item_register('hddtemp', undef, 'sb_hddtemp');
176 Irssi::settings_add_str("hddtemp", "hddtemp_hosts","localhost");
177 Irssi::settings_add_str("hddtemp", "hddtemp_ports","7634");
178 Irssi::settings_add_int("hddtemp", "hddtemp_threshold_green", 35);
179 Irssi::settings_add_int("hddtemp", "hddtemp_threshold_red", 45);
180 Irssi::settings_add_str("hddtemp", "hddtemp_degree_sign","°");
181 Irssi::signal_add("setup changed", \&get_temp);
182 get_temp();