~y-trudeau/mysql-mmm/NonBlocking

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package MMM::Monitor::NetworkChecker;

use strict;
use warnings FATAL => 'all';
use Log::Log4perl qw(:easy);
use MMM::Monitor::Checker;

our $VERSION = '0.01';

=head1 NAME

MMM::Monitor::NetworkChecker - Function for checking state of network

=head1 SYNOPSIS

	our $shutdown :shared = 0;
	our $have_net :shared = 0;
	$SIG{INT} = sub { $shutdown = 1; };
	my $thread  = new threads(\&MMM::Monitor::NetworkChecker::main)

=cut
sub main() {
	my @ips		= @{$main::config->{monitor}->{ping_ips}};
	
	# Create checker
	my $checker = new MMM::Monitor::Checker::('ping_ip');

	# Perform checks until shutdown
	while (!$main::shutdown) {
		my $state = 0;

		foreach my $ip (@ips) {
			last if ($main::shutdown);

			# Ping checker
			$checker->spawn() unless $checker->ping();

			my $res = $checker->check($ip);
			if ($res =~ /^OK/) {
				$state = 1;
				last;
			}
		}

		if ($main::have_net != $state) {
			FATAL "Network is reachable"	if     ($state);
			FATAL "Network is unreachable"	unless ($state);
			$main::have_net = $state;
		}

		# Sleep a while before checking every ip again
		sleep($main::config->{monitor}->{ping_interval});
	}
	$checker->shutdown();
}

sub wait_for_network() {
	my @ips		= @{$main::config->{monitor}->{ping_ips}};
	
	# Create checker
	my $checker = new MMM::Monitor::Checker::('ping_ip');

	while (!$main::shutdown) {
		# Ping all ips
		foreach my $ip (@ips) {
			last if ($main::shutdown);
			# Ping checker
			$checker->spawn() unless $checker->ping();
	
			my $res = $checker->check($ip);
			if ($res =~ /^OK/) {
				DEBUG "IP '$ip' is reachable: $res";
				$checker->shutdown();
				return 1;
			}
		}

		# Sleep a while before checking every ip again
		sleep($main::config->{monitor}->{ping_interval});
	}
	$checker->shutdown();
	return 0;
}

1;