2331321 发表于 2016-11-8 08:58:30

nagios-创建内存监控脚本及监控内存

客户端修改:


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

1
<span style="font-family:'黑体', SimHei;font-size:14px;">#!/usr/bin/perl -w<br># $Id: check_memory 2 2002-02-28 06:42:51Z egalstad $<br># Original script stolen from:<br># check_memory Copyright (C) 2000 Dan Larsson<br># hacked by<br># Justin Ellison<br>#<br># This program is free software; you can redistribute it and/or<br># modify it under the terms of the GNU General Public License<br># as published by the Free Software Foundation; either version 2<br># of the License, or (at your option) any later version.<br>#<br># This program is distributed in the hope that it will be useful,<br># but WITHOUT ANY WARRANTY; without even the implied warranty<br># of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the<br># GNU General Public License for more details.<br>#<br># you should have received a copy of the GNU General Public License<br># along with this program (or with Nagios);if not, write to the<br># Free Software Foundation, Inc., 59 Temple Place - Suite 330,<br># Boston, MA 02111-1307, USA<br># Tell Perl what we need to use<br>use strict;<br>use Getopt::Std;<br>#TODO - Convert to Nagios::Plugin<br>#TODO - Use an alarm<br># Predefined exit codes for Nagios<br>use vars qw($opt_c $opt_f $opt_u $opt_w $opt_C $opt_v %exit_codes);<br>%exit_codes   = ('UNKNOWN' ,-1,<br>      'OK'      , 0,<br>               'WARNING' , 1,<br>               'CRITICAL', 2,<br>               );<br># Get our variables, do our checking:<br>init();<br># Get the numbers:<br>my ($free_memory_kb,$used_memory_kb,$caches_kb) = get_memory_info();<br>print "$free_memory_kb Free\\n$used_memory_kb Used\\n$caches_kb Cache\\n" if ($opt_v);<br>if ($opt_C) { #Do we count caches as free?<br>    $used_memory_kb -= $caches_kb;<br>    $free_memory_kb += $caches_kb;<br>}<br># Round to the nearest KB<br>$free_memory_kb = sprintf('%d',$free_memory_kb);<br>$used_memory_kb = sprintf('%d',$used_memory_kb);<br>$caches_kb = sprintf('%d',$caches_kb);<br># Tell Nagios what we came up with<br>tell_nagios($used_memory_kb,$free_memory_kb,$caches_kb);<br>sub tell_nagios {<br>    my ($used,$free,$caches) = @_;<br>    # Calculate Total Memory<br>    my $total = $free + $used;<br>    print "$total Total\\n" if ($opt_v);<br>    my $perfdata = "|TOTAL=${total}KB;;;; USED=${used}KB;;;; FREE=${free}KB;;;; CACHES=${caches}KB;;;;";<br>    if ($opt_f) {<br>      my $percent    = sprintf "%.1f", ($free / $total * 100);<br>      if ($percent <= $opt_c) {<br>          finish("CRITICAL - $percent% ($free kB) free!$perfdata",$exit_codes{'CRITICAL'});<br>      }<br>      elsif ($percent = $opt_c) {<br>          finish("CRITICAL - $percent% ($used kB) used!$perfdata",$exit_codes{'CRITICAL'});<br>      }<br>      elsif ($percent >= $opt_w) {<br>          finish("WARNING - $percent% ($used kB) used!$perfdata",$exit_codes{'WARNING'});<br>      }<br>      else {<br>          finish("OK - $percent% ($used kB) used.$perfdata",$exit_codes{'OK'});<br>      }<br>    }<br>}<br># Show usage<br>sub usage() {<br>print "\\ncheck_memory v1.0 - Nagios Plugin\\n\\n";<br>print "usage:\\n";<br>print " check_memory - -w -c \\n\\n";<br>print "options:\\n";<br>print " -f         Check FREE memory\\n";<br>print " -u         Check USED memory\\n";<br>print " -C         Count OS caches as FREE memory\\n";<br>print " -w PERCENT   Percent free/used when to warn\\n";<br>print " -c PERCENT   Percent free/used when critical\\n";<br>print "\\nCopyright (C) 2000 Dan Larsson<br>\\n";<br>print "check_memory comes with absolutely NO WARRANTY either implied or explicit\\n";<br>print "This program is licensed under the terms of the\\n";<br>print "GNU General Public License (check source code for details)\\n";<br>exit $exit_codes{'UNKNOWN'};<br>}<br>sub get_memory_info {<br>    my $used_memory_kb= 0;<br>    my $free_memory_kb= 0;<br>    my $total_memory_kb = 0;<br>    my $caches_kb       = 0;<br>    my $uname;<br>    if ( -e '/usr/bin/uname') {<br>      $uname = `/usr/bin/uname -a`;<br>    }<br>    elsif ( -e '/bin/uname') {<br>      $uname = `/bin/uname -a`;<br>    }<br>    else {<br>      die "Unable to find uname in /usr/bin or /bin!\\n";<br>    }<br>    print "uname returns $uname" if ($opt_v);<br>    if ( $uname =~ /Linux/ ) {<br>      my @meminfo = `/bin/cat /proc/meminfo`;<br>      foreach (@meminfo) {<br>            chomp;<br>            if (/^Mem(Total|Free):\\s+(\\d+) kB/) {<br>                my $counter_name = $1;<br>                if ($counter_name eq 'Free') {<br>                  $free_memory_kb = $2;<br>                }<br>                elsif ($counter_name eq 'Total') {<br>                  $total_memory_kb = $2;<br>                }<br>            }<br>            elsif (/^(Buffers|Cached):\\s+(\\d+) kB/) {<br>                $caches_kb += $2;<br>            }<br>      }<br>      $used_memory_kb = $total_memory_kb - $free_memory_kb;<br>    }<br>    elsif ( $uname =~ /SunOS/ ) {<br>      eval "use Sun::Solaris::Kstat";<br>      if ($@) { #Kstat not available<br>            if ($opt_C) {<br>                print "You can't report on Solaris caches without Sun::Solaris::Kstat available!\\n";<br>                exit $exit_codes{UNKNOWN};<br>            }<br>            my @vmstat = `/usr/bin/vmstat 1 2`;<br>            my $line;<br>            foreach (@vmstat) {<br>            chomp;<br>            $line = $_;<br>            }<br>            $free_memory_kb = (split(/ /,$line)) / 1024;<br>            my @prtconf = `/usr/sbin/prtconf`;<br>            foreach (@prtconf) {<br>                if (/^Memory size: (\\d+) Megabytes/) {<br>                  $total_memory_kb = $1 * 1024;<br>                }<br>            }<br>            $used_memory_kb = $total_memory_kb - $free_memory_kb;<br>      }<br>      else { # We have kstat<br>            my $kstat = Sun::Solaris::Kstat->new();<br>            my $phys_pages = ${kstat}->{unix}->{0}->{system_pages}->{physmem};<br>            my $free_pages = ${kstat}->{unix}->{0}->{system_pages}->{freemem};<br>            # We probably should account for UFS caching here, but it's unclear<br>            # to me how to determine UFS's cache size.There's inode_cache,<br>            # and maybe the physmem variable in the system_pages module??<br>            # In the real world, it looks to be so small as not to really matter,<br>            # so we don't grab it.If someone can give me code that does this,<br>            # I'd be glad to put it in.<br>            my $arc_size = (exists ${kstat}->{zfs} && ${kstat}->{zfs}->{0}->{arcstats}->{size}) ?<br>               ${kstat}->{zfs}->{0}->{arcstats}->{size} / 1024<br>               : 0;<br>            $caches_kb += $arc_size;<br>            my $pagesize = `pagesize`;<br>            $total_memory_kb = $phys_pages * $pagesize / 1024;<br>            $free_memory_kb = $free_pages * $pagesize / 1024;<br>            $used_memory_kb = $total_memory_kb - $free_memory_kb;<br>      }<br>    }<br>    elsif ( $uname =~ /AIX/ ) {<br>      my @meminfo = `/usr/bin/vmstat -v`;<br>      foreach (@meminfo) {<br>            chomp;<br>            if (/^\\s*(+)\\s+(.*)/) {<br>                my $counter_name = $2;<br>                if ($counter_name eq 'memory pages') {<br>                  $total_memory_kb = $1*4;<br>                }<br>                if ($counter_name eq 'free pages') {<br>                  $free_memory_kb = $1*4;<br>                }<br>                if ($counter_name eq 'file pages') {<br>                  $caches_kb = $1*4;<br>                }<br>            }<br>      }<br>      $used_memory_kb = $total_memory_kb - $free_memory_kb;<br>    }<br>    else {<br>      if ($opt_C) {<br>            print "You can't report on $uname caches!\\n";<br>            exit $exit_codes{UNKNOWN};<br>      }<br>    my $command_line = `vmstat | tail -1 | awk '{print \\$4,\\$5}'`;<br>    chomp $command_line;<br>      my @memlist      = split(/ /, $command_line);<br>      # Define the calculating scalars<br>      $used_memory_kb= $memlist/1024;<br>      $free_memory_kb = $memlist/1024;<br>      $total_memory_kb = $used_memory_kb + $free_memory_kb;<br>    }<br>    return ($free_memory_kb,$used_memory_kb,$caches_kb);<br>}<br>sub init {<br>    # Get the options<br>    if ($#ARGV le 0) {<br>      &usage;<br>    }<br>    else {<br>      getopts('c:fuCvw:');<br>    }<br>    # Shortcircuit the switches<br>    if (!$opt_w or $opt_w == 0 or !$opt_c or $opt_c == 0) {<br>      print "*** You must define WARN and CRITICAL levels!\\n";<br>      &usage;<br>    }<br>    elsif (!$opt_f and !$opt_u) {<br>      print "*** You must select to monitor either USED or FREE memory!\\n";<br>      &usage;<br>    }<br>    # Check if levels are sane<br>    if ($opt_w = $opt_c and $opt_u) {<br>      print "*** WARN level must not be greater than CRITICAL when checking USED memory!\\n";<br>      &usage;<br>    }<br>}<br>sub finish {<br>    my ($msg,$state) = @_;<br>    print "$msg\\n";<br>    exit $state;<br>}<br></span>




chmod 755 check_memory
2、修改nrpe配置文件
vim /etc/nagios/nrpe.cfg
添加以下内容
command=/usr/lib64/nagios/plugins/check_memory -f -w 10 -c 5
3、重启nrpe服务
/etc/init.d/nrep restart
服务端的配置
1、修改主机配置文件
cd /etc/nagios/conf.d/
添加以下内容


vim vsftp_222.cfg

define service{
      use                            generic-service
      host_name                     vsftp_222
      service_description            check_memory
      check_command               check_nrpe!check_memory
      register                            1
      }
2、检查配置文件正确性并重载配置文件


nagios -v /etc/nagios/nagios.cfg
/etc/init.d/nagios restart
3、配置完毕,过一会监控内存使用情况就出来了!

页: [1]
查看完整版本: nagios-创建内存监控脚本及监控内存