Handle hostnames with upper-case letters
[webmin.git] / status / sensors-monitor.pl
1 # Check if some lm_sensors value is too high
2
3 sub get_sensors_status
4 {
5 return { 'up' => 1 } if (!&has_command("sensors"));
6 local @sens = &get_sensors_values();
7 local ($sens) = grep { $_->{'name'} eq $_[0]->{'name'} } @sens;
8 return { 'up' => 1 } if (!$sens);
9 if ($_[0]->{'mode'} == 0) {
10         return $sens->{'alarm'} ? { 'up' => 0 } : { 'up' => 1 };
11         }
12 elsif ($_[0]->{'mode'} == 1) {
13         return $sens->{'value'} < $_[0]->{'min'} ? { 'up' => 0 }
14                                                  : { 'up' => 1 };
15         }
16 elsif ($_[0]->{'mode'} == 2) {
17         return $sens->{'value'} > $_[0]->{'max'} ? { 'up' => 0 }
18                                                  : { 'up' => 1 };
19         }
20 }
21
22 sub show_sensors_dialog
23 {
24 if (!&has_command("sensors")) {
25         print &ui_table_row(undef, $text{'sensors_cmd'}, 4);
26         }
27 elsif (@sens = &get_sensors_values()) {
28         print &ui_table_row($text{'sensors_name'},
29            &ui_select("name", $_[0]->{'name'},
30                 [ map { [ $_->{'name'}, &text('sensors_cur', $_->{'name'}, $_->{'value'}, $_->{'units'}) ] } @sens ]), 3);
31
32         print &ui_table_row($text{'sensors_value'},
33           &ui_radio("mode", $_[0]->{'mode'} || 0,
34                 [ [ 0, $text{'sensors_value0'} ],
35                   [ 1, &text('sensors_value1',
36                              &ui_textbox("min", $_[0]->{'min'}, 8)) ],
37                   [ 2, &text('sensors_value2',
38                              &ui_textbox("max", $_[0]->{'max'}, 8)) ] ]), 3);
39         }
40 else {
41         print &ui_table_row(undef, $text{'sensors_none'}, 4);
42         }
43 }
44
45 sub parse_sensors_dialog
46 {
47 &has_command("sensors") || &error($text{'sensors_cmd'});
48 local @sens = &get_sensors_values();
49 @sens || &error($text{'sensors_none'});
50 $_[0]->{'name'} = $in{'name'};
51 $_[0]->{'mode'} = $in{'mode'};
52 $_[0]->{'max'} = $in{'max'};
53 $_[0]->{'min'} = $in{'min'};
54 if ($in{'mode'} == 1) {
55         $in{'min'} =~ /^[0-9\.\+\-]+$/ || &error($text{'sensors_emin'});
56         }
57 elsif ($in{'mode'} == 2) {
58         $in{'max'} =~ /^[0-9\.\+\-]+$/ || &error($text{'sensors_emax'});
59         }
60 }
61
62 # get_sensors_values()
63 # Returns a list of lm_sensors names, values and maxes
64 sub get_sensors_values
65 {
66 if (!scalar(@get_sensors_cache)) {
67         local @rv;
68         open(SENS, "sensors 2>/dev/null |");
69         while(<SENS>) {
70                 if (/^([^:]+):\s+([0-9\.\+\-]+)\s*(\S+)\s+\(min\s+=\s+([0-9\.\+\-]+)\s*(\S+),\s+max\s+=\s+([0-9\.\+\-]+)/) {
71                         # Value with min and max
72                         push(@rv, { 'name' => $1,
73                                     'value' => $2,
74                                     'units' => $3,
75                                     'min' => $4,
76                                     'max' => $6 });
77                         $rv[$#rv]->{'alarm'} = 1 if (/ALARM/);
78                         }
79                 elsif (/^([^:]+):\s+([0-9\.\+\-]+)\s*(\S+)\s+\(min\s+=\s+([0-9\.\+\-]+)\s*(\S+),\s+div\s+=\s+([0-9\.\+\-]+)/) {
80                         # Value with min and div
81                         push(@rv, { 'name' => $1,
82                                     'value' => $2,
83                                     'units' => $3,
84                                     'min' => $4,
85                                     'div' => $6 });
86                         $rv[$#rv]->{'alarm'} = 1 if (/ALARM/);
87                         }
88                 elsif (/^([^:]+):\s+([0-9\.\+\-]+)\s*(\S+)\s+\(min\s+=\s+([0-9\.\+\-]+)\s*(\S+)/) {
89                         # Value with min only
90                         push(@rv, { 'name' => $1,
91                                     'value' => $2,
92                                     'units' => $3,
93                                     'min' => $4 });
94                         $rv[$#rv]->{'alarm'} = 1 if (/ALARM/);
95                         }
96                 elsif (/^([^:]+):\s+([0-9\.\+\-]+)\s*(\S+)\s+\((limit|high)\s+=\s+([0-9\.\+\-]+)\s*(\S+)/) {
97                         # Value with max only
98                         push(@rv, { 'name' => $1,
99                                     'value' => $2,
100                                     'units' => $3,
101                                     'max' => $5 });
102                         $rv[$#rv]->{'alarm'} = 1 if (/ALARM/);
103                         }
104                 elsif (/^([^:]+):\s+([0-9\.\+\-]+)\s*(\S+)\s+\(low\s+=\s+([0-9\.\+\-]+)\s*(\S+)\s*,\s+high\s+=\s+([0-9\.\+\-]+)/) {
105                         # Value with low and high
106                         push(@rv, { 'name' => $1,
107                                     'value' => $2,
108                                     'units' => $3,
109                                     'min' => $4,
110                                     'max' => $6 });
111                         $rv[$#rv]->{'alarm'} = 1 if (/ALARM/);
112                         }
113                 }
114         close(SENS);
115         @get_sensors_cache = @rv;
116         }
117 return @get_sensors_cache;
118 }
119