nagios監控linux主機記憶體指令碼執行個體【營運備必】

來源:互聯網
上載者:User

說明

工作包括兩部分監控端(一二三)和被監控端(四)

一、nrpe.cfg中添加指令碼

nrpe.cfg中添加命令索引

command[check_used_mem]=/usr/local/nagios/libexec/check_used_mem.sh 80 90

說明:實際記憶體使用量量超過80%警告;超過90%嚴重警告

二、添加具體指令碼

參考指令碼check_used_mem.sh


warn=$1
critical=$2
all=`free | sed -n '2p' | awk '{print $2}'`
used=`free | sed -n '3p' | awk '{print $3}'`
let "c=$used*100/$all"
if [[ $c -lt $warn ]]
then
    echo "used mem/total < $warn%  [used:$used, total:$total:$all]"
    exit 0
elif [[ $c -lt $critical ]]
then
    echo "used mem/total ≥$warn%  [used:$used, total:$total:$all]"
    exit 1
else
    echo "used mem/total ≥$critical%  [used:$used, total:$total:$all]"
    exit 2
fi



解釋:

1. 1、2分別指輸入的第一、二個參數,比如:

del.sh指令碼內容為

#/bin/bash
echo $1

    sh del.sh  a                        #第一個參數是a
    chmod a+x  ./del.sh a         #第一個參數是a

2. free用來查看記憶體的使用方式

[root@xen_202_12 /]# free -m
             total       used       free     shared    buffers     cached
Mem:          3072       2459        612          0        207       1803
-/+ buffers/cache:        447       2624
Swap:         1913          0       1913

    total 記憶體總數: 3072
    used 已經使用的記憶體數: 2459
    free 閒置記憶體數: 612
    shared 當前已經廢棄不用,總是0
    buffers: Buffer Cache記憶體數: 13220
    cached: Page Cache記憶體數: 2720160

關係:total = used + free

第3行:
-/+ buffers/cache的意思:
-buffers/cache 的記憶體數: 447 (等於第1行的 used - buffers - cached)
+buffers/cache 的記憶體數: 2624 (等於第1行的 free + buffers + cached)
注:此處的記憶體數在用上面式子計算後,在大小上有一點點出入(還不知道是什麼原因)。

可見-buffers/cache反映的是被程式實實在在吃掉的記憶體,而+buffers/cache反映的是可以挪用的記憶體總數。

3. sed -n '2p'指的是尋找第二行

4. awk '{print $2} 指的是第二列,其中預設以空格分隔。可以利用-F指定分隔字元

$echo 1b234b56b7 | awk -F 'b' '{print $2}'
234

5. 實用[[]],<用來比較字串;-lt用來比較數字

三、重啟nrpe

/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

四、在監控端添加監控項


define service {
        use                             generic-service
        host_name                       100.61.73.2,100,61,73.3
        service_description             記憶體
        check_command                   check_nrpe!check_used_mem
        notifications_enabled           1
        }

        
在nagios中使用python指令碼監控linux主機

在被監控端192.168.5.110
1.先把getload.py放到/usr/local/nagios/libexec內
[root@nhserver1 ~]# vim /usr/local/nagios/libexec/getload.py
#! /usr/bin/env python
import os,sys
(d1,d2,d3) = os.getloadavg()
if d1 >= 5.0:
        print "GETLOADAVG CRITICAL: Load average is %.2f" % (d1)
        sys.exit(2)
elif d1 >= 2.0:
        print "GETLOADAVG WARNING: Load average is %.2f" %(d1)
else:
        print "GETLOADAVG OK: Load average is %.2f" %(d1)

[root@nhserver1 libexec]# chmod a+x getload.py
[root@nhserver1 libexec]# chown nagios:nagios getload.py


2. 在nrpe內加入自訂的命令
[root@nhserver1 libexec]# vim /usr/local/nagios/etc/nrpe.cfg
 command[nh_check_getload]=/usr/local/nagios/libexec/getload.py

------------------------------------------------------------------------------------------

在nagios服務端測試192.168.5.10

[root@nhserver2 libexec]# /usr/local/nagios/libexec/check_nrpe -H 192.168.5.110 -c nh_check_getload
GETLOADAVG OK: Load average is 0.06

在服務端測試192.168.5.10的nagios中加入自訂指令碼
[root@nhserver2 ~]# cd /usr/local/nagios/etc/objects
[root@nhserver2 objects]# vim hosts_192.168.5.110.cfg
define host{
        use                     linux-server
        host_name               192.168.5.110
        alias                   192.168.5.110
        address                 192.168.5.110
        }

define hostgroup{
        hostgroup_name          nh_linuxs
        alias                   nh_linuxs
        members                 192.168.5.110
        }

define service{
        use                     local-service
        host_name               192.168.5.110
        service_description     check-host-alive
        check_command           check-host-alive
        max_check_attempts      5
        normal_check_interval   3
        retry_check_interval    2
        check_period            24x7
        notification_interval   10
        notification_period     24x7
     }

define service{
        use                     local-service
        host_name               192.168.5.110
        service_description     SSH
        check_command           check_ssh
        max_check_attempts      5
        normal_check_interval   3
        retry_check_interval    2
        check_period            24x7
        notification_interval   10
        notification_period     24x7
        }

define service{
        use                     local-service
        host_name               192.168.5.110
        service_description     check_nrpe_check_users
        check_command           check_nrpe!nh_check_users
        max_check_attempts      5
        normal_check_interval   3
        retry_check_interval    2
        check_period            24x7
        notification_interval   10
        notification_period     24x7
        }
define service{
        use                     local-service
        host_name               192.168.5.110
        service_description     check_nrpe_check_getload
        check_command           check_nrpe!nh_check_getload
        max_check_attempts      5
        normal_check_interval   3
        retry_check_interval    2
        check_period            24x7
        notification_interval   10
        notification_period     24x7
        }

[root@nhserver2 objects]# service nagios reload

在nagios的services中就可看到的狀態了
192.168.5.110
check_nrpe_check_getload    
    OK     04-17-2014 16:21:53     0d 0h 4m 22s     1/5     GETLOADAVG OK: Load average is 0.00

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.