Handle hostnames with upper-case letters
[webmin.git] / status / tcp-monitor.pl
1 # tcp-monitor.pl
2 # Monitor a remote TCP server
3
4 sub get_tcp_status
5 {
6 # Connect to the server
7 socket(SOCK, PF_INET, SOCK_STREAM, getprotobyname("tcp")) ||
8         return { 'up' => -1 };
9 local $addr = inet_aton($_[0]->{'host'});
10 return { 'up' => -1 } if (!$addr);
11 local $st = time();
12 local $rv;
13 eval {
14         local $SIG{'ALRM'} = sub { die "alarm\n" };
15         alarm($_[0]->{'alarm'} ? $_[0]->{'alarm'} : 10);
16         $rv = connect(SOCK, pack_sockaddr_in($_[0]->{'port'}, $addr));
17         close(SOCK);
18         alarm(0);
19         };
20 return { 'up' => 0 } if ($@);
21 return { 'up' => $rv,
22          'time' => time() - $st };
23 }
24
25 sub show_tcp_dialog
26 {
27 print &ui_table_row($text{'tcp_host'},
28         &ui_textbox("host", $_[0]->{'host'}, 25));
29
30 print &ui_table_row($text{'tcp_port'},
31         &ui_textbox("port", $_[0]->{'port'}, 5));
32
33 print &ui_table_row($text{'tcp_alarm'},
34         &ui_opt_textbox("alarm", $_[0]->{'alarm'}, 5, $text{'default'}));
35 }
36
37 sub parse_tcp_dialog
38 {
39 &to_ipaddress($in{'host'}) || &to_ip6address($in{'host'}) ||
40         &error($text{'tcp_ehost'});
41 $_[0]->{'host'} = $in{'host'};
42
43 $in{'port'} =~ /^\d+$/ || &error($text{'tcp_eport'});
44 $_[0]->{'port'} = $in{'port'};
45
46 if ($in{'alarm_def'}) {
47         delete($_[0]->{'alarm'});
48         }
49 else {
50         $in{'alarm'} =~ /^\d+$/ || &error($text{'tcp_ealarm'});
51         $_[0]->{'alarm'} = $in{'alarm'};
52         }
53 }
54
55 1;
56