~vcs-imports/mysql-mmm/2.0

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package MMM::Monitor::Agents;
use base 'Class::Singleton';

use strict;
use warnings FATAL => 'all';
use Log::Log4perl qw(:easy);
use IO::Handle;
use MMM::Monitor::Agent;
use MMM::Monitor::Role;




=head1 NAME

MMM::Monitor::Agents - single instance class holding status information for all agent hosts

=head1 SYNOPSIS

	# Get the instance
	my $agents = MMM::Monitor::Agents->instance();

=cut

sub _new_instance($) {
	my $class = shift;
	my $data = {};

	my @hosts		= keys(%{$main::config->{host}});

	foreach my $host (@hosts) {
		$data->{$host} = new MMM::Monitor::Agent:: (
			host		=> $host,
			mode		=> $main::config->{host}->{$host}->{mode},
			ip			=> $main::config->{host}->{$host}->{ip},
			port		=> $main::config->{host}->{$host}->{agent_port},
			state		=> 'UNKNOWN',
			roles		=> [],
			uptime		=> 0,
			last_uptime => 0
		);
	}
	return bless $data, $class;
}


=head1 FUNCTIONS

=over 4

=item exists($host)

Check if host $host exists.

=cut

sub exists($$) {
	my $self	= shift;
	my $host	= shift;
	return defined($self->{$host});
}


=item get($host)

Get agent for host $host.

=cut

sub get($$) {
	my $self	= shift;
	my $host	= shift;
	return $self->{$host};
}


=item state($host)

Get state of host $host.

=cut

sub state($$) {
	my $self	= shift;
	my $host	= shift;
	LOGDIE "Can't get state of invalid host '$host'" if (!defined($self->{$host}));
	return $self->{$host}->state;
}


=item online_since($host)

Get time since host $host is online.

=cut

sub online_since($$) {
	my $self	= shift;
	my $host	= shift;
	LOGDIE "Can't get time since invalid host '$host' is online" if (!defined($self->{$host}));
	return $self->{$host}->online_since;
}


=item set_state($host, $state)

Set state of host $host to $state.

=cut

sub set_state($$$) {
	my $self	= shift;
	my $host	= shift;
	my $state	= shift;

	LOGDIE "Can't set state of invalid host '$host'" if (!defined($self->{$host}));
	$self->{$host}->state($state);
}


=item get_status_info

Get string containing status information.

=cut

sub get_status_info($) {
	my $self	= shift;
	my $detailed= shift || 0;
	my $res		= '';
	my $agent_res = '';

	keys (%$self); # reset iterator
	foreach my $host (sort(keys(%$self))) {
		my $agent = $self->{$host};
		next unless $agent;
		$agent_res	.= "# Warning: agent on host $host is not reachable\n" if ($agent->agent_down());
		$res		.= sprintf("  %s(%s) %s/%s. Roles: %s\n", $host, $agent->ip, $agent->mode, $agent->state, join(', ', sort(@{$agent->roles})));
	}
	$res = $agent_res . $res if ($detailed);
	return $res;
}


=item save_status

Save status information into status file.

=cut

sub save_status($) {
	my $self	= shift;
	
	my $filename = $main::config->{monitor}->{status_path};

	# TODO maybe it's safer to use File::Temp::tempfile()
	my $tempname = $filename . '.tmp';

	open(STATUS, '>', $tempname) || LOGDIE "Can't open temporary status file '$tempname' for writing!";

	keys (%$self); # reset iterator
	while (my ($host, $agent) = each(%$self)) {
		next unless $agent;
		printf(STATUS "%s|%s|%s\n", $host, $agent->state, join(',', sort(@{$agent->roles})));
	}
	IO::Handle::flush(*STATUS);
	IO::Handle::sync(*STATUS);
	close(STATUS);
	rename($tempname, $filename) || LOGDIE "Can't savely overwrite status file '$filename'!";
	return;
}


=item load_status

Load status information from status file

=cut

sub load_status($) {
	my $self	= shift;

	my $filename = $main::config->{monitor}->{status_path};
	
	# Open status file
	unless (open(STATUS, '<', $filename)) {
		FATAL "Couldn't open status file '$filename': Starting up without status information.";
		return;
	}

	while (my $line = <STATUS>) {
		chomp($line);
		my ($host, $state, $roles) = split(/\|/, $line);
		unless (defined($self->{$host})) {
			WARN "Ignoring saved status information for unknown host '$host'";
			next;
		}

		# Parse roles
		my @saved_roles_str = sort(split(/\,/, $roles));
		my @saved_roles = ();
		foreach my $role_str (@saved_roles_str) {
			my $role = MMM::Monitor::Role->from_string($role_str);
			push (@saved_roles, $role) if defined($role);
		}

		$self->{$host}->state($state);
		$self->{$host}->roles(\@saved_roles);
	}
	close(STATUS);
	return;
}

1;