Handle hostnames with upper-case letters
[webmin.git] / status / traffic-monitor.pl
1 # traffic-monitor.pl
2 # Check if network traffic is too high
3
4 sub get_traffic_status
5 {
6 local %traffic;
7 &read_file("$module_config_directory/traffic", \%traffic);
8 local $ifaces = &get_traffic_list();
9 local @i = @{$ifaces->{$_[0]->{'iface'}}};
10 if (@i) {
11         local @l = split(/\s+/, $traffic{$_[0]->{'iface'}.'-'.$_[0]->{'dir'}});
12         local $now = time();
13         local $diff;
14         if ($_[0]->{'dir'} == 0) {
15                 $diff = ($i[0]+$i[2]) - ($l[0]+$l[2]);
16                 }
17         elsif ($_[0]->{'dir'} == 1) {
18                 $diff = $i[0] - $l[0];
19                 }
20         else {
21                 $diff = $i[2] - $l[2];
22                 }
23         if ($now <= $l[4]) {
24                 return { 'up' => 1 };
25                 }
26         local $up = $diff / ($now - $l[4]) > $_[0]->{'bytes'} ? 0 : 1;
27         @l = ( $i[0], $i[1], $i[2], $i[3], $now );
28         $traffic{$_[0]->{'iface'}.'-'.$_[0]->{'dir'}} = join(" ", @l);
29         &write_file("$module_config_directory/traffic", \%traffic);
30         return { 'up' => $up };
31         }
32 else {
33         # Interface is gone!
34         return { 'up' => -1 };
35         }
36 }
37
38 sub show_traffic_dialog
39 {
40 print &ui_table_row(undef, $text{'traffic_desc'}, 4);
41
42 local $ifaces = &get_traffic_list();
43 print &ui_table_row($text{'traffic_iface'},
44         &ui_select("iface", $_[0]->{'iface'},
45                    [ map { [ $_ ] } sort { $a cmp $b } keys %$ifaces ]));
46
47 print &ui_table_row($text{'traffic_bytes'},
48         &ui_textbox("bytes", $_[0]->{'bytes'}, 6));
49
50 print &ui_table_row($text{'traffic_dir'},
51         &ui_radio("dir", int($_[0]->{'dir'}),
52                 [ [ 0, $text{'traffic_dir0'} ],
53                   [ 1, $text{'traffic_dir1'} ],
54                   [ 2, $text{'traffic_dir2'} ] ]), 3);
55 }
56
57 sub parse_traffic_dialog
58 {
59 local $ifaces = &get_traffic_list();
60 (keys %$ifaces) || &error($text{'traffic_eifaces'});
61 $in{'bytes'} =~ /^\d+$/ || &error($text{'traffic_ebytes'});
62 $_[0]->{'iface'} = $in{'iface'};
63 $_[0]->{'bytes'} = $in{'bytes'};
64 $_[0]->{'dir'} = $in{'dir'};
65 }
66
67 # get_traffic_list()
68 # Returns a map from interface names to arrays of bytes in, packets in,
69 # bytes out, packets out
70 sub get_traffic_list
71 {
72 local %rv;
73 if ($gconfig{'os_type'} eq 'freebsd') {
74         # Get interfaces from netstat command
75         open(NETSTAT, "netstat -nib |");
76         while(<NETSTAT>) {
77                 if (/^(\S+)\s+(\d+)\s+(\S+)\s+(\d+\.\d+\.\d+\.\d+)\s+(\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(\d+)/ && !$rv{$1}) {
78                         $rv{$1} = [ $7, $5, $10, $8 ];
79                         }
80                 }
81         close(NETSTAT);
82         }
83 else {
84         # Get interfaces from Linux proc file
85         open(TR, "/proc/net/dev");
86         while(<TR>) {
87                 if (/^\s*([a-z0-9]+):\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/) {
88                         $rv{$1} = [ $2, $3, $10, $11 ];
89                         }
90                 }
91         close(TR);
92         }
93 return \%rv;
94 }
95