欢迎来到天天文库
浏览记录
ID:10269647
大小:65.50 KB
页数:12页
时间:2018-06-14
《使用shell脚本进行服务器系统监控——进程监控实例》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、使用shell脚本进行服务器系统监控——进程监控#!/bin/kshtypeset-uRUN_PRE_EVENT#ForcetoUPPERCASEtypeset-uRUN_STARTUP_EVENT#ForcetoUPPERCASEtypeset-uRUN_POST_EVENT#forcetoUPPERCASERUN_PRE_EVENT='N'#A'Y'willexecute,anythingelsewillnotRUN_STARTUP_EVENT='Y'#A'Y'willexecute,anythingelsewillnotRUN_POST_EVENT='Y'#A
2、'Y'willexecute,anythingelsewillnotLOGFILE="/tmp/proc_status.log"[[!-s$LOGFILE]]&&touch$LOGFILESCRIPT_NAME=$(basename$0)TTY=$(tty)INTERVAL="1"#SecondsbetweensamplingJOBS=#################################################################DEFINEFUNCTIONSHERE#################################
3、###################################usage(){echo"tUSAGEERROR"echo"USAGE:$SCRIPT_NAMEsecondsprocess"echo"Willmonitorthespecifiedprocessforthe"echo"specifiednumberofseconds."echo"USAGE:$SCRIPT_NAME[-s-Sseconds][-m-Mminutes]"echo"[-h-Hhours][-d-Ddays][-p-Pprocess]"echo"Wil
4、lmonitorthespecifiedprocessfornumberof"echo"secondsspecifiedwithin-sseconds,-mminutes,"echo"-hhoursand-ddays.Anycombinationofcommand"echo"switchescanbeused."echo"EXAMPLE:$SCRIPT_NAME300dtcalc"echo"EXAMPLE:$SCRIPT_NAME-m5-pdtcalc"echo"Bothexampleswillmonitorthedtcalcprocess"ec
5、ho"for5minutes.Canspecifydays,hours,minutes"echo"andseconds,using-d,-h,-mand-s"}####################################################trap_exit(){#set-x#Uncommanttodebugthisfunction#Loganendingtimeforprocessmonitoringecho"INTERRUPT:ProgramReceivedanInterrupt...EXITING...">$TTYecho"IN
6、TERRUPT:ProgramReceivedanInterrupt...EXITING...">>$LOGFILETIMESTAMP=$(date+%D@%T)#Getanewtimestamp...echo"MON_STOPPED:Monitoringfor$PROCESSended==>$TIMESTAMP">>$TTYecho"MON_STOPPED:Monitoringfor$PROCESSended==>$TIMESTAMP">>$LOGFILEecho"LOGFILE:AllEventsareLogged==>$LOGFILE">$TT
7、Y#KillallfunctionsJOBS=$(jobs-p)if[[!-z$JOBS&&$JOBS!=''&&$JOBS!='0']]thenkill$(jobs-p)2>/dev/null1>&2fireturn2}####################################################pre_event_script(){#PutanythingthatyouwanttoexecuteBEFOREthe#monitoredprocessSTARTSinthisfunction:#No-OP-Neededasaplaceho
此文档下载收益归作者所有