Advertisement
m4ly

Adapters

Apr 7th, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.28 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # Autor: Dawid Mocek <[email protected]>
  4. # All rights reserved.
  5. # Date: 07-04-2015
  6. # Version: 0.1
  7.  
  8. # Checks ping and web connection to adapters defined in config.ini
  9.  
  10.  
  11. use strict;
  12. use warnings;
  13.  
  14. use threads;
  15. use LWP::UserAgent;
  16. use HTTP::Status qw(:constants :is status_message);
  17. use Config::IniFiles;
  18. use Getopt::Long;
  19. use Term::ANSIColor;
  20.  
  21. my $program_name = $0;
  22. my $program_author = "Dawid Mocek";
  23. my $program_desc = "Adpaters tester - checks ping and www service";
  24. my $program_ver = "0.1";
  25.  
  26. sub printcolored {
  27.     my $msg = $_[0];
  28.    
  29.     if($msg =~ /\[Error\]/) {
  30.     print colored($msg, 'red'), "\n";
  31.     }
  32.     elsif($msg =~ /\[Success\]/) {
  33.         print colored($msg, 'green'), "\n";
  34.     }
  35.     elsif($msg =~ /\[Warning\]/) {
  36.         print colored($msg, 'yellow'), "\n";
  37.     }
  38.     else {
  39.     print $msg . "\n";
  40.     }
  41. }
  42.  
  43. sub trim {
  44.     $_[0] =~ s/^\s+|\s+$//g;
  45. }
  46.  
  47.  
  48. sub parse_ping_output {
  49.     my $output = $_[0];
  50.     my %pinginfo = ();
  51.    
  52.     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) {
  53.     $pinginfo{'transmitted'} = $+{transmitted};
  54.         $pinginfo{'received'} = $+{received};
  55.     $pinginfo{'pct_loss'} = $+{pct_loss};
  56.         $pinginfo{'time'} = $+{time};
  57.     }
  58.     if($output =~ /rtt\s+min\/avg\/max\/mdev\s+=\s+(?<min>.*)\/(?<avg>.*)\/(?<max>.*)\/(?<mdev>.*)\s+ms$/) {
  59.     $pinginfo{'min'} = $+{min};
  60.         $pinginfo{'avg'} = $+{avg};
  61.     $pinginfo{'max'} = $+{max};
  62.         $pinginfo{'mdev'} = $+{mdev};
  63.     }
  64.     return %pinginfo;
  65. }
  66.  
  67. sub ping_check {
  68.     my ($name, $ip, $cnt)  = @_;
  69.  
  70. #    if(length($name)) return "[Critical] section name not defined !";
  71.  
  72.     if(defined $ip && defined $cnt) {
  73.     trim($ip);
  74.     trim($cnt);
  75.     my $out = (`ping -q -c $cnt $ip`);
  76.    
  77.     my %pinginfo = parse_ping_output($out);
  78.    
  79.     if($pinginfo{'pct_loss'} >= 100) {
  80.         return "[$name][Error] " . $pinginfo{'pct_loss'} . "% packet loss to $ip";
  81.     }
  82.     elsif($pinginfo{'pct_loss'} >= 50) {
  83.         return "[$name][Error] " . $pinginfo{'pct_loss'} . "% packet loss to $ip. Avg time: " . $pinginfo{'avg'} . "ms";
  84.     }
  85.     elsif($pinginfo{'pct_loss'} >= 1) {
  86.         return "[$name][Warning] " . $pinginfo{'pct_loss'} . "% packet loss to $ip. Avg time: " . $pinginfo{'avg'} . "ms";
  87.     }
  88.     else {
  89.         return "[$name][Success] " . $pinginfo{'pct_loss'} . "% packet loss to $ip. Avg time: " . $pinginfo{'avg'} . "ms";
  90.     }
  91.     }
  92.     else {
  93.     return "[$name][Error] ip or ping_count not defined !" ;
  94.     }
  95. }
  96.  
  97. sub adapter_check {
  98.     my($name, $ip, $port, $timeout, $scheme)  = @_;
  99.  
  100. #    if(length($name)) return "[Critical] section name not defined !";
  101.  
  102.     if(defined $ip && defined $port && defined $timeout && defined $scheme) {
  103.         trim($ip);
  104.     trim($port);
  105.     trim($timeout);
  106.     trim($scheme);
  107.    
  108.     $scheme = lc($scheme);
  109.    
  110.     if($timeout <= 0) {
  111.          return "[$name][Error] Timeout <= 0";
  112.     }
  113.    
  114.     if(!($scheme eq 'http' || $scheme eq 'https')) {
  115.         return "[$name][Error] Invalid scheme. Must be http or https";
  116.     }
  117.    
  118.     my $ua = LWP::UserAgent->new();
  119.     $ua->timeout($timeout);
  120.    
  121.     my $url = $scheme . "://" . $ip . ":" . $port;
  122.         my $resp = $ua->get($url);
  123.    
  124.     if($resp->is_success) {
  125.         return "[$name][Success][HTTP " . $resp->code . "] $url returned: ". status_message($resp->code) . ". Bytes recv: " . length($resp->as_string);
  126.     }
  127.     else {
  128.         return "[$name][Error][HTTP "   . $resp->code . "] $url returned: ". status_message($resp->code) . ". Bytes recv: " . length($resp->as_string);
  129.     }
  130.     }
  131. }
  132.  
  133.  
  134. sub load_ini {
  135.  
  136.     my $inifile = $_[0];
  137.     my %ini;
  138.    
  139.     unless(-e $inifile) {
  140.      print("File: $inifile does not exists\n");
  141.      exit;
  142.      }
  143.  
  144.     tie %ini, 'Config::IniFiles', (-file => $inifile, -fallback => "General");
  145.     return %ini;
  146. }
  147.  
  148.  
  149. sub usage {
  150.    
  151.     print "Unknown option: @_\n" if (@_);
  152.     print "Author: $program_author\n";
  153.     print "Usage: $program_name [--type [PP|PL]] [--help|-?]\n\tpp = partnerzy projektu\n\tpl = podmioty lecznicze\n";
  154.     exit;
  155. }
  156.  
  157.  
  158.  
  159.  
  160. my($help, $type);
  161.  
  162. usage() if( @ARGV < 1 or ! GetOptions('help|?' => \$help, 'type=s' => \$type) or defined $help );
  163.  
  164. my $inifile = "/root/scripts/config.ini";
  165. my %ini = load_ini($inifile);
  166.  
  167. my @threads_ping_check;
  168. my @threads_adapter_check;
  169.  
  170.  
  171. $type = lc($type);
  172.  
  173. while(my($item, $data) = each(%ini)) {
  174.  
  175.     if(!($item eq "General")) {
  176.     if(lc($data->{'type'}) eq $type) {
  177.         push(@threads_ping_check, async { ping_check($item, $data->{'ip'}, $data->{'ping_count'}) });
  178.         push(@threads_adapter_check, async { adapter_check($item, $data->{'ip'}, $data->{'adapter_port'}, $data->{'adapter_timeout'}, $data->{'adapter_scheme'}) });
  179.         }
  180.     }
  181. }
  182.  
  183. if(@threads_ping_check > 0 || @threads_adapter_check > 0) {
  184.     print "Author:\t$program_author\n";
  185.     print "Desc:\t$program_desc\n";
  186.     print "Version:\t$program_ver\n";
  187. }
  188.  
  189.  
  190. if(@threads_ping_check > 0) {
  191.     print "Executing ping_check threads...\n";
  192.     for my $thread (@threads_ping_check) {
  193.         my $response = $thread->join;
  194.     printcolored($response);
  195.     }
  196. }
  197.  
  198. if(@threads_adapter_check > 0) {
  199.     print "Executing adapter_check threads...\n";
  200.     for my $thread (@threads_adapter_check) {
  201.     my $response = $thread->join;
  202.     printcolored($response);
  203.     }
  204. }
  205.  
  206.  
  207. exit 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement