This file is indexed.

/usr/share/perl5/MMM/Monitor/StartupStatus.pm is in mysql-mmm-monitor 2.2.1-1.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package MMM::Monitor::StartupStatus;

use strict;
use warnings FATAL => 'all';
use List::Util qw(max);
use Log::Log4perl qw(:easy);
use MMM::Common::Role;
use MMM::Monitor::Role;
use MMM::Monitor::Roles;

our $VERSION = '0.01';

=head1 NAME

MMM::Monitor::StartupStatus - holds information about agent/system/stored status during startup

=cut

sub new($) {
	my $class   = shift;

	my $self = {
		roles => {},
		hosts => {},
		result=> {}
	};
	return bless $self, $class;
}


=head1 FUNCTIONS

=over 4

=item set_agent_status($host, $state, $roles, $master)

Set agent status

=cut

sub set_agent_status($$\@$) {
	my $self	= shift;
	my $host	= shift;
	my $state	= shift;
	my $roles	= shift;
	my $master	= shift;

	$self->{hosts}->{$host}				= {} unless (defined($self->{hosts}->{$host}));
	$self->{hosts}->{$host}->{agent}	= {
		state	=> $state,
		master	=> $master
	};
	foreach my $role (@{$roles}) {
		unless (MMM::Monitor::Roles->instance()->exists_ip($role->name, $role->ip)) {
			WARN "Detected change in role definitions: Role '$role' was removed.";
			next;
		}
		unless (MMM::Monitor::Roles->instance()->can_handle($role->name, $host)) {
			WARN "Detected change in role definitions: Host '$host' can't handle role '$role' anymore.";
			next;
		}
		my $role_str = $role->to_string();
		$self->{roles}->{$role_str}						= {} unless (defined($self->{roles}->{$role_str}));
		$self->{roles}->{$role_str}->{$host}			= {} unless (defined($self->{roles}->{$role_str}->{$host}));
		$self->{roles}->{$role_str}->{$host}->{agent}	= 1;
	}
}


=item set_stored_status($host, $state, $roles)

Set stored status

=cut

sub set_stored_status($$\@$) {
	my $self	= shift;
	my $host	= shift;
	my $state	= shift;
	my $roles	= shift;

	$self->{hosts}->{$host}				= {} unless (defined($self->{hosts}->{$host}));
	$self->{hosts}->{$host}->{stored}	= {
		state	=> $state,
	};
	foreach my $role (@{$roles}) {
		unless (MMM::Monitor::Roles->instance()->exists_ip($role->name, $role->ip)) {
			WARN "Detected change in role definitions: Role '$role' was removed.";
			next;
		}
		unless (MMM::Monitor::Roles->instance()->can_handle($role->name, $host)) {
			WARN "Detected change in role definitions: Host '$host' can't handle role '$role' anymore.";
			next;
		}
		my $role_str = $role->to_string();
		$self->{roles}->{$role_str}						= {} unless (defined($self->{roles}->{$role_str}));
		$self->{roles}->{$role_str}->{$host}			= {} unless (defined($self->{roles}->{$role_str}->{$host}));
		$self->{roles}->{$role_str}->{$host}->{stored}	= 1;
	}
}


=item set_system_status($host, $writable, $roles, $master)

Set system status

=cut

sub set_system_status($$\@$) {
	my $self	= shift;
	my $host	= shift;
	my $writable= shift;
	my $roles	= shift;
	my $master	= shift;

	$self->{hosts}->{$host}				= {} unless (defined($self->{hosts}->{$host}));
	$self->{hosts}->{$host}->{system}	= {
		writable=> $writable,
		master	=> $master
	};
	foreach my $role (@{$roles}) {
		unless (MMM::Monitor::Roles->instance()->exists_ip($role->name, $role->ip)) {
			WARN "Detected change in role definitions: Role '$role' was removed.";
			next;
		}
		unless (MMM::Monitor::Roles->instance()->can_handle($role->name, $host)) {
			WARN "Detected change in role definitions: Host '$host' can't handle role '$role' anymore.";
			next;
		}
		my $role_str = $role->to_string();
		$self->{roles}->{$role_str}						= {} unless (defined($self->{roles}->{$role_str}));
		$self->{roles}->{$role_str}->{$host}			= {} unless (defined($self->{roles}->{$role_str}->{$host}));
		$self->{roles}->{$role_str}->{$host}->{system}	= 1;
	}
}

sub determine_status() {
	my $self	= shift;
	my $roles	= MMM::Monitor::Roles->instance();

	my $is_manual = MMM::Monitor::Monitor->instance()->is_manual();

	my $conflict = 0;

    foreach my $host (keys(%{$main::config->{host}})) {

		# Figure out host state

		my $stored_state = 'UNKNOWN';
		my $agent_state  = 'UNKNOWN';
		my $state;

		$stored_state = $self->{hosts}->{$host}->{stored}->{state} if (defined($self->{hosts}->{$host}->{stored}->{state}));
		$agent_state  = $self->{hosts}->{$host}->{agent}->{state}  if (defined($self->{hosts}->{$host}->{agent}->{state} ));

		if (   $stored_state eq 'ADMIN_OFFLINE'     || $agent_state eq 'ADMIN_OFFLINE'    ) { $state = 'ADMIN_OFFLINE';     }
		elsif ($stored_state eq 'HARD_OFFLINE'      || $agent_state eq 'HARD_OFFLINE'     ) { $state = 'HARD_OFFLINE';      }
		elsif ($stored_state eq 'REPLICATION_FAIL'  || $agent_state eq 'REPLICATION_FAIL' ) { $state = 'REPLICATION_FAIL';  }
		elsif ($stored_state eq 'REPLICATION_DELAY' || $agent_state eq 'REPLICATION_DELAY') { $state = 'REPLICATION_DELAY'; }
		elsif ($stored_state eq 'ONLINE'            || $agent_state eq 'ONLINE'           ) { $state = 'ONLINE';            }
		else                                                                                { $state = 'AWAITING_RECOVERY'; }

		$self->{result}->{$host} = { state => $state, roles => [] };
	}

    foreach my $role_str (keys(%{$self->{roles}})) {
		my $role = MMM::Monitor::Role->from_string($role_str);
		next unless(defined($role));

		if ($roles->is_active_master_role($role->name)) {
			# active master role
			my $max        = 0;
			my $target     = undef;
			my $system_cnt = 0;
			foreach my $host (keys(%{$self->{roles}->{$role_str}})) {
				my $votes = 0;
				my $info  = $self->{roles}->{$role_str}->{$host};
				my $host_info = $self->{hosts}->{$host};

				# host is writable
				$votes += 4 if (defined($host_info->{system}->{writable}) && $host_info->{system}->{writable});

				# IP is configured
				if (defined($info->{system})) {
					$votes += 2;
					$system_cnt++;
				}

				$votes += 1 if (defined($info->{stored}));
				$votes += 1 if (defined($info->{agent}));

				foreach my $slave_host (keys(%{$self->{hosts}})) {
					my $slave_info = $self->{hosts}->{$slave_host};
					next if MMM::Monitor::Roles->instance()->is_master($slave_host);
					$votes++ if (defined($slave_info->{system}->{master}) && $slave_info->{system}->{master} eq $host);
				}
	
	
				my $state = $self->{result}->{$host}->{state};
				$votes = 0 if ($state eq 'ADMIN_OFFLINE');
				$votes = 0 if ($state eq 'HARD_OFFLINE' && !$is_manual);

				if ($votes > $max) {
					$target = $host;
					$max = $votes;
				}
			}
			if ($system_cnt > 1) {
				WARN "Role '$role_str' was configured on $system_cnt hosts during monitor startup.";
				$conflict = 1;
			}
			if (defined($target)) { 
				push (@{$self->{result}->{$target}->{roles}}, $role);
				my $state = $self->{result}->{$target}->{state};
				$self->{result}->{$target}->{state} = 'ONLINE' if (!$is_manual || $state eq 'REPLICATION_FAIL' || $state eq 'REPLICATION_DELAY');
			}
			next;
		}

		# Handle non-writer roles
		my $max        = 0;
		my $target     = undef;
		my $system_cnt = 0;
		foreach my $host (keys(%{$self->{roles}->{$role_str}})) {
			my $votes = 0;
			my $info  = $self->{roles}->{$role_str}->{$host};

			# IP is configured
			if (defined($info->{system})) {
				$votes += 4;
				$system_cnt++;
			}

			$votes += 2 if (defined($info->{stored}));
			$votes += 1 if (defined($info->{agent}));


			my $state = $self->{result}->{$host}->{state};
			if ($state eq 'ADMIN_OFFLINE' || (!$is_manual && $state ne 'ONLINE' && $state ne 'AWAITING_RECOVERY')) {
				$votes = 0;
			}
			if ($votes > $max) {
				$target    = $host;
				$max       = $votes;
			}
		}
		if ($system_cnt > 1) {
			WARN "Role '$role_str' was configured on $system_cnt hosts during monitor startup.";
		}
		if (defined($target)) { 
			push (@{$self->{result}->{$target}->{roles}}, $role);
			$self->{result}->{$target}->{state} = 'ONLINE' if ($self->{result}->{$target}->{state} eq 'AWAITING_RECOVERY');
		}
	}
	return $conflict;
}


sub to_string($) {
	my $self	= shift;
	my $ret = "Startup status:\n";
	$ret .= "\nRoles:\n";

    my $role_len = 4; # "Role"
    my $host_len = 6; # "Master"

    foreach my $role (keys(%{$main::config->{role}})) { $role_len = max($role_len, length $role) }
    foreach my $host (keys(%{$main::config->{host}})) { $host_len = max($host_len, length $host) }
	$role_len += 17; # "(999.999.999.999)"

	$ret .= sprintf("    %-*s  %-*s  %-6s  %-6s  %-5s\n", $role_len, 'Role', $host_len, 'Host', 'Stored', 'System', 'Agent');
	foreach my $role (keys(%{$self->{roles}})) {
		foreach my $host (keys(%{$self->{roles}->{$role}})) {
			my $info = $self->{roles}->{$role}->{$host};
			$ret .= sprintf("    %-*s  %-*s  %-6s  %-6s  %-5s\n", $role_len, $role, $host_len, $host,
				defined($info->{stored}) ? 'Yes'  : '-',
				defined($info->{system}) ? 'Yes'  : '-',
				defined($info->{agent})  ? 'Yes'  : '-'
			);
		}
	}

	$ret .= "\nHosts:\n";
	$ret .= sprintf("    %-*s  %-*s  %-8s  %-16s  %-16s\n", $host_len, 'Host', $host_len, 'Master', 'Writable', 'Stored state', 'Agent state');
	foreach my $host (keys(%{$self->{hosts}})) {
		my $info = $self->{hosts}->{$host};
		my $is_master = MMM::Monitor::Roles->instance()->is_master($host);
		$ret .= sprintf("    %-*s  %-*s  %-8s  %-16s  %-16s\n", $host_len, $host, $host_len,
			$is_master ? '-' : (defined($info->{system}->{master})   ? $info->{system}->{master}   : '?'),
			defined($info->{system}->{writable}) ? ($info->{system}->{writable} ? 'Yes' : 'No') : '?',
			defined($info->{stored}->{state})    ? $info->{stored}->{state} : '?',
			defined($info->{agent}->{state})     ? $info->{agent}->{state}  : '?',
		);
	}
	return $ret;
}

1;