Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check for empty array

Status
Not open for further replies.

twantrd

Technical User
Feb 13, 2005
26
0
0
US
Hi,

I'm writing a simple check for a jbod device. If all the drives come back with a status sign of "HEALTHY" then I print an "OK" statement. If it doesn't come with that status, then I print "CRITICAL". Very simple. My script prints CRITICAL when the condition is met but it doesn't print anything when all drives are healthy.

Code:
#!/usr/bin/perl

# Read the contents of the file that has the status of all drives
my $file="/tmp/drive_stats";

# Save contents of file into an array and if it doesn't match HEALTHY, do something
open (FILE, $file) || die "Cannot open file $!";
@list=<FILE>;
close FILE;
@health=grep(!/HEALTHY/,@list);
        # Array is not empty
        if (@health) {
                print "CRITICAL: Problem with drive: @health";
        }
        else {
                print "OK: All drives are healthy";
        }

THanks,

-twantrd
 
Is this a question?

Code:
if (scalar(@array) == 0) { # empty

-------------
Cuvou.com | The NEW Kirsle.net
 
Sorry, yes, this is a question. I'm wondering why my statement doesn't print anything when all drives are healthy. If everything is healthy, it should print "OK".

Kirsle,
I tried your if statement and the results are the same.

The contents of the file, /tmp/drive_stats looks like this:
Code:
Bay 1        Lnum 0       [HEALTHY]      SN:jfdasjdfsla      /dev/da12

-twantrd
 
but it prints nothing???? That seems very strange considering it is a if/else statement
 
how about this
Code:
#!/usr/bin/perl

# Read the contents of the file that has the status of all drives
my $file="/temp/drivestats.txt";

# Save contents of file into an array and if it doesn't match HEALTHY, do something
open (FILE, $file) || die "Cannot open file $!";
@list=<FILE>;
chomp @list;
close FILE;
@health=grep(!/HEALTHY/,@list);
        # Array is not empty
        if (scalar(@health) == 0) {
		print "OK: All drives are healthy";                
        }
        else {
                print "CRITICAL: Problem with drive: @health";
        }
[code]
 
note that I changed your $file so you might want to correct that.

Tested fine on my end.
 
thanks for the input, but same result. Weird isn't it?

-twantrd
 
can you print @health before the if statement and show it to us?
 
Ok, I think I found the problem. The script works fine on a linux server but not on this storage device. Perl is messed up somehow on this unit. Thanks for the help everyone!

-twantrd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top