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

Time:03-15

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) {

CodePudding user response:

$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.

CodePudding user response:

I'm discovering the language PERL.

Then take a look at CPAN. Among many modules there is Filesys::DiskSpace which does what you want. You need to install it first. In order to do that you need to learn how to INSTALL modules from CPAN, following

cpan App::cpanminus
cpanm Filesys::DiskSpace

should work in your case. Note that if you did not use cpan earlier it might ask you if you want it to autoconfigure itself. Hit enter to say yes.

After installation usage is as simple as

use Filesys::DiskSpace;
($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;

Note that it does not provide percentage implicitly, so you would need to follow df behavior

      The percentage of the normally available space that is currently allocated  to  all
      files on the file system. This shall be calculated using the fraction:

      <space used>/( <space used>  <space free>)

   expressed as a percentage. This percentage may be greater than 100 if <space free> is less
   than zero. The percentage value shall  be  expressed  as  a  positive  integer,  with  any
   fractional result causing it to be rounded to the next highest integer.
  • Related