用了整整一天的時間,我終於實現了在 Cacti 下對 Tomcat 的監控,本來挺簡單的事,但是具體的實踐過程真是費勁周折。廢話少說,我先介紹一下一般的配置過程,在介紹的一般配置過程中我們假定 Tomcat 的服務跑在 8080 端口上,在這之後,我會寫下我遇到的錯誤和解決方法,一般配置的步驟如下:
下載 模板和腳本
配置 Tomcat 的 conf 目錄下的 conf/tomcat-users.xml 文件,添加如下內容:
<user username=”admin” password=”passwords” roles=”admin,manager”/>
重啓 Tomcat 服務
在瀏覽器中瀏覽如下地址:
http://admin:[email protected]:8080/manager/status?XML=true
瀏覽器會顯示相應的 Tomcat 狀態信息,如下所示:
Tomcat Status
JVM:free:11466816total:16318464max:259522560
Connector — http-8080
threadInfomaxThreads:200minSpareThreads:maxSpareThreads: currentThreadCount:4currentThreadsBusy:1
requestInfomaxTime:797processingTime:5711requestCount:570errorCount:51 bytesReceived:0bytesSent:1228726
StageTimeB SentB RecvClientVHostRequest
S00010.1.2.2410.1.2.24GET/manager/status?XML=trueHTTP/1.1
R00 ???????
R00 ???????
R00 ???????
Connector — jk-8009
threadInfomaxThreads:200minSpareThreads:maxSpareThreads: currentThreadCount:4currentThreadsBusy:1
requestInfomaxTime:0processingTime:0requestCount:0errorCount:0 bytesReceived:0bytesSent:0
StageTimeB SentB RecvClient VHostRequest複製 tomcatstats.pl 腳本文件到 cacti/scripts/ 路徑下,並修改相應的文件權限。
確保安裝了需要的 XML::Simple模塊,如果你的 cacti 服務的主機可以訪問外網,那麼請在 Shell 下輸入以下命令安裝:
perl -MCPAN -e”install XML::Simple”
很不幸公司的服務器不允許訪問外網,所以我只能手動安裝該模塊。登錄 CPAN 的官方網站搜索並下載安裝包,安裝前請仔細查看 README 文件,該文件會介紹的安裝包的依賴包,如果機器上沒有還需要先安裝上依賴包。實踐中具體的安裝和測試腳本過程如下:
[root@pubservicedb scripts]# ./tomcatstats.pl 10.1.2.24:8080 admin passwords http-8080
Can’t locate XML/Simple.pm in @INC (@INC contains: /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/5.8.8 .) at ./tomcatstats.pl line 7.
BEGIN failed–compilation aborted at ./tomcatstats.pl line 7.
[root@pubservicedb scripts]#[root@pubservicedb XML-Simple-2.18]# perl Makefile.PL
Checking installed modules …
You don’t have either XML::SAX or XML::Parser installed!
Checking if your kit is complete…
Looks good
Warning: prerequisite XML::NamespaceSupport 1.04 not found.
Warning: prerequisite XML::SAX 0 not found.
Warning: prerequisite XML::SAX::Expat 0 not found.
Writing Makefile for XML::Simple
[root@pubservicedb XML-Simple-2.18]#[root@pubservicedb XML-NamespaceSupport-1.11]# perl Makefile.PL
Cannot determine perl version info from lib/XML/NamespaceSupport.pm
Checking if your kit is complete…
Looks good
Warning: prerequisite ExtUtils::MakeMaker 6.42 not found. We have 6.30.
Writing Makefile for XML::NamespaceSupport
[root@pubservicedb XML-NamespaceSupport-1.11]# make
cp lib/XML/NamespaceSupport.pm blib/lib/XML/NamespaceSupport.pm
Manifying blib/man3/XML::NamespaceSupport.3pm
[root@pubservicedb XML-NamespaceSupport-1.11]# make install
Installing /usr/lib/perl5/site_perl/5.8.8/XML/NamespaceSupport.pm
Installing /usr/share/man/man3/XML::NamespaceSupport.3pm
Writing /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/XML/NamespaceSupport/.packlist
Appending installation info to /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/perllocal.pod
[root@pubservicedb XML-NamespaceSupport-1.11]#[root@pubservicedb XML-NamespaceSupport-1.11]# cd ..
[root@pubservicedb monitor]# cd XML-SAX-0.96
[root@pubservicedb XML-SAX-0.96]# perl Makefile.PL
Checking if your kit is complete…
Looks good
Writing Makefile for XML::SAX::Base
Writing Makefile for XML::SAX
[root@pubservicedb XML-SAX-0.96]# make
cp SAX/PurePerl/Exception.pm blib/lib/XML/SAX/PurePerl/Exception.pm
cp SAX/PurePerl/UnicodeExt.pm blib/lib/XML/SAX/PurePerl/UnicodeExt.pm
… …
cp SAX/PurePerl.pm blib/lib/XML/SAX/PurePerl.pm
cp SAX/Intro.pod blib/lib/XML/SAX/Intro.pod
cp SAX/PurePerl/Reader.pm blib/lib/XML/SAX/PurePerl/Reader.pm
make[1]: Entering directory `/root/monitor/XML-SAX-0.96/XML-SAX-Base’
cp lib/XML/SAX/placeholder.pl ../blib/lib/XML/SAX/placeholder.pl
cp lib/XML/SAX/Base.pm ../blib/lib/XML/SAX/Base.pm
cp lib/XML/SAX/Exception.pm ../blib/lib/XML/SAX/Exception.pm
Manifying ../blib/man3/XML::SAX::Base.3pm
Manifying ../blib/man3/XML::SAX::Exception.3pm
make[1]: Leaving directory `/root/monitor/XML-SAX-0.96/XML-SAX-Base’
Manifying blib/man3/XML::SAX::DocumentLocator.3pm
Manifying blib/man3/XML::SAX.3pm
Manifying blib/man3/XML::SAX::PurePerl.3pm
Manifying blib/man3/XML::SAX::Intro.3pm
Manifying blib/man3/XML::SAX::ParserFactory.3pm
Manifying blib/man3/XML::SAX::PurePerl::Reader.3pm
[root@pubservicedb XML-SAX-0.96]# make install
make[1]: Entering directory `/root/monitor/XML-SAX-0.96/XML-SAX-Base’
make[1]: Leaving directory `/root/monitor/XML-SAX-0.96/XML-SAX-Base’
Installing /usr/lib/perl5/site_perl/5.8.8/XML/SAX.pm
… …
Installing /usr/share/man/man3/XML::SAX::PurePerl.3pm
Writing /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/XML/SAX/.packlist
Appending installation info to /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/perllocal.pod
could not find ParserDetails.ini in /usr/lib/perl5/site_perl/5.8.8/XML/SAX
[root@pubservicedb XML-SAX-0.96]#[root@pubservicedb monitor]# cd XML-Simple-2.18
[root@pubservicedb XML-Simple-2.18]# perl Makefile.PL
Checking installed modules …
XML::SAX is installed, it will be used by the test suite
Writing Makefile for XML::Simple
[root@pubservicedb XML-Simple-2.18]# make
cp lib/XML/Simple/FAQ.pod blib/lib/XML/Simple/FAQ.pod
cp lib/XML/Simple.pm blib/lib/XML/Simple.pm
Manifying blib/man3/XML::Simple::FAQ.3pm
Manifying blib/man3/XML::Simple.3pm
[root@pubservicedb XML-Simple-2.18]# make install
Installing /usr/lib/perl5/site_perl/5.8.8/XML/Simple.pm
Installing /usr/lib/perl5/site_perl/5.8.8/XML/Simple/FAQ.pod
Installing /usr/share/man/man3/XML::Simple::FAQ.3pm
Installing /usr/share/man/man3/XML::Simple.3pm
Writing /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/XML/Simple/.packlist
Appending installation info to /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/perllocal.pod
[root@pubservicedb XML-Simple-2.18]#[root@pubservicedb scripts]# ./tomcatstats.pl 10.1.2.24:8080 admin passwords http-8080
jvm_memory_free:11742544 jvm_memory_max:259522560 jvm_memory_total:16318464 connector_max_time:797 connector_error_count:5 connector_bytes_sent:49396 connector_processing_time:1405 connector_request_count:12 connector_bytes_received:0 connector_current_thread_count:2 connector_min_spare_threads: connector_max_threads:200 connector_max_spare_threads: connector_current_threads_busy:1 [root@pubservicedb scripts]#在 Cacti 的模板管理界面中導入 cacti_host_template_tomcat_server.xml 模板文件。
在 Cacti 的管理界面中進入 “Data Input Methods” 選擇 “Tomcat Status” 修改 “Input String” 爲如下內容:
perl /scripts/tomcatstats.pl <hostname>:8080 admin passwords http-8080
創建 Host 模板並關聯相關 Tomcat 圖像模板即可實現對 Tomcat 的監控。
下面我來說說配置中遇到的錯誤和問題:
如果出現不出圖像的問題,那麼請嘗試把相關圖像模板的 “Unit Grid Value (–unit/–y-grid)”值清空或設爲0。
具體的實踐中圖像出來了,但是圖像中的數值一直是 NaN,通過查詢 Poller 的日誌我發現如下報錯:
05/20/2011 11:05:07 AM – POLLER: Poller[0] CACTI2RRD: /usr/local/bin/rrdtool update /usr/local/apache/htdocs/cacti/rra/10_1_2_24_current_threads_bus_747.rrd –template 1305860706:0
ERROR: Not enough arguments這個問題很詭異,我提高了 Poller 的日誌等級才發現,而且我在 Poller 的設置中如果選擇 cmd.php 的方式輪詢則系統會取到數據,這就基本確定了這個問題出在了 Spine 上。自己上網找了很長時間,終於在 Cacti 的 官方論壇 中找到答案。安裝論壇中的解決方法給 Cacti 和 Spine 分別打了補丁,並重新編譯了 Spine 組件,問題迎刃而解啦。
考慮到 Tomcat 的應用的安全性和不同 Tomcat 的應用配置路徑不同的問題,我推薦大家使用 INSTALL 文件中介紹配置 Tomcat 方式,修改 server.xml 配置文件,並添加以下內容:
<Service name=”Admin”>
<Connector port=”8444″ maxHttpHeaderSize=”8192″ maxThreads=”150″ minSpareThreads=”25″ maxSpareThreads=”75″ enableLookups=”false” disableUploadTimeout=”true” acceptCount=”100″ scheme=”https” secure=”true” clientAuth=”false” sslProtocol=”TLS” />
<Engine name=”Admin” defaultHost=”localhost”>
<Realm className=”org.apache.catalina.realm.UserDatabaseRealm” resourceName=”UserDatabase”/>
<Host name=”localhost” appBase=”server/webapps” unpackWARs=”false” autoDeploy=”false” xmlValidation=”false” xmlNamespaceAware=”false”>
<Valve className=”org.apache.catalina.valves.FastCommonAccessLogValve” directory=”logs” prefix=”admin_access_log.” suffix=”.txt” pattern=”combined” resolveHosts=”false” fileDateFormat=”yyy-MM-dd”/>
<Context path=”/manager” cookies=”false” docBase=”server/webapps/manager” debug=”0″ privileged=”true” reloadable=”false” />
</Host>
</Engine>
</Service>該服務跑在了 8444 端口,請在 Cacti 界面的 “Data Input Methods” 中修改相應的路徑和端口號。
想完全理解 Cacti 真的很難,很高興我又前進了一小步,這次的配置過程讓我更加深入的瞭解了 Cacti 的模板機制,以及底層的 RRDTool 的工作原理和服務器上 Poller.php 的 cron 輪詢機制,最重要的通過查詢 Cacti 的 使用手冊,我的 Debugging 技巧又提高不少,希望在不久的將來我能自己寫出牛逼的監控腳本和模板,也爲 Cacti 社區做出自己的一點點貢獻