在 Cacti 下实现对 Tomcat 的监控

用了整整一天的时间,我终于实现了在 Cacti 下对 Tomcat 的监控,本来挺简单的事,但是具体的实践过程真是费劲周折。废话少说,我先介绍一下一般的配置过程,在介绍的一般配置过程中我们假定 Tomcat 的服务跑在 8080 端口上,在这之后,我会写下我遇到的错误和解决方法,一般配置的步骤如下:

  1. 下载 模板和脚本

  2. 配置 Tomcat 的 conf 目录下的 conf/tomcat-users.xml 文件,添加如下内容:

    <user username=”admin” password=”passwords” roles=”admin,manager”/>


  3. 重启 Tomcat 服务

  4. 在浏览器中浏览如下地址:

    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


  5. 复制 tomcatstats.pl 脚本文件到 cacti/scripts/ 路径下,并修改相应的文件权限。

  6. 确保安装了需要的 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]#


  7. 在 Cacti 的模板管理界面中导入 cacti_host_template_tomcat_server.xml 模板文件。

  8. 在 Cacti 的管理界面中进入 “Data Input Methods” 选择 “Tomcat Status” 修改 “Input String” 为如下内容:

    perl /scripts/tomcatstats.pl <hostname>:8080 admin passwords http-8080


  9. 创建 Host 模板并关联相关 Tomcat 图像模板即可实现对 Tomcat 的监控。

下面我来说说配置中遇到的错误和问题:

  1. 如果出现不出图像的问题,那么请尝试把相关图像模板的 “Unit Grid Value (–unit/–y-grid)”值清空或设为0。

  2. 具体的实践中图像出来了,但是图像中的数值一直是 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 社区做出自己的一点点贡献


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章