#!/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 exitecho "$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.";; esacdone ########################################################### 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 > run_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;echo "mysql command execute.";; 3310);; esacdone# EOF.