2023年2月2日 星期四

Install Percona Monitoring Plugins for Cacti

話說最近apache常常自己死掉,然後mysql常常不理我。所以打算在另一台server上用cacti監控它們。我選用的是percona,它在cacti上有開發相關的監控套件。

環境:rhel 8, apache 2.4.52, mysql 5.5.60-MariaDB

percona的部份,可以參考以下說明:

https://www.percona.com/doc/percona-monitoring-plugins/LATEST/cacti/index.html

percona的說明算是詳細,但照說明書做的話一定搞不定,我自己都搞了快兩天。所以我還是自己弄了一份教學,希望可以幫上其它人的忙。

由於我們要抓apache的資料,因此要幫他開新功能,請參考下列網頁。難度不高照作即可。

https://httpd.apache.org/docs/2.4/mod/mod_status.html

裝完之後要試一下能不能跑:(網址記得代換為自己的IP或網址)

wget wget -U Cacti/1.0 -q -O - -T 5 "http://www.omg.com/server-status?auto"

如果你apache裝對了,應該會有下面的回應:(我只節錄部份)

www.omg.com

ServerVersion: Apache/2.4.52 (codeit) OpenSSL/1.1.1m PHP/5.6.40 SVN/1.7.14 mod_wsgi/3.4 Python/2.7.5

ServerMPM: event

Server Built: Dec 20 2021 11:29:54

CurrentTime: Thursday, 02-Feb-2023 16:24:36 CST

我這一台server裝的是rhel8,所以我用yum來裝percona。

yum install percona-cacti-templates

這樣裝起來是1.1.8版,也是最後一版,以後也不會有新的版本了。

接著我們要先修改一下它的設定檔。

vi /usr/share/cacti/scripts/ss_get_by_ssh.php
vi /usr/share/cacti/scripts/ss_get_mysql_stats.php
MySQL的部份要修改帳號及密碼,ssh的部份你要設定一個可以免密碼登入要監控主機的帳號。 不過我只打算透過網頁抓Apache的資料,因此我就不設定ssh的部份。

修改完成後,我們也來測試一下,先試一下apache:

php /usr/share/cacti/scripts/ss_get_by_ssh.php --type apache --host www.omg.com --items gg,gh
有正常輸出的話,再試一下mysql:
/usr/bin/php -q /usr/share/cacti/scripts/ss_get_mysql_stats.php --host www.omg.com --items kx,ky --user omg --pass ohmygod --port 3306

再來就是麻煩的部份:

cd /usr/share/cacti/resource/percona/bin

讓自己產生符合你需要的template:
perl pmp-cacti-template --script ../../../scripts/ss_get_by_ssh.php ../definitions/apache.def > apache.xml

正常狀況下,你會得到以下的錯誤訊息:
Redundant argument in sprintf

這個不難解決,有人幫忙做了patch,我們將之下載,但在下載前,先把舊的檔案刪掉。
rm pmp-cacti-template
wget https://raw.githubusercontent.com/percona/percona-monitoring-plugins/master/cacti/bin/pmp-cacti-template

然後稍微修一下:
vi pmp-cacti-template

找到這一段:
my %hash_version_codes = (
把"0.8.6j" => "0013"的最後補上一個逗號,,
再新增add   "1.2.2"  => "0103"
我用的Cacti是1.2.2,因此我加上對應的0103。

接著我們要讓它產生新的hash,以配合Cacti的版本:
先做mysql的部份:
./pmp-cacti-make-hashes --refresh /usr/share/cacti/resource/percona/definitions/mysql.def > /usr/share/cacti/resource/percona/definitions/mysql_now.def

再來做Apache的部份:
./pmp-cacti-make-hashes --refresh /usr/share/cacti/resource/percona/definitions/apache.def > /usr/share/cacti/resource/percona/definitions/apache_now.def

再來要產出給Cacti import的XML檔。
perl pmp-cacti-template --script ../../../scripts/ss_get_by_ssh.php ../definitions/apache_nowdef > apache.xml
然後我們失敗了,得到一串字:
Both the meta file and the script file must have a version at pmp-cacti-template line 641.

好吧,回頭修一下,既然他說沒有版本資訊,我們就塞給他:
vi /usr/share/cacti/scripts/ss_get_by_ssh.php 
找到
$version = '$VERSION$';
改成
$version = '1.2.2';

接著再試試看有沒有正常產出:
perl pmp-cacti-template --script ../../../scripts/ss_get_by_ssh.php ../definitions/apache_nowdef > apache.xml

然後是mysql:
perl pmp-cacti-template --script ../../../scripts/ss_get_mysql_stats.php ../definitions/mysql_now.def > mysql.xml

接著你可以把檔案傳出來,再用cacti的介面上傳。
或者也可以用cli,然後你會遇到另一個問題,原因是你的filename後面要加上等號= (之前Cacti 1.2.15好像不需要加=號,這個問題我花了一段時間,最後去trace source code才發現...)

cd /usr/share/cacti
產出apache的xml:
php cli/import_template.php --filename=resource/percona/bin/apache.xml
產出mysql的xml:
php cli/import_template.php --filename=resource/percona/bin/mysql.xml
這樣就大功告成了,最後回到你的Cacti 在既有的device或者新增一個device,在Add Graph Template,可以找到像這種的東西,就真的是大功告成了。






沒有留言:

張貼留言