简单的Bash脚本在不同的端口上远程监视您的Web服务器

简单的Bash脚本可以在不同端口上远程监控Web服务器

简单的bash脚本来监视不同端口上的网络服务器(这里是smtp,dns,http&https,但可以定制); 我相信有超过100个可用的程序这样做,但我想要一些小内存使用的东西。 另外,我只想收到一次通知,通过SMS在我的单元格上收到通知。 使用我之前使用的软件,我每分钟都收到通知,直到我可以到达计算机,解决问题或停止监视是非常烦人的。

资料来源: http : //blogama.org

软件安装

你需要邮件,挖和telnet安装。

剧本

注意:脚本不会继续,直到你做:

./whatever_you_called_this_script fix

这是为了只收到一个通知...

#!/bin/bash
# Script to check important ports on remote webserver
# Copyright (c) 2009 blogama.org
# This script is licensed under GNU GPL version 2.0 or above
# ---------------------------------------------------------------------
 
### This script does a verification on port 25, 53, 80 and 443 ###
### After 2 failed check it will send a mail notification ###
 
######To be modified######
WORKDIR="/root"
###HTTP###
HTTPSERVERIP="192.168.1.106"
HTTPSERVERPORT="80"
##########
###HTTPS###
HTTPSSERVERIP="192.168.1.106"
HTTPSSERVERPORT="443"
##########
###MAIL###
SMTPSERVERIP="192.168.1.106"
SMTPSERVERPORT="25"
##########
###DNS###
DNSSERVERIP="192.168.1.106"
DOMAINTOCHECKDNS="example.com"
ANSWERIP="192.168.1.106"
#########
###NOTIFICATIONS###
EMAIL="admin@example.com"
##########
######End to be modified######
 
 
######Do not make modifications below######
### Binaries ###
MAIL=$(which mail)
TELNET=$(which telnet)
DIG=$(which dig)
###Change dir###
cd $WORKDIR
###Restore when problem fix###
if [ $1 ]; then
  if [ $1=="fix" ]; then
    rm server_problem*.txt
	exit 1;
  fi
fi
###Check if already notified###
if [ -f server_problem.txt ]; then
  exit 1;
fi
 
###Test SMTP###
(
echo "quit"
) | $TELNET $SMTPSERVERIP $SMTPSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
  echo "PORT CONNECTED"
  if [ -f server_problem_first_time_smtp.txt ]; then #remove file if problem fixed
    rm -rf server_problem_first_time_smtp.txt
  fi
else #Connection failure
  if [ -f server_problem_first_time_smtp.txt ]; then #Second time, send notification below
    echo "SMTP PORT NOT CONNECTING" >> server_problem.txt
	rm -rf server_problem_first_time_smtp.txt
  else #First notification
    > server_problem_first_time_smtp.txt
  fi
fi
 
###Test HTTP###
(
echo "quit"
) | $TELNET $HTTPSERVERIP $HTTPSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
  echo "PORT CONNECTED"
  if [ -f server_problem_first_time_http.txt ]; then #remove file if problem fixed
    rm -rf server_problem_first_time_http.txt
  fi
else #Connection failure
  if [ -f server_problem_first_time_http.txt ]; then #Second time, send notification below
    echo "HTTP PORT NOT CONNECTING" >> server_problem.txt
	rm -rf server_problem_first_time_http.txt
  else #First notification
    > server_problem_first_time_http.txt
  fi
fi
 
###Test HTTPS###
(
echo "quit"
) | $TELNET $HTTPSSERVERIP $HTTPSSERVERPORT | grep Connected > /dev/null 2>&1
if [ "$?" -ne "1" ]; then #Ok
  echo "PORT CONNECTED"
  if [ -f server_problem_first_time_https.txt ]; then #remove file if problem fixed
    rm -rf server_problem_first_time_https.txt
  fi
else #Connection failure
  if [ -f server_problem_first_time_https.txt ]; then #Second time, send notification below
    echo "HTTPS PORT NOT CONNECTING" >> server_problem.txt
	rm -rf server_problem_first_time_https.txt
  else #First notification
    > server_problem_first_time_https.txt
  fi
fi
 
 
 
###Test DNS###
$DIG $DOMAINTOCHECKDNS @$DNSSERVERIP | grep $ANSWERIP
 
if [ "$?" -ne "1" ]; then #Ok
  echo "PORT CONNECTED"
  if [ -f server_problem_first_time_dns.txt ]; then #remove file if problem fixed
    rm -rf server_problem_first_time_dns.txt
  fi
else #Connection failure
  if [ -f server_problem_first_time_dns.txt ]; then #Second time, send notification below
    echo "DNS PORT NOT CONNECTING" >> server_problem.txt
	rm -rf server_problem_first_time_dns.txt
  else #First notification
    > server_problem_first_time_dns.txt
  fi
fi
 
###Send mail notification after 2 failed check###
if [ -f server_problem.txt ]; then
  $MAIL -s "Server problem" $EMAIL < /root/server_problem.txt
fi

使其可执行:

chmod +x whatever_you_called_this_script

将其添加到您的crontab中:

crontab -e
* * * * * /path/to/whatever_you_called_this_script >/dev/null 2>&1
赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏