shell脚本实现服务器进程监控的方法
- 写作背景:
项目是基于java、weblogic及timer的处理模式,每次服务部署之后timer的整体状态是一个盲区,因为100多个进程,是否有启动遗漏或者启动重复的,想做到一目了然是件困难的事情。所以我考虑用shell脚本的文件查找与匹配方法来解决这个问题,简单记录下,仅供参考。
- 程序说明:
1)通过将系统定义的进程(timer)配置到配置文件process_signal.config中
2)将服务器当前所有的java进程存储到process_current.txt中
3)逐行读取process_signal.config中的进程,到process_current.txt中进行匹配
4)没有匹配到则为启动遗漏,存入到process_invalid.txt中,匹配到1次正常(不做处理),匹配到大于1次,则将timer名称及启动次数记录到process_invalid.txt中。
5)通过定时器30分钟一次执行脚本,使用SQLLOAD将process_invalid.txt加载到数据表中,对数据库表进行监控。
- 脚本说明:
1、整体文件结构:
webusr@iomtimer1:/weblogic/script/process_signal >ls process_current.txt process_invalid.txt process_invalid_his.txt process_signal.config process_signal.sh
2、进程配置示例:
webusr@iomtimer1:/weblogic/script/process_signal >cat process_signal.config #停复发NAS extapp.tftonas.PutTfToNas 10 2 extapp.tftonas.PutTfToNas 11 2 extapp.tftonas.PutTfToNas 12 3
3、执行结果记录:
webusr@iomtimer1:/weblogic/script/process_signal >cat process_invalid.txt 127.0.0.1 process [extapp.leaseroute.PutToLeaseRoute 180] is not exist 2018-04-11 08:29:00 127.0.0.1 process [ DealWorkTaskTimer 3001 ] is repeatRun 2 Times 2018-04-11 17:51:56
4、逻辑匹配入口:
webusr@iomtimer1:/weblogic/script/process_signal >cat process_signal.sh #!/bin/sh path=/weblogic/script/process_signal #脚本路径 file_config=process_signal.config #配置文件名 file_result=process_invalid.txt #错误进程信息 file_tmp=process_current.txt #当前进程信息(全量,临时) if_get_exist="N" #是否输出日志(N不输出,Y输出) hostip="127.0.0.1" #主机IP currenttime=`date "+%Y-%m-%d %H:%M:%S"` #当前系统时间 #定义方法get_process_signal() get_process_signal(){ # 转储上次记录到历时,追加 cat /weblogic/script/process_signal/process_invalid.txt >> process_invalid_his.txt #清空错误进程信息,当前进程信息 >/weblogic/script/process_signal/process_invalid.txt >/weblogic/script/process_signal/process_current.txt #输出当前时间并获取主机上所有java类进程 echo "######################### get ps info time is: " $currenttime " #########################">>/weblogic/script/process_signal/process_current.txt ps -ef|grep java >>/weblogic/script/process_signal/process_current.txt echo "######################### get ps info time is: " $currenttime " #########################">>/weblogic/script/process_signal/process_current.txt #逐条读取配置文件process_signal.config cat /weblogic/script/process_signal/process_signal.config | while read LINE do # 如果配置文件含有#号,则跳过不处理 if [[ `echo $LINE|cut -c1-1` != "#" ]]; then #如果配置文件记录在主机进程中存在 if [[ -n `find $path -name $file_tmp | xargs grep -w "$LINE"` ]]; then #开关为Y,输出到屏幕 if [[ $if_get_exist = "Y" ]]; then echo $hostip " process [" $LINE "] is exist " $currenttime >>/weblogic/script/process_signal/process_invalid.txt fi #配置文件记录与主机进程匹配 run=`find $path -name $file_tmp | xargs grep -w "$LINE"|wc -l` if (($run>1)); then #打印配置文件中记录与主机进程匹配数大于1的记录 echo $hostip " process [" $LINE "] is repeatRun "$run " Times" $currenttime >>/weblogic/script/process_signal/process_invalid.txt fi #如果配置文件记录在主机进程中不存在 else echo $hostip " process ["$LINE"] is not exist " $currenttime >>/weblogic/script/process_signal/process_invalid.txt fi fi done } #调用方法 get_process_signal #fhs.workflow.EventMonitorMain 4 #find /weblogic/script/process_signal -name process_current.txt | xargs grep -i "fhs.workflow.EventMonitorMain 4" #ps -ef|grep java|grep "fhs.workflow.EventMonitorMain 4"
5、SqlLOAD入库(含控制文件conl.ctl)
efile92@iomitfdb2:/interface/contabfile/monitor_time >cat conl.ctl load data infile '/interface/contabfile/monitor_time/process_invalid.txt' truncate into table T_MONITOR_TIMER fields terminated by '\n' -- Create table create table T_MONITOR_TIMER ( info VARCHAR2(1000) ) sqlldr userid=用户名/密码@sid control=/interface/contabfile/monitor_time/conl.ctl log=/interface/contabfile/monitor_time/conl.log
总结
上一篇:Linux查看端口占用lsof -i:port的方法
栏 目:Shell
本文标题:shell脚本实现服务器进程监控的方法
本文地址:http://www.codeinn.net/misctech/102775.html