使用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加载到数据表中,对数据库表进行监控。
脚本说明:
整体文件结构:
webusr@iomtimer1:/weblogic/script/process_signal >lsprocess_current.txt process_invalid.txt process_invalid_his.txt process_signal.config process_signal.sh
进程配置示例:
webusr@iomtimer1:/weblogic/script/process_signal >cat process_signal.config #停复发NASextapp.tftonas.PutTfToNas 10 2extapp.tftonas.PutTfToNas 11 2extapp.tftonas.PutTfToNas 12 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:00127.0.0.1 process [ DealWorkTaskTimer 3001 ] is repeatRun 2 Times 2018-04-11 17:51:56
逻辑匹配入口:
webusr@iomtimer1:/weblogic/script/process_signal >cat process_signal.sh#!/bin/shpath=/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" #主机IPcurrenttime=`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"
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_TIMERfields terminated by '\n'-- Create tablecreate table T_MONITOR_TIMER( info VARCHAR2(1000))sqlldr userid=用户名/密码@sid control=/interface/contabfile/monitor_time/conl.ctl log=/interface/contabfile/monitor_time/conl.log
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注编程网行业资讯频道,感谢您对编程网的支持。