Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. #
  3. # check_squid - Nagios check plugin for testing a Squid proxy
  4. #
  5. # Christoph Haas <email@christoph-haas.de>
  6. # Andre Osti <andreoandre@gmail.com>
  7. #
  8. # License: GPL 2
  9. #
  10. # V0.2
  11. #
  12.  
  13. require 5.004;
  14. use POSIX;
  15. use strict;
  16. use Getopt::Long;
  17. use vars qw($opt_V $opt_h $opt_t $opt_u $opt_n $opt_s
  18. $opt_p $opt_l $opt_o $opt_m $opt_e);
  19. use vars qw($PROGNAME);
  20. use lib "/usr/lib/nagios/plugins";
  21. use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
  22. use LWP::UserAgent;
  23. use HTTP::Request::Common qw(POST GET);
  24. use HTTP::Headers;
  25. my ($url, $urluser, $urlpass, $proxy, $proxyport,
  26. $proxyuser, $proxypass, $expectstatus, $res, $req);
  27.  
  28. $PROGNAME = "check_squid_lw.pl";
  29.  
  30. sub print_help();
  31. sub print_usage();
  32.  
  33. Getopt::Long::Configure('bundling');
  34. GetOptions("V" => \$opt_V, "version" => \$opt_V,
  35. "h" => \$opt_h, "help" => \$opt_h,
  36. "t=s" => \$opt_t, "timeout=i" => \$opt_t,
  37. "u=s" => \$opt_u, "url=s" => \$opt_u,
  38. "n=s" => \$opt_n, "urluser=s" => \$opt_n,
  39. "s=s" => \$opt_s, "urlpass=s" => \$opt_s,
  40. "p=s" => \$opt_p, "proxy=s" => \$opt_p,
  41. "l=s" => \$opt_l, "proxyport=s" => \$opt_l,
  42. "o=s" => \$opt_o, "proxyuser=s" => \$opt_o,
  43. "m=s" => \$opt_m, "proxypass=s" => \$opt_m,
  44. "e=i" => \$opt_e, "status=i" => \$opt_e);
  45.  
  46. if ($opt_V) {
  47. print_revision($PROGNAME,'$Revision: 0.1 $'); #'
  48. exit $ERRORS{'OK'};
  49. }
  50.  
  51. if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
  52.  
  53. ($opt_u) || ($opt_u = shift) || usage("Use -h for more info\n");
  54. $url = $opt_u;
  55.  
  56. ($opt_p) || ($opt_p = shift) || usage("Use -h for more info\n");
  57. $proxy = $opt_p;
  58.  
  59. ($opt_l) || ($opt_l = shift) || usage("Use -h for more info\n");
  60. $proxyport = $opt_l;
  61.  
  62. ($opt_e) || ($opt_e = shift) || usage("Use -h for more info");
  63. $expectstatus = $opt_e;
  64.  
  65. if(defined($opt_n)) { $urluser = $opt_n; }
  66.  
  67. if(defined($opt_s)) { $urlpass = $opt_s; }
  68.  
  69. if(defined($opt_o)) { $proxyuser = $opt_o; }
  70.  
  71. if(defined($opt_m)) { $proxypass = $opt_m; }
  72.  
  73. my $ua = new LWP::UserAgent;
  74. my $h = HTTP::Headers->new();
  75.  
  76. if ($proxy)
  77. {
  78. $ua->proxy(['http', 'ftp'], "http://$proxy:$proxyport");
  79.  
  80. if ($proxyuser)
  81. {
  82. $h->proxy_authorization_basic($proxyuser,$proxypass);
  83. }
  84. }
  85.  
  86. if ($urluser)
  87. {
  88. $h->authorization_basic($urluser, $urlpass);
  89. }
  90.  
  91. $req = HTTP::Request->new('GET', $url, $h);
  92.  
  93. $res = $ua->request($req);
  94.  
  95. if ($res->status_line =~ /^$expectstatus/)
  96. {
  97. print "OK - Status: ".$res->status_line."\n";
  98. exit $ERRORS{"OK"};
  99. }
  100. else
  101. {
  102. print "CRITICAL - Status: ".$res->status_line." (but expected $expectstatus...)\n";
  103. exit $ERRORS{"CRITICAL"};
  104. }
  105.  
  106. sub print_usage () {
  107. print "Usage: $PROGNAME -u <internet site> -p <proxy> -l <port proxy> -e";
  108. print "<return http code> \n";
  109. }
  110.  
  111. sub print_help () {
  112. print_revision($PROGNAME,'$Revision: 0.1 $');
  113. print "Perl check squid proxy\n";
  114.  
  115. print_usage();
  116.  
  117. print "
  118. -V, --version
  119. Version this script
  120. -h, --help
  121. Help
  122. -t, --timeout=INTEGER
  123. default 15s
  124. -u, --url=http://<site>
  125. The URL to check on the internet (http://www.google.com)
  126. -n, --urluser=username
  127. Username if the web site required authentication
  128. -s, --urlpass=password
  129. Password if the web site required authentication
  130. -p, --proxy=proxy
  131. Server that squid runs on (proxy.mydomain)
  132. -l, --proxyport=INTEGER
  133. TCP port that Squid listens on (3128)
  134. -o, --proxyuser=proxyuser
  135. Username if the web site required authentication
  136. -m, --proxypass=proxypass
  137. Password if the web site required authentication
  138. -e, --status=INTEGER
  139. HTTP code that should be returned
  140.  
  141. ";
  142.  
  143. support();
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement