客户端修改:


1、进入/usr/lib64/nagios/plugins目录下并更改脚本权限

cd/usr/lib64/nagios/pluginsvimcheck_memory

#!/usr/bin/perl-w#$Id:check_memory22002-02-2806:42:51Zegalstad$#Originalscriptstolenfrom:#check_memoryCopyright(C)2000DanLarsson#hackedby#JustinEllison##Thisprogramisfreesoftware;youcanredistributeitand/or#modifyitunderthetermsoftheGNUGeneralPublicLicense#aspublishedbytheFreeSoftwareFoundation;eitherversion2#oftheLicense,or(atyouroption)anylaterversion.##Thisprogramisdistributedinthehopethatitwillbeuseful,#butWITHOUTANYWARRANTY;withouteventheimpliedwarranty#ofMERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe#GNUGeneralPublicLicenseformoredetails.##youshouldhavereceivedacopyoftheGNUGeneralPublicLicense#alongwiththisprogram(orwithNagios);ifnot,writetothe#FreeSoftwareFoundation,Inc.,59TemplePlace-Suite330,#Boston,MA02111-1307,USA#TellPerlwhatweneedtouseusestrict;useGetopt::Std;#TODO-ConverttoNagios::Plugin#TODO-Useanalarm#PredefinedexitcodesforNagiosusevarsqw($opt_c$opt_f$opt_u$opt_w$opt_C$opt_v%exit_codes);%exit_codes=('UNKNOWN',-1,'OK',0,'WARNING',1,'CRITICAL',2,);#Getourvariables,doourchecking:init();#Getthenumbers:my($free_memory_kb,$used_memory_kb,$caches_kb)=get_memory_info();print"$free_memory_kbFree\n$used_memory_kbUsed\n$caches_kbCache\n"if($opt_v);if($opt_C){#Dowecountcachesasfree?$used_memory_kb-=$caches_kb;$free_memory_kb+=$caches_kb;}#RoundtothenearestKB$free_memory_kb=sprintf('%d',$free_memory_kb);$used_memory_kb=sprintf('%d',$used_memory_kb);$caches_kb=sprintf('%d',$caches_kb);#TellNagioswhatwecameupwithtell_nagios($used_memory_kb,$free_memory_kb,$caches_kb);subtell_nagios{my($used,$free,$caches)=@_;#CalculateTotalMemorymy$total=$free+$used;print"$totalTotal\n"if($opt_v);my$perfdata="|TOTAL=${total}KB;;;;USED=${used}KB;;;;FREE=${free}KB;;;;CACHES=${caches}KB;;;;";if($opt_f){my$percent=sprintf"%.1f",($free/$total*100);if($percent<=$opt_c){finish("CRITICAL-$percent%($freekB)free!$perfdata",$exit_codes{'CRITICAL'});}elsif($percent<=$opt_w){finish("WARNING-$percent%($freekB)free!$perfdata",$exit_codes{'WARNING'});}else{finish("OK-$percent%($freekB)free.$perfdata",$exit_codes{'OK'});}}elsif($opt_u){my$percent=sprintf"%.1f",($used/$total*100);if($percent>=$opt_c){finish("CRITICAL-$percent%($usedkB)used!$perfdata",$exit_codes{'CRITICAL'});}elsif($percent>=$opt_w){finish("WARNING-$percent%($usedkB)used!$perfdata",$exit_codes{'WARNING'});}else{finish("OK-$percent%($usedkB)used.$perfdata",$exit_codes{'OK'});}}}#Showusagesubusage(){print"\ncheck_memoryv1.0-NagiosPlugin\n\n";print"usage:\n";print"check_memory--w-c\n\n";print"options:\n";print"-fCheckFREEmemory\n";print"-uCheckUSEDmemory\n";print"-CCountOScachesasFREEmemory\n";print"-wPERCENTPercentfree/usedwhentowarn\n";print"-cPERCENTPercentfree/usedwhencritical\n";print"\nCopyright(C)2000DanLarsson\n";print"check_memorycomeswithabsolutelyNOWARRANTYeitherimpliedorexplicit\n";print"Thisprogramislicensedunderthetermsofthe\n";print"GNUGeneralPublicLicense(checksourcecodefordetails)\n";exit$exit_codes{'UNKNOWN'};}subget_memory_info{my$used_memory_kb=0;my$free_memory_kb=0;my$total_memory_kb=0;my$caches_kb=0;my$uname;if(-e'/usr/bin/uname'){$uname=`/usr/bin/uname-a`;}elsif(-e'/bin/uname'){$uname=`/bin/uname-a`;}else{die"Unabletofindunamein/usr/binor/bin!\n";}print"unamereturns$uname"if($opt_v);if($uname=~/Linux/){my@meminfo=`/bin/cat/proc/meminfo`;foreach(@meminfo){chomp;if(/^Mem(Total|Free):\s+(\d+)kB/){my$counter_name=$1;if($counter_nameeq'Free'){$free_memory_kb=$2;}elsif($counter_nameeq'Total'){$total_memory_kb=$2;}}elsif(/^(Buffers|Cached):\s+(\d+)kB/){$caches_kb+=$2;}}$used_memory_kb=$total_memory_kb-$free_memory_kb;}elsif($uname=~/SunOS/){eval"useSun::Solaris::Kstat";if($@){#Kstatnotavailableif($opt_C){print"Youcan'treportonSolariscacheswithoutSun::Solaris::Kstatavailable!\n";exit$exit_codes{UNKNOWN};}my@vmstat=`/usr/bin/vmstat12`;my$line;foreach(@vmstat){chomp;$line=$_;}$free_memory_kb=(split(//,$line))[5]/1024;my@prtconf=`/usr/sbin/prtconf`;foreach(@prtconf){if(/^Memorysize:(\d+)Megabytes/){$total_memory_kb=$1*1024;}}$used_memory_kb=$total_memory_kb-$free_memory_kb;}else{#Wehavekstatmy$kstat=Sun::Solaris::Kstat->new();my$phys_pages=${kstat}->{unix}->{0}->{system_pages}->{physmem};my$free_pages=${kstat}->{unix}->{0}->{system_pages}->{freemem};#WeprobablyshouldaccountforUFScachinghere,butit'sunclear#tomehowtodetermineUFS'scachesize.There'sinode_cache,#andmaybethephysmemvariableinthesystem_pagesmodule??#Intherealworld,itlookstobesosmallasnottoreallymatter,#sowedon'tgrabit.Ifsomeonecangivemecodethatdoesthis,#I'dbegladtoputitin.my$arc_size=(exists${kstat}->{zfs}&&${kstat}->{zfs}->{0}->{arcstats}->{size})?${kstat}->{zfs}->{0}->{arcstats}->{size}/1024:0;$caches_kb+=$arc_size;my$pagesize=`pagesize`;$total_memory_kb=$phys_pages*$pagesize/1024;$free_memory_kb=$free_pages*$pagesize/1024;$used_memory_kb=$total_memory_kb-$free_memory_kb;}}elsif($uname=~/AIX/){my@meminfo=`/usr/bin/vmstat-v`;foreach(@meminfo){chomp;if(/^\s*([0-9.]+)\s+(.*)/){my$counter_name=$2;if($counter_nameeq'memorypages'){$total_memory_kb=$1*4;}if($counter_nameeq'freepages'){$free_memory_kb=$1*4;}if($counter_nameeq'filepages'){$caches_kb=$1*4;}}}$used_memory_kb=$total_memory_kb-$free_memory_kb;}else{if($opt_C){print"Youcan'treporton$unamecaches!\n";exit$exit_codes{UNKNOWN};}my$command_line=`vmstat|tail-1|awk'{print\$4,\$5}'`;chomp$command_line;my@memlist=split(//,$command_line);#Definethecalculatingscalars$used_memory_kb=$memlist[0]/1024;$free_memory_kb=$memlist[1]/1024;$total_memory_kb=$used_memory_kb+$free_memory_kb;}return($free_memory_kb,$used_memory_kb,$caches_kb);}subinit{#Gettheoptionsif($#ARGVle0){&usage;}else{getopts('c:fuCvw:');}#Shortcircuittheswitchesif(!$opt_wor$opt_w==0or!$opt_cor$opt_c==0){print"***YoumustdefineWARNandCRITICALlevels!\n";&usage;}elsif(!$opt_fand!$opt_u){print"***YoumustselecttomonitoreitherUSEDorFREEmemory!\n";&usage;}#Checkiflevelsaresaneif($opt_w<=$opt_cand$opt_f){print"***WARNlevelmustnotbelessthanCRITICALwhencheckingFREEmemory!\n";&usage;}elsif($opt_w>=$opt_cand$opt_u){print"***WARNlevelmustnotbegreaterthanCRITICALwhencheckingUSEDmemory!\n";&usage;}}subfinish{my($msg,$state)=@_;print"$msg\n";exit$state;}

chmod755check_memory


2、修改nrpe配置文件

vim/etc/nagios/nrpe.cfg###添加以下内容command[check_free_mem]=/usr/lib64/nagios/plugins/check_memory-f-w10-c5

3、重启nrpe服务

/etc/init.d/nreprestart

服务端的配置
1、修改主机配置文件

cd/etc/nagios/conf.d/

添加以下内容

vimvsftp_222.cfgdefineservice{usegeneric-servicehost_namevsftp_222service_descriptioncheck_memorycheck_commandcheck_nrpe!check_memoryregister1}

2、检查配置文件正确性并重载配置文件

nagios-v/etc/nagios/nagios.cfg/etc/init.d/nagiosrestart

3、配置完毕,过一会监控内存使用情况就出来了!