check_pop3.pl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/perl
  2. # ------------------------------------------------------------------------------
  3. # File Name: check_pop3.pl
  4. # Author: Richard Mayhew - South Africa
  5. # Date: 2000/01/21
  6. # Version: 1.0
  7. # Description: This script will check to see if an POP3 is running
  8. # and whether authentication can take place.
  9. # Email: netsaint@splash.co.za
  10. # ------------------------------------------------------------------------------
  11. # Copyright 1999 (c) Richard Mayhew
  12. # Credits go to Ethan Galstad for coding Nagios
  13. # If any changes are made to this script, please mail me a copy of the
  14. # changes :)
  15. # License GPL
  16. # ------------------------------------------------------------------------------
  17. # Date Author Reason
  18. # ---- ------ ------
  19. # 1999/09/20 RM Creation
  20. # 1999/09/20 TP Changed script to use strict, more secure by
  21. # specifying $ENV variables. The bind command is
  22. # still insecure through. Did most of my work
  23. # with perl -wT and 'use strict'
  24. # 2000/01/20 RM Corrected POP3 Exit State.
  25. # 2000/01/21 RM Fix Exit Codes Again!!
  26. # 2003/12/30 CZ Proper CRLF in communication w/server
  27. # Fixed infinite loop
  28. # Error checking on welcome banner, USER, PASS commands
  29. # Better error condition handling
  30. # ------------------------------------------------------------------------------
  31. # -----------------------------------------------------------------[ Require ]--
  32. require 5.004;
  33. # --------------------------------------------------------------------[ Uses ]--
  34. use Socket;
  35. use strict;
  36. # --------------------------------------------------------------[ Enviroment ]--
  37. $ENV{PATH} = "/bin";
  38. $ENV{BASH_ENV} = "";
  39. $|=1;
  40. # ------------------------------------------------------------------[ Global ]--
  41. my $TIMEOUT = 60;
  42. # -------------------------------------------------------------------[ usage ]--
  43. sub usage
  44. {
  45. print "Minimum arguments not supplied!\n";
  46. print "\n";
  47. print "Perl Check POP3 plugin for Nagios\n";
  48. print "Copyright (c) 2000 Richard Mayhew\n";
  49. print "\n";
  50. print "Usage: check_pop3.pl <host> <username> <password> [port]\n";
  51. print "\n";
  52. print "<port> = Port that the pop3 daemon is running on <host>. Defaults to 110.\n";
  53. exit -1;
  54. }
  55. # --------------------------------------------------------------[ bindRemote ]--
  56. sub bindRemote
  57. {
  58. my ($in_remotehost, $in_remoteport, $in_hostname) = @_;
  59. my $proto;
  60. my $sockaddr;
  61. my $this;
  62. my $thisaddr;
  63. my $that;
  64. my ($name, $aliases,$type,$len,$thataddr) = gethostbyname($in_remotehost);
  65. if (!socket(ClientSocket,AF_INET, SOCK_STREAM, $proto)) { die $!; }
  66. $sockaddr = 'S n a4 x8';
  67. $this = pack($sockaddr, AF_INET, 0, $thisaddr);
  68. $that = pack($sockaddr, AF_INET, $in_remoteport, $thataddr);
  69. if (!bind(ClientSocket, $this)) { print "Connection Refused\n"; exit 2; }
  70. if (!connect(ClientSocket, $that)) { print "Connection Refused\n"; exit 2; }
  71. select(ClientSocket); $| = 1; select(STDOUT);
  72. return \*ClientSocket;
  73. }
  74. # ====================================================================[ MAIN ]==
  75. MAIN:
  76. {
  77. my $hostname;
  78. my $remotehost = shift || &usage;
  79. my $username = shift || &usage;
  80. my $password = shift || &usage;
  81. my $remoteport = shift || 110;
  82. # Just in case of problems, let's not hang Nagios
  83. $SIG{'ALRM'} = sub {
  84. print "Something is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n";
  85. exit -1;
  86. };
  87. alarm($TIMEOUT);
  88. chop($hostname = `hostname`);
  89. my ($name, $alias, $proto) = getprotobyname('tcp');
  90. my $ClientSocket = &bindRemote($remotehost,$remoteport,$hostname);
  91. &err("no welcome banner\n") unless $_ = <ClientSocket>;
  92. &err("bad welcome banner: " . $_) unless $_ =~ /^\+OK/;
  93. print ClientSocket "USER $username\r\n";
  94. &err("no response to USER command\n") unless $_ = <ClientSocket>;
  95. &err("bad response to USER: " . $_) unless $_ =~ /^\+OK/;
  96. print ClientSocket "PASS $password\r\n";
  97. &err("no response to PASS command\n") unless $_ = <ClientSocket>;
  98. &err("bad response to PASS: " . $_) unless $_ =~ /^\+OK/;
  99. print ClientSocket "LIST\r\n";
  100. my $bad = 1;
  101. my $msgs = 0;
  102. while (<ClientSocket>) {
  103. &err(($1||' UNKNOWN')."\n") if (m/\-ERR(.*)/);
  104. $bad = 0 if /^\+OK/;
  105. $msgs = $1 if /^(\d+)\s+/;
  106. last if /^\./;
  107. }
  108. &message("$msgs\n") unless $bad;
  109. &err("missing +OK to LIST command\n");
  110. }
  111. sub message
  112. {
  113. my $msg = shift;
  114. alarm(0);
  115. print ClientSocket "QUIT\r\n";
  116. print "POP3 OK - Total Messages On Server: $msg";
  117. exit 0;
  118. }
  119. sub err
  120. {
  121. my $msg = shift;
  122. alarm(0);
  123. print ClientSocket "QUIT\r\n";
  124. print "POP3 Error: $msg";
  125. exit 2;
  126. }