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) {
2
Answers
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, followingshould 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
Note that it does not provide percentage implicitly, so you would need to follow
df
behaviorThe backticks interpolate variables, so
$5
will be interpolated by Perl. You can solve this by escaping the dollar sign with a backslash$5
, or useqx''
, 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.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:The other error:
…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.