Code

fix loop and \r\n (Jason Burnett - http://sourceforge.net/tracker/index.php?func...
[nagiosplug.git] / contrib / check_pop3.pl
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
31 # ------------------------------------------------------------------------------
33 # -----------------------------------------------------------------[ Require ]--
34 require 5.004;
36 # --------------------------------------------------------------------[ Uses ]--
37 use Socket;
38 use strict;
40 # --------------------------------------------------------------[ Enviroment ]--
41 $ENV{PATH} = "/bin";
42 $ENV{BASH_ENV} = "";
43 $|=1;
44 # ------------------------------------------------------------------[ Global ]--
45 my $TIMEOUT = 60;
47 # -------------------------------------------------------------------[ usage ]--
48 sub usage
49 {
50         print "Minimum arguments not supplied!\n";
51         print "\n";
52         print "Perl Check POP3 plugin for Nagios\n";
53         print "Copyright (c) 2000 Richard Mayhew\n";
54         print "\n";
55         print "Usage: check_pop3.pl <host> <username> <password> [port]\n";
56         print "\n";
57         print "<port> = Port that the pop3 daemon is running on <host>. Defaults to 110.\n";
58         exit -1;
60 }
62 # --------------------------------------------------------------[ bindRemote ]--
63 sub bindRemote
64 {
65         my ($in_remotehost, $in_remoteport, $in_hostname) = @_;
66         my $proto;
67         my $sockaddr;
68         my $this;
69         my $thisaddr;
70         my $that;
71         my ($name, $aliases,$type,$len,$thataddr) = gethostbyname($in_remotehost);
73         if (!socket(ClientSocket,AF_INET, SOCK_STREAM, $proto)) { die $!; }
74         $sockaddr = 'S n a4 x8';
75         $this = pack($sockaddr, AF_INET, 0, $thisaddr);
76         $that = pack($sockaddr, AF_INET, $in_remoteport, $thataddr);
77         if (!bind(ClientSocket, $this)) { print "Connection Refused\n"; exit 2; }
78         if (!connect(ClientSocket, $that)) { print "Connection Refused\n"; exit 2; }
79         select(ClientSocket); $| = 1; select(STDOUT);
80         return \*ClientSocket;
81 }
83 # ====================================================================[ MAIN ]==
84 MAIN:
85 {
86         my $hostname;
87         my $remotehost = shift || &usage;
88         my $username = shift || &usage;
89         my $password = shift || &usage;
90         my $remoteport = shift || 110;
92         # Just in case of problems, let's not hang Nagios
93         $SIG{'ALRM'} = sub {
94                 print "Something is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n";
95                 exit -1;
96         };
97         
98         alarm($TIMEOUT);
100         chop($hostname = `hostname`);
101         my ($name, $alias, $proto) = getprotobyname('tcp');
102         my $ClientSocket = &bindRemote($remotehost,$remoteport,$hostname);
103         
105         &err("no welcome banner\n") unless $_ = <ClientSocket>;
106         &err("bad welcome banner: " . $_) unless $_ =~ /^\+OK/;
108         print ClientSocket "USER $username\r\n";
110         &err("no response to USER command\n") unless $_ = <ClientSocket>;
111         &err("bad response to USER: " . $_) unless $_ =~ /^\+OK/;
113         print ClientSocket "PASS $password\r\n";
115         &err("no response to PASS command\n") unless $_ = <ClientSocket>;
116         &err("bad response to PASS: " . $_) unless $_ =~ /^\+OK/;
118         print ClientSocket "LIST\r\n";
120         my $bad = 1;
121         my $msgs = 0;
122         while (<ClientSocket>) {
123                 &err(($1||' UNKNOWN')."\n") if (m/\-ERR(.*)/);
124                 $bad = 0 if /^\+OK/;
125                 $msgs = $1 if /^(\d+)\s+/;
126                 last if /^\./;
127         }
128         &message("$msgs\n") unless $bad;
129         &err("missing +OK to LIST command\n");
132 sub message 
134         my $msg = shift;
135         alarm(0);
136         print ClientSocket "QUIT\r\n";
137         print "POP3 OK - Total Messages On Server: $msg";
138         exit 0;
141 sub err
143         my $msg = shift;
144         alarm(0);
145         print ClientSocket "QUIT\r\n";
146         print "POP3 Error: $msg";
147         exit 2;