前面介紹了2篇munin使用的相關文章:
<a href="http://www.cnblogs.com/rond/p/3757804.html">[Linux實用工具]Linux監控工具munin的安裝和配置</a>
<a href="http://www.cnblogs.com/rond/p/3757804.html">[Linux實用工具]Linux監控工具munin的展示(Nginx)</a>
這次介紹一下munin-node的插件的安裝配置和插件的編寫。
munin-node本身就內建了很多的插件,隻需要直接建個軟鍊就可以了。像Nginx、Apach、mysql都是有現成的插件可以使用的。
munin的插件預設儲存在/etc/munin/plugins裡面。進去檢視會發現很多軟鍊,軟鍊到/usr/share/munin/plugins 這個目錄下的檔案。沒錯,munin的插件都是放置在/usr/share/munin/plugins 下的。使用相當簡單,隻需要參考原有的例子,直接建軟鍊過去就可以了。
下面以mysql的插件為例:
然後重新開機一下munin-node 就可以生效了。

如果需要監控的應用沒有相關的插件支援怎辦?也很簡單。随意打開一個現有的插件,會發現具體的代碼内容,觀察發現編寫一個插件也不是很難。而且munin-node的插件支援shell、python、perl。也可以根據自己熟悉的語言來編寫插件。
以threads (/usr/share/munin/plugins/threads)插件為例,我們可以檢視内部代碼:
<a></a>
可以直接執行插件的檔案檢視輸出資料的結構。可以發現所有的輸出的格式都是一樣的,而且都有1個參數。
分别使用參數autoconf、config和不使用參數,檢視輸出内容:
# sh threads autoconf
yes
# sh threads config
graph_title Number of threads
graph_vlabel number of threads
graph_category processes
graph_info This graph shows the number of threads.
threads.label threads
threads.info The current number of threads.
# sh threads
threads.value 174
autoconf
用于檢測是否有程序的存在。非必要。
config
使用config參數的時候,輸出的内容是繪圖的配置資訊,檢視字段意思也可以知道字段的意思。
不使用參數
不使用參數的時候,輸出的是具體的程序數量。這個跟具體的指令輸出有關系。
grep -s '^Threads' /proc/[0-9]*/status | awk '{ sum += $2; } END { print "threads.value", sum; }'
既然知道每個參數需要輸出的具體内容,那麼我們就可以依葫蘆畫瓢,我們就可以寫出我們自己的插件。下面給出一個具體的例子,該例子用于監控一個遊戲伺服器的記憶體和CPU。
這裡沒有使用autoconf參數,其實應該使用的,萬一該程序不存在怎麼辦,是吧。其實沒有統計到數值的時候,圖表是空的,所有才說autoconf參數不是必須的。
這個例子與threads不一樣的是,這裡收集到的是多條資料。
插件寫好了之後,需要在/etc/munin/plugin-conf 配置一下。加入上面例子以munin-node_cpu 檔案名儲存。
在/etc/munin/plugin-conf 下新增一個檔案,名字可以任意,如munin_info:
然後重新開機munin-node就可以了。
是吧,插件編寫其實很簡單吧。隻要輸出的格式符合要求,随便你使用perl、python還是shell。具體的監控完全可以自定義。譬如我假設有通路遊戲服線上人數的接口,我也可以拿出來監控,當線上人數超過多少的時候給予相關提示。
開始使用獨立部落格了,原文位址在這裡:
<a href="http://pertest.sinaapp.com/?page_id=16">munin的安裝和配置</a>
<a href="http://pertest.sinaapp.com/?page_id=21">munin的展示(Nginx)</a>
<a href="http://pertest.sinaapp.com/?page_id=54">munin插件配置和插件編寫</a>
本文轉自 Ron Ngai 部落格園部落格,原文連結: http://www.cnblogs.com/rond/p/3804345.html ,如需轉載請自行聯系原作者