check_pop3.pl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. # ------------------------------------------------------------------------------
  27. # -----------------------------------------------------------------[ Require ]--
  28. require 5.004;
  29. # --------------------------------------------------------------------[ Uses ]--
  30. use Socket;
  31. use strict;
  32. # --------------------------------------------------------------[ Enviroment ]--
  33. $ENV{PATH} = "/bin";
  34. $ENV{BASH_ENV} = "";
  35. $|=1;
  36. # ------------------------------------------------------------------[ Global ]--
  37. my $TIMEOUT = 60;
  38. # -------------------------------------------------------------------[ usage ]--
  39. sub usage
  40. {
  41. print "Minimum arguments not supplied!\n";
  42. print "\n";
  43. print "Perl Check POP3 plugin for Nagios\n";
  44. print "Copyright (c) 2000 Richard Mayhew\n";
  45. print "\n";
  46. print "Usage: check_pop3.pl <host> <username> <password> [port]\n";
  47. print "\n";
  48. print "<port> = Port that the pop3 daemon is running on <host>. Defaults to 110.\n";
  49. exit -1;
  50. }
  51. # --------------------------------------------------------------[ bindRemote ]--
  52. sub bindRemote
  53. {
  54. my ($in_remotehost, $in_remoteport, $in_hostname) = @_;
  55. my $proto;
  56. my $sockaddr;
  57. my $this;
  58. my $thisaddr;
  59. my $that;
  60. my ($name, $aliases,$type,$len,$thataddr) = gethostbyname($in_remotehost);
  61. if (!socket(ClientSocket,AF_INET, SOCK_STREAM, $proto)) { die $!; }
  62. $sockaddr = 'S n a4 x8';
  63. $this = pack($sockaddr, AF_INET, 0, $thisaddr);
  64. $that = pack($sockaddr, AF_INET, $in_remoteport, $thataddr);
  65. if (!bind(ClientSocket, $this)) { print "Connection Refused"; exit 2; }
  66. if (!connect(ClientSocket, $that)) { print "Connection Refused"; exit 2; }
  67. select(ClientSocket); $| = 1; select(STDOUT);
  68. return \*ClientSocket;
  69. }
  70. # ====================================================================[ MAIN ]==
  71. MAIN:
  72. {
  73. my $hostname;
  74. my $remotehost = shift || &usage;
  75. my $username = shift || &usage;
  76. my $password = shift || &usage;
  77. my $remoteport = shift || 110;
  78. # Just in case of problems, let's not hang Nagios
  79. $SIG{'ALRM'} = sub {
  80. print "Something is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n";
  81. exit -1;
  82. };
  83. alarm($TIMEOUT);
  84. chop($hostname = `hostname`);
  85. my ($name, $alias, $proto) = getprotobyname('tcp');
  86. my $ClientSocket = &bindRemote($remotehost,$remoteport,$hostname);
  87. print ClientSocket "user $username\n";
  88. #Debug Server
  89. #print "user $username\n";
  90. #Sleep or 3 secs, incase server is slow.
  91. sleep 3;
  92. print ClientSocket "pass $password\n";
  93. #Debug Server
  94. #print "pass $password\n";
  95. while (<ClientSocket>) {
  96. print ClientSocket "pass $password\n";
  97. #Debug Server
  98. #print $_;
  99. err($_) if (m/\-ERR\s+(.*)\s+.*/);
  100. message($_) if (m/\+OK Mailbox open,\s+(.*\d)\s+messages.*/);
  101. }
  102. }
  103. sub message
  104. {
  105. my $answer = "UNKNOWN";
  106. $answer = "Pop3 OK - Total Messages On Server :- $1";
  107. alarm(0);
  108. print ClientSocket "quit\n";
  109. print "$answer";
  110. exit 0;
  111. }
  112. sub err
  113. {
  114. my $answer = "UNKNOWN";
  115. $answer = "Pop3 Error :- $1";
  116. alarm(0);
  117. print ClientSocket "quit\n";
  118. print "$answer";
  119. exit 2;
  120. }