User Tools

Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

perl:checker [2020-02-15 00:57] (current)
Line 1: Line 1:
 +=====Service check/​validation tool======
  
 +Rapid-made tool for IT company staff.
 +
 +<code perl ckecker-tmpl.pl>​
 +use strict;
 +
 +use Net::Ping;
 +use Socket;
 +use Net::​Traceroute::​PurePerl;​
 +use DBI;
 +use DBD::Pg;
 +use Data::​Dumper;​
 +use LWP::​UserAgent;​
 +
 +use utf8;
 +binmode(STDOUT,':​utf8'​);​
 +
 +sub logwrite($) {
 +    my ($str) = @_;
 +    my $log = "/​checker-log.txt";​
 +    open(my $fh, ">>",​ $log);
 +    my $str = shift; ​
 +    print $str;
 +    print $fh $str;
 +    close $fh;
 +}
 +
 +sub host($) {
 +    my ($host) = @_;
 +    my @addresses = gethostbyname($host);​
 +    @addresses = map { inet_ntoa($_) } @addresses[4 .. $#​addresses];​
 +
 +    if (@addresses) {
 +        logwrite("#​ resolve $host to @addresses: success\n"​);​
 +    } else {
 +        logwrite("#​ resolve $host: error\n"​);​
 +    }
 +
 +    foreach my $n (1..3) {
 +        my $ping = Net::​Ping->​new();​
 +        if ($ping->​ping($host)) {
 +            logwrite("#​ ping $n $host: success\n"​);​
 +        } else {
 +            logwrite("#​ ping $n $host: error\n"​);​
 +        }
 +    }
 +
 +    my $tr = new Net::​Traceroute::​PurePerl(
 +        host => $host,
 +        max_ttl => 8,
 +        protocol => '​icmp',​
 +        query_timeout ​ => 2,
 +    );
 +
 +    my $res;
 +    eval {
 +        $res = $tr->​traceroute;​
 +    };
 +
 +    if ($@) {
 +        logwrite("#​ traceroute $host: error $@\n"​);​
 +    } else {
 +        my $hops = $tr->​hops;​
 +        if ($res) {
 +            my $hops = $tr->​hops();​
 +            logwrite("#​ traceroute $host: success by $hops hops\n"​);​
 +        } else {
 +            logwrite("#​ traceroute $host: error by $hops hops\n"​);​
 +        }
 +    }
 +}
 +
 +
 +sub pg($$) {
 +
 +  my ($pghost, $dbname) = @_;
 +  my $pguser = "​mosaic";​
 +  my $pgpass = "​aszretir";​
 +
 +  my $dsn = "​dbi:​Pg:​dbname=$dbname;​host=$pghost;​port=6432";​
 +  my $dbh;
 +
 +  eval {
 +    $dbh = DBI->​connect($dsn,​ $pguser, $pgpass);
 +  };
 +
 +  if ($@ || !$dbh) {
 +    logwrite("#​ postgreql connect: error $@");
 +  } else {
 +        if ($dbh) {
 +            logwrite("#​ postgreql connect to $pghost:​$dbname:​ success\n"​);​
 +            my $query = "​select project_name,​ project_guid from projects5d limit 5";
 +            my $sth = $dbh->​prepare($query);​
 +            my $rv = $sth->​execute;​
 +
 +            logwrite("#​ postgresql test request result:​\n"​);​
 +
 +            while (my @row = $sth->​fetchrow_array) {
 +                logwrite("​project_guid=$row[1]\n"​);​
 +            }
 +
 +            $sth->​finish;​
 +            $dbh->​disconnect;​
 +            logwrite("#​ postgresql $pghost total result: success\n"​); ​
 +        }
 +    }
 +}
 +
 +
 +sub web($) {
 +    my ($url) = @_;
 +    my $ua = LWP::​UserAgent->​new;​
 +    my $req = HTTP::​Request->​new(GET => $url);
 +    my $res = $ua->​request($req);​
 +    my $status = $res->​status_line;​
 +    logwrite("#​ srv1c auth web request: $status\n"​);​
 +}
 +
 +my $reg = "​_REG_";​
 +
 +my $REG = uc($reg);
 +
 +my $pghost = "​xxxxxxxxxx-$reg.lazurit.us";​
 +my $dbname = "​xxxxxxx_$reg";​
 +
 +my $srvhost = "​web.$reg.lazurit.us";​
 +my $url = '​http://​_LOGIN_@'​ . "​$srvhost/​xxxxxxxxx$REG/​xxx/​xxxxxx";​
 +
 +my $timestamp = localtime;
 +logwrite("​\n#​ timestamp: $timestamp\n"​);​
 +
 +host($pghost);​
 +pg($pghost, $dbname);
 +
 +host($srvhost);​
 +web($url);
 +
 +logwrite("#​ end\n\n"​);​
 +
 +print "the window will close after 30 sec\n\n";​
 +sleep 30;
 +#EOF
 +</​code>​