發新話題

用MUNIN監控LINUX服務器性能

用MUNIN監控LINUX服務器性能

監控LINUX主機的性能是一個很複雜的操作,它有賴於命令行工具和集中數據。分析可能會很困難,需要大量的數據來建立相互關係從而得出結論。好在我們可以用一些現有工具來搭建這個過程的模型,讓數據分析變得相對容易一些。接下來我們能看到其中的一個工具,它叫做Munin(代表記憶,是奧丁神的一隻烏鴉的名字)。

  Munin是通過客戶端-服務器架構收集數據並將其圖形化的工具。Munin允許你跟蹤你的主機的運行記錄,就是所謂的『節點』,然後將它們發送到中央服務器,隨後你就能在這裡以圖像形式展示它們。以下你可以看到一個用Munin圖形界面顯示磁盤IO信息的例子:

Munin,Linux,监控,服务器性能

  Munin可以通過安裝包快速簡易地安裝到大多數Linux系統上。在紅帽和Ubuntu/Debian上你都需要Munin,munin-node和munin-common安裝包(這個組合假設你還需要監控服務器的情況),以紅帽為例:

$ yum install munin munin-common munin-node

  Munin安裝將它的配置文件放在目錄/etc/munin下。讓我們就從配置munin服務器開始吧。主要的服務器配置文件是 munin.conf,它控制服務器的設置和每個節點的配置信息。在大多數情況下用默認設置就可以了,但是其它幾個選項你也應該知道:dbdir、 htmldir、logdir 和rundir。

  Dbdir設置決定munin把收集到的RRD格式的統計數據放在哪個目錄下,在紅帽和ubuntu系統中默認的是放在/var/lib/munin這個目錄下。

  Htmldir設置控制munin輸出數據的位置,它是以html文件來顯示圖形界面的。在紅帽系統中默認的是放在/var/www/html /munin目錄下,而在ubuntu中的是放在/var/cache/munin/www目錄下。這個目錄正是我們一般為網頁服務器服務時改採用的目錄,例如Apache。最好的操作辦法之一就是運用Apache虛擬主機,創建一個虛擬主機可以用來圖形化顯示我們的節點狀態:

<VirtualHost *:80>
     ServerAdmin webmaster@localhost     ServerName   munin.example.com     DocumentRoot /var/www/html/munin     <Directory />         Options FollowSymLinks         AllowOverride None     </Directory>     LogLevel notice     CustomLog /var/log/apache2/munin.access.log combined     ErrorLog /var/log/apache2/munin.error.log     ServerSignature On </VirtualHost>

  Logdir和rundir設置控制munin的日誌文件和pid文件放置位置。

  最後,我們也必須在munin.conf文件中定義所有將向服務器發送報告的節點主機,命令是:

[hostname.example.com]  address 10.0.0.1  use_node_name yes [hostname2.example.com]  address 10.0.0.2  use_node_name yes

  括號中是每個節點的名字,後面是它的IP地址,use_node_name命令控制munin命名節點的方式,如果後面跟的參數為yes就是用括號中的值來命名,如果是no則將執行一個DNS查詢。另外,你可以用includedir選項來命名一個單獨的目錄,munin將在這裡加載所有的文件,例如:

includedir /etc/munin/munin-conf.d/

  我經常用puppet導出資源來管理munin配置,首先在每個puppet客戶端上創建一個獨立的文件,然後用munin在這個文件中加載節點信息。

  到這一步,服務器就已經配置好了,接下來我們需要配置節點。將munin-node包安裝在每個節點並且配置/etc/munin目錄下的 munin-node.conf文件。大多數配置信息都不需要改變,但是你需要更改allow選項,它控制哪一個主機能訪問munin和檢索統計。我們用 IP地址來配置munin服務器,例如:

allow ^10\.0\.0\.100$

  正如你看到的,IP地址必須用perl常用格式來輸入。如果你的munin服務器不只一台,那麼你可以用多行允許命令來定義它們。

  每個munin節點都用tcp端口4949來和munin服務器通訊,所以你必須確保這個端口在主機防火牆上是打開的,並且在節點和服務器中間環節的任何一個防火牆上都允許數據通過。你可以在munin-node.conf文件中的port選項來定義這個端口號。

  除了基本設置外,我們還需要指示節點哪些數據是需要收集的。Munin採用插件程序的架構方式來定義監控內容。例如,有專門監控CPU的插件,還有監控負載、內存和其它內容的各種插件。Munin的所有插件清單可以在/etc/munin/plugins目錄下找到,它們以鏈接到插件的sym- links形式顯示。添加一個插件到munin的過程就是將插件文件的鏈接寫入到/etc/munin/plugins目錄下。如果插件需要某個配置,例如定義運行插件要求的用戶,你都可以在目錄/etc/munin/plugins.conf.d下找到該配置文件。

  munin默認包含很豐富的內容,你可能不需要改變初始的基本設置,因為它們已經提供了足夠的採集數據。Munin還提供大量有用的插件,你還能在社區中找到許多別人交流的插件。你可以用你熟悉的語言很容易開發出新的插件來。

  最後,我們通過運行munin-node init腳本來啟動munin服務器和節點。

$ sudo /etc/init.d/munin-node start

  這樣就能開始munin監控並收集需要的數據,munin控制將週期性地查看每個節點的數據然後將其上傳到munin服務器上。接著就可以通過munin控制上的網頁服務來查看結果數據了。

  所有的內容就是這些了。現在你可以用圖形界面查看你的服務器的行為狀態,並且可以探測到性能趨勢和可能發生的問題。如果你覺得munin不適合你,也可以考慮使用另一個叫做collected的工具,它和munin比較相似,用C語言編寫,它提供的功能可能比munin更加完善。它要求安裝額外的軟件來獲取這些功能,但也是一個不錯的可供參考的平台。

TOP

Getting munin to work on your cPanel-based server

Munin is a monitoring tool that can be used to monitor the performance of a server or groups of servers. Somewhat similar (in some ways) to MRTG (it’s more popular counterpart).

By default, new installations of cPanel/WHM come preinstalled with Munin but it doesn’t work. Trying to access the utility from your WHM menu Munin Service Monitor will result in an error similar to this:

The server was not able to find the document (./munin/index.html) you requested Please check the url and try again.  You might also want to report this error to your webhost.

After googling for a solution, I then tried running the programs in the package manually and discovered that the reason it doesn’t work is because of a missing dependency - the Perl HTML::Template module.

To install it, login to your server as the root/administrative user then type in the following statement on the console.

/scripts/perlinstaller HTML::Template

Next, you will have to manually generate the HTML files. You do so by typing in the statement:

/usr/share/munin/munin-html --force-root

Now you have to specify the —force-root switch since you’re running it as the root/administrative user.

Now you can check your munin reports from your WHM interface.

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。