This file is indexed.

/usr/share/perl5/MMM/Monitor/Roles.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
package MMM::Monitor::Roles;
use base 'Class::Singleton';

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

our $VERSION = '0.01';

=head1 NAME

MMM::Monitor::Roles - holds information for all roles

=cut

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

	my $self = {};

	# create list of roles - each role will be orphaned by default
	foreach my $role (keys(%{$main::config->{role}})) {
		my $role_info = $main::config->{role}->{$role};
		my $ips = {};
		foreach my $ip (@{$role_info->{ips}}) {
			$ips->{$ip} = { 'assigned_to'	=> '' }
		}
		$self->{$role} = {
			mode	=> $role_info->{mode},
			hosts	=> $role_info->{hosts},
			ips		=> $ips
		};
		if ($role_info->{mode} eq 'exclusive' && $role_info->{prefer}) {
			$self->{$role}->{prefer} = $role_info->{prefer};
		}
	}

	return bless $self, $class; 
}


=head1 FUNCTIONS

=over 4

=item assign($role, $host)

Assign role $role to host $host

=cut

sub assign($$$) {
	my $self	= shift;
	my $role	= shift;
	my $host	= shift;

	LOGDIE "Can't assign role '$role' - no host given" unless (defined($host));

	# Check if the ip is still configured for this role
	unless (defined($self->{$role->name}->{ips}->{$role->ip})) {
		WARN sprintf("Detected configuration change: ip '%s' was removed from role '%s'", $role->ip, $role->name);
		return;
	}
	INFO sprintf("Adding role '%s' with ip '%s' to host '%s'", $role->name, $role->ip, $host);

	$self->{$role->name}->{ips}->{$role->ip}->{assigned_to} = $host;
}

=item get_role_hosts($role)

Get all hosts which may handle role $role

=cut

sub get_role_hosts($$) {
	my $self	= shift;
	my $role	= shift;

	return () unless (defined($role));

	my $role_info = $self->{$role};
	return () unless $role_info;

	return @{$role_info->{hosts}};
}


=item get_host_roles($host)

Get all roles assigned to host $host

=cut

sub get_host_roles($$) {
	my $self	= shift;
	my $host	= shift;

	return () unless (defined($host));

	my @roles	= ();
	foreach my $role (keys(%$self)) {
		my $role_info = $self->{$role};
		foreach my $ip (keys(%{$role_info->{ips}})) {
			my $ip_info = $role_info->{ips}->{$ip};
			next unless ($ip_info->{assigned_to} eq $host);
			push(@roles, new MMM::Monitor::Role::(name => $role, ip => $ip));
		}
	}
	return @roles;
}


=item host_has_roles($host)

Check whether there are roles assigned to host $host

=cut

sub host_has_roles($$) {
	my $self	= shift;
	my $host	= shift;

	return 0 unless (defined($host));

	foreach my $role (keys(%$self)) {
		my $role_info = $self->{$role};
		foreach my $ip (keys(%{$role_info->{ips}})) {
			my $ip_info = $role_info->{ips}->{$ip};
			return 1 if ($ip_info->{assigned_to} eq $host);
		}
	}
	return 0;
}


=item count_host_roles($host)

Count all roles assigned to host $host

=cut

sub count_host_roles($$) {
	my $self	= shift;
	my $host	= shift;

	return 0 unless (defined($host));

	my $cnt	= 0;
	foreach my $role (keys(%$self)) {
		my $role_info = $self->{$role};
		foreach my $ip (keys(%{$role_info->{ips}})) {
			my $ip_info = $role_info->{ips}->{$ip};
			next if ($ip_info->{assigned_to} ne $host);
			$cnt++;
			$cnt -= 0.5 if ($role eq $main::config->{active_master_role});
		}
	}
	return $cnt;
}


=item get_active_master

Get the host with the active master-role

=cut

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

	my $role = $self->{$main::config->{active_master_role}};
	return '' unless $role;

	my @ips = keys( %{ $role->{ips} } );
	return $role->{ips}->{$ips[0]}->{assigned_to};
}


=item get_passive_master

Get the passive master

=cut

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

	my $role = $self->{$main::config->{active_master_role}};
	my $active_master = $self->get_active_master();
	return '' unless $role;
	return '' unless $active_master;

	foreach my $host ( @{ $role->{hosts} } ) {
		return $host if ($host ne $active_master);
	}
	return '';
}


=item get_first_master

Get the first master

=cut

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

	my $role = $self->{$main::config->{active_master_role}};
	return '' unless $role;
	return '' unless $role->{hosts}[0];
	return $role->{hosts}[0];
}


=item get_second_master

Get the second master

=cut

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

	my $role = $self->{$main::config->{active_master_role}};
	return '' unless $role;
	return '' unless $role->{hosts}[1];
	return $role->{hosts}[1];
}


=item get_master_hosts

Get the hosts which can handle the active master-role

=cut

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

	my $role = $self->{$main::config->{active_master_role}};
	return '' unless $role;
	return $self->{$role}->{hosts};
}


=item get_exclusive_role_owner($role)

Get the host which has the exclusive role $role assigned

=cut

sub get_exclusive_role_owner($$) {
	my $self	= shift;
	my $role	= shift;

	my $role_info = $self->{$role};
	return '' unless $role_info;

	my @ips = keys( %{ $role_info->{ips} } );
	return $role_info->{ips}->{$ips[0]}->{assigned_to};
}


=item get_exclusive_role_ip($role)

Get the ip of an exclusive role $role

=cut

sub get_exclusive_role_ip($$) {
	my $self	= shift;
	my $role	= shift;

	my $role_info = $self->{$role};
	return undef unless $role_info;

	my @ips = keys( %{ $role_info->{ips} } );
	return $ips[0];
}


=item assigned_to_preferred_host($role)

Check if role is assigned to preferred host

=cut

sub assigned_to_preferred_host($$) {
	my $self	= shift;
	my $role	= shift;

	my $role_info = $self->{$role};
	return undef unless $role_info;
	return undef unless ($role_info->{prefer});

	my @ips = keys( %{ $role_info->{ips} } );
	return ($role_info->{ips}->{$ips[0]}->{assigned_to} eq $role_info->{prefer});
	
}


=item clear_roles($host)

Remove all roles from host $host.

=cut

sub clear_roles($$) {
	my $self	= shift;
	my $host	= shift;

	INFO "Removing all roles from host '$host':";

	my $orphaned_master_role = 0;
	foreach my $role (keys(%$self)) {
		my $role_info = $self->{$role};
		foreach my $ip (keys(%{$role_info->{ips}})) {
			my $ip_info = $role_info->{ips}->{$ip};
			next unless ($ip_info->{assigned_to} eq $host);
			INFO "    Removed role '$role($ip)' from host '$host'";
			$ip_info->{assigned_to} = '';
			$orphaned_master_role = 1 if ($role eq $main::config->{active_master_role});
		}
	}
	return $orphaned_master_role;
}


=item clear_balanced_role($host, $role)

Remove balanced role $role from host $host.

=cut

sub clear_balanced_role($$$) {
	my $self	= shift;
	my $host	= shift;
	my $role	= shift;

	INFO "Removing balanced role $role from host '$host':";

	my $role_info = $self->{$role};
	return 0 unless $role_info;
	my $cnt = 0;
	next unless ($role_info->{mode} eq 'balanced');
	foreach my $ip (keys(%{$role_info->{ips}})) {
		my $ip_info = $role_info->{ips}->{$ip};
		next unless ($ip_info->{assigned_to} eq $host);
		$cnt++;
		INFO "    Removed role '$role($ip)' from host '$host'";
		$ip_info->{assigned_to} = '';
	}
	return $cnt;
}


=item find_eligible_host($role)

find host which can take over the role $role

=cut

sub find_eligible_host($$) {
	my $self	= shift;
	my $role	= shift;

	my $min_host	= '';
	my $min_count	= 0;

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

	# Maybe role has a preferred hosts
	if ($self->{$role}->{prefer}) {
		my $host = $self->{$role}->{prefer};
 		if ($agents->{$host}->state eq 'ONLINE' && !$agents->{$host}->agent_down) {
			return $host;
		}
	}

	# Use host with fewest roles
	foreach my $host ( @{ $self->{$role}->{hosts} } ) {
		next unless ($agents->{$host}->state eq 'ONLINE');
		next if ($agents->{$host}->agent_down);
		my $cnt = $self->count_host_roles($host);
		next unless ($cnt < $min_count || $min_host eq '');
		$min_host	= $host;
		$min_count	= $cnt;
	}
	
	return $min_host;
}


=item find_eligible_hosts($role)

find all hosts which can take over the role $role.

=cut

sub find_eligible_hosts($$) {
	my $self	= shift;
	my $role	= shift;

	my $hosts	= {};

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

	foreach my $host ( @{ $self->{$role}->{hosts} } ) {
		next unless ($agents->{$host}->state eq 'ONLINE');
		next if ($agents->{$host}->agent_down);
		my $cnt = $self->count_host_roles($host);
		$hosts->{$host} = $cnt;
	}
	
	return $hosts;
}


=item process_orphans

Find orphaned roles and assign them to a host if possible.

=cut

sub process_orphans($$) {
	my $self	= shift;
	my $mode	= shift;
	
	foreach my $role (keys(%$self)) {
		my $role_info = $self->{$role};
		next if ($mode && $role_info->{mode} ne $mode);

		foreach my $ip (keys(%{$role_info->{ips}})) {
			my $ip_info = $role_info->{ips}->{$ip};
			next unless ($ip_info->{assigned_to} eq '');

			# Find host which can take over the role - skip if none found
			my $host = $self->find_eligible_host($role);
			last unless ($host);
			
			# Assign this ip to host
			$ip_info->{assigned_to} = $host;
			INFO "Orphaned role '$role($ip)' has been assigned to '$host'";
		}
	}
}


=item obey_preferences

Obey preferences by moving roles to preferred hosts

=cut
sub obey_preferences($) {
	my $self	= shift;

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

	foreach my $role (keys(%$self)) {
		my $role_info = $self->{$role};

		next unless ($role_info->{prefer});

		my $host = $role_info->{prefer};

		next unless ($agents->{$host}->state eq 'ONLINE');
		next if ($agents->{$host}->agent_down);

		my @ips			= keys( %{ $role_info->{ips} } );
		my $ip			= $ips[0];
		my $ip_info		= $role_info->{ips}->{$ip};
		my $old_host	= $ip_info->{assigned_to};

		next if ($old_host eq $host);

		$ip_info->{assigned_to} = $host;
		INFO "Moving role '$role($ip)' from host '$old_host' to preferred host '$host'";
	}
}


=item get_preference_info

Get information about roles with preferred hosts

=cut
sub get_preference_info($) {
	my $self	= shift;

	my $ret = '';

	foreach my $role (keys(%$self)) {
		my $role_info = $self->{$role};
		next unless ($role_info->{prefer});

		my $host       = $role_info->{prefer};
		my $other_host = $self->get_exclusive_role_owner($role);
		if ($host eq $other_host) {
			$ret .= "# Role $role is assigned to it's preferred host $host.\n";
		}
		elsif($other_host ne '') {
			$ret .= "# Role $role has $host configured as it's preferred host but is assigned to $other_host at the moment.\n";
		}
		else {
			$ret .= "# Role $role has $host configured as it's preferred host.\n";
		}
	}
	return $ret;
}


=item balance

Balance roles with mode 'balanced'

=cut

sub balance($) {
	my $self	= shift;
	
	foreach my $role (keys(%$self)) {
		my $role_info = $self->{$role};

		next unless ($role_info->{mode} eq 'balanced');

		my $hosts = $self->find_eligible_hosts($role);
		next if (scalar(keys(%$hosts)) < 2);

		while (1) {
			my $max_host = '';
			my $min_host = '';
			foreach my $host (keys(%$hosts)) {
				$max_host = $host if ($max_host eq '' || $hosts->{$host} > $hosts->{$max_host});
				$min_host = $host if ($min_host eq '' || $hosts->{$host} < $hosts->{$min_host});
			}
			
			if ($hosts->{$max_host} - $hosts->{$min_host} <= 1) {
				last;
			}
			
			$self->move_one_ip($role, $max_host, $min_host);
			$hosts->{$max_host}--;
			$hosts->{$min_host}++;
		}
	}
}


=item move_one_ip($role, $host1, $host2)

Move one IP of role $role from $host1 to $host2.

=cut

sub move_one_ip($$$$) {
	my $self	= shift;
	my $role	= shift;
	my $host1	= shift;
	my $host2	= shift;
	
	foreach my $ip (keys(%{$self->{$role}->{ips}})) {
		my $ip_info = $self->{$role}->{ips}->{$ip};
		next unless ($ip_info->{assigned_to} eq $host1);

		INFO "Moving role '$role($ip)' from host '$host1' to host '$host2'";
		$ip_info->{assigned_to} = $host2;
		return 1;
	}

	# No ip was moved
	return 0;
}


=item find_by_ip($ip)

Find name of role with IP $ip.

=cut

sub find_by_ip($$) {
	my $self	= shift;
	my $ip		= shift;

	foreach my $role (keys(%$self)) {
		return $role if (defined($self->{$role}->{ips}->{$ip}));
	}
	
	return undef;
}


=item set_role($role, $ip, $host)

Set role $role with IP $ip to host $host.

NOTE: No checks are done. Caller should assure that:
Role is valid, IP is valid, Host is valid, Host can handle role

=cut
sub set_role($$$$) {
	my $self	= shift;
	my $role	= shift;
	my $ip		= shift;
	my $host	= shift;

	$self->{$role}->{ips}->{$ip}->{assigned_to} = $host;
}


=item exists($role)

Check if role $role exists.

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


=item exists_ip($role, $ip)

Check if role $role with IP $ip exists.

=cut

sub exists_ip($$$) {
	my $self	= shift;
	my $role	= shift;
	my $ip		= shift;
	return 0 unless defined($self->{$role});
	return defined($self->{$role}->{ips}->{$ip});
}


=item is_exclusive($role)

Determine whether given role is an exclusive role.

=cut

sub is_exclusive($$) {
	my $self	= shift;
	my $role	= shift;
	return 0 unless defined($self->{$role});
	return ($self->{$role}->{mode} eq 'exclusive');
}


=item get_valid_hosts($role)

Get all valid hosts for role $role.

=cut

sub get_valid_hosts($$) {
	my $self	= shift;
	my $role	= shift;
	return () unless defined($self->{$role});
	return $self->{$role}->{hosts};
}


=item can_handle($role, $host)

Check if host $host can handle role $role.

=cut

sub can_handle($$$) {
	my $self	= shift;
	my $role	= shift;
	my $host	= shift;
	return 0 unless defined($self->{$role});
	return grep({$_ eq $host} @{$self->{$role}->{hosts}});
}


=item is_master($host)

Check if host $host can handle role $role.

=cut

sub is_master($$) {
	my $self	= shift;
	my $host	= shift;
	my $role = $self->{$main::config->{active_master_role}};
	return 0 unless defined($role);
	return grep({$_ eq $host} @{$role->{hosts}});
}


=item is_active_master_role($role)

Check whether $role is the active master role.

=cut

sub is_active_master_role($$) {
	my $self	= shift;
	my $role	= shift;
	
	return ($role eq $main::config->{active_master_role});
}

1;