shell 之 监控java应用的脚本集合----持续更新中

1.监控和展示应用版本号

shell 之 监控java应用的脚本集合----持续更新中_第1张图片

#!/bin/bash

function eureka {
ver=`sudo /usr/local/jdk1.8.0_151/bin/jps -l|grep -eureka-|awk -F '-' '{print $3}'|tr -cd "[0-9]"`
echo $ver
}


function eureka02 {
ver=`sudo /usr/local/jdk1.8.0_151/bin/jps -l|grep -eureka02|awk -F '-' '{print $3}'|tr -cd "[0-9]"`
echo $ver
}

$1

2.监控java应用线程数

#!/bin/bash


function webManager {
pid=`ps -ef | grep "webManager" | grep -v grep | awk '{print $2}'`
th=`ps huH p $pid| wc -l`
echo $th

}

function gateWay {
pid=`ps -ef | grep "gateWay" | grep -v grep | awk '{print $2}'`
th=`ps huH p $pid| wc -l`
echo $th
}

$1

3. 监控java应用TCP连接状态等参数

#!/bin/bash
#this script is used to get tcp and udp connetion status
#tcp status
metric=$1
tmp_file=/tmp/tcp_status.txt
/bin/netstat -an|awk '/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}' > $tmp_file
 
case $metric in
   closed)
          output=$(awk '/CLOSED/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   listen)
          output=$(awk '/LISTEN/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   synrecv)
          output=$(awk '/SYN_RECV/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   synsent)
          output=$(awk '/SYN_SENT/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   established)
          output=$(awk '/ESTABLISHED/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   timewait)
          output=$(awk '/TIME_WAIT/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   closing)
          output=$(awk '/CLOSING/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   closewait)
          output=$(awk '/CLOSE_WAIT/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   lastack)
          output=$(awk '/LAST_ACK/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
         ;;
   finwait1)
          output=$(awk '/FIN_WAIT1/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
         ;;
   finwait2)
          output=$(awk '/FIN_WAIT2/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
         ;;
         *)
          #echo -e "\e[033mUsage: sh  $0 [closed|closing|closewait|synrecv|synsent|finwait1|finwait2|listen|established|lastack|timewait]\e[0m"
   
esac

4.按照端口、pid、项目名、返回值来检测应用的存活

#1端口
#!/bin/bash
function aihealth {
Count=$(netstat -ln|awk '/^tcp/'|grep "18500"|wc -l)
if [ $Count -eq 0 ];then
    echo 0
else
    echo 1 
fi

}

$1
#2.pid
PID=$(netstat -nlp | grep :8001 | awk '{print $7}' | awk -F"/" '{ print $1 }')
if [ ! -n $PID ];then
echo "Skill is down,is just restart...">>/tmp/skill.out
kill -9 $PID
cd $dir
nohup python3  manage.py  runserver 0.0.0.0:8001 >nohup.out 2>&1  &

else
echo "$PID"

fi
#3.项目名
#!/bin/bash
function nginx_status {
Count=`ps -C nginx --no-header|  wc -l`
if [ $Count -eq 0 ];then
    echo 0
else 
    echo 1 
fi

}

function eureka {
Count=`ps -ef |grep eureka |grep -v grep|  wc -l`
if [ $Count -eq 0 ];then
    echo 0
else
    echo 1 
fi

}

$1
#4.返回值
#!/bin/bash
getcodes=`curl http://www.baidu.com:88/recommend/information`
returnchar=$(echo $getcodes|awk -F " " '{print $2}'|awk -F '<' '{print $1}')
if [ $returnchat=='请求测试成功' ] ;then
        echo "成功,No restart recommend"
	
else
        echo "失败"
	cd /home/recommender-system && /bin/bash shutAndstart.sh

fi

5.定时检测并重启java应用

			```bash
			#!/bin/bash
			source /etc/profile
			
			alive_web=`netstat -lnpt|grep :10006|wc -l`
			alive_job=`ps -ef|grep wjj-job|grep -v grep|awk '{print $2}'|wc -l`
			alive_wechatweb=`netstat -lnpt|grep :10136|wc -l`
			
			
			if [ $alive_web -eq 0 ];then
			        cd /jar/webmanager
				x=`ls -lt|grep .jar|head -n 1|awk '{print $NF}'`
			        nohup java -javaagent:/opt/oss/servicemgr/ICAgent/pinpoint/pinpoint-bootstrap.jar -Dapm_application=prod -Dapm_tier=prod-webmanager -javaagent:/jar/agent/skywalking-agent.jar -Dskywalking.agent.service_name=prod_webmanager  -jar $x --spring.profiles.active=prod --server.port=10006  > nohup.out 2>&1 &
			        echo "webmanager-01尝试自动重启,时间:`date`" >> /root/restart_record.txt
			fi
			
			
			if [ $alive_wechatweb -eq 0 ];then
			        cd /jar/wechat_webmanager
			        x=`ls -lt|grep .jar|head -n 1|awk '{print $NF}'`
			        nohup java -javaagent:/opt/oss/servicemgr/ICAgent/pinpoint/pinpoint-bootstrap.jar -Dapm_application=prod -Dapm_tier=prod-wechat-webmanager -javaagent:/jar/agent/skywalking-agent.jar -Dskywalking.agent.service_name=prod_wechat_webmanager  -jar $x --spring.profiles.active=prod --server.port=10136  > nohup.out 2>&1 &
			        echo "wechat-webmanager-01尝试自动重启,时间:`date`" >> /root/restart_record.txt
			fi
			
			
			if [ $alive_job -eq 0 ];then
			        cd /jar/job
				y=`ls -lt|grep .jar|head -n 1|awk '{print $NF}'`
			        nohup java -jar $y --spring.profiles.active=prod  > nohup.out 2>&1 &
			        echo "job尝试自动重启,时间:`date`" >> /root/restart_record.txt
			  else
			  exit 0
			fi
			```

6.监控大数据java应用进程的内存使用情况

		```bash
		#!/bin/bash
		source /etc/profile
		
		
		function zookeeper {
			cd /usr/java/jdk1.8.0_191/bin
			#pid=$(jps -l|grep "zookeeper"|awk -F " " '{print $1}')
			pid=$(ps -ef|grep zookeeper|grep ^zookee|awk -F " " '{print $2}'|head -1)
			used_mem=$(cat /proc/$pid/status|grep -e VmRSS|awk -F " " '{print $2}')
			used_mem=$(($used_mem/1024))
			
		echo ${used_mem}
		
		}
		
		
		
		
		function sqoop2 {
			pid=$(ps -ef|grep sqoop2|grep -v grep|head -1|awk -F " " '{print $2}')
			used_mem=$(cat /proc/$pid/status|grep -e VmRSS|awk -F " " '{print $2}')
			used_mem=$(($used_mem/1024))
			echo ${used_mem}
		
		}
		
		
		function kafka {
		        pid=$(ps -ef|grep kafka|grep ^kafka|grep -v grep|awk -F " " '{print $2}')
		        used_mem=$(cat /proc/$pid/status|grep -e VmRSS|awk -F " " '{print $2}')
		        used_mem=$(($used_mem/1024))
		        echo ${used_mem}
		
		}
		
		function hue {
			used_mem=0
			pid_str=$(ps -ef|grep hue|grep ^hue|head -4|awk -F " " '{print $2}')
			arr=(${pid_str// /})
			for p in ${arr[*]}
			  do
		   	   mem=$(cat /proc/$p/status|grep -e VmRSS|awk -F " " '{print $2}')
			   mem=$(($mem/1024))
			   used_mem=$(($mem+$used_mem))
			done
			echo ${used_mem}
		
		}
		function sentry {
		        pid=$(ps -ef|grep sentry|grep ^sentry|grep -v grep|head -1|awk -F " " '{print $2}')
		        used_mem=$(cat /proc/$pid/status|grep -e VmRSS|awk -F " " '{print $2}')
		        used_mem=$(($used_mem/1024))
		        echo ${used_mem}
		
		}
		
		function hbase {
			pid=$(ps -ef|grep org.apache.hadoop.hbase|grep -v grep|awk -F " " '{print $2}')
		        used_mem=$(cat /proc/$pid/status|grep -e VmRSS|awk -F " " '{print $2}')
		        used_mem=$(($used_mem/1024))
		        echo ${used_mem}
		
		}
		
		
		function yarn {
		        pid=$(ps -ef|grep org.apache.hadoop.yarn.server.resourcemanager.ResourceManager|grep -v grep|awk -F " " '{print $2}')
		        used_mem=$(cat /proc/$pid/status|grep -e VmRSS|awk -F " " '{print $2}')
		        used_mem=$(($used_mem/1024))
		        echo ${used_mem}
		
		}
		
		function hive {
		        used_mem=0
		        pid_str=$(ps -ef|grep hive|grep ^hive|awk -F " " '{print $2}')
		        arr=(${pid_str// /})
		        for p in ${arr[*]}
		          do
		           mem=$(cat /proc/$p/status|grep -e VmRSS|awk -F " " '{print $2}')
		           mem=$(($mem/1024))
		           used_mem=$(($mem+$used_mem))
		        done
		        echo ${used_mem}
		
		}
		
		function flume {
		        used_mem=0
		        pid_str=$(ps -ef|grep org.apache.flume.node.Application|grep -v grep |awk -F " " '{print $2}')
		        arr=(${pid_str// /})
		        for p in ${arr[*]}
		          do
		           mem=$(cat /proc/$p/status|grep -e VmRSS|awk -F " " '{print $2}')
		           mem=$(($mem/1024))
		           used_mem=$(($mem+$used_mem))
		        done
		        echo ${used_mem}
		
		}
		
		function hdfs {
		        used_mem=0
		        pid_str=$(ps -ef|grep org.apache.hadoop.hdfs|grep -v grep|awk -F " " '{print $2}')
		        arr=(${pid_str// /})
		        for p in ${arr[*]}
		          do
		           mem=$(cat /proc/$p/status|grep -e VmRSS|awk -F " " '{print $2}')
		           mem=$(($mem/1024))
		           used_mem=$(($mem+$used_mem))
		        done
		        echo ${used_mem}
		
		}
		
		function  spark {
		        pid=$(ps -ef|grep org.apache.spark|grep -v grep|awk -F " " '{print $2}')
		        used_mem=$(cat /proc/$pid/status|grep -e VmRSS|awk -F " " '{print $2}')
		        used_mem=$(($used_mem/1024))
		        echo ${used_mem}
		
		}
		
		function azkaban {
		        used_mem=0
		        pid_str=$(ps -ef|grep Azkaban|grep -v grep|awk -F " " '{print $2}')
		        arr=(${pid_str// /})
		        for p in ${arr[*]}
		          do
		           mem=$(cat /proc/$p/status|grep -e VmRSS|awk -F " " '{print $2}')
		           mem=$(($mem/1024))
		           used_mem=$(($mem+$used_mem))
		        done
		        echo ${used_mem}
		
		}
		function  mapreduce {
		        pid=$(ps -ef|grep org.apache.hadoop.mapreduce|grep -v grep|awk -F " " '{print $2}')
		        used_mem=$(cat /proc/$pid/status|grep -e VmRSS|awk -F " " '{print $2}')
		        used_mem=$(($used_mem/1024))
		        echo ${used_mem}
		
		}
		function cloudera {
			used_mem=0
		        pid_str=$(ps -ef|grep cmf|grep ^cloud|awk -F " " '{print $2}')
		        arr=(${pid_str// /})
		        for p in ${arr[*]}
		          do
		           mem=$(cat /proc/$p/status|grep -e VmRSS|awk -F " " '{print $2}')
		           mem=$(($mem/1024))
		           used_mem=$(($mem+$used_mem))
		        done
		        echo ${used_mem}
		
		
		
		
		
		}
		
		
		
		
		$1
		```

x.未完待续…

你可能感兴趣的:(监控系统及应用,shell基础及实践)