/usr/share/perl5/MMM/Agent/Helpers/Network.pm is in mysql-mmm-agent 2.2.1-1.1.
This file is owned by root:root, with mode 0o664.
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 | package MMM::Agent::Helpers::Network;
use strict;
use warnings FATAL => 'all';
use English qw( OSNAME );
our $VERSION = '0.01';
if ($OSNAME eq 'linux' || $OSNAME eq 'freebsd') {
# these libs will always be loaded, use require and then import to avoid that
use Net::ARP;
use Time::HiRes qw( usleep );
}
=head1 NAME
MMM::Agent::Helpers::Network - network related functions for the B<mmm_agentd> helper programs
=cut
=head1 FUNCTIONS
=over 4
=item check_ip($if, $ip)
Check if the IP $ip is configured on interface $if. Returns 0 if not, 1 otherwise.
=cut
sub check_ip($$) {
my $if = shift;
my $ip = shift;
my $output;
if ($OSNAME eq 'linux') {
$output = `/sbin/ip addr show dev $if`;
_exit_error("Could not check if ip $ip is configured on $if: $output") if ($? >> 8 == 255);
}
elsif ($OSNAME eq 'solaris') {
# FIXME $if is not used here
$output = `/usr/sbin/ifconfig -a | grep inet`;
_exit_error("Could not check if ip $ip is configured on $if: $output") if ($? >> 8 == 255);
}
elsif ($OSNAME eq 'freebsd') {
$output = `/sbin/ifconfig $if | grep inet`;
_exit_error("Could not check if ip $ip is configured on $if: $output") if ($? >> 8 == 255);
}
else {
_exit_error("ERROR: Unsupported platform!");
}
return ($output =~ /\D+$ip\D+/) ? 1 : 0;
}
=item add_ip($if, $ip)
Add IP $ip to the interface $if.
=cut
sub add_ip($$) {
my $if = shift;
my $ip = shift;
my $output;
if ($OSNAME eq 'linux') {
$output = `/sbin/ip addr add $ip/32 dev $if`;
_exit_error("Could not configure ip $ip on interface $if: $output") if ($? >> 8 == 255);
}
elsif ($OSNAME eq 'solaris') {
$output = `/usr/sbin/ifconfig $if addif $ip`;
_exit_error("Could not configure ip $ip on interface $if: $output") if ($? >> 8 == 255);
my $logical_if = _solaris_find_logical_if($ip);
unless ($logical_if) {
_exit_error("ERROR: Can't find logical interface with IP = $ip");
}
$output = `/usr/sbin/ifconfig $logical_if up`;
_exit_error("Could not activate logical interface $logical_if with ip $ip on interface: $output") if ($? >> 8 == 255);
}
elsif ($OSNAME eq 'freebsd') {
$output = `/sbin/ifconfig $if inet $ip netmask 255.255.255.255 alias`;
_exit_error("Could not configure ip $ip on interface $if: $output") if ($? >> 8 == 255);
}
else {
_exit_error("ERROR: Unsupported platform!");
}
return check_ip($if, $ip);
}
=item clear_ip($if, $ip)
Remove the IP $ip from the interface $if.
=cut
sub clear_ip($$) {
my $if = shift;
my $ip = shift;
my $output;
if ($OSNAME eq 'linux') {
$output = `/sbin/ip addr del $ip/32 dev $if`;
_exit_error("Could not remove ip $ip from interface $if: $output") if ($? >> 8 == 255);
}
elsif ($OSNAME eq 'solaris') {
$output = `/usr/sbin/ifconfig $if removeif $ip`;
_exit_error("Could not remove ip $ip from interface $if: $output") if ($? >> 8 == 255);
}
elsif ($OSNAME eq 'freebsd') {
$output = `/sbin/ifconfig $if inet $ip -alias`;
_exit_error("Could not remove ip $ip from interface $if: $output") if ($? >> 8 == 255);
}
else {
exit(1);
}
}
=item send_arp($if, $ip)
Send arp requests for the IP $ip to the broadcast address on network interface $if.
=cut
sub send_arp($$) {
my $if = shift;
my $ip = shift;
if ($OSNAME eq 'linux' || $OSNAME eq 'freebsd') {
my $mac = '';
if ($Net::ARP::VERSION < 1.0) {
Net::ARP::get_mac($if, $mac);
}
else {
$mac = Net::ARP::get_mac($if);
}
return "ERROR: Couldn't get mac adress of interface $if" unless ($mac);
for (my $i = 0; $i < 5; $i++) {
Net::ARP::send_packet($if, $ip, $ip, $mac, 'ff:ff:ff:ff:ff:ff', 'request');
usleep(50);
Net::ARP::send_packet($if, $ip, $ip, $mac, 'ff:ff:ff:ff:ff:ff', 'reply');
usleep(50) if ($i < 4);
}
}
elsif ($OSNAME eq 'solaris') {
# Get params for send_arp
my $ipaddr = `/usr/sbin/ifconfig $if`;
# Get broadcast address and netmask
$ipaddr =~ /netmask\s*([0-9a-f]+)\s*broadcast\s*([\d\.]+)/i;
my $if_bcast = $1;
my $if_mask = $2;
`/bin/send_arp -i 100 -r 5 -p /tmp/send_arp $if $ip auto $if_bcast $if_mask`;
}
else {
_exit_error("ERROR: Unsupported platform!");
}
}
sub _exit_error {
my $msg = shift;
print "ERROR: $msg\n" if ($msg);
print "ERROR\n" unless ($msg);
exit(255);
}
#-------------------------------------------------------------------------------
sub _solaris_find_logical_if($) {
my $ip = shift;
my $ifconfig = `/usr/sbin/ifconfig -a`;
$ifconfig =~ s/\n/ /g;
while ($ifconfig =~ s/([a-z0-9\:]+)(\:\s+.*?)inet\s*([0-9\.]+)//) {
return $1 if ($3 eq $ip);
}
return undef;
}
1;
|