• About

http://blog.fakrul.com

http://blog.fakrul.com

Tag Archives: ping

Monitor Network~PERL Script

09 Wednesday Jan 2008

Posted by Fakrul Alam in Uncategorized

≈ Leave a comment

Tags

GSM, monitor, My Work, mysql, network, perl, ping, sms, sms server

I have written a perl script (!) which will monitor the network & integrate this script with SMS Server Tools. What it does is check the network in every 5 mins. If any host down sms it to me with host name and down time. If the host up again it sms me the uptime. In the script following perl module are used :

use File::Copy;
use File::stat;
use DBI;
use Net::Ping::External qw(ping);

Hostnames are taken from MySql Database. Database has the followind table structure:

+——————-+———————+———+——-+————-+————————+
| Field               | Type               | Null   | Key | Default    | Extra |
+——————-+———————+———+——-+————-+————————+
| sl_no              | int(11)             | NO    | PRI   | NULL | auto_increment |
| ip_address         | varchar(15)         | YES   |       | NULL |                               |
| flag               | varchar(1)          | YES   |       | NULL |                              |
| description        | varchar(100)        | YES   |       | NULL |                              |
+——————-+———————+———+——-+————-+————————+

Complete perl script:


#!/usr/bin/perl
#———————————————————————————————-
# This script check reachability of remote hosts on a network
# fakrul@fakrul.com
# You only need to change the phone no in this file.
#———————————————————————————————-

use Net::Ping;
use File::Copy;
use File::stat;
use DBI;
use Net::Ping::External qw(ping);

#———define the phone no to send sms

my @phone_no =
  qw( 123456789 987654321);

#——-define the mysql variables
my $dsn = ‘dbi:mysql:sms:localhost:3306’;
my $user = “root”;
my $pass = “my_sql_password”;
my $sms_log = “/var/spool/sms/outgoing/output.” . time;

#——-time function
@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
@weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
$year = 1900 + $yearOffset;
$theTime = “$hour:$minute $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year”;

#——-create connection string
my $dbh = DBI->connect($dsn, $user, $pass) or die “Can not connect to the DB: $DBI::errstrn”;

#——-execute sql query
my $q_sth = $dbh->prepare(“select * from sms_host”);
$q_sth->execute;
while(@row = $q_sth->fetchrow_array()) {
        $alive = ping(hostname => $row[1], count => 6, size => 64, timeout => 100);
        if ($alive eq “1” ) {
                if ($row[2] eq “D”) {
                        my $qd_update = $dbh->prepare(“update sms_host set flag=’U’ where sl_no=’$row[0]’”) or die “Cannot prepare: ” . $dbh->errstr();
                        $qd_update->execute;
                        $qd_update->finish();
                        print “$row[3] UP at $theTimen”;

                        #—————now send the sms 🙂
                        foreach my $phone_no (@phone_no) {
                                open (OUT, “>$sms_log.$phone_no.$row[1]”) or die “Can’t open log-file”;
                                print OUT “To: $phone_non”;
                                print OUT “n”;
                                print OUT “$row[3] UP at $theTimen”;
                                close(OUT);
                        }
                }
                else {
                print “Nothing to Do, Host $row[1] already upn”;
                }
        }
        else {
                if ($row[2] eq “U”) {
                        my $qu_update = $dbh->prepare(“update sms_host set flag=’D’ where sl_no=’$row[0]’”) or die “Cannot prepare: ” . $dbh->errstr();
                        $qu_update->execute;
                        $qu_update->finish();
                        print “$row[3] DOWN at $theTimen”;
                        sleep(1);

                        #—————now send the sms 🙂
                        foreach my $phone_no (@phone_no) {
                                open (OUT, “>$sms_log.$phone_no.$row[1]”) or die “Can’t open log-file”;
                                print OUT “To: $phone_non”;
                                print OUT “n”;
                                print OUT “$row[3] DOWN at $theTime n”;
                                close(OUT);
                        }
                }
                else {
                print “Host $row[1] already DOWNn”;
                }
        }
}

Social

  • View rapappu’s profile on Twitter
  • View fakrulalam’s profile on LinkedIn
  • View fakrul’s profile on GitHub
  • View FakrulAlamPappu’s profile on Google+
  • View fakrulalam’s profile on Flickr

Twitter Updates

  • 1976 forming the modern-day #Vietnam 1971 #Bangladesh independence When we could see something like this happening… twitter.com/i/web/status/1… 4 days ago
  • The beauty is here #Keychron #K2 Mechanical Keyboard https://t.co/j5OVbpmvKi 5 days ago
  • blog.fitbit.com/2021-update/?u… #fitbit + #google https://t.co/X2g7HpIMvI 1 week ago
  • #DevNet Class of 2020. View my verified achievement from @learningatcisco. youracclaim.com/badges/bf864fa… 1 month ago
  • #pythonprogramminglanguage #python #pyenv towardsdatascience.com/how-to-set-up-… 1 month ago
  • wsj.com/articles/u-s-c… 1 month ago
  • bleepingcomputer.com/news/security/… 1 month ago
  • RT @GinNTTnet: Carriers, Internet-centric businesses and enterprises can prepare themselves to fight against #DDoS attacks. Download this w… 2 months ago
  • #LoRaWAN GPS payload is coming nicely to #AzureIotHub @GlobalNTT @CiscoDevNet #ntthackfest2020 https://t.co/VqYi7dMBoq 2 months ago
  • RT @GinNTTnet: Paolo Lucente @Paolo_Lucente from the #globalipnetwork division at @GlobalNTT will present this Monday Oct 12 at the Next Ho… 3 months ago
  • RT @bdNetOpGroup: Make sure we always "Learning from failures". Wanna know more? Please join bdnog.org/bdnog12/index.…. Because our very own… 3 months ago
  • RT @bdNetOpGroup: #bdNOG12 Panel Discussion: "Social CDN Business : Paid or Free". Make sure you register and join bdnog.org/bdnog12/index.…… 3 months ago
  • RT @teamcymru: #DDoS Attacks Hit 1 Tbps in #2020 #infosec bit.ly/2ZLmAlK https://t.co/Ezx3s5xVcw 4 months ago
  • github.blog/2020-09-10-ann… #msteams #github 4 months ago
  • I just backed Keychron K3 -Ultra-slim Compact Wireless Mechanical Keyboard on @Kickstarter kickstarter.com/projects/keytr… 4 months ago
Follow @rapappu

Tags

antismap antivirus automation Azure bangladesh BASH BASH Script BDCERT bgp bind ccsp centos CentOS mirror CERT CISA cisco Cyber Security ddos dhaka dhakacom DNS DNSSEC GSM intrusion detectoin system Intrusion prevention system ips IPv6 ISACA junos linux Looking Glass lxc lxc profile lxd mailqueue mailscanner Mail Server mailwatch Meraki mikrotik monitor mpls MPLS L3 VPN mysql My Work network network management nginx NSD observium OpenVPN perl PHP ping postfix Proxy PTA python RANCID Reading RPKI Shell Script sms sms server SNMP SSH Tutorial ubuntu Ubuntu Mirror Server Virtual Box vispan vmware websvn Youtube hack খামাখা

Blog at WordPress.com.