2
# ------------------------------------------------------------------------------
3
# File Name: check_pop3.pl
4
# Author: Richard Mayhew - South Africa
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
16
# ------------------------------------------------------------------------------
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 ]--
31
# --------------------------------------------------------------------[ Uses ]--
35
# --------------------------------------------------------------[ Enviroment ]--
39
# ------------------------------------------------------------------[ Global ]--
42
# -------------------------------------------------------------------[ usage ]--
45
print "Minimum arguments not supplied!\n";
47
print "Perl Check POP3 plugin for Nagios\n";
48
print "Copyright (c) 2000 Richard Mayhew\n";
50
print "Usage: check_pop3.pl <host> <username> <password> [port]\n";
52
print "<port> = Port that the pop3 daemon is running on <host>. Defaults to 110.\n";
57
# --------------------------------------------------------------[ bindRemote ]--
60
my ($in_remotehost, $in_remoteport, $in_hostname) = @_;
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;
78
# ====================================================================[ MAIN ]==
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
89
print "Something is Taking a Long Time, Increase Your TIMEOUT (Currently Set At $TIMEOUT Seconds)\n";
95
chop($hostname = `hostname`);
96
my ($name, $alias, $proto) = getprotobyname('tcp');
97
my $ClientSocket = &bindRemote($remotehost,$remoteport,$hostname);
100
print ClientSocket "user $username\n";
103
#print "user $username\n";
105
#Sleep or 3 secs, incase server is slow.
108
print ClientSocket "pass $password\n";
111
#print "pass $password\n";
113
while (<ClientSocket>) {
115
print ClientSocket "pass $password\n";
120
err($_) if (m/\-ERR\s+(.*)\s+.*/);
121
message($_) if (m/\+OK Mailbox open,\s+(.*\d)\s+messages.*/);
127
my $answer = "UNKNOWN";
128
$answer = "Pop3 OK - Total Messages On Server :- $1";
130
print ClientSocket "quit\n";
137
my $answer = "UNKNOWN";
138
$answer = "Pop3 Error :- $1";
140
print ClientSocket "quit\n";