zabbix监控多个tomcat实例--自动发现

参考:http://www.iyunv.com/thread-227674-1-1.html

1、上传 catalina-jmx-remote.jar,

cmdline-jmxclient-0.10.3.jar,

jmx_discovery.sh 和 add_service_tomcat.sh 到 /tmp/目录下

2、给 add_service_tomcat.sh 赋执行权限,并执行

执行完这个脚本之后,需要重启你的tomcat服务,和zabbix_agentd的服务

3  cat  add_service_tomcat.sh  脚本内容

#,根据自己的情况各自改下你们安装tomcat的路径即可。

t_datadir=`find /home/edu-*/*/bin/ -name catalina.sh|awk -F "/bin/catalina.sh" ‘{print $1}‘`
tomcat_no=`find /home/edu-*/*/bin/ -name catalina.sh|awk -F "/bin/catalina.sh" ‘{print $1}‘|wc -l`
n_port=12345                                                                                #配置监控初始端口
local_ip=`ifconfig eth0 |awk -F ‘[ :]+‘ ‘NR==2 {print $4}‘`                                 #提取主机IP
for tomcat in $t_datadir
do
    #m_no=`cat -n $tomcat/bin/catalina.sh|grep ‘Execute The Requested Command‘|awk ‘{print $1}‘`    #提取代码插入位置
    cp $tomcat/bin/catalina.sh  $tomcat/bin/catalina.sh_bak                                        #备份catalina.sh
    cp /tmp/catalina-jmx-remote.jar  $tomcat/lib/catalina-jmx-remote.jar                           #复制文件到实例lib目录
    sed -i ‘‘84‘a export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote"‘  $tomcat/bin/catalina.sh                        #插入监控配置
    #let "m_no=m_no+1"                                                                                                                      #设置行号
    sed -i ‘‘85‘a export CATALINA_OPTS="$CATALINA_OPTS -Djava.rmi.server.hostname=‘$local_ip‘"‘ $tomcat/bin/catalina.sh                 #插入监控配置
    #let "m_no=m_no+1"                                                                                                                      #设置行号
    sed -i ‘‘86‘a export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.port=‘$n_port‘"‘ $tomcat/bin/catalina.sh          #插入监控配置
    #let "m_no=m_no+1"                                                                                                                      #设置行号
    sed -i ‘‘87‘a export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.ssl=false"‘ $tomcat/bin/catalina.sh               #插入监控配置
                                                                                                                 
    #let "m_no=m_no+1"                                                                                                                      #设置行号
    sed -i ‘‘88‘a export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.authenticate=false"‘ $tomcat/bin/catalina.sh      #插入监控配置
    let "n_port=n_port+1"
done
local_ip=`ifconfig eth0 |awk -F ‘[ :]+‘ ‘NR==2 {print $4}‘`                 #提取主机IP
cat >> /usr/local/zabbix/etc/zabbix_agentd.conf <<END                                 #修改zabbix_agentd.conf,添加KEY
UserParameter=java.jmx.discovery[*],/home/zabbix/bin/jmx_discovery.sh  jmx_port_discovery
UserParameter=java.Runtime.status[*],java -jar /lib/cmdline-jmxclient-0.10.3.jar - $local_ip:\$1 java.lang:type=Runtime \$2 2>&1 |grep \$2 |awk ‘{print \$NF}‘
UserParameter=java.Memory.status[*],java -jar /lib/cmdline-jmxclient-0.10.3.jar - $local_ip:\$1 java.lang:type=Memory \$2 2>&1 |grep \$2 |awk ‘{print \$NF}‘
UserParameter=java.System.status[*],java -jar /lib/cmdline-jmxclient-0.10.3.jar - $local_ip:\$1 java.lang:type=OperatingSystem \$2 2>&1 |grep \$2 |awk ‘{print \$NF}‘
UserParameter=java.HeapMemoryUsage.status[*],java -jar /lib/cmdline-jmxclient-0.10.3.jar - $local_ip:\$1 java.lang:type=Memory HeapMemoryUsage 2>&1 |grep \$2 |awk ‘{print \$NF}‘
UserParameter=java.NonHeapMemoryUsage.status[*],java -jar /lib/cmdline-jmxclient-0.10.3.jar - $local_ip:\$1 java.lang:type=Memory NonHeapMemoryUsage 2>&1 |grep \$2 |awk ‘{print \$NF}‘
UserParameter=java.LoadClass.status[*],java -jar /lib/cmdline-jmxclient-0.10.3.jar - $local_ip:\$1 java.lang:type=ClassLoading \$2 2>&1 |awk ‘{print \$NF}‘
UserParameter=java.Threading.status[*],java -jar /lib/cmdline-jmxclient-0.10.3.jar - $local_ip:\$1 java.lang:type=Threading \$2 2>&1 |awk ‘{print \$NF}‘
END

4 jmx_discovery.sh脚本

#!/bin/bash
# function:monitor tcp connect status from zabbix
source /etc/bashrc >/dev/null 2>&1
source /etc/profile >/dev/null 2>&1
jmx_port_discovery () {
jmx_port=($(cat /home/zabbix/scripts/java.txt|cut -d "|" -f2))
Tomcat_Name=($(cat /home/zabbix/scripts/java.txt|cut -d "|" -f1))
printf ‘{\n‘
printf ‘\t"data":[\n‘
for((i=0;i<${#jmx_port[@]};++i))
{
num=$(echo $((${#jmx_port[@]}-1)))
if [ "$i" != ${num} ];then
printf "\t\t{ \n"
printf "\t\t\t\"{#JMX_PORT}\":\"${jmx_port[$i]}\",\n"
printf "\t\t\t\"{#JAVA_NAME}\":\"${Tomcat_Name[$i]}\"},\n"
else
printf "\t\t{ \n"
printf "\t\t \n"
printf "\t\t\t\"{#JMX_PORT}\":\"${jmx_port[$i]}\",\n"
printf "\t\t\t\"{#JAVA_NAME}\":\"${Tomcat_Name[$i]}\"}]}\n"
fi
}
}
case "$1" in
jmx_port_discovery)
jmx_port_discovery
;;
*)
echo "Usage:$0 {jmx_port_discovery}"
;;
esac

5cat  find_port.sh

##获取tomcat的目录和端口

#!/bin/bash
tomcat_path=`find /home/*/*/bin/ -name catalina.sh|awk -F "/bin/catalina.sh" ‘{print $1}‘`
#tomcat_path=`ps -ef | grep [t]omcat | awk ‘{print $9}‘| awk -F"=|conf" ‘{print $3}‘`
for t_path in $tomcat_path
do
  t_service=`echo "$t_path"|awk -F"/" ‘{print $(NF)}‘`
  cd $t_path/bin
  t_port=`cat "$t_path"/bin/catalina.sh | grep 1234* |awk -F ‘"‘ ‘{print $2}‘| awk -F ‘=‘ ‘{print $2}‘`
  #t_port=`grep "Dcom.sun.management.jmxremote.port" catalina.sh |awk -F "=|\"" ‘{print $4}‘`
  echo "$t_path | $t_port"
  if [ $? -eq 0 ] && [ ! -z "$t_port" ];then
    echo "$t_service|$t_port"
   # echo $t_port
  else
       echo "$t_port must be number or is null"
  fi
done

6  执行find_port.sh脚本获取到的内容放到java.txt这个文本里

/home/edu-hdkt/tomcat-app-service | 12345
/home/edu-hdkt/tomcat-eclass-app | 12346
/home/edu-hdkt/tomcat-eclass-service | 12347
/home/edu-hdkt/tomcat-homework-services | 12348
/home/edu-hdkt/tomcat-redis-service | 12349
/home/edu-hdkt/tomcat-socket-1 | 12350
/home/edu-hdkt/tomcat-socket-2 | 12351
/home/edu-hdkt/tomcat-socket | 12352
/home/edu-hdkt/tomcat-statistics-service | 12353
/home/edu-hdkt/tomcat-student-service | 12354
/home/edu-hdkt/tomcat-sync | 12355
/home/edu-hdkt/tomcat-webapp | 12356
/home/edu-jcpt/edu-cas-server | 12357
/home/edu-jcpt/edu-platform-openApi | 12358
/home/edu-jcpt/edu-platform-recovery | 12359
/home/edu-jcpt/edu-platform-service | 12360
/home/edu-jcpt/edu-platform-webapp | 12361
/home/edu-jcpt/statistical-agent | 12362
/home/edu-jcpt/statistical-service | 12363
/home/edu-jcpt/tomcat-sync | 12364

7  测试在服务端执行下面的命令

/usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k java.jmx.discovery    测试 ip是客户端ip

返回下面这个即为正常

{
        "data":[
                { 
                        "{#JMX_PORT}":"12345",
                        "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-app-service"},
                { 
                        "{#JMX_PORT}":"12346",
                        "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-eclass-app"},
                { 
                        "{#JMX_PORT}":"12347",
                        "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-eclass-service"},
                { 
                        "{#JMX_PORT}":"12348",
                        "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-homework-services"},
                { 
                        "{#JMX_PORT}":"12349",
                        "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-redis-service"},
                { 
                        "{#JMX_PORT}":"12350",
                        "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-socket-1"},
                { 
                        "{#JMX_PORT}":"12351",
                        "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-socket-2"},
                { 
                        "{#JMX_PORT}":"12352",
                        "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-socket"},
                { 
                        "{#JMX_PORT}":"12353",
                        "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-statistics-service"},
                { 
                        "{#JMX_PORT}":"12354",
                        "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-student-service"},
                { 
                        "{#JMX_PORT}":"12355",
                        "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-sync"},
                { 
                        "{#JMX_PORT}":"12356",
                        "{#JAVA_NAME}":"/home/edu-hdkt/tomcat-webapp"},
                { 
                        "{#JMX_PORT}":"12357",
                        "{#JAVA_NAME}":"/home/edu-jcpt/edu-cas-server"},
                { 
                        "{#JMX_PORT}":"12358",
                        "{#JAVA_NAME}":"/home/edu-jcpt/edu-platform-openApi"},
                { 
                        "{#JMX_PORT}":"12359",
                        "{#JAVA_NAME}":"/home/edu-jcpt/edu-platform-recovery"},
                { 
                        "{#JMX_PORT}":"12360",
                        "{#JAVA_NAME}":"/home/edu-jcpt/edu-platform-service"},
                { 
                        "{#JMX_PORT}":"12361",
                        "{#JAVA_NAME}":"/home/edu-jcpt/edu-platform-webapp"},
                { 
                        "{#JMX_PORT}":"12362",
                        "{#JAVA_NAME}":"/home/edu-jcpt/statistical-agent"},
                { 
                        "{#JMX_PORT}":"12363",
                        "{#JAVA_NAME}":"/home/edu-jcpt/statistical-service"},
                { 
                 
                        "{#JMX_PORT}":"12364",
                        "{#JAVA_NAME}":"/home/edu-jcpt/tomcat-sync"}]}

第二个测试:

java -jar /home/soft/cmdline-jmxclient-0.10.3.jar - 58.116.1.2:12345 java.lang:type=Memory NonHeapMemoryUsage
07/10/2017 13:58:56 +0800 org.archive.jmx.Client NonHeapMemoryUsage: 
committed: 145186816
init: 2555904
max: -1
used: 141596768

第三个测试:

 /home/usr/local/zabbix/bin/zabbix_get -s 58.116.7.248 -k  java.HeapMemoryUsage.status[12346,max]
 返回下面的值即为正常
1072168960

8 最后倒入模板见附件

9 确认出图

时间: 2024-10-10 13:56:09

zabbix监控多个tomcat实例--自动发现的相关文章

Mysql 监控 支持 mysql 多实例自动发现以及主从监控

在[/usr/local/zabbix327/bin] 目录下新建python文件,并增加执行权限,如下: #!/usr/bin/env /usr/bin/python # _*_ coding:utf-8 _*_ import subprocess import json import re import ConfigParser def main(): # mysqld is started with mysqld_safe ,and with the configfile,or use 3

zabbix监控之模板使用、网络发现及邮件报警功能

一. 简介 1.介绍 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案,能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统工程师快速定位/解决存在的各种问题. zabbix由2部分构成,zabbix server与可选组件zabbix agent. zabbix server可以通过SNMP,zabbix agent,ping,端口监视等方法提供对远程服务器/网络状态的监视,数据收集等功能. zabbix agent需要安装在被监

zabbix根据主机和端口列表自动发现监控远程MongoDB实例

在有些情况下,只能使用远程的MongoDB实例而不能登录到MongoDB实例所在的服务器上,这时可以在一台服务器上维护一个远程MongoDB实例的主机和端口列表,然后通过zabbix的自动发现功能去根据主机和端口自动添加相应的监控.

Zabbix监控DB,Tomcat

因项目需要,Zabbix要监控SQL server的连接数,MySQL以及tomcat,下面是对监控上述item的简要介绍. 1. SQL server连接数 1.1 SQL server连接数 1. SQL Server查询当前连接数 2. Sql Server当前并发连接数的查看方法 3. 亲测SQLServer的最大连接数 4. SQL SERVER用户数.连接数.连接池License SELECT * FROM [Master].[dbo].[SYSPROCESSES] WHERE [D

zabbix监控Java和Tomcat

zabbix服务器需安装java,编译需添加启动参数--enable-java #zabbix服务器操作(监控Java和Tomcat都需配置此两步步骤) cat /usr/local/zabbix/sbin/zabbix_java/settings.sh LISTEN_IP="0.0.0.0" LISTEN_PORT=10052 START_POLLERS=12 #zabbix服务器操作 #JavaGateway现在和Zabbix-Server在同一台服务器上 cat /usr/loc

zabbix监控云端mysql等实例。

传统的mysql 服务器 用zabbix很容易实现,单对于云端的mysql 实例 我想大家都是用的云自带的监控.我们前端时间aws 云端mysql 实现主从 进程的出现问题,然而aws 自带的监控没有. 我们就想到 zabbix 实现对aws mysql 实例的监控. 1监控的服务远程去连接 awsmysql 取监控的数值就很容易实现了. 创建用户 GRANT USAGE,PROCESS,REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO [email 

用prometheus监控容器、apiserver以及自动发现并监控服务

使用内置的cAdvisor监控容器cAdvisor已经内置在了 kubelet 组件之中,所以我们不需要单独去安装,cAdvisor的数据路径为/api/v1/nodes/<node>/proxy/metrics1.增加job,更新prometheus配置 - job_name: 'kubernetes-cadvisor' kubernetes_sd_configs: - role: node scheme: https tls_config: ca_file: /var/run/secret

zabbix -- 监控一台主机实例演示

早起的鸟儿有虫吃,周末早起的虫儿不被吃,嘿嘿.                     ---- 小 Q 1.创建主机 2.创建监控项 3. 查看监控数据 4.创建用户 短信(微信)脚本 ------------------------------------------------------------------------------------1.创建主机 configuration(配置)-->Hosts(主机)-->Create host(创建主机) 注:此之前要创建个主机群组,

基于zabbix的Redis、Sentinel、Slave多实例自动发现监控

约定 保证whereis redis-cli 能够正确返回redis-cli程序的路径 保证 redis的配置文件在模板宏{$REDIS_SERVER_CONFIG_PATH}的路径,并且后缀名 为.conf 确保redis-server 配置文件配置:pidfile.requirepass 项,且有配置值 不满足条件会产生告警通知(详细见模板配置) 部署步骤 zabbix_agent 端配置 在/usr/local/zabbix327/bin 下增加lld_redis.py,并配置可执行权限