---
title: "Cacti监控Tomcat服务器实现过程"
author: "xitonga"
date: "2014-06-08T16:21:00Z"
tags: []
slug: "cacti-tomcat"
draft: false
lastmod: "2014-06-23T15:09:49Z"
id: 2083693
url: "https://xitonga.iteye.com/blog/2083693"
---

**1首先去官网上面下载通用的监控模板**

一般使用TomcatStats-0.1.zip模板居多，下载地址：

[http://forums.cacti.net/download/file.php?id=12310](http://forums.cacti.net/download/file.php?id=12310)，

在linux服务器上面使用wget下载，wget[http://forums.cacti.net/download/file.php?id=12310](http://forums.cacti.net/download/file.php?id=12310)，报如下错误：

正在连接forums.cacti.net|173.225.179.10|:80...已连接。

已发出HTTP请求，正在等待回应... 301 Moved Permanently

位置：http://forums.cacti.net/ \[跟随至新的URL]

已超过20次重定向。

可以在windows7的google浏览器上输入[http://forums.cacti.net/download/file.php?id=12310](http://forums.cacti.net/download/file.php?id=12310)点击回车下载，然后通过SecureFX客户端工具scp到linux服务器上即可。

然后解压缩得到3个文件cacti\_host\_template\_tomcat\_server.xml，INSTALL.txt，tomcatstats.pl 3个文件。

**2在cacti主服务器的网址上面导入模板**

打开cacti服务器的网址：

进入[http://10.xx.1.xx/cacti/index.php](http://10.xx.1.xx/cacti/index.php)-&gt;Console–&gt;Templates-&gt;Import Templates，选择文件导入（cacti\_host\_template\_tomcat\_server.xml）文件，选择Import按钮加载tomcat模板。  
​![](http://blog.itpub.net/attachment/201406/8/26230597_1402215444rcJd.jpg)  
​

**3 cacti服务器端加载tomcatstats.pl统计脚本**

将解压缩TomcatStats-0.1.zip得到的tomcatstats.pl放到/var/www/html/cacti/scritps目录下，并且赋予执行权限

Chown –R apache.apache tomcatstats.pl

chmod 755 tomcatstats.pl

修改tomcatstats.pl，大概第20行，将

1\. my $xml = \`GET $url\`;

修改为

1\. my $xml = \`wget -qO - $url\`;

不然在你导入之后很可能出现数据出不来。

Check URL连接是否正确

Vim tomcatstats.pl后，在第17行后面添加print $url，在控制台打出url，如下所示：

my $url = "http://$username:$password"."\\@$host/manager/status?XML=true";

print $url;

print “ ”;

然后用perl来check下统计脚本，如下：

\[root@squid-2 scripts]# perl tomcatstats.pl 10.xx.3.xx:9444 'tomcat' '9ssspw ' http-9444

[http://tomcat:tomcat0401@10.xx.3.xx:9444/manager/status?XML=true](http://tomcat:tomcat0401@10.xx.3.xx:9444/manager/status?XML=true) jvm\_memory\_free:2925944104 jvm\_memory\_max:3892314112 jvm\_memory\_total:3892314112 connector\_max\_time:131 connector\_error\_count:4464 connector\_bytes\_sent:19559190 connector\_processing\_time:11552 connector\_request\_count:8931 connector\_bytes\_received:0 connector\_current\_thread\_count:4 connector\_min\_spare\_threads: connector\_max\_threads:150 connector\_max\_spare\_threads: connector\_current\_threads\_busy:1

​

也可以在windows7的google浏览器里面，输入上面URL，也会出现很多tomcat的统计信息，如下所示：

![](http://blog.itpub.net/attachment/201406/8/26230597_1402215488y8A4.jpg)

**4修改cacti中tomcat获取数据的url地址**

进入cacti---&gt; console ---&gt; Data Input Methods ---&gt;Tomcat Status

修改Input String中字段

修改为:perl &lt;path\_cacti&gt;/scripts/tomcatstats.pl &lt;hostname&gt;:8080 tomcat 9ssspw http-8080

注意：替换上面的username和password为实际的tomcat管理员账号和密码。

要开启tomcat的manager管理员账号 附件内有Enable the Tomcat manager application介绍，如下图：

![](http://blog.itpub.net/attachment/201406/8/26230597_1402215545zAU1.jpg)

**5 web端tomcat服务器的配置调整**

在tomcat/conf/server.xml里面添加如下：

&lt;Service name="Admin"&gt;

&lt;Connector port="9444" maxHttpHeaderSize="8192"

maxThreads="150" minSpareThreads="25" maxSpareThreads="75"

enableLookups="false" disableUploadTimeout="true"

acceptCount="100" scheme="https" secure="true"

clientAuth="false" sslProtocol="TLS" /&gt;

&lt;Engine name="Admin" defaultHost="localhost"&gt;

&lt;Realm className="org.apache.catalina.realm.UserDatabaseRealm"

resourceName="UserDatabase"/&gt;

&lt;Host name="localhost" appBase="webapps"

unpackWARs="false" autoDeploy="false"

xmlValidation="false" xmlNamespaceAware="false"&gt;

&lt;Valve className="org.apache.catalina.valves.FastCommonAccessLogValve"

directory="logs" prefix="admin\_access\_log." suffix=".txt"

pattern="combined" resolveHosts="false"

fileDateFormat="yyy-MM-dd"/&gt;

&lt;Context path="/manager" cookies="false"

docBase="server/webapps/manager" debug="0"

privileged="true" reloadable="false" /&gt;

&lt;/Host&gt;

&lt;/Engine&gt;

&lt;/Service&gt;

然后在tomcat/conf/tomcat-users.xml里面 添加

&lt;role rolename="manager-gui"/&gt;

&lt;user username="tomcat" password="9ssspw " roles="manager-gui"/&gt;

passwords 为连接密码 可自行更改，然后重启tomcat 服务

去cacti服务器端执行perl tomcatstats.pl 10.xx.3.xx:9444 'tomcat' '9ssspw ' http-9444 check统计脚本是否能取到tomcat的数据，有数据即正常，如下所示：

\[root@squid-2 scripts]# /usr/bin/perl tomcatstats.pl 10.xx.3.xx:9444 'tomcat' 'xxx’ http-9444

http://tomcat:tomcat0401@10.254.3.29:9444/manager/status?XML=true jvm\_memory\_free:3409129096 jvm\_memory\_max:3892314112 jvm\_memory\_total:3892314112 connector\_max\_time:131 connector\_error\_count:2 connector\_bytes\_sent:20241 connector\_processing\_time:168 connector\_request\_count:5 connector\_bytes\_received:0 connector\_current\_thread\_count:4 connector\_min\_spare\_threads: connector\_max\_threads:150 connector\_max\_spare\_threads: connector\_current\_threads\_busy:1

\[root@squid-2 scripts]#

记录debug期间的报错信息如下：

\[root@squid-2 scripts]# perl tomcatstats.pl 10.xx.3.xx:9300 admin 9ssspw http-9300

Can't locate XML/Simple.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor\_perl /usr/share/perl5/vendor\_perl /usr/lib64/perl5 /usr/share/perl5 .) at tomcatstats.pl line 7.

BEGIN failed--compilation aborted at tomcatstats.pl line 7.

需要安装XML::Simple

\[root@squid-2 ~]# find / -name cpan

/usr/bin/cpan

\[root@squid-2 ~]# /usr/bin/cpan XML::Simple

……

Prepending /root/.cpan/build/XML-Simple-2.20-7zpaOY/blib/arch /root/.cpan/build/XML-Simple-2.20-7zpaOY/blib/lib to PERL5LIB for 'install'

Installing /usr/local/share/perl5/XML/Simple.pm

Installing /usr/local/share/perl5/XML/Simple/FAQ.pod

Installing /usr/local/share/man/man3/XML::Simple.3pm

Installing /usr/local/share/man/man3/XML::Simple::FAQ.3pm

Appending installation info to /usr/lib64/perl5/perllocal.pod

GRANTM/XML-Simple-2.20.tar.gz

/usr/bin/make install -- OK

Warning (usually harmless): 'YAML' not installed, will not store persistent state

继续报错File does not exist: at tomcatstats.pl line 20

\[root@squid-2 scripts]# /usr/bin/perl tomcatstats.pl 10.254.3.29:9300 tomcat xxx http-9300

File does not exist: at tomcatstats.pl line 20

需要安装如下组件：

\[root@squid-2 scripts]#yum install perl-HTML-Parser.i386 perl-HTML-Tagset.noarch perl-libwww-perl.noarch perl-URI.noarch

之后启动tomcat报错如下：

\[tomcat@web-9 logs]$ cat catalina.out

2014-6-6 15:11:15 org.apache.catalina.core.AprLifecycleListener init

信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/jdk1.6.0\_45/jre/lib/amd64/server:/usr/java/jdk1.6.0\_45/jre/lib/amd64:/usr/java/jdk1.6.0\_45/jre/../lib/amd64:/usr/local/lib::/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib

2014-6-6 15:11:16 org.apache.tomcat.util.digester.SetPropertiesRule begin

警告: \[SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'debug' to '0' did not find a matching property.

跟踪后发现是连接池的配置问题：

&lt;Context path="/n" docBase="server/webapps/manager " debug="0" reloadable="true" crossContext="false"&gt;

把debug="0"去除即可。

分析：tomcat的6.0.37这个版本中可能用别的属性替代了debug功能。

**6 Cacti监控tomcat的选项**

Graph templates，总共就4个选项，如下：

![](http://blog.itpub.net/attachment/201406/8/26230597_1402215574ohoH.jpg)

点击监控项连接进去，看到RRDTool Says: ERROR: invalid y-grid format报错信息，

解决办法是：

进右上角的连接\*Edit Graph Template--&gt;Tomcat-Connection Rate--&gt;Unit Grid Value (--unit/--y-grid)，默认的值为1，去掉改成为0即可。

同理解决Tomcat - Heap Statistics不出图的问题 将1048576值改空即可。

之后如果10分钟内还是出不了图，那么可以继续等待，tomcat监控模板出图时间比较漫长，我这里等待了3个小时候后，图出来了，如下所示：

1)Tomcat - Connection Rate：连接效率

![](http://blog.itpub.net/attachment/201406/8/26230597_1402215636ZEJH.jpg)  
​

2)Tomcat - Heap Statistics：JVM堆得使用情况

![](http://blog.itpub.net/attachment/201406/8/26230597_1402215624L279.jpg)

3)Tomcat - Thread Statistics：线程状态

![](http://blog.itpub.net/attachment/201406/8/26230597_14022156392uuE.jpg)

4)Tomcat – Throughput：Tomcat吞吐量

![](http://blog.itpub.net/attachment/201406/8/26230597_1402215690aBgg.jpg)

​