a shell script for monitoring the services

 

#!/bin/sh
#what's it:Monitoring services from netstat and report.
 
#configure here:
 
#help&error function:
help()
{
echo   "srvstat.sh:Monitoring srvs.
        USAGE: ./srvstat.sh
        OPTIONS: -h help
        EXAMPLE:
        HELP"
exit 0
}
 
error()
{
# print an error and exit
echo "$1"
exit 1          #exit with status "1".
}
 
 
######### Volidate the input arguments #################
while [ -n "$1" ]; do
 case $1 in
        -h) help;; # function help is called, "shift means variable 1.
        --) help;; # end of option
        -*) error "error: no such option $1. -h for help.";;
        *) error "Don't accept the args except -h.";;
 esac
done
 
##########################################################
# The main program
##########################################################
cd /usr/jxw/scripts #the directory where this script belong to.
# Check which the ports are running.
netstat -an | grep LISTEN | awk '{print $4}' | awk -F. '{print $NF}' | uniq > ru
n_port
 
# the next is to compare run_port with all_port are already specified.
awk '(NR==FNR){a[$0]}(NR!=FNR)&&!($0 in a){print $0}' run_port all_port > unrun_
port
# take a look if some services is dead.
num=`cat unrun_port | wc -l`
if [ $num = 0 ]; then
        echo " All services are running."
fi
# then run the service which are not running.
while [ $num -gt 0 ]; do
        port=`sed -n "${num}p" unrun_port`
        num=`expr $num - 1`
        case "$port" in
         22) /usr/sbin/sshd;;
         25) qmailctl start;;
         53) echo "dns not running.";;
         80) /usr/local/bin/apachectl -h start;;
         110);;
         143);;
         443) echo "https not running.";;
         783);;
         993);;
         3306) echo "mysql not running.";/usr/local/bin/mysqld_safe -u mysql;ech
o "mysql command execute.";;
         3310);;
        esac
done
# EOF.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值