zabbix監控自動發現監控tomcat(V1)

背景說明:html

因爲zabbix監控使用自帶的模版,只能監控主機上只有1個tomcat的場景適合,雖然網上不少朋友都是在每一個監控項上面添加一個空格來解決問題。可是我的感受這種方法仍是蠻麻煩的,因此寫一篇使用自動發現tomcat並監控。java

 

1,在一臺主機上安裝2個tomcat,安裝完1個tomcat,複製1個便可。複製完以後修改tomcat的端口便可。python

http://www.javashuo.com/article/p-eoiugjke-gp.htmlweb

 

 

2,進入到apache-tomcat1目錄,開啓jmxport端口。shell

在被監控的apache-tomcat1應用程序的找到catalina.sh,添加以下內容apache

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,添加以下內容json

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.vim

 

 

3,下載獲取數據的測試工具緩存

cd  /etc/zabbix/zb_monitor_scripttomcat

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

 

 

相關文章
相關標籤/搜索