Advertisement
Guest User

Untitled

a guest
Jul 14th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. #! /usr/bin/perl -w
  2. # $Id: check_ftp_rw,v 1.0 2011/07/06 14:17:10 root Exp root $
  3.  
  4. #
  5. # Checks an ftp site
  6. #
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty
  15. # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # you should have received a copy of the GNU General Public License
  19. # along with this program (or with Nagios); if not, write to the
  20. # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. # Boston, MA 02111-1307, USA
  22.  
  23. use strict;
  24. use Getopt::Long;
  25. use Net::FTP;
  26. use Net::FTPSSL;
  27. use vars qw($PROGNAME);
  28. use lib "/usr/local/nagios/libexec" ;
  29. use utils qw (%ERRORS &print_revision &support);
  30.  
  31. sub print_help ();
  32. sub print_usage ();
  33.  
  34. my ($opt_host, $opt_user, $opt_pass, $opt_dir, $opt_file, $opt_v, $opt_h, $opt_write, $opt_ssl) =
  35. ("", "", "", "", "");
  36. my $result;
  37.  
  38. my $outstring="";
  39.  
  40. $PROGNAME="check_ftp_rw";
  41.  
  42.  
  43. GetOptions(
  44. "version" => \$opt_v,
  45. "help" => \$opt_h,
  46. "host=s" => \$opt_host,
  47. "user=s" => \$opt_user,
  48. "password=s" => \$opt_pass,
  49. "dir=s" => \$opt_dir,
  50. "file=s" => \$opt_file,
  51. "write" => \$opt_write,
  52. "ssl" => \$opt_ssl,
  53. );
  54.  
  55. if ($opt_v) {
  56. print_revision($PROGNAME, '$Id: check_hgsc_ftp,v 1.1 2007/01/02 16:11:33 root Exp root $');
  57. exit $ERRORS{'OK'};
  58. }
  59.  
  60. if ($opt_h) {
  61. print_help();
  62. exit $ERRORS{'OK'};
  63. }
  64.  
  65. unless ($opt_host) {
  66. print "must supply hostname with --host\n";
  67. print_usage();
  68. exit $ERRORS{'UNKNOWN'};
  69. }
  70.  
  71. $result = 'OK';
  72.  
  73. my $ftp;
  74. if ($opt_ssl) {
  75. $ftp = Net::FTPSSL -> new($opt_host,
  76. Debug => 0,
  77. Encryption => EXP_CRYPT,
  78. Croak => 0,
  79. Passive => 1 #must be in passive mode to work
  80. );
  81. } else {
  82. $ftp = Net::FTP -> new($opt_host,
  83. Debug => 0,
  84. Passive => 1 #must be in passive mode to work
  85. );
  86. }
  87.  
  88. unless ($ftp) {
  89. $result="CRITICAL";
  90. print "Cannot connect to host: $opt_host\n";
  91. exit $ERRORS{$result};
  92. } else {
  93. $outstring .="Connected to $opt_host";
  94. }
  95.  
  96. $opt_user ||= "anonymous";
  97. $opt_pass ||= "nagios";
  98.  
  99. unless ($ftp->login($opt_user, $opt_pass)){
  100. $result="CRITICAL";
  101. print "Cannot login as user $opt_user\n";
  102. exit $ERRORS{$result};
  103. } else {
  104. $outstring .=", logged in as $opt_user";
  105. }
  106.  
  107. if ($opt_dir ne ""){
  108. unless ($ftp->cwd($opt_dir)){
  109. $result="WARNING";
  110. print "Cannot chdir to $opt_dir\n";
  111. exit $ERRORS{$result};
  112. } else {
  113. $outstring .=", chdir'ed to $opt_dir";
  114. }
  115. }
  116.  
  117. if ($opt_file ne ""){
  118. unless ($ftp->get($opt_file, "/dev/null")){
  119. $result="WARNING";
  120. print "Cannot get file: $opt_file\n";
  121. exit $ERRORS{$result};
  122. } else {
  123. $outstring .=", downloaded $opt_file";
  124. }
  125. }
  126.  
  127. my $remote_filename = "__check_ftp.nagios";
  128. my $memfile = "check_ftp Nagios plugin's test file.\n";
  129. if ($opt_write){
  130. open(file_handle, "<", \$memfile);
  131. # unless ($remote_filename = $ftp->put_unique(*file_handle, "__check_ftp.nagios")){
  132. unless ($ftp->put(\*file_handle, $remote_filename)){
  133. $result="WARNING";
  134. print "Cannot put file: $remote_filename\n";
  135. exit $ERRORS{$result};
  136. } else {
  137. $outstring .=", uploaded $remote_filename";
  138. }
  139. close(file_handle);
  140. if ($remote_filename ne ""){
  141. $ftp->delete($remote_filename);
  142. }
  143. }
  144.  
  145. print $outstring, "\n";
  146. exit $ERRORS{$result};
  147.  
  148. sub print_usage () {
  149. print "Usage:\n";
  150. print " $PROGNAME --host <host> [--user <user>][--password <password>][--dir <dir>][--file <file>][--write][--ssl]\n";
  151. print " $PROGNAME [--help]\n";
  152. print " $PROGNAME [--version]\n";
  153. }
  154.  
  155. sub print_help () {
  156. print_revision($PROGNAME, '$Id: check_hgsc_ftp,v 1.1 2007/01/02 16:11:33 root Exp root $');
  157. print "Copyright (c) 2007 Paul Archer\n\n";
  158. print_usage();
  159. print "\n";
  160. print " --host host to check\n";
  161. print " --user username to use (uses 'anonymous' if user not given)\n";
  162. print " --password password to use (uses 'nagios' if password not given)\n";
  163. print " --dir cd to this directory (stays in base directory otherwise)\n";
  164. print " --file file to retrieve (can be absolute path, or relative to 'dir' (or / if no 'dir' given))\n";
  165. print " --write write random file (writes relative to 'dir' (or / if no 'dir' given))\n";
  166. print " --ssl connect using FTPS instead of plain FTP\n";
  167. print "\n";
  168. print "Will return CRITICAL if host cannot be contacted or logged into.\n";
  169. print "Will return WARNING if specified directory or file isn't accessible/uploadable.\n";
  170. print "\n";
  171. support();
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement