Простой bash скрипт для мониторнга сервера
Рубрика: Apache
Метки: Linux | shell | мониторинг | скрипты
Воскресенье, 15 февраля 2009 г.
Просмотров: 14587
Подписаться на комментарии по RSS
Метки: Linux | shell | мониторинг | скрипты
Воскресенье, 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
Постовой
Компания "Такелажник" занимается перевозкой спецтехники, предоставляет в аренду спецтехнику, выполняет любые такелажные работы.
Еще записи по теме
- Вебсервер Apache не перезагружается или не стартует - диагностика
- Apache: применяем изменения в конфиге без перезагрузки сервера
- Apache2 авторизация через MySQL backend
- Скрываем версию PHP (X-Powered-By)
- Установка Zend Optimizer
- Установка MyDNS и MyDNSConfig 3 в Fedora 10, часть 1
- Руководство по установке SwitchMap в CactiEZ
Комментариев: 1
А почему вместо телнета не использовался ssh?