perlawksednagiosnagiosxi

Use of uninitialized value $5 and argument isn't numeric in numeric lt (<)


I'm discovering the language Perl. I try to create a script to integrate inside my Nagios server, but i got two errors that I'm not able to resolve. Can you help me?

The errors are the following:

Use of uninitialized value $5 in concatenation (.) or string at check_disque.pl line 53.

Argument "/dev/mapper/centos-root 50G 5,5G 45G 11 /\n" isn't numeric in numeric lt (<) at check_disque.pl line 55.

My line 55 :

$espace_utilise=`df -h / | awk 'FNR == 2 {print $5}' | sed 's/%//g'`;

And the line 56 :

if ($espace_utilise < $warning) {

Solution

  • $espace_utilise=`df -h / | awk 'FNR == 2 {print $5}' | sed 's/%//g'`;
    #                                               ^^--- here 
    

    The backticks interpolate variables, so $5 will be interpolated by Perl. You can solve this by escaping the dollar sign with a backslash \$5, or use qx'', which does the same as backticks, but the single quote delimiters disables interpolation. It will cause some issues with your awk/sed commands, though. Which will require more escaping. This is one reason using shell commands inside Perl is a bad idea.

    $espace_utilise=`df -h / | awk 'FNR == 2 {print \$5}' | sed 's/%//g'`;
    $espace_utilise=qx'df -h / | awk \'FNR == 2 {print $5}\' | sed \'s/%//g\'';
    

    Luckily for you, you can just do the df command directly and use the text processing with Perl commands, which will be a lot easier. I would help you, but I don't know exactly what that awk command does. I would guess:

    $espace_utilise=`df -h /`;                # get the line
    my $df = (split ' ', $espace_utilise)[4]; # get the 5th field
    $df =~ s/%//g;                            # remove %. Can also use tr/%d//d
    

    The other error:

    Argument "/dev/mapper/centos-root 50G 5,5G 45G 11 /\n" isn't numeric in numeric lt (<) at check_disque.pl line 55. My line 55 :

    ...is just because the first statement failed. Perl interpolates $5 even though it warns about it, and it becomes the empty string instead. So your awk line just says { print }, which I assume is the same as printing the whole line. So if you fix the first part, you can ignore this.