zabbix监控自动发现监控tomcat(V1)

背景说明:

由于zabbix监控使用自带的模版,只能监控主机上只有1个tomcat的场景适合,虽然网上很多朋友都是在每个监控项上面添加一个空格来解决问题。但是个人感觉这种方法还是蛮麻烦的,所以写一篇使用自动发现tomcat并监控。

 

1,在一台主机上安装2个tomcat,安装完1个tomcat,复制1个即可。复制完之后修改tomcat的端口即可。

https://www.cnblogs.com/lin1/p/5689622.html

 

 

2,进入到apache-tomcat1目录,开启jmxport端口。

在被监控的apache-tomcat1应用程序的找到catalina.sh,添加如下内容

CATALINA_OPTS="$CATALINA_OPTS
-Djavax.management.builder.initial=
-Dcom.sun.management.jmxremote=true
-Dcom.sun.management.jmxremote.port=12345 
-Dcom.sun.management.jmxremote.authenticate=false  
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=127.0.0.1"

 

在被监控的apache-tomcat2应用程序的找到catalina.sh,添加如下内容

CATALINA_OPTS="$CATALINA_OPTS
-Djavax.management.builder.initial=
-Dcom.sun.management.jmxremote=true
-Dcom.sun.management.jmxremote.port=12346 
-Dcom.sun.management.jmxremote.authenticate=false  
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=127.0.0.1"

 

2个tomcat应用程序启动jmx端口后,重启tomcat.

 

 

3,下载获取数据的测试工具

cd  /etc/zabbix/zb_monitor_script

wget  https://coding.net/s/73ce6ac2-a902-45f3-b781-d97d5cbc79d1

简单测试:

java -jar jmxcmd.jar -  127.0.0.1:12345

java -jar jmxcmd.jar -  127.0.0.1:12346

 

4,定义自动发现脚本

cd /etc/zabbix/zb_monitor_script

vim tomcat_discovery.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/9/17 下午9:13
# @Author  : lin
# @File    : discovery tomcat.py

import json,subprocess

args="sudo /bin/find /usr/local/ -name 'server.xml'|awk -F '/' '{print $4}'|sort|uniq"

t=subprocess.Popen(args,shell=True,stdout=subprocess.PIPE).communicate()[0]

tomcats=[]



for tomcat in t.split('\n'):
    if len(tomcat) != 0:
        tomcats.append({'{#TOMCAT_NAME}':tomcat})

print json.dumps({'data':tomcats},indent=4,separators=(',',':'))

chmod a+x  tomcat_discovery.py

 

测试,自动发现,看看能不能发现tomcat,如下说明无问题。

python tomcat_discovery.py

 

5,配置监控tomcat的脚本

vim  tomcat_monitor.sh

#!/bin/bash
source /etc/profile
[  $# -ne 2 ] && echo 'The scripts need 2 parameters'&&exit 1 
TOMCAT_NAME=$1
ITEM=$2

JMX_PORT=`ps -ef|grep $1|grep -vE 'grep|sh'|awk -F '=' '{print $6}'|awk '{print $1}'`
#读取server.xml配置文件,获取端口
xml=/usr/local/$1/conf/server.xml

PORT=`sed -n '69'p $xml |awk -F '[= " ]+' '{print $4}' `
cmd=/etc/zabbix/zb_monitor_script/cmdline-jmxclient-0.10.3.jar
logdir=/tmp/zabbix_tmp
[ ! -d "$logdir" ] && mkdir -p $logdir && chmod 777 $logdir
cd $logdir

function HeapMemoryUsage()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=Memory HeapMemoryUsage 2> $ITEM.$JMX_PORT
}

function EdenSpaceUsage()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=MemoryPool,name=Eden\ Space Usage  2> $ITEM.$JMX_PORT
}


function SurvivorSpaceUsage()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=MemoryPool,name=Survivor\ Space Usage   2> $ITEM.$JMX_PORT
}

function TenuredGenUsage() 
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=MemoryPool,name=Tenured\ Gen Usage  2> $ITEM.$JMX_PORT
}

function NonHeapMemoryUsage()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=Memory  NonHeapMemoryUsage 2> $ITEM.$JMX_PORT
}

function MetaspaceUsage()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=MemoryPool,name=Metaspace Usage 2> $ITEM.$JMX_PORT
}

function CodeCacheUsage()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=MemoryPool,name=Code\ Cache Usage 2> $ITEM.$JMX_PORT
}

function CompressedClassSpaceUsage()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=MemoryPool,name=Compressed\ Class\ Space Usage 2> $ITEM.$JMX_PORT
}

function TotalLoadedClassCount()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=ClassLoading TotalLoadedClassCount  2> $ITEM.$JMX_PORT
}

function LoadedClassCount()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=ClassLoading LoadedClassCount  2> $ITEM.$JMX_PORT
}

function UnloadedClassCount()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=ClassLoading UnloadedClassCount  2> $ITEM.$JMX_PORT
}

function TotalStartedThreadCount()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=Threading TotalStartedThreadCount  2> $ITEM.$JMX_PORT
}

function ThreadCount()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=Threading ThreadCount 2> $ITEM.$JMX_PORT
}

function PeakThreadCount()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=Threading PeakThreadCount  2> $ITEM.$JMX_PORT
}

function maxThreads()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT Catalina:name=\"http-nio-$PORT\",type=ThreadPool maxThreads  2> $ITEM.$JMX_PORT
}

function currentThreadCount()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT Catalina:name=\"http-nio-$PORT\",type=ThreadPool currentThreadCount 2>$ITEM.$JMX_PORT
}

function currentThreadsBusy()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT Catalina:name=\"http-nio-$PORT\",type=ThreadPool currentThreadsBusy 2>$ITEM.$JMX_PORT      
}

function GlobalRequestProcessor_bytesReceived()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT Catalina:name=\"http-nio-$PORT\",type=GlobalRequestProcessor bytesReceived 2>$ITEM.$JMX_PORT      
}

function GlobalRequestProcessor_bytesSent()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT Catalina:name=\"http-nio-$PORT\",type=GlobalRequestProcessor bytesSent 2>$ITEM.$JMX_PORT      
}

function requestCount()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT Catalina:name=\"http-nio-$PORT\",type=GlobalRequestProcessor requestCount 2>$ITEM.$JMX_PORT      
}

function errorCount()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT Catalina:name=\"http-nio-$PORT\",type=GlobalRequestProcessor errorCount 2>$ITEM.$JMX_PORT    
}

function jvmUptime()
{
java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=Runtime Uptime 2> $ITEM.$JMX_PORT  
}



# function collectdata()
# {
# java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=Memory HeapMemoryUsage 2> $ITEM.$JMX_PORT
# java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=MemoryPool,name=Eden\ Space Usage  2> $ITEM.$JMX_PORT
# java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=MemoryPool,name=Survivor\ Space Usage   2> $ITEM.$JMX_PORT
# java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=MemoryPool,name=Tenured\ Gen Usage  2> $ITEM.$JMX_PORT
# java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=Memory  NonHeapMemoryUsage 2> $ITEM.$JMX_PORT
# java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=MemoryPool,name=Metaspace Usage 2> $ITEM.$JMX_PORT
# java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=MemoryPool,name=Code\ Cache Usage  2> $ITEM.$JMX_PORT
# java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=MemoryPool,name=Compressed\ Class\ Space Usage 2> $ITEM.$JMX_PORT
# java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=ClassLoading TotalLoadedClassCount  2> $ITEM.$JMX_PORT
# java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=ClassLoading LoadedClassCount  2> $ITEM.$JMX_PORT
# java  -jar $cmd - 127.0.0.1:$JMX_PORT java.lang:type=ClassLoading UnloadedClassCount  2> $ITEM.$JMX_PORT
# }

case $ITEM in
        #统计堆空间堆                   
        HeapMemoryUsage.max)
        HeapMemoryUsage
        sed -n '4p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        HeapMemoryUsage.used)
        HeapMemoryUsage
        sed -n '5p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        HeapMemoryUsage.committed)
        HeapMemoryUsage
        sed -n '2p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        #统计伊甸元代空间
        EdenSpaceUsage.max)
        EdenSpaceUsage
        sed -n '4p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        EdenSpaceUsage.used)
        EdenSpaceUsage
        sed -n '5p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        EdenSpaceUsage.committed)
        EdenSpaceUsage
        sed -n '2p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        #统计内存池survivor space(幸存区空间)
        SurvivorSpaceUsage.max)
        SurvivorSpaceUsage
        sed -n '4p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        SurvivorSpaceUsage.used)
        SurvivorSpaceUsage
        sed -n '5p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        SurvivorSpaceUsage.committed)
        SurvivorSpaceUsage
        sed -n '2p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        #统计内存池old gen(Tenured Gen 老年代空间)
        TenuredGenUsage.max)
        TenuredGenUsage
        sed -n '4p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        TenuredGenUsage.used)
        TenuredGenUsage
        sed -n '5p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        TenuredGenUsage.committed)
        TenuredGenUsage
        sed -n '2p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        #统计非堆内存
        NonHeapMemoryUsage.used)
        NonHeapMemoryUsage
        sed -n '5p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        NonHeapMemoryUsage.committed)
        NonHeapMemoryUsage
        sed -n '2p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        #统计内存池meta space(元数据空间)
        MetaspaceUsage.used)
        MetaspaceUsage
        sed -n '5p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        MetaspaceUsage.committed)
        MetaspaceUsage
        sed -n '2p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        #统计内存池code cache(代码缓存)
        CodeCacheUsage.max)
        CodeCacheUsage
        sed -n '4p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        CodeCacheUsage.used)
        CodeCacheUsage
        sed -n '5p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        CodeCacheUsage.committed)
        CodeCacheUsage
        sed -n '2p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        #统计compressed class space(压缩类的空间)
        CompressedClassSpaceUsage.max)
        CompressedClassSpaceUsage
        sed -n '4p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        CompressedClassSpaceUsage.used)
        CompressedClassSpaceUsage
        sed -n '5p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        CompressedClassSpaceUsage.committed)
        CompressedClassSpaceUsage
        sed -n '2p' $ITEM.$JMX_PORT|awk '{print $2}'
        ;;
        #统计类加载的个数
        ClassLoading.TotalLoadedClassCount)
        TotalLoadedClassCount
        awk '{print $6}' $ITEM.$JMX_PORT
        ;;
        ClassLoading.LoadedClassCount)
        LoadedClassCount
        awk '{print $6}' $ITEM.$JMX_PORT
        ;;
        ClassLoading.UnloadedClassCount)
        UnloadedClassCount
        awk '{print $6}' $ITEM.$JMX_PORT
        ;;
        #统计java线程数
        Threading.TotalStartedThreadCount)
        TotalStartedThreadCount
        awk '{print $6}' $ITEM.$JMX_PORT
        ;;
        ThreadCount)
        ThreadCount
        awk '{print $6}' $ITEM.$JMX_PORT
        ;;
        PeakThreadCount)
        PeakThreadCount
        awk '{print $6}' $ITEM.$JMX_PORT
        ;;
        #统计tomcat的线程数
        maxThreads)
        maxThreads
        awk '{print $6}' $ITEM.$JMX_PORT
        ;;
        currentThreadCount)
        currentThreadCount
        awk '{print $6}' $ITEM.$JMX_PORT
        ;;
        currentThreadsBusy)
        currentThreadsBusy
        awk '{print $6}' $ITEM.$JMX_PORT
        ;;
        #统计tomcat网络流量
        bytesReceived)
        GlobalRequestProcessor_bytesReceived
        awk '{print $6}' $ITEM.$JMX_PORT
        ;;
        bytesSent)
        GlobalRequestProcessor_bytesSent
        awk '{print $6}' $ITEM.$JMX_PORT
        ;;
        #统计tomcat的请求数
        requestCount)
        requestCount
        awk '{print $6}' $ITEM.$JMX_PORT
        ;;
        errorCount)
        errorCount
        awk '{print $6}' $ITEM.$JMX_PORT
        ;;
        #jvm运行时间,如果运行时间没获取到数据,则表示jvm stop ,从而判断tomcat stop
        jvmUptime)
        jvmUptime
        [ $? -eq 0 ]&&awk '{print $6/1000}' $ITEM.$JMX_PORT||echo 0
        ;;
esac
View Code

 

6,定义监控配置文件

cd /etc/zabbix/zabbix_agentd.d/

vim userparameter_tomcat.conf

UserParameter=tomcat.discovery,/usr/bin/python /etc/zabbix/zb_monitor_script/tomcat_discovery.py
UserParameter=tomcat.status[*],/bin/bash /etc/zabbix/zb_https://coding.net/s/03996946-8b2c-4b42-9bdf-957f17800315monitor_script/tomcat_monitor.sh  $1 $2

 

7,重启zabbix-agent服务

/etc/init.d/zabbix-agent restart 

 

8,web界面添加主机,导入模版。

模版地址:https://coding.net/s/03996946-8b2c-4b42-9bdf-957f17800315

 

 

 

posted @ 2018-09-26 11:17  某电商首席日志工程师  阅读(1471)  评论(2编辑  收藏  举报