Handle hostnames with upper-case letters
[webmin.git] / status / consume-monitor.pl
1 # consume-monitor.pl
2 # Check the rate at which disk space is being consumed on some filesystem
3
4 sub get_consume_status
5 {
6 return { 'up' => -1 } if (!&foreign_check("mount", 1));
7 &foreign_require("mount", "mount-lib.pl");
8 local $m;
9 foreach $f (&mount::list_mounted()) {
10         $m = $f if ($f->[0] eq $_[0]->{'fs'});
11         }
12 local %consume;
13 &read_file("$module_config_directory/consume", \%consume);
14 if ($m) {
15         local @sp = &mount::disk_space($m->[2], $m->[0]);
16         local $now = time();
17         local ($lastfree, $lasttime) = split(/\s+/, $consume{$_[0]->{'fs'}});
18         local $rv = { 'up' => 1 };
19         if ($lasttime) {
20                 # Compare with last time
21                 local $diff = ($lastfree - $sp[1]) / ($now - $lasttime);
22                 if ($diff > $_[0]->{'rate'}) {
23                         $rv = { 'up' => 0,
24                                 'desc' => &text('consume_high',
25                                                 &nice_size($diff*1024)) };
26                         }
27                 }
28         $consume{$_[0]->{'fs'}} = "$sp[1] $now";
29         &write_file("$module_config_directory/consume", \%consume);
30         return $rv;
31         }
32 else {
33         return { 'up' => -1,
34                  'desc' => $text{'space_nofs'} };
35         }
36 }
37
38 sub show_consume_dialog
39 {
40 &foreign_require("mount", "mount-lib.pl");
41 local @mounted = &mount::list_mounted();
42 local ($got) = grep { $_->[0] eq $_[0]->{'fs'} } @mounted;
43 print &ui_table_row($text{'space_fs'},
44         &ui_select("fs", !$_[0]->{'fs'} ? $mounted[0]->[0] :
45                          !$got ? "" : $_[0]->{'fs'},
46                    [ (map { [ $_->[0] ] } @mounted),
47                      [ "", $text{'space_other'} ] ])."\n".
48         &ui_textbox("other", $got ? "" : $_[0]->{'fs'}, 30),
49         3);
50
51 print &ui_table_row($text{'consume_rate'},
52         &ui_bytesbox("rate", $_[0]->{'rate'}*1024));
53
54 }
55
56 sub parse_consume_dialog
57 {
58 &depends_check($_[0], "mount");
59 if ($in{'fs'}) {
60         $_[0]->{'fs'} = $in{'fs'};
61         }
62 else {
63         $in{'other'} =~ /^\// || &error($text{'space_eother'});
64         $_[0]->{'fs'} = $in{'other'};
65         }
66 $in{'rate'} =~ /^[0-9\.]+$/ || &error($text{'consume_erate'});
67 $_[0]->{'rate'} = $in{'rate'}*$in{'rate_units'}/1024;
68 }
69
70 1;
71