歡迎您光臨本站 註冊首頁

Nagios 監控 Redis 內存使用量的腳本

←手機掃碼閱讀     火星人 @ 2014-03-03 , reply:0


合同已經到期了, 沒有打算續簽, 想好好休息一下, 過完年再出來找工作.  這幾天在辦社保之類的手續, 在公司天天打醬油, 就寫了下面的這個腳本.

本人能力有限, 學 Perl 還不到 3 個月, 腳本某些地方寫的比較醜陋, 望各位大牛多多指點.



這個腳本主要是監控 Redis 的內存使用量.

寫這個監控腳本的主要原因有兩個:
(1) 因為網上找的幾個腳本只支持本地監控, 不能監控遠程主機.
(2) 因為網上找的幾個腳本都是Redis沒有連接密碼的情況下使用的, 如果Redis設置了連接密碼, 則不能監控了.

基於以上兩點, 寫了這個監控腳本.

幾點說明:

測試環境:
System :  CentOS 5.8 x86_64
Nagios  :  Nagios 3.4.1
Plugins :   Nagios Plugins 1.4.16
Redis    :  Redis 2.4.15
Perl      :   Perl 5.8.8

1. 請先確認已經安裝了Nagios的插件(Nagios Plugins);

2. 修改這個腳本的第28行, 替換對應的目錄, 默認目錄是: /usr/local/nagios/libexec

3. 此腳本需要Perl中的Redis模塊,可通過以下方式安裝:
     shell> perl -MCPAN -e shell
    CPAN> install Redis


4. 此腳本中, Redis 內存使用量是按 Byte 監控的, 所以 WARNING 和 CRITICAL 只支持純數字, 單位是 Byte, 且 WARNING 必須小於 CRITICAL.


5. -H 的選項為 hostname或者IP:埠, 例如: -H 192.168.1.200:6379 或 -H localhost:6379
    當初寫腳本的時候, 就沒有打算將 IP和埠 分開, 並不是不會寫, 只是個人喜歡這種方式.
    PS: 不喜歡這種方式的可以繞道.

   -P 為連接 Redis 時需要的密碼, 視實際情況而定, 可有可無

6. 簡單的幾個示例:

定義check_redis_used_memory, 在 /usr/local/nagios/etc/objects/commands.cfg 最後添加以下內容:

# 'check_redis_used_memory' command definition
# 連接Redis時需要密碼
define command{
    command_name  check_redis_used_memory
    command_line     /usr/local/nagios/libexec/check_redis_used_memory -H $ARG1$ -P $ARG2$ -w $ARG3$ -c $ARG4$
}

# 'check_redis_used_memory' command definition
# 連接Redis時不需要密碼
define command{
    command_name  check_redis_used_memory_no_passwd
    command_line     /usr/local/nagios/libexec/check_redis_used_memory -H $ARG1$ -w $ARG2$ -c $ARG3$
}

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

配置示例, 在 /usr/local/nagios/etc/objects/localhost.cfg 最後添加以下配置

配置示例1,需要密碼:
define service{
        use                             local-service
        host_name                  localhost
        service_description       Redis used memory 6380
        check_command           check_redis_used_memory!127.0.0.1:6380!redispasswd!3000000!5000000!
}

配置示例2,不需要密碼:
define service{
        use                             local-service
        host_name                  localhost
        service_description       Redis used memory 6370
        check_command           check_redis_used_memory_no_passwd!127.0.0.1:6379!3000000!5000000!
}

相關截圖:


------------------------------------------------------------------------------------------------------------------------------------------
修改一下, 調整一下 WARNING 和 CRITICAL:
配置示例1修改為:
define service{
        use                             local-service
        host_name                  localhost
        service_description       Redis used memory 6380
        #check_command           check_redis_used_memory!127.0.0.1:6380!redispasswd!3000000!5000000!
        check_command           check_redis_used_memory!127.0.0.1:6380!redispasswd!1000000!2000000!
}

配置示例2修改為:
define service{
        use                             local-service
        host_name                  localhost
        service_description       Redis used memory 6370
        #check_command           check_redis_used_memory_no_passwd!127.0.0.1:6379!3000000!5000000!
        check_command           check_redis_used_memory_no_passwd!127.0.0.1:6379!2000000!3000000!
}


相關截圖:


監控遠程的主機, 只需要配置相應的IP和埠及密碼等就可以了, 例如:
define service{
        use                             local-service
        host_name                  Redis Server
        service_description       Redis used memory
        check_command           check_redis_used_memory!192.168.1.200:6379!redispasswd!1000000!2000000!
}

#/usr/local/nagios/libexec/check_redis_used_memory -H 172.16.10.112:6380
Can't locate Redis.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/local/nagios/libexec/check_redis_used_memory line 25.
我是CENTOS6.2的系統。按照你說的執行報錯,請問是什麼原因?

 此腳本需要Perl中的Redis模塊,可通過以下方式安裝:
     shell> perl -MCPAN -e shell
    CPAN> install Redis

此腳本需要Perl中的Redis模塊,可通過以下方式安裝:
     shell> perl -MCPAN -e shell
    CPAN> install Redis

不是的,還有問題,我像多測試幾下再發給你,你看:
# ./check_redis_used_memory -H  172.16.10.112:6380 -P miam2012&&^^$$ -w 100000000 -c 200000000
Warning threshold not specified
# ./check_redis_used_memory -H  172.16.10.112:6380 -P  'miam2012&&^^$$' -w 100000000 -c 200000000000OK ! Redis Used Memory : 726160Bytes
這樣注意密碼,我必須要加''號才能獲取到。
按照你上述配置,增加到server.cfg中,沒密碼的埠是正常,有密碼的埠在nagios網頁界面提示
UNKNOWN Redis Used Memory
我直接寫在commands.cfg一樣不行(如下),不知道為什麼。直接執行
# 'check_redis_passwd' command definition
define command{
    command_name  check_redis_used_memory
    command_line    /usr/local/nagios/libexec/check_redis_used_memory -H 172.16.10.112:6380 -P 'miam2012&&^^$$'  -w 2000000 -c 3000000
}

我測試的環境:
CentOS 5.5 x86_64
Nagios 3.4.3

Nagios 配置文件:

/usr/local/nagios/etc/objects/commands.cfg# 'check_redis_used_memory' command definition
define command{
    command_name    check_redis_used_memory
    command_line    /usr/local/nagios/libexec/check_redis_used_memory -H $ARG1$ -P $ARG2$ -w $ARG3$ -c $ARG4$
}/usr/local/nagios/etc/objects/localhost.cfgdefine service{
        use                             local-service
        host_name                       localhost
        service_description             Redis used memory 6379
        check_command                   check_redis_used_memory!192.168.1.191:6379!miam2012\\&\\&\\^\\^\\$\\$!3000000!5000000!
}轉義方法:
miam2012\\&\\&\\^\\^\\$\\$

截圖:


參考文檔:
https://kb.groundworkopensource.com/display/SUPPORT/5.3.0-10+Escape+character+handling+is+different+in+Nagios+3.x+-+Extra+backslashes+may+be+needed+(DEPRECATED)

http://www.nagiosql.org/forum/general-questions/1102-solved-escape-exclamation-mark-in-arguments.html

http://permalink.gmane.org/gmane.network.nagios.user/60951

http://www.krenger.ch/blog/nagios-escape-dollar-character/

[火星人 ] Nagios 監控 Redis 內存使用量的腳本已經有1445次圍觀

http://coctec.com/docs/service/show-post-452.html