用了整整一天的时间,我终于实现了在 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:passwords@10.1.2.24:8080/manager/status?XML=true

    浏览器会显示相应的 Tomcat 状态信息,如下所示:

    Tomcat Status
    JVM: free:11466816 total:16318464 max:259522560
    Connector — http-8080
    threadInfo maxThreads:200 minSpareThreads: maxSpareThreads: currentThreadCount:4 currentThreadsBusy:1
    requestInfo maxTime:797 processingTime:5711 requestCount:570 errorCount:51 bytesReceived:0 bytesSent:1228726
    Stage Time B Sent B Recv Client VHost Request
    S 0 0 0 10.1.2.24 10.1.2.24 GET/manager/status?XML=trueHTTP/1.1
    R 0 0 ? ? ?????
    R 0 0 ? ? ?????
    R 0 0 ? ? ?????
    Connector — jk-8009
    threadInfo maxThreads:200 minSpareThreads: maxSpareThreads: currentThreadCount:4 currentThreadsBusy:1
    requestInfo maxTime:0 processingTime:0 requestCount:0 errorCount:0 bytesReceived:0 bytesSent:0
    Stage Time B Sent B Recv Client VHost Request

  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 社区做出自己的一点点贡献!

相关日志:

Tagged with:
 

9 Responses to 在 Cacti 下实现对 Tomcat 的监控

  1. cwalet 说道:

    hI,我安装之后perl /scripts/tomcatstats.pl :8080 admin passwords http-8080有XML解析的结果,可出的图没数据,但是看实时数据图时又有数据,其中connection rate中还有error的数据,请教如何解决!
    使用的CentOS6.0&&CactiEZ&&tomcat6.0

  2. Robin 说道:

    我大概知道你的问题了,这个不是因为出图没数据,而是 error 数据的值过大,而其他值过低,所以你只能看到 error 的数值!至于为什么会有 error 数据,就视情况而定了。

  3. cwalet 说道:

    但是4张图都没有数据显示啊?还有就是你所写的这一点:“如果出现不出图像的问题,那么请尝试把 相关图像模板的 “Unit Grid Value (–unit/–y-grid)”值清空或设为0。”在CactiEZ中文版中并没有,请问具体是哪一项?此外,poller的日志是否就是cacti的日志?我在日志中并没有看到你列出的那一项

  4. Robin 说道:

    你的是中文版的吗?具体的那个值如何翻译我就不知道了。你看看相关图像模板的参数都有什么,然后告诉我,poller的日志不是cacti的日志,poller一般是通过linux主机上的crontab运行的,一般情况下,我们都把poller的输出重定向到了null设备,所以你最好先把这个输出重定向到一个文本文件中,然后查看poller日志。

  5. cwalet 说道:

    php poller.php查看到一些信息,不知道是不是poller的日志,但其中也只有关于locale的WARN信息,并没有你提到的那些。我的Q:770328785,还有其他方法吗,是不是数据库没有保存实时数据才没有生成图?

  6. myl 说道:

    你好,我tomcat 4个监控图都是-nan

    是怎么回事呢,直接执行脚本时是有数据的,就是显示不到图上

    perl /var/www/html/scripts/tomcatstats.pl 10.8.60.201:80 admin admin http-80
    jvm_memory_free:2639307856 jvm_memory_max:4278190080 jvm_memory_total:4278190080 connector_max_time:7185188 connector_error_count:209489 connector_bytes_sent:21931788757 connector_processing_time:228373660 connector_request_count:1124901 connector_bytes_received:2296686340 connector_current_thread_count:483 connector_min_spare_threads:25 connector_max_threads:1024 connector_max_spare_threads:75 connector_current_threads_busy:401

  7. Robin 说道:

    请在 Data input method 里面把“perl”修改为绝对路径。比如“/usr/local/bin/perl”。

  8. 酱油 说道:

    你好 我想问下 如果tomcat是多端口的 目前的监控方法是怎样的 多谢

  9. Robin 说道:

    目前我介绍的方法,只能制定一种端口,可以通过修改脚本的方法实现多端口,当然最偷懒的方法就是上传多份脚本,不同文件名,然后在cacti界面下配置不同的端口!

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*


*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>