轻量级监控平台之java进程监控脚本
轻量级监控平台之java进程监控脚本
#!/bin/bash
#进程监控脚本
#功能需求: 上报机器Java进程的进程ID,对应的端口号service tcp端口号,tomcat http 端口号,以及tcp链接数数据
. /etc/profile
. ~/.bash_profile
pushurl="https://lightmonitorapi.test.cn/push/process"
proNum=$(jps | grep -v Jps | cut -d' ' -f1)
i=1
cd /opt/web/
webDir=$( ls )
serviceRunning=$(service list | grep RUNNING)
msg=""
for pid in $proNum
do
port=$(echo "$serviceRunning" | grep $pid | awk -F " " '{print $8}')
if [ -n "$port" ];then
serviceName=$(echo "$serviceRunning" | grep $pid | awk -F " " '{print $1}')
threadTime=$(ps -eo pid,tty,user,comm,stime,etime | grep "$pid"| cut -c 46-60)
tcpStatus=$(netstat -nat | grep $port | awk '{print $6}' | sort | uniq -c)
escount=$( echo "$tcpStatus" | grep "ESTABLISHED" | awk '{print $1}')
cwcount=$( echo "$tcpStatus" | grep "CLOSE_WAIT" | awk '{print $1}')
twcount=$( echo "$tcpStatus" | grep "TIME_WAIT" | awk '{print $1}')
licount=$( echo "$tcpStatus" | grep "LISTEN" | awk '{print $1}')
msg=$msg"$pid,$serviceName,$port,$threadTime,T,E$escount,C$cwcount,T$twcount,L$licount\n"
else
for fileName in $webDir
do
fileDir=$(ps -ef | grep "$pid" | grep "$fileName")
if [ -n "$fileDir" ];then
serverPath="/opt/web/$fileName/conf/server.xml"
threadTime=$(ps -eo pid,tty,user,comm,stime,etime | grep "$pid"| cut -c 46-60)
httpPort=$(cat "$serverPath" | grep 'Connector' | cut -d' ' -f6 | awk -F "[\"\"]" '{print $2}')
tcpStatus=$(netstat -nat | grep $httpPort | awk '{print $6}' | sort | uniq -c)
escount=$( echo "$tcpStatus" | grep "ESTABLISHED" | awk '{print $1}')
cwcount=$( echo "$tcpStatus" | grep "CLOSE_WAIT" | awk '{print $1}')
twcount=$( echo "$tcpStatus" | grep "TIME_WAIT" | awk '{print $1}')
licount=$( echo "$tcpStatus" | grep "LISTEN" | awk '{print $1}')
msg=$msg"$pid,$fileName,$httpPort,$threadTime,H,E$escount,C$cwcount,T$twcount,L$licount\n"
fi
done
fi
done
ip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d '/')
ticket=$(cat /opt/shell/ticket.txt)
curl $pushurl \
-H "Accept: application/json" \
-H 'Content-Type: application/json' \
-d \ '{"ip": "'"${ip}"'", "ticket": "'"${ticket}"'", "data": "'"${msg}"'"}'