Linux и Windows: помощь админам и пользователям

Администрируем и настраиваем Windows, Linux.



Простой bash скрипт для мониторнга сервера

Рубрика: Apache
Метки: | | |
Воскресенье, 15 февраля 2009 г.
Просмотров: 14587
Подписаться на комментарии по RSS

В этой заметке я представляю простой bash скрипт для мониторинга вебсервера на различных портах (smtp, dns, http & https , но могут быть легко добавлены другие).  СУществуют более мощные программы мониторинга, однако мне в некоторых условиях хватает и его

Спонсор поста:
Кировский БиоХимЗавод: продам спирт технический. Объемы любые! Недорого

Необходимые компоненты

Вам необходимо иметь установленные mail, dig и telnet.

Скрипт

<span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># Скрипт для провверки доступности портов вашего сервера</span>
<span style="color: #666666; font-style: italic;"># This script is licensed under GNU GPL version 2.0 or above</span>
<span style="color: #666666; font-style: italic;"># ---------------------------------------------------------------------</span>
 
<span style="color: #666666; font-style: italic;">### Этот скрипт проверяет порты 25, 53, 80 и 443 ###</span>
<span style="color: #666666; font-style: italic;">### После двух неудачных проверок будет послано уведомление на email ###</span>
 
<span style="color: #666666; font-style: italic;">###### Эта секция может модифицироваться######</span>
 
<span style="color: #007800;">WORKDIR</span>=<span style="color: #ff0000;">"/root"</span>
 
<span style="color: #666666; font-style: italic;">###HTTP###</span>
<span style="color: #007800;">HTTPSERVERIP</span>=<span style="color: #ff0000;">"192.168.1.106"</span>
<span style="color: #007800;">HTTPSERVERPORT</span>=<span style="color: #ff0000;">"80"</span>
<span style="color: #666666; font-style: italic;">##########</span>
 
<span style="color: #666666; font-style: italic;">###HTTPS###</span>
<span style="color: #007800;">HTTPSSERVERIP</span>=<span style="color: #ff0000;">"192.168.1.106"</span>
<span style="color: #007800;">HTTPSSERVERPORT</span>=<span style="color: #ff0000;">"443"</span>
<span style="color: #666666; font-style: italic;">##########</span>
 
<span style="color: #666666; font-style: italic;">###MAIL###</span>
<span style="color: #007800;">SMTPSERVERIP</span>=<span style="color: #ff0000;">"192.168.1.106"</span>
<span style="color: #007800;">SMTPSERVERPORT</span>=<span style="color: #ff0000;">"25"</span>
<span style="color: #666666; font-style: italic;">##########</span>
 
<span style="color: #666666; font-style: italic;">###DNS###</span>
<span style="color: #007800;">DNSSERVERIP</span>=<span style="color: #ff0000;">"192.168.1.106"</span>
<span style="color: #007800;">DOMAINTOCHECKDNS</span>=<span style="color: #ff0000;">"example.com"</span>
<span style="color: #007800;">ANSWERIP</span>=<span style="color: #ff0000;">"192.168.1.106"</span>
<span style="color: #666666; font-style: italic;">#########</span>
 
<span style="color: #666666; font-style: italic;">### Мыло админа###</span>
<span style="color: #007800;">EMAIL</span>=<span style="color: #ff0000;">"admin@example.com"</span>
<span style="color: #666666; font-style: italic;">##########</span>
 
<span style="color: #666666; font-style: italic;">############</span>
 
 
<span style="color: #666666; font-style: italic;">###### В эту секцию не нужно вносить изменения#####</span>
<span style="color: #666666; font-style: italic;">### Binaries ###</span>
<span style="color: #007800;">MAIL</span>=$<span style="color: #7a0874; font-weight: bold;">(</span><span style="color: #c20cb9; font-weight: bold;">which</span> mail<span style="color: #7a0874; font-weight: bold;">)</span>
<span style="color: #007800;">TELNET</span>=$<span style="color: #7a0874; font-weight: bold;">(</span><span style="color: #c20cb9; font-weight: bold;">which</span> telnet<span style="color: #7a0874; font-weight: bold;">)</span>
<span style="color: #007800;">DIG</span>=$<span style="color: #7a0874; font-weight: bold;">(</span><span style="color: #c20cb9; font-weight: bold;">which</span> dig<span style="color: #7a0874; font-weight: bold;">)</span>
 
<span style="color: #666666; font-style: italic;">###Change dir###</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #007800;">$WORKDIR</span>
 
<span style="color: #666666; font-style: italic;">###Restore when problem fix###</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">[</span> $<span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">]</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">[</span> $<span style="color: #000000;">1</span>==<span style="color: #ff0000;">"fix"</span> <span style="color: #7a0874; font-weight: bold;">]</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #c20cb9; font-weight: bold;">rm</span> server_problem<span style="color: #000000; font-weight: bold;">*</span>.txt
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>;
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">fi</span>
 
<span style="color: #666666; font-style: italic;">###Check if already notified###</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">[</span> <span style="color: #660033;">-f</span> server_problem.txt <span style="color: #7a0874; font-weight: bold;">]</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>;
<span style="color: #000000; font-weight: bold;">fi</span>
 
<span style="color: #666666; font-style: italic;">###Проверяем SMTP###</span>
<span style="color: #7a0874; font-weight: bold;">(</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">"quit"</span>
<span style="color: #7a0874; font-weight: bold;">)</span> | <span style="color: #007800;">$TELNET</span> <span style="color: #007800;">$SMTPSERVERIP</span> <span style="color: #007800;">$SMTPSERVERPORT</span> | <span style="color: #c20cb9; font-weight: bold;">grep</span> Connected <span style="color: #000000; font-weight: bold;">></span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">>&</span><span style="color: #000000;">1</span>
 
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">[</span> <span style="color: #ff0000;">"$?"</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">"1"</span> <span style="color: #7a0874; font-weight: bold;">]</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #666666; font-style: italic;">#Ok</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">"PORT CONNECTED"</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">[</span> <span style="color: #660033;">-f</span> server_problem_first_time_smtp.txt <span style="color: #7a0874; font-weight: bold;">]</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #666666; font-style: italic;">#remove file if problem fixed</span>
    <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> server_problem_first_time_smtp.txt
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #666666; font-style: italic;">#Connection failure</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">[</span> <span style="color: #660033;">-f</span> server_problem_first_time_smtp.txt <span style="color: #7a0874; font-weight: bold;">]</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #666666; font-style: italic;">#Second time, send notification below</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">"SMTP PORT NOT CONNECTING"</span> <span style="color: #000000; font-weight: bold;">>></span> server_problem.txt
	<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> server_problem_first_time_smtp.txt
  <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #666666; font-style: italic;">#First notification</span>
    <span style="color: #000000; font-weight: bold;">></span> server_problem_first_time_smtp.txt
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">fi</span>
 
<span style="color: #666666; font-style: italic;">###Проверяем HTTP###</span>
<span style="color: #7a0874; font-weight: bold;">(</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">"quit"</span>
<span style="color: #7a0874; font-weight: bold;">)</span> | <span style="color: #007800;">$TELNET</span> <span style="color: #007800;">$HTTPSERVERIP</span> <span style="color: #007800;">$HTTPSERVERPORT</span> | <span style="color: #c20cb9; font-weight: bold;">grep</span> Connected <span style="color: #000000; font-weight: bold;">></span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">>&</span><span style="color: #000000;">1</span>
 
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">[</span> <span style="color: #ff0000;">"$?"</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">"1"</span> <span style="color: #7a0874; font-weight: bold;">]</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #666666; font-style: italic;">#Ok</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">"PORT CONNECTED"</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">[</span> <span style="color: #660033;">-f</span> server_problem_first_time_http.txt <span style="color: #7a0874; font-weight: bold;">]</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #666666; font-style: italic;">#remove file if problem fixed</span>
    <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> server_problem_first_time_http.txt
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #666666; font-style: italic;">#Connection failure</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">[</span> <span style="color: #660033;">-f</span> server_problem_first_time_http.txt <span style="color: #7a0874; font-weight: bold;">]</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #666666; font-style: italic;">#Second time, send notification below</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">"HTTP PORT NOT CONNECTING"</span> <span style="color: #000000; font-weight: bold;">>></span> server_problem.txt
	<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> server_problem_first_time_http.txt
  <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #666666; font-style: italic;">#First notification</span>
    <span style="color: #000000; font-weight: bold;">></span> server_problem_first_time_http.txt
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">fi</span>
 
<span style="color: #666666; font-style: italic;">###Проверяем HTTPS###</span>
<span style="color: #7a0874; font-weight: bold;">(</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">"quit"</span>
<span style="color: #7a0874; font-weight: bold;">)</span> | <span style="color: #007800;">$TELNET</span> <span style="color: #007800;">$HTTPSSERVERIP</span> <span style="color: #007800;">$HTTPSSERVERPORT</span> | <span style="color: #c20cb9; font-weight: bold;">grep</span> Connected <span style="color: #000000; font-weight: bold;">></span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">>&</span><span style="color: #000000;">1</span>
 
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">[</span> <span style="color: #ff0000;">"$?"</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">"1"</span> <span style="color: #7a0874; font-weight: bold;">]</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #666666; font-style: italic;">#Ok</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">"PORT CONNECTED"</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">[</span> <span style="color: #660033;">-f</span> server_problem_first_time_https.txt <span style="color: #7a0874; font-weight: bold;">]</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #666666; font-style: italic;">#remove file if problem fixed</span>
    <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> server_problem_first_time_https.txt
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #666666; font-style: italic;">#Connection failure</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">[</span> <span style="color: #660033;">-f</span> server_problem_first_time_https.txt <span style="color: #7a0874; font-weight: bold;">]</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #666666; font-style: italic;">#Second time, send notification below</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">"HTTPS PORT NOT CONNECTING"</span> <span style="color: #000000; font-weight: bold;">>></span> server_problem.txt
	<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> server_problem_first_time_https.txt
  <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #666666; font-style: italic;">#First notification</span>
    <span style="color: #000000; font-weight: bold;">></span> server_problem_first_time_https.txt
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">fi</span>
 
 
 
<span style="color: #666666; font-style: italic;">###Проверяем DNS###</span>
<span style="color: #007800;">$DIG</span> <span style="color: #007800;">$DOMAINTOCHECKDNS</span> <span style="color: #000000; font-weight: bold;">@</span><span style="color: #007800;">$DNSSERVERIP</span> | <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #007800;">$ANSWERIP</span>
 
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">[</span> <span style="color: #ff0000;">"$?"</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">"1"</span> <span style="color: #7a0874; font-weight: bold;">]</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #666666; font-style: italic;">#Ok</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">"PORT CONNECTED"</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">[</span> <span style="color: #660033;">-f</span> server_problem_first_time_dns.txt <span style="color: #7a0874; font-weight: bold;">]</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #666666; font-style: italic;">#удаляем файл если проблема решена</span>
    <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> server_problem_first_time_dns.txt
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #666666; font-style: italic;">#Connection failure</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">[</span> <span style="color: #660033;">-f</span> server_problem_first_time_dns.txt <span style="color: #7a0874; font-weight: bold;">]</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #666666; font-style: italic;">#Second time, send notification below</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">"DNS PORT NOT CONNECTING"</span> <span style="color: #000000; font-weight: bold;">>></span> server_problem.txt
	<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> server_problem_first_time_dns.txt
  <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #666666; font-style: italic;">#First notification</span>
    <span style="color: #000000; font-weight: bold;">></span> server_problem_first_time_dns.txt
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">fi</span>
 
<span style="color: #666666; font-style: italic;">###Send mail notification after 2 failed check###</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">[</span> <span style="color: #660033;">-f</span> server_problem.txt <span style="color: #7a0874; font-weight: bold;">]</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #007800;">$MAIL</span> <span style="color: #660033;">-s</span> <span style="color: #ff0000;">"Server problem"</span> <span style="color: #007800;">$EMAIL</span> <span style="color: #000000; font-weight: bold;"><</span> <span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>server_problem.txt
<span style="color: #000000; font-weight: bold;">fi</span>

Делаем скрипт исполняемым:

chmod +x whatever_you_called_this_script

И добавляем его в crontab :

* * * * * /root/check >/dev/null 2>&1

Постовой

Компания "Такелажник" занимается перевозкой спецтехники, предоставляет в аренду спецтехнику, выполняет любые такелажные работы.

twitter.com facebook.com vkontakte.ru odnoklassniki.ru mail.ru ya.ru rutvit.ru myspace.com technorati.com digg.com friendfeed.com pikabu.ru blogger.com liveinternet.ru livejournal.ru memori.ru google.com bobrdobr.ru mister-wong.ru yahoo.com yandex.ru del.icio.us

Комментариев: 1

  1. crazyman2004 | 2009-03-02 в 09:06:19

    А почему вместо телнета не использовался ssh?

Оставьте комментарий!

Используйте нормальные имена.

Имя и сайт используются только при регистрации

Если вы уже зарегистрированы как комментатор или хотите зарегистрироваться, укажите пароль и свой действующий email. При регистрации на указанный адрес придет письмо с кодом активации и ссылкой на ваш персональный аккаунт, где вы сможете изменить свои данные, включая адрес сайта, ник, описание, контакты и т.д., а также подписку на новые комментарии.

(обязательно)