Code

updates
[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 # ------------------------------------------------------------------------------
28 # -----------------------------------------------------------------[ Require ]--
29 require 5.004;
31 # --------------------------------------------------------------------[ Uses ]--
32 use Socket;
33 use strict;
35 # --------------------------------------------------------------[ Enviroment ]--
36 $ENV{PATH} = "/bin";
37 $ENV{BASH_ENV} = "";
38 $|=1;
39 # ------------------------------------------------------------------[ Global ]--
40 my $TIMEOUT = 60;
41         
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;
55 }
57 # --------------------------------------------------------------[ bindRemote ]--
58 sub bindRemote
59 {
60         my ($in_remotehost, $in_remoteport, $in_hostname) = @_;
61         my $proto;
62         my $sockaddr;
63         my $this;
64         my $thisaddr;
65         my $that;
66         my ($name, $aliases,$type,$len,$thataddr) = gethostbyname($in_remotehost);
68         if (!socket(ClientSocket,AF_INET, SOCK_STREAM, $proto)) { die $!; }
69         $sockaddr = 'S n a4 x8';
70         $this = pack($sockaddr, AF_INET, 0, $thisaddr);
71         $that = pack($sockaddr, AF_INET, $in_remoteport, $thataddr);
72         if (!bind(ClientSocket, $this)) { print "Connection Refused"; exit 2; }
73         if (!connect(ClientSocket, $that)) { print "Connection Refused"; exit 2; }
74         select(ClientSocket); $| = 1; select(STDOUT);
75         return \*ClientSocket;
76 }
78 # ====================================================================[ MAIN ]==
79 MAIN:
80 {
81         my $hostname;
82         my $remotehost = shift || &usage;
83         my $username = shift || &usage;
84         my $password = shift || &usage;
85         my $remoteport = shift || 110;
87         # Just in case of problems, let's not hang Nagios
88         $SIG{'ALRM'} = sub {
89                 print "Something is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n";
90                 exit -1;
91         };
92         
93         alarm($TIMEOUT);
95         chop($hostname = `hostname`);
96         my ($name, $alias, $proto) = getprotobyname('tcp');
97         my $ClientSocket = &bindRemote($remotehost,$remoteport,$hostname);
98         
100 print ClientSocket "user $username\n";
102 #Debug Server
103 #print "user $username\n";
105 #Sleep or 3 secs, incase server is slow.
106 sleep 3;
108 print ClientSocket "pass $password\n";
110 #Debug Server
111 #print "pass $password\n";
113 while (<ClientSocket>) {
115 print ClientSocket "pass $password\n";
117 #Debug Server
118 #print $_;
120 err($_) if (m/\-ERR\s+(.*)\s+.*/);
121 message($_) if (m/\+OK Mailbox open,\s+(.*\d)\s+messages.*/);
125 sub message 
127         my $answer = "UNKNOWN";
128         $answer = "Pop3 OK - Total Messages On Server :- $1";   
129         alarm(0);
130         print ClientSocket "quit\n";
131         print "$answer";
132         exit 0;
135 sub err
137         my $answer = "UNKNOWN";
138         $answer = "Pop3 Error :- $1";
139         alarm(0);
140         print ClientSocket "quit\n";
141         print "$answer";
142         exit 2;