#!/usr/bin/perl

=head1 NAME

Script to call when there is a change in the management state

=head1 SYNOPSIS

bin/cluster/management_update <change_type> <vrrp_ip> <state> <priority>

=head1 DESCRIPTION

Made for keepalived so the pf services can be adjusted depending of the running node

=cut

use constant INSTALL_DIR => '/usr/local/pf';
use lib INSTALL_DIR . "/lib";

use pf::log;
use Net::Interface;
use Socket;

my $logger = get_logger();

$logger->info("Refreshing services for management mode");

my $vrrp_ip = $ARGV[1];
my $state = $ARGV[2];
my $priority = $ARGV[3];

if($state eq "MASTER"){
    $logger->info("Transition to master state, starting management services.");
    my $interface = interface_for_ip($vrrp_ip);
    `arping -c 4 -A -I $interface $vrrp_ip`;
}
elsif($state eq "SLAVE"){
    $logger->info("Transition to master state, stopping management services.");
}

=head2 interface_for_ip

Get the name of the interface on which the IP is binded

=cut

sub interface_for_ip {
    my ($ip) = @_;
    my @all_ifs = Net::Interface->interfaces();
    foreach my $inf (@all_ifs) {
        my @masks = $inf->netmask(AF_INET());
        my @addresses = $inf->address(AF_INET());
        for my $i (0 .. $#masks) {
            if (inet_ntoa($addresses[$i]) eq $ip) {
                my $name = $inf->name;
                $logger->debug("Found ifname $name for VRRP IP $ip");
                return $name;
            }
        }
    }
}

=back

=head1 AUTHOR

Inverse inc. <info@inverse.ca>

=head1 COPYRIGHT

Copyright (C) 2005-2021 Inverse inc.

=head1 LICENSE

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
USA.

=cut

1;

# vim: set shiftwidth=4:
# vim: set expandtab:
# vim: set backspace=indent,eol,start:

