zabbix

/ 网络技术 / 0 条评论 / 992浏览

agent install
开放监听端口 firewall-cmd --zone=public --add-port=10050/tcp --permanent
firewall-cmd --reload

安装软件源
rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
rpm -ivh http://repo.zabbix.com/zabbix/2.2/rhel/7/x86_64/zabbix-release-2.2-1.el7.noarch.rpm
安装zabbix-agent
yum install zabbix-sender zabbix-agent zabbix

配置zabbix-agent
vi /etc/zabbix/zabbix_agentd.conf
Server ServerActive Hostname

启动并设置开机自启
systemctl restart zabbix-agent.service
systemctl enable zabbix-agent.service server install
参考:http://www.ttlsa.com/zabbix/install-zabbix-on-linux-5-ttlsa/

Java内存监控脚本

1.jvm_list.sh – 获得当前java进程的包名和目录

#cat jvm_list.sh #!/bin/bash packagePath=/home/zabbix/script/package_path.txt echo -n >$packagePath for i in `ps -fC java|tail -n +2|grep -v auto|awk '{print $2}'`; do pgrootpath=`ls -l /proc/$i/cwd|awk '{print $NF}'` packagename=$(basename $pgrootpath) echo "$packagename $pgrootpath" >> tmp.ini done grep emm4.2 tmp.ini > $packagePath sed -i "s/emm4.2/current/g" $packagePath rm -rf tmp.ini

2. get_jvmlist.sh – json化key值
#cat get_jvmlist.sh #!/bin/bash TABLESPACE=`awk '{print $1}' /home/zabbix/script/package_path.txt` COUNT=`echo "$TABLESPACE" |wc -l` INDEX=0 echo '{"data":[' echo "$TABLESPACE" | while read LINE; do echo -n '{"{#TABLENAME}":"'$LINE'"}' INDEX=`expr $INDEX + 1` if [ $INDEX -lt $COUNT ]; then echo ',' fi done echo ']}' 3. get_jvmstatus.sh – 使用jstat获得java进程的内存信息 #cat get_jvmstatus.sh #!/bin/bash scriptPath=/home/zabbix/script cat $scriptPath/package_path.txt|while read line do packageName=$(echo $line|awk '{print $1}') appPath=$(echo $line|awk '{print $2}') ps -ef|grep java|grep -v auto > id.txt pid=$(cat id.txt|grep "$appPath"|awk '{print $2}') javaHome=/usr/local/jdk18 echo --- $packageName $pid sleep 5 echo gcnew >> $scriptPath/package/$packageName 2>/dev/null $javaHome/bin/jstat -gcnew $pid >> $scriptPath/package/$packageName 2>/dev/null echo gcutil >> $scriptPath/package/$packageName 2>/dev/null $javaHome/bin/jstat -gcutil $pid >> $scriptPath/package/$packageName 2>/dev/null echo gcnewcapacity >> $scriptPath/package/$packageName 2>/dev/null $javaHome/bin/jstat -gcnewcapacity $pid >> $scriptPath/package/$packageName 2>/dev/null echo gccapacity >> $scriptPath/package/$packageName 2>/dev/null $javaHome/bin/jstat -gccapacity $pid >> $scriptPath/package/$packageName 2>/dev/null echo gc >> $scriptPath/package/$packageName 2>/dev/null $javaHome/bin/jstat -gc $pid >> $scriptPath/package/$packageName 2>/dev/null echo class >> $scriptPath/package/$packageName 2>/dev/null $javaHome/bin/jstat -class $pid >> $scriptPath/package/$packageName 2>/dev/null done rm -rf id.txt 4. set_jvmstatus.sh – 抓捕数据并输出,内存数据处理 # cat set_jvmstatus.sh #!/bin/bash packageName=$1 key=$2 if [ $2 == "S0C" -o $2 == "S0U" -o $2 == "S1C" -o $2 == "S1U" -o $2 == "DSS" -o $2 == "EC" -o $2 == "EU" ];then part=gcnew elif [ $2 == "S0" -o $2 == "S1" -o $2 == "E" -o $2 == "O" -o $2 == "M" -o $2 == "CCS" -o $2 == "YGCT" -o $2 == "FGCT" -o $2 == "GCT" ];then part=gcutil elif [ $2 == "S0CMX" -o $2 == "S1CMX" -o $2 == "ECMX" ];then part=gcnewcapacity elif [ $2 == "NGCMN" -o $2 == "NGCMX" -o $2 == "NGC" -o $2 == "OGCMX" -o $2 == "OGCMN" -o $2 == "OGC" -o $2 == "MCMN" -o $2 == "MCMX" -o $2 == "MC" -o $2 == "CCSMN" -o $2 == "CCSMX" -o $2 == "CCSC" -o $2 == "YGC" -o $2 == "FGC" ];then part=gccapacity elif [ $2 == "MU" -o $2 == "CCSU" -o $2 == "OC" -o $2 == "OU" ];then part=gc elif [ $2 == "Loaded" -o $2 == "Unloaded" ];then part=class elif [ $2 == "CPU" ];then part=cpu elif [ $2 == "MEM" ];then part=mem else echo "Error input:" exit 0 fi case $2 in S0C) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $1*1024}' ;; S0U) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $3*1024}' ;; S0) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%f\n", $0}' ;; S0CMX) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $4*1024}' ;; S1C) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $2*1024}' ;; S1U) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $4*1024}' ;; S1) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%f\n", $2}' ;; S1CMX) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $6*1024}' ;; DSS) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $7*1024}' ;; EC) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $8*1024}' ;; EU) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $9*1024}' ;; ECMX) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $8*1024}' ;; E) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%f\n", $3}' ;; NGCMN) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $1*1024}' ;; NGCMX) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $2*1024}' ;; NGC) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $3*1024}' ;; OC) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $7*1024}' ;; OU) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $8*1024}' ;; OGCMX) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $8*1024}' ;; OGCMN) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $7*1024}' ;; O) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%f\n", $4}' ;; OGC) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $9*1024}' ;; M) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%f\n", $5}' ;; MC) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $13*1024}' ;; MU) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $10*1024}' ;; MCMN) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $11*1024}' ;; MCMX) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $12*1024}' ;; CCS) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%f\n", $6}' ;; CCSC) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $13*1024}' ;; CCSU) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $12*1024}' ;; CCSMN) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $14*1024}' ;; CCSMX) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $15*1024}' ;; YGC) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $17}' ;; YGCT) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%f\n", $8}' ;; FGC) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $18}' ;; FGCT) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%f\n", $10}' ;; GCT) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%f\n", $11}' ;; TT) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $5}' ;; MTT) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $6}' ;; Loaded) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $1}' ;; Unloaded) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $3}' ;; CPU) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%f\n", $1}' ;; MEM) grep -wA 2 ^"$part" /home/zabbix/script/package/$1|tail -1|awk '{printf "%d\n", $1*1024}' ;; *) echo "Error input:" ;; esac exit 0

配置 在zabbix_agentd.conf添加以下两行,重启zabbix_agentd进程
UserParameter=jmx.discovery,/home/zabbix/script/get_jvmlist.sh
UserParameter=jmx.resource[*],/home/zabbix/script/set_jvmstatus.sh $1 $2

脚本在/home/zabbix/script目录下,需要给权限 在crontab里面定义jvm_list.sh和get_jvmstatus.sh脚本的定时任务
* */1 * * * /home/zabbix/script/jvm_list.sh
*/2 * * * * /home/zabbix/script/get_jvmstatus.sh

在zabbix_server上面通过zabbix_get命令去检查是否获取到了相应的数据
./zabbix_get -s 192.168.30.4 -k jmx.resource[AppServer,YGC]

导入模板 ....