Handle hostnames with upper-case letters
[webmin.git] / smart-status / status_monitor.pl
1
2 do 'smart-status-lib.pl';
3
4 # status_monitor_list()
5 # Just one type is supported
6 sub status_monitor_list
7 {
8 if (&has_command($config{'smartctl'})) {
9         return ( [ "smart", $text{'monitor_type'} ] );
10         }
11 else {
12         return ( );
13         }
14 }
15
16 # status_monitor_status(type, &monitor, from-ui)
17 # Check the drive status
18 sub status_monitor_status
19 {
20 local @drives = &list_smart_disks_partitions();
21 local ($d) = grep { ($_->{'device'} eq $_[1]->{'drive'} ||
22                      $_->{'id'} eq $_[1]->{'drive'}) &&
23                     $_->{'subdisk'} eq $_[1]->{'subdisk'} } @drives;
24 if (!$d) {
25         # Not in list?!
26         return { 'up' => -1,
27                  'desc' => $text{'monitor_nosuch'} };
28         }
29 local $st = &get_drive_status($d->{'device'}, $d);
30
31 # Record number of errors since last time
32 local %errors;
33 local $errors_file = "$module_config_directory/last-errors";
34 &read_file($errors_file, \%errors);
35 local %lasterrors = %errors;
36 $errors{$_[1]->{'drive'}} = $st->{'errors'};
37 &write_file($errors_file, \%errors);
38
39 if (!$st->{'support'} || !$st->{'enabled'}) {
40         # SMART not enabled on device
41         return { 'up' => -1,
42                  'desc' => $text{'monitor_nosmart'} };
43         }
44 elsif (!$st->{'check'}) {
45         # Check failed
46         return { 'up' => 0 };
47         }
48 elsif ($st->{'errors'} && $_[1]->{'errors'} == 1) {
49         # Errors found, and failing on any errors
50         return { 'up' => 0,
51                  'desc' => &text('monitor_errorsfound', $st->{'errors'}) };
52         }
53 elsif ($st->{'errors'} && $_[1]->{'errors'} == 2 &&
54        $st->{'errors'} > $lasterrors{$_[1]->{'drive'}}) {
55         # Errors found and have increased
56         return { 'up' => 0,
57                  'desc' => &text('monitor_errorsinced', $st->{'errors'},
58                                  $lasterrors{$_[1]->{'drive'}}) };
59         }
60 else {
61         # All OK!
62         return { 'up' => 1 };
63         }
64 }
65
66 # status_monitor_dialog(type, &monitor)
67 # Return form for selecting a drive
68 sub status_monitor_dialog
69 {
70 local $rv;
71 local @drives = &list_smart_disks_partitions();
72 local ($inlist) = grep { ($_->{'device'} eq $_[1]->{'drive'} ||
73                           $_->{'id'} eq $_[1]->{'drive'}) &&
74                          $_->{'subdisk'} eq $_[1]->{'subdisk'} } @drives;
75 $inlist = 1 if (!$_[1]->{'drive'});
76 $rv .= &ui_table_row($text{'monitor_drive'},
77       &ui_select("drive",
78                  !$_[1]->{'drive'} ? $drives[0]->{'device'} :
79                  $inlist ? ($inlist->{'id'} || $inlist->{'device'}).':'.
80                              $inlist->{'subdisk'} :
81                            undef,
82                  [ (map { [ ($_->{'id'} || $_->{'device'}).':'.$_->{'subdisk'},
83                            $_->{'desc'}.($_->{'model'} ?
84                                 " ($_->{'model'})" : "") ] } @drives),
85                    [ "", $text{'monitor_other'} ] ]).
86       &ui_textbox("other", $inlist ? "" : $_[1]->{'drive'}, 15), 3);
87
88 $rv .= &ui_table_row($text{'monitor_errors'},
89         &ui_radio("errors", $_[1]->{'errors'} || 0,
90                 [ [ 1, $text{'yes'} ], [ 0, $text{'no'} ],
91                   [ 2, $text{'monitor_errorsinc'} ] ]));
92 return $rv;
93 }
94
95 # status_monitor_parse(type, &monitor, &in)
96 # Parse form for selecting a rule
97 sub status_monitor_parse
98 {
99 if ($_[2]->{'drive'}) {
100         ($_[1]->{'drive'}, $_[1]->{'subdisk'}) = split(/:/, $_[2]->{'drive'});
101         }
102 else {
103         $_[1]->{'drive'} = $_[2]->{'other'};
104         $_[1]->{'subdisk'} = undef;
105         $_[1]->{'drive'} =~ /^\S+$/ || &error($text{'monitor_edrive'});
106         }
107 $_[1]->{'errors'} = $_[2]->{'errors'};
108 }
109
110 1;
111