Мониторинг сервисов (Apache, MySQL, Nginx, Postfix, Sendmail, ...)
-
Мониторинг Apache (требуется модуль mod_status)
в файл httpd.conf добавьте:ExtendedStatus On <Location /server-status> SetHandler server-status Order deny,allow Deny from all Allow from localhost </Location>в файл nxagentd.conf добавьте (вариант для FreeBSD, Apache 1.3):
# parameters for monitoring WebServer Apache ExternalParameter = ApacheAccesses(*):fetch -qo - http://$1/server-status?auto | head -n 9 | grep Accesses | cut -d " " -f 3 ExternalParameter = ApacheTotalKb(*):fetch -qo - http://$1/server-status?auto | head -n 9 | grep kBytes | cut -d " " -f 3 ExternalParameter = ApacheCPULoad(*):fetch -qo - http://$1/server-status?auto | head -n 9 | grep CPULoad | cut -d " " -f 2 ExternalParameter = ApacheUptime(*):fetch -qo - http://$1/server-status?auto | head -n 9 | grep Uptime | cut -d " " -f 2 ExternalParameter = ApacheReqPerSec(*):fetch -qo - http://$1/server-status?auto | head -n 9 | grep ReqPerSec | cut -d " " -f 2 ExternalParameter = ApacheBytesPerSec(*):fetch -qo - http://$1/server-status?auto | head -n 9 | grep BytesPerSec | cut -d " " -f 2 ExternalParameter = ApacheBytesPerReq(*):fetch -qo - http://$1/server-status?auto | head -n 9 | grep BytesPerReq | cut -d " " -f 2 ExternalParameter = ApacheBusyServers(*):fetch -qo - http://$1/server-status?auto | head -n 9 | grep BusyServers | cut -d " " -f 2 ExternalParameter = ApacheIdleServers(*):fetch -qo - http://$1/server-status?auto | head -n 9 | grep IdleServers | cut -d " " -f 2пример вывода:
# fetch -qo - http://localhost/server-status?auto Total Accesses: 5 Total kBytes: 3 Uptime: 6 ReqPerSec: .833333 BytesPerSec: 512 BytesPerReq: 614.4 BusyServers: 1 IdleServers: 4 Scoreboard: W____...заметка:
- в качестве аргумента использется - ip-адрес или hostname, в качестве дополнительного параметра добавьте port
- для Linux вместо fetch используйте wget, GET (perl-libwww), lynxсписок параметров: ApacheAccesses(*), ApacheTotalKb(*), ApacheCPULoad(*), ApacheUptime(*), ApacheReqPerSec(*), ApacheBytesPerSec(*), ApacheBytesPerReq(*), ApacheBusyServers(*), ApacheIdleServers(*)
Мониторинг Nginx
откройте файл nginx.conf, найдите строчку 'server {' и добавьте ниже код:location /nginx_status { stub_status on; access_log off; allow 127.0.0.254; deny all; }в файл nxagentd.conf добавьте:
# parameters for monitoring WebServer Nginx ExternalParameter = NginxActiveConnections(*):fetch -qo - http://$1:$2/nginx_status | grep "Active connections" | cut -f3 -d" " ExternalParameter = NginxServerAccepts(*):fetch -qo - http://$1:$2/nginx_status | head -n3 | tail -n1 | cut -f2 -d" " ExternalParameter = NginxServerHandled(*):fetch -qo - http://$1:$2/nginx_status | head -n3 | tail -n1 | cut -f3 -d" " ExternalParameter = NginxServerRequests(*):fetch -qo - http://$1:$2/nginx_status | head -n3 | tail -n1 | cut -f4 -d" " ExternalParameter = NginxServerReading(*):fetch -qo - http://$1:$2/nginx_status | head -n4 | tail -n1 | cut -f2 -d" " ExternalParameter = NginxServerWriting(*):fetch -qo - http://$1:$2/nginx_status | head -n4 | tail -n1 | cut -f4 -d" " ExternalParameter = NginxServerWaiting(*):fetch -qo - http://$1:$2/nginx_status | head -n4 | tail -n1 | cut -f6 -d" "пример вывода:
# fetch -qo - http://localhost:85/nginx_status Active connections: 13 server accepts handled requests 234 327 236 Reading: 0 Writing: 412 Waiting: 0список параметров: NginxActiveConnections(*), NginxServerAccepts(*), NginxServerHandled(*), NginxServerRequests(*), NginxServerReading(*), NginxServerWriting(*), NginxServerWaiting(*)
Мониторинг MySQL
в файл nxagentd.conf добавьте:# parameters for monitoring MySQL server ExternalParameter = MySQL_Ping,mysqladmin -uroot ping|grep alive|wc -l ExternalParameter = MySQL_Uptime,mysqladmin -uroot status|cut -f2 -d":"|cut -f1 -d"T" ExternalParameter = MySQL_Threads,mysqladmin -uroot status|cut -f3 -d":"|cut -f1 -d"Q" ExternalParameter = MySQL_Questions,mysqladmin -uroot status|cut -f4 -d":"|cut -f1 -d"S" ExternalParameter = MySQL_SlowQueries,mysqladmin -uroot status|cut -f5 -d":"|cut -f1 -d"O" ExternalParameter = MySQL_qps,mysqladmin -uroot status|cut -f9 -d":" ExternalParameter = MySQL_Version,mysql -Vзаметка: определите -u<username> и добавьте -p<password> если потребуется
-
-
-
-
для мониторинга у Apache есть модуль mod_status, более подробная информация
http://httpd.apache.org/docs/1.3/mod/mod_status.html
там же содержится описание URL /server-status?auto -
-
пример shell-строки для мониторинга tcp-сервисов
if echo $1 | nc -w $2 $3 $4 2>&- | grep -q $5; then echo 1; else echo 0; fiсписок аргументов:
$1 - запрос;
$2 - время ожидания в секундах (timeout);
$3 - адрес хоста;
$4 - порт;
$5 - ожидаемый ответ (элемент ответа);загоняем строчку в 'ExternalParameter' nx-агента (параметры указываются через запятую)
на выходе получаем 0(ложь) либо 1(истина)заметка: программа nc(NetCat) имеется далеко не во всех Unix-системах и если её нет на Вашей системе, то просто установите её
nc также имеет возможность работать с udp-сервисами и сканировать портыпример perl-скрипта для реализации мониторинга tcp-сервисов
#!/usr/local/bin/perl # Service Poller use strict; use IO::Socket::INET; die "usage: ./service_poller.pl <request> <timeout> <host> <port> <response>" unless $ARGV[0]; die "usage: ./service_poller.pl <request> <timeout> <host> <port> <response>" unless $ARGV[1]; die "usage: ./service_poller.pl <request> <timeout> <host> <port> <response>" unless $ARGV[2]; die "usage: ./service_poller.pl <request> <timeout> <host> <port> <response>" unless $ARGV[3]; die "usage: ./service_poller.pl <request> <timeout> <host> <port> <response>" unless $ARGV[4]; my $recive_response = ""; my $request=$ARGV[0]; my $timeout=$ARGV[1]; my $host=$ARGV[2]; my $port=$ARGV[3]; my $response=$ARGV[4]; my $sock = new IO::Socket::INET( PeerAddr=>$host, PeerPort=>$port, Proto=>'tcp', #LocalAddr => 'localhost', #LocalPort => 27005, #Broadcast => 1 Timeout=>$timeout); #die "Could not create socket: $!\n" unless $sock; die "0\n" unless $sock; $sock->autoflush(1); syswrite $sock, $request."\n"; sysread $sock,$recive_response,65535; #chomp($recive_response); #chop($recive_response); #print $recive_response; if($recive_response =~ /$response/) { print "1"."\n"; } else { print "0"."\n"; } close $sock;
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login