📜 ⬆️ ⬇️

Script to monitor the status of hosts on the network

I work as an admin and in my area of ​​responsibility there are a number of servers: routers, database servers, terminal servers, etc. And I want to be aware if some of the servers suddenly disappeared from the network - a link with a remote network fell somewhere, a textbook case with technicals, a mop and cable suddenly happened somewhere, some force majeure happened. And at the same time I do not want to bother with heavy monitoring systems, like Zabbix.

I want to share with the community my script for timely notification of the missing connection with the servers. The script is written in Perl and revolves on my desktop in a Debian GNU / Linux environment. Most likely, no extra sit-ups will be launched in another Linux distribution.
So, the script with comments:
 #!/usr/bin/perl #  : use strict; #    . use Net::Ping::External qw(ping); #    ICMP- #  : my $tries = 3; #      my $timeout = 5; #   my $interval = 5; #    # -    .    =>    my %hosts = ( "HOST-1" => "10.0.0.1", "HOST-2" => "10.0.0.2" ); # , ,   #   ,       my %state = (); #      while (my ($name, $ip) = each(%hosts)) { $state{$name} = "UP"; } #    while () { #         my @uphosts = (); my @downhosts = (); #        while (my ($name, $ip) = each(%hosts)) { #      my $cnt = 0; #    ,       for (my $i = 1; $i <= $tries; $i++) { if (ping(hostname => $ip, timeout => $timeout)) { $cnt++; } } #      ,    if ($cnt == 0) { #      ,       ,    if ($state{$name} eq "UP") { $state{$name} = "DOWN"; push(@downhosts, $name); } #        -   } else { #     if ($state{$name} eq "DOWN") { $state{$name} = "UP"; push(@uphosts, $name); } } } #  ,   ,      , #    libnotify if (scalar(@uphosts) > 0) { my $lst = join(", ", @uphosts); `notify-send -u normal NetMon "Hosts becomes up: $lst"`; } if (scalar(@downhosts) > 0) { my $lst = join(", ", @downhosts); `notify-send -u critical NetMon "Hosts becomes down: $lst"`; } #     sleep($interval * 60); }; 


Now reservations on the script.
Pearl, and specifically Net: Ping has a feature. If you use the integrated Net :: Ping pearl-barley module with ICMP ping type, you need root privileges. This is due to the implementation of ICMP ping in this module via RAW sockets, which only root can use. Therefore, I used Net :: Ping :: External in this script, which implements ping via external calls.
In addition to notifying the administrator with pop-up notifies, you can add, for example, recording information about falls-raises to a text log (yours or via syslog), you can send messages to soap, to jabber, SMS to phone, etc. Full flight for fantasy.
Until the heap, you can add a check for liveliness of services, such as web, mail, FTP, samba. But it will already go beyond the bounds of a simple ping baler and will be similar to heavy monitoring systems.

')

Source: https://habr.com/ru/post/66142/


All Articles