针对特定的格式开发的监控脚本。如有类似可以参考,或者针对文件名进行修改即可。本脚本也经历了两次重构。
说明:
1.我的文件格式:cr-out.1.0.0.jar
2.开发使用的是springboot进行开发,这里脚本也是利用springboot中health来获得监控信息
3.shell目录结构
op
|-- bin #存放所有脚本路径
| |-- EnVariable.sh
| |-- out.sh
|-- conf #脚本配置文件
| |-- filejar.txt
|-- logs #日志
| |-- out.lst
| |-- out.lst.2021-05-01
| |-- out.lst.2021-06-01
| |-- out.lst.2021-07-01
| |-- out.lst.2021-08-01
|-- olds #旧脚本存放路径,这里考虑万一原来的重构没弄好,旧的还能继续用。
|-- temporary #存放临时文件
|-- share #存放公共shell调用路径
监控jar健康状态变量脚本
#!/bin/sh
# Author:
# Date: 2019/10/25
# DES:
# filename: EnVariable.sh
# Designation JAVA_HOME and HOME
# Environmental variable
HOME=/home/credit
JAVA_HOME=$HOME/apps/jdk1.8.0_121
# jar file storage path
_JARGC=$HOME/new
# out.sh Environmental variable
_JARLOGS=$_JARGC/logs
_JARFILE=`cat $HOME/op/config/filejar.txt`
# clear logs date
_LOGS=`date +%d%H%M`
# log file
_FILELOG=$HOME/op/logs/out.lst
监控jar状态配置文件
cat $HOME/op/config/filejar.txt
ABC 10001
ccc 10002
bbbb 10004
ddd 10003
监控jar健康状态主程序
#!/usr/bin/bash
# Date:2019/11/04
# Author:
# DESC:
#
# crontab配置 crontab: */5 * * * * /usr/bin/sh $HOME/op/bin/out.sh
# filename: out.sh
. $HOME/op/bin/EnVariable.sh
# On the 1st of the month, 24 clock status log and add #.
if [[ "010005" == ${_LOGS} ]];then
mv ${_FILELOG} ${_FILELOG}.`date +%F`
true > ${_FILELOG}
find ${_FILELOG}.* -mtime +30 -exec rm -rf {} \;
fi
# End identifier
IFS=$'\n'
# 批量处理
for i in ${_JARFILE};do
# 获取要启动的jar程序名
_name=`echo $i|awk '{print $(NF-1)}'`
# 获取端口号
_port=`echo $i|awk '{print $NF}'`
# 获取pid号,重启时需要杀掉进程
_pid=`ps -ef|grep -v grep | grep ${_name}| grep java | awk '{print $2}'`
# **获取health状态码**
_url=`curl -I -m 30 -o /dev/null -s -w %{http_code} http://127.0.0.1:${_port}/${_name}/health`
# 针对内存溢出是做的监控,出现指定的java.lang.OutOfMemoryError: Metaspace 也重启
_num=`grep 'java.lang.OutOfMemoryError: Metaspace' ${_JARLOGS}/${_name}0.log | wc -l`
if [[ ${_num} -gt 0 || ${_url} -ne 200 ]];then
# 条件满足就重启jar程序
cd ${_JARGC};sh ./auto ${_name} > /dev/null 2>&1
# 日志输出
echo "`date +%F\ %r` is restart \"cr-${_name}-1.0.0.jar\".PID number is "${_pid}".status code ${_url}." >> ${_FILELOG}
#sleep 60
fi
sleep 1
done