/usr/share/perl5/MMM/Monitor/Commands.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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | package MMM::Monitor::Commands;
use strict;
use warnings FATAL => 'all';
use Log::Log4perl qw(:easy);
use List::Util qw(max);
use threads;
use threads::shared;
use Log::Log4perl::DateFormat;
use MMM::Common::Socket;
use MMM::Monitor::Agents;
use MMM::Monitor::ChecksStatus;
use MMM::Monitor::Monitor;
use MMM::Monitor::Roles;
our $VERSION = '0.01';
sub main($$) {
my $queue_in = shift;
my $queue_out = shift;
my $socket = MMM::Common::Socket::create_listener($main::config->{monitor}->{ip}, $main::config->{monitor}->{port});
while (!$main::shutdown) {
DEBUG 'Listener: Waiting for connection...';
my $client = $socket->accept();
next unless ($client);
DEBUG 'Listener: Connect!';
while (my $cmd = <$client>) {
chomp($cmd);
last if ($cmd eq 'quit');
$queue_out->enqueue($cmd);
my $res;
until ($res) {
lock($queue_in);
cond_timedwait($queue_in, time() + 1);
$res = $queue_in->dequeue_nb();
return 0 if ($main::shutdown);
}
print $client $res;
return 0 if ($main::shutdown);
}
close($client);
DEBUG 'Listener: Disconnect!';
}
}
sub ping() {
return 'OK: Pinged successfully!';
}
sub show() {
my $agents = MMM::Monitor::Agents->instance();
my $monitor = MMM::Monitor::Monitor->instance();
my $roles = MMM::Monitor::Roles->instance();
my $ret = '';
if ($monitor->is_passive) {
$ret .= "--- Monitor is in PASSIVE MODE ---\n";
$ret .= sprintf("Cause: %s\n", $monitor->passive_info);
$ret =~ s/^/# /mg;
}
$ret .= $agents->get_status_info(1);
$ret .= $roles->get_preference_info();
return $ret;
}
sub checks {
my $host = shift || 'all';
my $check = shift || 'all';
my $checks = MMM::Monitor::ChecksStatus->instance();
my $ret = '';
my $dateformat = Log::Log4perl::DateFormat->new('yyyy/MM/dd HH:mm:ss');
my @valid_checks = qw(ping mysql rep_threads rep_backlog);
return "ERROR: Unknown check '$check'!" unless ($check eq 'all' || grep(/^$check$/, @valid_checks));
if ($host ne 'all') {
return "ERROR: Unknown host name '$host'!" unless (defined($main::config->{host}->{$host}));
if ($check ne 'all') {
return sprintf("%s %s [last change: %s] %s",
$host,
$check,
$dateformat->format($checks->last_change($host, $check)),
$checks->message($host, $check)
);
}
foreach $check (@valid_checks) {
$ret .= sprintf("%s %-11s [last change: %s] %s\n",
$host,
$check,
$dateformat->format($checks->last_change($host, $check)),
$checks->message($host, $check)
);
}
return $ret;
}
my $len = 0;
foreach my $host (keys(%{$main::config->{host}})) { $len = max($len, length $host) }
if ($check ne 'all') {
foreach my $host (keys(%{$main::config->{host}})) {
$ret .= sprintf("%*s %s [last change: %s] %s\n",
$len * -1,
$host,
$check,
$dateformat->format($checks->last_change($host, $check)),
$checks->message($host, $check)
);
}
return $ret;
}
foreach my $host (keys(%{$main::config->{host}})) {
foreach $check (@valid_checks) {
$ret .= sprintf("%*s %-11s [last change: %s] %s\n",
$len * -1,
$host,
$check,
$dateformat->format($checks->last_change($host, $check)),
$checks->message($host, $check)
);
}
}
return $ret;
}
sub set_online($) {
my $host = shift;
my $agents = MMM::Monitor::Agents->instance();
return "ERROR: Unknown host name '$host'!" unless (defined($main::config->{host}->{$host}));
my $host_state = $agents->state($host);
return "OK: This host is already ONLINE. Skipping command." if ($host_state eq 'ONLINE');
unless ($host_state eq 'ADMIN_OFFLINE' || $host_state eq 'AWAITING_RECOVERY') {
return "ERROR: Host '$host' is '$host_state' at the moment. It can't be switched to ONLINE.";
}
my $checks = MMM::Monitor::ChecksStatus->instance();
if ((!$checks->ping($host) || !$checks->mysql($host))) {
return "ERROR: Checks ping and/or mysql are not ok for host '$host'. It can't be switched to ONLINE.";
}
# Check peer replication state
if ($main::config->{host}->{$host}->{peer}) {
my $peer = $main::config->{host}->{$host}->{peer};
if ($agents->state($peer) eq 'ONLINE' && (!$checks->rep_threads($peer) || !$checks->rep_backlog($peer))) {
return "ERROR: Some replication checks failed on peer '$peer'. We can't set '$host' online now. Please, wait some time.";
}
}
my $agent = MMM::Monitor::Agents->instance()->get($host);
if (!$agent->cmd_ping()) {
return "ERROR: Can't reach agent daemon on '$host'! Can't switch its state!";
}
FATAL "Admin changed state of '$host' from $host_state to ONLINE";
$agents->set_state($host, 'ONLINE');
$agent->flapping(0);
MMM::Monitor::Monitor->instance()->send_agent_status($host);
return "OK: State of '$host' changed to ONLINE. Now you can wait some time and check its new roles!";
}
sub set_offline($) {
my $host = shift;
my $agents = MMM::Monitor::Agents->instance();
return "ERROR: Unknown host name '$host'!" unless (defined($main::config->{host}->{$host}));
my $host_state = $agents->state($host);
return "OK: This host is already ADMIN_OFFLINE. Skipping command." if ($host_state eq 'ADMIN_OFFLINE');
unless ($host_state eq 'ONLINE' || $host_state eq 'REPLICATION_FAIL' || $host_state eq 'REPLICATION_DELAY') {
return "ERROR: Host '$host' is '$host_state' at the moment. It can't be switched to ADMIN_OFFLINE.";
}
my $agent = MMM::Monitor::Agents->instance()->get($host);
return "ERROR: Can't reach agent daemon on '$host'! Can't switch its state!" unless ($agent->cmd_ping());
FATAL "Admin changed state of '$host' from $host_state to ADMIN_OFFLINE";
$agents->set_state($host, 'ADMIN_OFFLINE');
MMM::Monitor::Roles->instance()->clear_roles($host);
MMM::Monitor::Monitor->instance()->send_agent_status($host);
return "OK: State of '$host' changed to ADMIN_OFFLINE. Now you can wait some time and check all roles!";
}
sub set_ip($$) {
my $ip = shift;
my $host = shift;
return "ERROR: This command is only allowed in passive mode" unless (MMM::Monitor::Monitor->instance()->is_passive);
my $agents = MMM::Monitor::Agents->instance();
my $roles = MMM::Monitor::Roles->instance();
my $role = $roles->find_by_ip($ip);
return "ERROR: Unknown ip '$ip'!" unless (defined($role));
return "ERROR: Unknown host name '$host'!" unless ($agents->exists($host));
unless ($roles->can_handle($role, $host)) {
return "ERROR: Host '$host' can't handle role '$role'. Following hosts could: " . join(', ', @{ $roles->get_valid_hosts($role) });
}
my $host_state = $agents->state($host);
unless ($host_state eq 'ONLINE') {
return "ERROR: Host '$host' is '$host_state' at the moment. Can't move role with ip '$ip' there.";
}
FATAL "Admin set role '$role($ip)' to host '$host'";
$roles->set_role($role, $ip, $host);
# Determine all roles and propagate them to agent objects.
foreach my $one_host (@{ $roles->get_valid_hosts($role) }) {
my $agent = $agents->get($one_host);
my @agent_roles = sort($roles->get_host_roles($one_host));
$agent->roles(\@agent_roles);
}
return "OK: Set role '$role($ip)' to host '$host'.";
}
sub move_role($$) {
my $role = shift;
my $host = shift;
my $monitor = MMM::Monitor::Monitor->instance();
return "ERROR: This command is not allowed in passive mode" if ($monitor->is_passive);
my $agents = MMM::Monitor::Agents->instance();
my $roles = MMM::Monitor::Roles->instance();
return "ERROR: Unknown role name '$role'!" unless ($roles->exists($role));
return "ERROR: Unknown host name '$host'!" unless ($agents->exists($host));
unless ($roles->is_exclusive($role)) {
$roles->clear_balanced_role($host, $role);
return "OK: Balanced role $role has been removed from host '$host'. Now you can wait some time and check new roles info!";
}
my $host_state = $agents->state($host);
return "ERROR: Can't move role to host with state $host_state." unless ($host_state eq 'ONLINE');
unless ($roles->can_handle($role, $host)) {
return "ERROR: Host '$host' can't handle role '$role'. Only following hosts could: " . join(', ', @{ $roles->get_valid_hosts($role) });
}
my $old_owner = $roles->get_exclusive_role_owner($role);
return "OK: Role is on '$host' already. Skipping command." if ($old_owner eq $host);
my $agent = MMM::Monitor::Agents->instance()->get($host);
return "ERROR: Can't reach agent daemon on '$host'! Can't move roles there!" unless ($agent->cmd_ping());
if ($monitor->is_active && $roles->assigned_to_preferred_host($role)) {
return "ERROR: Role '$role' is assigned to preferred host '$old_owner'. Can't move it!";
}
my $ip = $roles->get_exclusive_role_ip($role);
return "Error: Role $role has no IP." unless ($ip);
FATAL "Admin moved role '$role' from '$old_owner' to '$host'";
# Assign role to new host
$roles->set_role($role, $ip, $host);
# Notify old host (if is_active_master_role($role) this will make the host non writable)
$monitor->send_agent_status($old_owner);
# Notify slaves (this will make them switch the master)
$monitor->notify_slaves($host) if ($roles->is_active_master_role($role));
# Notify new host (if is_active_master_role($role) this will make the host writable)
$monitor->send_agent_status($host);
return "OK: Role '$role' has been moved from '$old_owner' to '$host'. Now you can wait some time and check new roles info!";
}
sub forced_move_role($$) {
my $role = shift;
my $host = shift;
my $monitor = MMM::Monitor::Monitor->instance();
return "ERROR: This command is not allowed in passive mode" if (MMM::Monitor::Monitor->instance()->is_passive);
my $agents = MMM::Monitor::Agents->instance();
my $roles = MMM::Monitor::Roles->instance();
my $checks = MMM::Monitor::ChecksStatus->instance();
return "ERROR: Unknown role name '$role'!" unless ($roles->exists($role));
return "ERROR: Unknown host name '$host'!" unless ($agents->exists($host));
return "ERROR: move_role --forced may be used for the active master role only!" unless ($roles->is_active_master_role($role));
my $host_state = $agents->state($host);
unless ($host_state eq 'REPLICATION_FAIL' || $host_state eq 'REPLICATION_DELAY') {
return "ERROR: Can't force move of role to host with state $host_state.";
}
unless ($roles->can_handle($role, $host)) {
return "ERROR: Host '$host' can't handle role '$role'. Only following hosts could: " . join(', ', @{ $roles->get_valid_hosts($role) });
}
my $old_owner = $roles->get_exclusive_role_owner($role);
return "OK: Role is on '$host' already. Skipping command." if ($old_owner eq $host);
my $agent = $agents->get($host);
my $old_agent = $agents->get($old_owner);
return "ERROR: Can't reach agent daemon on '$host'! Can't move roles there!" unless ($agent->cmd_ping());
my $ip = $roles->get_exclusive_role_ip($role);
return "Error: Role $role has no IP." unless ($ip);
FATAL "Admin forced move of role '$role' from '$old_owner' to '$host'";
# Assign role to new host
$roles->set_role($role, $ip, $host);
FATAL "State of host '$host' changed from $host_state to ONLINE (because of move_role --force)";
$agent->state('ONLINE');
if (!$checks->rep_threads($old_owner)) {
FATAL "State of host '$old_owner' changed from ONLINE to REPLICATION_FAIL (because of move_role --force)";
$old_agent->state('REPLICATION_FAIL');
$roles->clear_roles($old_owner) if ($monitor->is_active);
}
elsif (!$checks->rep_backlog($old_owner)) {
FATAL "State of host '$old_owner' changed from ONLINE to REPLICATION_DELAY (because of move_role --force)";
$old_agent->state('REPLICATION_DELAY');
$roles->clear_roles($old_owner) if ($monitor->is_active);
}
# Notify old host (this will make the host non writable)
MMM::Monitor::Monitor->instance()->send_agent_status($old_owner);
# Notify slaves (this will make them switch the master)
MMM::Monitor::Monitor->instance()->notify_slaves($host);
# Notify new host (this will make the host writable)
MMM::Monitor::Monitor->instance()->send_agent_status($host);
return "OK: Role '$role' has been moved from '$old_owner' to '$host' enforcedly. Now you can wait some time and check new roles info!";
}
=item mode
Get information about current mode (active, manual or passive)
=cut
sub mode() {
my $monitor = MMM::Monitor::Monitor->instance();
return $monitor->get_mode_string();
}
=item set_active
Switch to active mode.
=cut
sub set_active() {
my $monitor = MMM::Monitor::Monitor->instance();
return 'OK: Already in active mode.' if ($monitor->is_active);
my $old_mode = $monitor->get_mode_string();
INFO "Admin changed mode from '$old_mode' to 'ACTIVE'";
if ($monitor->is_passive) {
$monitor->set_active(); # so that we can send status to agents
$monitor->cleanup_and_send_status();
$monitor->passive_info('');
}
elsif ($monitor->is_manual) {
# remove all roles from hosts which are not ONLINE
my $roles = MMM::Monitor::Roles->instance();
my $agents = MMM::Monitor::Agents->instance();
my $checks = MMM::Monitor::ChecksStatus->instance();
foreach my $host (keys(%{$main::config->{host}})) {
my $host_state = $agents->state($host);
next if ($host_state eq 'ONLINE' || $roles->get_host_roles($host) == 0);
my $agent = $agents->get($host);
$roles->clear_roles($host);
my $ret = $monitor->send_agent_status($host);
# next if ($host_state eq 'REPLICATION_FAIL');
# next if ($host_state eq 'REPLICATION_DELAY');
# NOTE host_state should never be ADMIN_OFFLINE at this point
if (!$ret) {
ERROR sprintf("Can't send offline status notification to '%s' - killing it!", $host);
$monitor->_kill_host($host, $checks->ping($host));
}
}
}
$monitor->set_active();
return 'OK: Switched into active mode.';
}
=item set_manual
Switch to manual mode.
=cut
sub set_manual() {
my $monitor = MMM::Monitor::Monitor->instance();
return 'OK: Already in manual mode.' if ($monitor->is_manual);
my $old_mode = $monitor->get_mode_string();
INFO "Admin changed mode from '$old_mode' to 'MANUAL'";
if ($monitor->is_passive) {
$monitor->set_manual(); # so that we can send status to agents
$monitor->cleanup_and_send_status();
$monitor->passive_info('');
}
$monitor->set_manual();
return 'OK: Switched into manual mode.';
}
=item set_passive
Switch to passive mode.
=cut
sub set_passive() {
my $monitor = MMM::Monitor::Monitor->instance();
return 'OK: Already in passive mode.' if ($monitor->is_passive);
my $old_mode = $monitor->get_mode_string();
INFO "Admin changed mode from '$old_mode' to 'PASSIVE'";
$monitor->set_passive();
$monitor->passive_info('Admin switched to passive mode.');
return 'OK: Switched into passive mode.';
}
sub help() {
return: "Valid commands are:
help - show this message
ping - ping monitor
show - show status
checks [<host>|all [<check>|all]] - show checks status
set_online <host> - set host <host> online
set_offline <host> - set host <host> offline
mode - print current mode.
set_active - switch into active mode.
set_manual - switch into manual mode.
set_passive - switch into passive mode.
move_role [--force] <role> <host> - move exclusive role <role> to host <host>
(Only use --force if you know what you are doing!)
set_ip <ip> <host> - set role with ip <ip> to host <host>
";
}
1;
|