我正在發現語言 Perl。我嘗試創建一個腳本以集成到我的 Nagios 服務器中,但我遇到了兩個無法解決的錯誤。你能幫助我嗎?
錯誤如下:
在 check_disque.pl 第 53 行的連接 (.) 或字串中使用未初始化的值 $5。
引數 "/dev/mapper/centos-root 50G 5,5G 45G 11 /\n" 在 check_disque.pl 第 55 行的數字 lt (<) 中不是數字。
我的第 55 行:
$espace_utilise=`df -h / | awk 'FNR == 2 {print $5}' | sed 's/%//g'`;
第 56 行:
if ($espace_utilise < $warning) {
uj5u.com熱心網友回復:
$espace_utilise=`df -h / | awk 'FNR == 2 {print $5}' | sed 's/%//g'`;
# ^^--- here
反引號內插變數,因此$5將由 Perl 內插。\$5您可以通過使用反斜杠或 use轉義美元符號來解決此問題qx'',這與反引號相同,但單引號分隔符禁用插值。不過,這會導致您的 awk/sed 命令出現一些問題。這將需要更多的逃避。這是在 Perl 中使用 shell 命令是個壞主意的原因之一。
$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\'';
幸運的是,您可以直接執行df命令并使用 Perl 命令進行文本處理,這會容易得多。我會幫助你,但我不知道那個 awk 命令到底是做什么的。我猜:
$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
另一個錯誤:
引數 "/dev/mapper/centos-root 50G 5,5G 45G 11 /\n" 在 check_disque.pl 第 55 行的數字 lt (<) 中不是數字。我的第 55 行:
...只是因為第一個陳述句失敗了。$5即使Perl 發出警告,它也會進行插值,并變成空字串。因此,您的 awk 行只是說{ print },我認為這與列印整行相同。所以如果你修復了第一部分,你可以忽略它。
uj5u.com熱心網友回復:
我正在發現語言 PERL。
然后看看CPAN。在許多模塊中,有Filesys::DiskSpace哪些可以滿足您的需求。您需要先安裝它。為此,您需要學習如何從 CPAN安裝模塊,如下
cpan App::cpanminus
cpanm Filesys::DiskSpace
應該適用于您的情況。請注意,如果您之前沒有使用cpan它,它可能會詢問您是否希望它自動配置。點擊進入說是。
安裝后使用很簡單
use Filesys::DiskSpace;
($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;
請注意,它不會隱式提供百分比,因此您需要遵循df行為
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.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/453194.html
上一篇:從Windows批處理檔案執行Perl腳本,將Perl腳本中設定的變數傳遞回Windows批處理檔案
下一篇:Perl模塊安裝在錯誤的根目錄
