#!/usr/bin/perl
#
# css_ccw_device_availability
#   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;


#
# Global variables
#

# Exception IDs
our $LNXHC_EXCEPTION_UNUSABLE_DEVICE = "unusable_device";

# Path to the file containing data for sysinfo item 'ccw_devices_availability'.
our $sysinfo_ccw_devices_availability = $ENV{"LNXHC_SYSINFO_ccw_devices_availability"};


#
# Code entry
#

local *SI_FILE;
my %device_status;
my $line;

open(SI_FILE, "<", $sysinfo_ccw_devices_availability) or
	die("css_ccw_device_availability: Could not read file ".
	    "'$sysinfo_ccw_devices_availability': $!\n");

# Creating the list of devices, which are having availability other than 'good'
foreach $line ( <SI_FILE> ) {
	if ($line =~ /^([^:]+):(.*)$/) {
		my $device = $1;
		my $status = $2;
		if ( $status ne "good" ) {
			$device_status{$device}=$status;
		}
	}
}

close(SI_FILE);

# When we have some list of devices with 'availability' other than 'good'
if (%device_status) {
	lnxhc_exception($LNXHC_EXCEPTION_UNUSABLE_DEVICE);
	my $count = 1;

	# For summary want to show only 4 devices
	foreach my $device (sort (keys %device_status)) {
		if ($count > 4) {
			lnxhc_exception_var("devices_list", "...");
			last;
		}
		lnxhc_exception_var("devices_list", $device);
		$count++;
	}

	# Printing all in the explanation section
	lnxhc_exception_var("all_devices", sprintf("#%-10s   %s", "BUS ID", "AVAILABILTY"));
	foreach my $device (sort (keys %device_status)) {
		my $status = $device_status{$device};
		lnxhc_exception_var("all_devices", sprintf("#%-10s   %s", $device, $status));
	}
}


exit(0);
