天天看點

munin-因為plugin而亮

munin is a networked resource monitoring tool that can help analyze resource trends and "what just happened to kill our performance?" problems. it is designed to be very plug and play. a default installation provides a lot of graphs with almost no work.

munin是一個網絡監控軟體。

munin是一個server-nodes的結構,一個server對應多台node,每台node就是被監控的節點,server是統計node的資料并進行繪圖生成html的節點。server的配置檔案是:/etc/munin/munin.conf ,每個node都有各自的配置檔案:/etc/munin/munin-node.conf

先看一下最後的統計效果:

munin-因為plugin而亮
munin-因為plugin而亮

如何安裝:

<a href="http://munin-monitoring.org/wiki/linuxinstallation">http://munin-monitoring.org/wiki/linuxinstallation</a>

談及為什麼使用munin,一定會提到的一點:munin能讓使用者非常快捷友善的編寫和使用插件。當需要定制某些特制的功能的時候,寫一個插件的代價非常小。插件就是一個腳本,可以使用php,bash,perl等語言進行編寫。下面以一個插件,使用php語言,完成統計redis的使用記憶體為例:

1 在有redis的機子上(192.168.0.19)安裝munin-node

2 進入/usr/share/munin/plugins/ (其實在任意一個目錄都是可以的)

3 建立檔案redis_memory_

這裡的redis_memory_最後一個下劃線後面的内容我預設是希望填寫端口号,因為我在192.168.0.19上有2個redis,分别放在7000和7001兩個端口上

是以最後的redis插件應該是redis_memory_7000 和 redis_memory_7001

4 編輯redis_memory_

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<code>#!/usr/local/bin/php</code>

<code>&lt;?php</code>

<code>if</code> <code>(isset($argv[1]) &amp;&amp; $argv[1] ==</code><code>'config'</code><code>) {</code>

<code>    </code> 

<code>    </code><code>echo</code><code>'graph_title redis_memory_'</code> <code>. sgetport($argv[0]) . php_eol;</code>

<code>    </code><code>echo</code><code>'graph_category open_message'</code> <code>. php_eol;</code>

<code>    </code><code>echo</code><code>'graph_vlabel memory'</code> <code>. php_eol;</code>

<code>    </code><code>echo</code><code>'memory.label memory'</code> <code>. php_eol;</code>

<code>    </code><code>exit;</code>

<code>}</code>

<code>echo</code><code>'memory.value '</code> <code>. igetmemory(sgetport($argv[0])) . php_eol;</code>

<code>if</code> <code>(isset($argv[1]) &amp;&amp; $argv[1] ==</code><code>'suggest'</code><code>) {</code>

<code>    </code><code>echo</code><code>'7000'</code> <code>. php_eol;</code>

<code>    </code><code>echo</code><code>'7001'</code> <code>. php_eol;</code>

<code>function igetmemory($port)</code>

<code>{</code>

<code>    </code><code>$cmd = sprintf(</code><code>"/usr/local/bin/redis-cli -p %s info | grep '^used_memory:'|awk -f : '{print $2}'"</code><code>, $port);</code>

<code>    </code><code>return</code> <code>exec($cmd);</code>

a) 腳本成功的标志是

./redis_memory_ 能出現:memory.value 653653653

./redis_memroy_ config 能出現:

graph_title redis_memory_7000

graph_category open_message

graph_vlabel memory

memory.label memory

munin在生成圖檔的時候會調用這兩個指令來生成統計圖檔的,graph_title是圖檔标題,graph_category是分類,memory.label 是memory的标簽,graph_vlabel memory是縱坐标的内容,memory.value 是具體的值。

還有更多的屬性:

<a href="http://munin-monitoring.org/wiki/protocol-config">http://munin-monitoring.org/wiki/protocol-config</a>

b)這個腳本運作成功的條件有:

安裝了php,并且php 指令安裝在/usr/local/bin/php

安裝了redis,redis-cli 安裝在/usr/local/bin/redis-cli

c)我這邊是使用redis-cli指令來取memory資訊,當然也可以使用redis的用戶端phpredis等來取

5 chmod +x redis_memory_

6 做連結(必須使用絕對路徑)

ln –s /usr/share/munin/plugins/redis_memory_ /etc/munin/plugins/redis_memory_7000

ln –s /usr/share/munin/plugins/redis_memory_ /etc/munin/plugins/redis_memory_7001

7 設定有哪些server能進入munin取資料,/etc/munin/munin-node.conf

allow ^127\.0\.0\.1allow192\.168\.0\.19allow192\.168\.0\.19

8 /etc/init.d/munin-node restart

server(192.168.0.19):

1 安裝munin

2 修改/etc/munin/munin.conf

要修改的部分:

htmldir /home/yejianfeng/www/munin

要增加的部分:

[zwt-01] 

address 192.168.0.17 

use_node_name yes

好了,完成了。但是還有幾個想說的:

1 如果在過程中出現錯誤, /var/log/munin/munin_node.log是非常好的資訊記錄

2 曾經被問過一個問題

為什麼不在server端使用http來擷取node的資訊(比如要擷取ngnix status或者redis的),這樣node就不用配置munin-node了,這樣在server同樣可以生成圖檔。

我是這樣說服自己的:

a) munin是使用tcp在server和node之間進行通信的,如果使用http,這就像是大炮換鳥槍的做法

b) 使用node模式有node的log,便于調試

c) 如果不使用server-node,那就相當于純粹把munin當做畫圖工具而已,還不如xxoo

d) 如果不使用node的話category的排版會相當複雜

3 munin  server會每5分鐘到node中進行munin node中進行取資料操作,是以,如果剛修改後發現圖檔還沒變,請稍安勿躁。

一些有用的munin連結:

<a href="http://munin-monitoring.org/">http://munin-monitoring.org</a>

<a href="http://os.51cto.com/art/201012/238964.htm">http://os.51cto.com/art/201012/238964.htm</a>

<a href="http://huacnlee.com/blog/munin-plugin-seo-check-for-gogle-baidu-collected-pages/">http://huacnlee.com/blog/munin-plugin-seo-check-for-gogle-baidu-collected-pages/</a>

<a href="http://blog.jploh.com/2007/06/14/how-to-install-munin-on-centos/">http://blog.jploh.com/2007/06/14/how-to-install-munin-on-centos/</a>