Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- # Autor: Dawid Mocek <[email protected]>
- # All rights reserved.
- # Date: 07-04-2015
- # Version: 0.1
- # Checks ping and web connection to adapters defined in config.ini
- use strict;
- use warnings;
- use threads;
- use LWP::UserAgent;
- use HTTP::Status qw(:constants :is status_message);
- use Config::IniFiles;
- use Getopt::Long;
- use Term::ANSIColor;
- my $program_name = $0;
- my $program_author = "Dawid Mocek";
- my $program_desc = "Adpaters tester - checks ping and www service";
- my $program_ver = "0.1";
- sub printcolored {
- my $msg = $_[0];
- if($msg =~ /\[Error\]/) {
- print colored($msg, 'red'), "\n";
- }
- elsif($msg =~ /\[Success\]/) {
- print colored($msg, 'green'), "\n";
- }
- elsif($msg =~ /\[Warning\]/) {
- print colored($msg, 'yellow'), "\n";
- }
- else {
- print $msg . "\n";
- }
- }
- sub trim {
- $_[0] =~ s/^\s+|\s+$//g;
- }
- sub parse_ping_output {
- my $output = $_[0];
- my %pinginfo = ();
- if($output =~ /(?<transmitted>\d{1,})\s+packets\s+transmitted,\s+(?<received>\d{1,})\s+received,\s+(?<pct_loss>\d{1,})\%\s+packet\s+loss,\s+time\s+(?<time>\d{1,})ms/m) {
- $pinginfo{'transmitted'} = $+{transmitted};
- $pinginfo{'received'} = $+{received};
- $pinginfo{'pct_loss'} = $+{pct_loss};
- $pinginfo{'time'} = $+{time};
- }
- if($output =~ /rtt\s+min\/avg\/max\/mdev\s+=\s+(?<min>.*)\/(?<avg>.*)\/(?<max>.*)\/(?<mdev>.*)\s+ms$/) {
- $pinginfo{'min'} = $+{min};
- $pinginfo{'avg'} = $+{avg};
- $pinginfo{'max'} = $+{max};
- $pinginfo{'mdev'} = $+{mdev};
- }
- return %pinginfo;
- }
- sub ping_check {
- my ($name, $ip, $cnt) = @_;
- # if(length($name)) return "[Critical] section name not defined !";
- if(defined $ip && defined $cnt) {
- trim($ip);
- trim($cnt);
- my $out = (`ping -q -c $cnt $ip`);
- my %pinginfo = parse_ping_output($out);
- if($pinginfo{'pct_loss'} >= 100) {
- return "[$name][Error] " . $pinginfo{'pct_loss'} . "% packet loss to $ip";
- }
- elsif($pinginfo{'pct_loss'} >= 50) {
- return "[$name][Error] " . $pinginfo{'pct_loss'} . "% packet loss to $ip. Avg time: " . $pinginfo{'avg'} . "ms";
- }
- elsif($pinginfo{'pct_loss'} >= 1) {
- return "[$name][Warning] " . $pinginfo{'pct_loss'} . "% packet loss to $ip. Avg time: " . $pinginfo{'avg'} . "ms";
- }
- else {
- return "[$name][Success] " . $pinginfo{'pct_loss'} . "% packet loss to $ip. Avg time: " . $pinginfo{'avg'} . "ms";
- }
- }
- else {
- return "[$name][Error] ip or ping_count not defined !" ;
- }
- }
- sub adapter_check {
- my($name, $ip, $port, $timeout, $scheme) = @_;
- # if(length($name)) return "[Critical] section name not defined !";
- if(defined $ip && defined $port && defined $timeout && defined $scheme) {
- trim($ip);
- trim($port);
- trim($timeout);
- trim($scheme);
- $scheme = lc($scheme);
- if($timeout <= 0) {
- return "[$name][Error] Timeout <= 0";
- }
- if(!($scheme eq 'http' || $scheme eq 'https')) {
- return "[$name][Error] Invalid scheme. Must be http or https";
- }
- my $ua = LWP::UserAgent->new();
- $ua->timeout($timeout);
- my $url = $scheme . "://" . $ip . ":" . $port;
- my $resp = $ua->get($url);
- if($resp->is_success) {
- return "[$name][Success][HTTP " . $resp->code . "] $url returned: ". status_message($resp->code) . ". Bytes recv: " . length($resp->as_string);
- }
- else {
- return "[$name][Error][HTTP " . $resp->code . "] $url returned: ". status_message($resp->code) . ". Bytes recv: " . length($resp->as_string);
- }
- }
- }
- sub load_ini {
- my $inifile = $_[0];
- my %ini;
- unless(-e $inifile) {
- print("File: $inifile does not exists\n");
- exit;
- }
- tie %ini, 'Config::IniFiles', (-file => $inifile, -fallback => "General");
- return %ini;
- }
- sub usage {
- print "Unknown option: @_\n" if (@_);
- print "Author: $program_author\n";
- print "Usage: $program_name [--type [PP|PL]] [--help|-?]\n\tpp = partnerzy projektu\n\tpl = podmioty lecznicze\n";
- exit;
- }
- my($help, $type);
- usage() if( @ARGV < 1 or ! GetOptions('help|?' => \$help, 'type=s' => \$type) or defined $help );
- my $inifile = "/root/scripts/config.ini";
- my %ini = load_ini($inifile);
- my @threads_ping_check;
- my @threads_adapter_check;
- $type = lc($type);
- while(my($item, $data) = each(%ini)) {
- if(!($item eq "General")) {
- if(lc($data->{'type'}) eq $type) {
- push(@threads_ping_check, async { ping_check($item, $data->{'ip'}, $data->{'ping_count'}) });
- push(@threads_adapter_check, async { adapter_check($item, $data->{'ip'}, $data->{'adapter_port'}, $data->{'adapter_timeout'}, $data->{'adapter_scheme'}) });
- }
- }
- }
- if(@threads_ping_check > 0 || @threads_adapter_check > 0) {
- print "Author:\t$program_author\n";
- print "Desc:\t$program_desc\n";
- print "Version:\t$program_ver\n";
- }
- if(@threads_ping_check > 0) {
- print "Executing ping_check threads...\n";
- for my $thread (@threads_ping_check) {
- my $response = $thread->join;
- printcolored($response);
- }
- }
- if(@threads_adapter_check > 0) {
- print "Executing adapter_check threads...\n";
- for my $thread (@threads_adapter_check) {
- my $response = $thread->join;
- printcolored($response);
- }
- }
- exit 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement