#!/usr/bin/perl
#
# net_bond_ineffective
#   Health check program for the Linux Health Checker
#
# Copyright IBM Corp. 2012
#
# Author(s): Nageswara R Sastry <nasastry@in.ibm.com>
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors: See file CONTRIBUTORS which is part of this package
#
#
use strict;
use warnings;

use lib $ENV{"LNXHC_LIBDIR"};
use LNXHC::Check::Base;

# Exception IDs
my $LNXHC_EXCEPTION_FOUND_SINGLE_NW_DEV = "single_slave";

# Path to the file containing data for sysinfo item 'bonding_info'.
my $sysinfo_bonding_info = $ENV{"LNXHC_SYSINFO_bonding_info"};

# Verifying bonding setup configured or not
if (-z "$sysinfo_bonding_info") {
	lnxhc_fail_dep("There is no bonding setup");
}

my $handle;
my (%interface_chpid, %bond_slaves, %exception_data);


# Reading the data from the sysinfo item 'bonding_info'
open ($handle, "<", $sysinfo_bonding_info) or
	die ("net_bond_ineffective: couldn't open file: ".
		"'$sysinfo_bonding_info' : $!\n");
while (<$handle>){
	chomp;
	# Example line: bond0=eth0
	my ($bond_dev, $bond_slaves) = split /=/;
	push (@{$bond_slaves{$bond_dev}}, $bond_slaves);
}
close($handle);

foreach (sort(keys(%bond_slaves))) {
	if (scalar(@{$bond_slaves{$_}}) < 2) {
		$exception_data{$_} = $bond_slaves{$_};
	}
}
# Raising the exception basing on the data
if (%exception_data) {
        my @summary = sort(keys(%exception_data));
	lnxhc_exception($LNXHC_EXCEPTION_FOUND_SINGLE_NW_DEV);

	lnxhc_exception_var_list("bond_devices", \@summary, ", ");
	lnxhc_exception_var("bond_slaves",
		sprintf("#%-20s     %-10s", "Bonding Interface",
			"Bonding Slave interface"));

	foreach (sort(keys(%exception_data))) {
		lnxhc_exception_var("bond_slaves",
			sprintf("#%-20s     %-10s", $_,
				join(", ", @{$exception_data{$_}})));
	}
}

exit(0);
