Monitoring java programs using jstat autodiscovery

The project has been running for several days and summarizes the reasons:

  1. Autodiscover registration monitoring does not understand the principles, and many legacy scripts are not supported before.
  2. I don't know the script completely yet, so I'll have to look at it when I have time
  3. Or ideas, solutions to the problem tens of millions of ways, don't hang on a tree, try several methods of death I'm lazy, I planned to use jmx monitoring before, found that the company used supervisorctl management jar package to start, I also used it for the first time, do not elaborate here.
  4. Use the jstat command to get the parameters, use the scheduled task here, put it in the script before, the service is dead.

    Dry goods go directly here:

Monitoring Indicators:
S0C: Capacity (in bytes) of the first survivor in the younger generation
 S1C: Capacity (in bytes) of the second survivor in the younger generation
 S0U: The first survivor of the younger generation is currently using space (bytes)
S1U: The second survivor of the younger generation is currently using space (bytes)
EC: Capacity (in bytes) of Eden in the younger generation
 EU: Eden (Garden of Eden) is using space (bytes) in the younger generation
 OC:Old generation capacity (bytes)
Space (bytes) currently in use in OU:Old generation
 Capacity (bytes) of PC:Perm (persistent generation)
PU:Perm (persistent generation) Space in use (bytes)
YGC: The number of GCs in the younger generation from application startup to sampling!
YGCT: Time taken from application startup to gc in the younger generation at sampling time (s)
FGC: Number of GC from application startup to old generation (full gc) sampling!
FGCT: Time taken from application startup to old generation (full gc)gc of sampling (s)
S0: Percentage of current capacity used by the first survivor in the younger generation
 S1: Percentage of current capacity used by the second survivor (survival zone) in the younger generation
 E: Percentage of current capacity used by Eden in the younger generation
 Percentage of current capacity used by O:old generation
 perm Generation Used Percentage of Current Capacity

Dry goods directly

Script 1:

[root@iZj6chku2ng9ofxksew0oiZ userparameter_scripts]# cat java_name_discovery.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
import subprocess
import simplejson as json

t=subprocess.Popen("ps -ef | grep java|grep -vE '(grep|/bin/bash)'|awk '{print $9}'|awk -F '=' '{print $2}'",stdout=subprocess.PIPE,shell=True).communicate()[0]
#data=t.stdout.readlines()
#print(data)
tomcats=[]

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

# Print out zabbix recognizable json format
print json.dumps({'data':tomcats},sort_keys=True,indent=4,separators=(',',':'))

//This script is used to automatically discover java program print names and return them to json format for zabbix to read!
//Require pip commands and install related modules

Script 2:

#!/bin/bash
JAVA_NAME=$1
STATUS=$2
export PATH=$PATH:/usr/java/jdk1.8.0_144/bin/
PID=$(ps -ef | grep $JAVA_NAME | grep '/bin/java' | grep -v grep | awk '{print $2}')
#echo $PID
fun1 () {
#    echo "1"
     jstat -gc  $PID | awk -v st=$STATUS '{for(i=1;i<=NF;i++) if($i ==st) n=i} END{print $n}'
}
fun2 () {
#    echo "2"
    jstat -gcutil $PID | awk -v st=$STATUS '{for(i=1;i<=NF;i++) if($i ==st) n=i} END{print $n}'
}
case $STATUS in
   "S0C"| "S1C"|"S0U"|"S1U"|"EC"|"EU"|"OC"|"OU"| "YGC"|"YGCT"|"FGC"|"FGCT")
        fun1;;
   "S0"|"S1"|"E"|"O")
        fun2;;
   *)echo "print number error"
    exit1;;
esac

Zabbix Profile

UserParameter=java.name.discovery,sudo  /usr/bin/python /etc/zabbix/zabbix_agentd.d/userparameter_scripts/java_name_discovery.py
UserParameter=java.status.monitor[*],sudo  /etc/zabbix/zabbix_agentd.d/userparameter_scripts/java_status_monitor.sh $1 $2

Here's a note about permissions: sudo Secret-Free Configuration

visudo

zabbix  ALL=(ALL)       NOPASSWD: ALL. #Secret-free
Defaults:zabbix    !requiretty        #No login required

Where the Zabbix configuration file needs to be modified:

AllowRoot=1                            #AllowRoot=1 (allow running as root)

Note: Restart the zabbix-agent after modifying the configuration and use the zabbix_get to get the data to see if it is available properly

Keywords: Linux Zabbix Java JSON Tomcat

Added by mbaroz on Tue, 07 Jan 2020 01:11:29 +0200