This file is indexed.

/usr/share/perl5/MMM/Monitor/Checker/Checks.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package MMM::Monitor::Checker::Checks;

use strict;
use warnings FATAL => 'all';
use English qw(OSNAME EFFECTIVE_USER_ID);
use DBI;
use Net::Ping;
use POSIX ':signal_h';

our $VERSION = '0.01';

=head1 NAME

MMM::Monitor::Checker::Checks - functions for the B<mmm_mond> helper program B<checker>

=cut


my $fping_path;

=head1 FUNCTIONS

=over 4

=item ping($timeout, $host)

Check if the host $host is reachable.

=cut

sub ping($$) {
	my $timeout	= shift;
	my $host	= shift;

	my $ip = $main::config->{host}->{$host}->{ip};
	return "ERROR: Invalid host '$host'" unless ($ip);

	# if super user, use Net::Ping - it's faster
	if ($EFFECTIVE_USER_ID == 0) {
		my $p = Net::Ping->new('icmp');
		$p->hires();
		if ($p->ping($ip, 0.5)) {
			return 'OK';
		}
		return "ERROR: Could not ping $ip";
	}

	# Find appropriate fping version
	_determine_fping_path() unless defined($fping_path);
	unless (defined($fping_path)) {
		return "ERROR: fping is not functional - please, install your own version of fping on this server!";
	}

	my $res = `$fping_path -q -u -t 500 -C 1 $ip 2>&1`;
	return "ERROR: fping could not reach $ip" if ($res =~ /$ip.*\-$/);
	return 'OK';
}

=item ping_ip($timeout, $ip)

Check if the IP $ip is reachable.

=cut

sub ping_ip($$) {
	my $timeout	= shift;
	my $ip	= shift;

	# if super user, use Net::Ping - it's faster
	if ($EFFECTIVE_USER_ID == 0) {
		my $p = Net::Ping->new('icmp');
		$p->hires();
		if ($p->ping($ip, 0.5)) {
			return 'OK';
		}
		return "ERROR: Could not ping $ip";
	}

	# Find appropriate fping version
	_determine_fping_path() unless defined($fping_path);
	unless (defined($fping_path)) {
		return "ERROR: fping is not functional - please, install your own version of fping on this server!";
	}

	my $res = `$fping_path -q -u -t 500 -C 1 $ip 2>&1`;
	return "ERROR: fping could not reach $ip" if ($res =~ /$ip.*\-$/);
	return 'OK';
}


=item mysql($timeout, $host)

Check if the mysql server on host $host is reachable.

=cut

sub mysql($$) {
	my $timeout	= shift;
	my $host	= shift;

	my ($peer_host, $peer_port, $peer_user, $peer_password) = _get_connection_info($host);
	return "ERROR: Invalid host '$host'" unless ($peer_host);

	my $mask = POSIX::SigSet->new( SIGALRM );
	my $action = POSIX::SigAction->new(
		sub { die 'TIMEOUT'; },
		$mask,
	);
	my $oldaction = POSIX::SigAction->new();
	sigaction( SIGALRM, $action, $oldaction );

	my $res = eval {
		alarm($timeout + 1);
		
		# connect to server
		my $dsn = "DBI:mysql:host=$peer_host;port=$peer_port;mysql_connect_timeout=$timeout";
		my $dbh = DBI->connect($dsn, $peer_user, $peer_password, { PrintError => 0 });
		
		unless ($dbh) {
			alarm(0);
			# We don't want to trigger any action because of a simple 'too many connections' error
			return "UNKNOWN: Too many connections! " . $DBI::errstr if ($DBI::err == 1040);
			return "ERROR: Connect error (host = $peer_host:$peer_port, user = $peer_user)! " . $DBI::errstr;
		}
	
		# Check server (simple)
		my $res = $dbh->do('SELECT NOW()');
		unless ($res) {
			alarm(0);
			return 'ERROR: SQL Query Error: ' . $dbh->errstr;
		}

		alarm(1);
		$dbh->disconnect();
		$dbh = undef;

		alarm(0);
		return 0;
	};	
	alarm(0);

	return $res if ($res);
	return 'ERROR: Timeout' if ($@ =~ /^TIMEOUT/);
	return "UNKNOWN: Error occurred: $@" if $@;
	return 'OK';	

}


=item rep_backlog($timeout, $host)

Check the replication backlog on host $host.

=cut

sub rep_backlog($$) {
	my $timeout	= shift;
	my $host	= shift;

	my ($peer_host, $peer_port, $peer_user, $peer_password) = _get_connection_info($host);
	return "ERROR: Invalid host '$host'" unless ($peer_host);

	my $mask = POSIX::SigSet->new( SIGALRM );
	my $action = POSIX::SigAction->new(
		sub { die 'TIMEOUT'; },
		$mask,
	);
	my $oldaction = POSIX::SigAction->new();
	sigaction( SIGALRM, $action, $oldaction );

	my $res = eval {
		alarm($timeout + 1);
	
		# connect to server
		my $dsn = "DBI:mysql:host=$peer_host;port=$peer_port;mysql_connect_timeout=$timeout";
		my $dbh = DBI->connect($dsn, $peer_user, $peer_password, { PrintError => 0 });
		unless ($dbh) {
			alarm(0);
			return "UNKNOWN: Connect error (host = $peer_host:$peer_port, user = $peer_user)! " . $DBI::errstr;
		}
	
		# Check server (replication backlog)
		my $sth = $dbh->prepare('SHOW SLAVE STATUS');
		my $res = $sth->execute;

		if ($dbh->err) {
			alarm(1);
			my $ret = 'UNKNOWN: Unknown state. Execute error: ' . $dbh->errstr;
			$ret = "ERROR: The monitor user '$peer_user' doesn't have the required REPLICATION CLIENT privilege! " . $dbh->errstr if ($dbh->err == 1227);
			$sth->finish();
			$dbh->disconnect();
			$dbh = undef;
			alarm(0);
			return $ret;
		}

		unless ($res) {
			alarm(1);
			$sth->finish();
			$dbh->disconnect();
			$dbh = undef;
			alarm(0);
			return 'ERROR: Replication is not running';
		}
	
		my $status = $sth->fetchrow_hashref;
		alarm(1);
		$sth->finish();
		$dbh->disconnect();
		$dbh = undef;
		alarm(0);

		return 'ERROR: Replication is not set up' unless defined($status);

	
		# Check backlog size
		my $backlog = $status->{Seconds_Behind_Master};
		$backlog = 0 unless ($backlog);

		return 'OK: Backlog is null' if ($backlog == 0);
		return 'ERROR: Backlog is too big' if ($backlog > $main::check->{max_backlog});
		return 0;
	};
	alarm(0);

	return $res if ($res);
	return 'ERROR: Timeout' if ($@ =~ /^TIMEOUT/);
	return "UNKNOWN: Error occurred: $@" if $@;
	return 'OK';
}


=item rep_threads($timeout, $host)

Check if the mysql slave threads on host $host are running.

=cut

sub rep_threads($$) {
	my $timeout	= shift;
	my $host	= shift;

	my ($peer_host, $peer_port, $peer_user, $peer_password) = _get_connection_info($host);
	return "ERROR: Invalid host '$host'" unless ($peer_host);

	my $mask = POSIX::SigSet->new( SIGALRM );
	my $action = POSIX::SigAction->new(
		sub { die 'TIMEOUT'; },
		$mask,
	);
	my $oldaction = POSIX::SigAction->new();
	sigaction( SIGALRM, $action, $oldaction );

	my $res = eval {
		alarm($timeout + 1);
	
		# connect to server
		my $dsn = "DBI:mysql:host=$peer_host;port=$peer_port;mysql_connect_timeout=$timeout";
		my $dbh = DBI->connect($dsn, $peer_user, $peer_password, { PrintError => 0 });
		return "UNKNOWN: Connect error (host = $peer_host:$peer_port, user = $peer_user)! " . $DBI::errstr unless ($dbh);
	
		# Check server (replication backlog)
		my $sth = $dbh->prepare('SHOW SLAVE STATUS');
		my $res = $sth->execute;

		if ($dbh->err) {
			alarm(1);
			my $ret = 'UNKNOWN: Unknown state. Execute error: ' . $dbh->errstr;
			$ret = "ERROR: The monitor user '$peer_user' doesn't have the required REPLICATION CLIENT privilege! " . $dbh->errstr if ($dbh->err == 1227);
			$sth->finish();
			$dbh->disconnect();
			$dbh = undef;
			alarm(0);
			return $ret;
		}

		unless ($res) {
			alarm(1);
			$sth->finish();
			$dbh->disconnect();
			$dbh = undef;
			alarm(0);
			return 'ERROR: Replication is not running';
		}
	
		my $status = $sth->fetchrow_hashref;

		alarm(1);
		$sth->finish();
		$dbh->disconnect();
		$dbh = undef;
		alarm(0);

		return 'ERROR: Replication is not set up' unless defined($status);

		# Check peer replication state
		if ($status->{Slave_IO_Running} eq 'No' || $status->{Slave_SQL_Running} eq 'No') {
			return 'ERROR: Replication is broken';
		}
		return 0;
	};
	alarm(0);

	return $res if ($res);
	return 'ERROR: Timeout' if ($@ =~ /^TIMEOUT/);
	return "UNKNOWN: Error occurred: $@" if $@;
	return 'OK';
}


=item _get_connection_info($host)

Get connection info for host $host.

=cut

sub _get_connection_info($) {
	my $host = shift;
	return (
		$main::config->{host}->{$host}->{ip},
		$main::config->{host}->{$host}->{mysql_port},
		$main::config->{host}->{$host}->{monitor_user},
		$main::config->{host}->{$host}->{monitor_password}
	);
}


=item _determine_fping_path()

Determine path of fping binary

=cut

sub _determine_fping_path() {
	$fping_path = _determine_path('fping');
}

sub _determine_path($) {
	my $program = shift;
	my @paths = qw(/usr/sbin /sbin /usr/bin /bin);

	foreach my $path (@paths) {
		my $fullpath = "$path/$program";
		if (-f $fullpath && -x $fullpath) {
			return $fullpath;
		}
	}
	return undef;
}

1;