commit 0847f7bca7400f0ca8271dcce863a2040d3466c6 Author: Jack-Benny Persson Date: Fri Jun 17 04:54:26 2016 +0200 Initial commit diff --git a/check_imap_starttls.sh b/check_imap_starttls.sh new file mode 100755 index 0000000..e85d0dd --- /dev/null +++ b/check_imap_starttls.sh @@ -0,0 +1,170 @@ +#!/bin/bash + +################################################################################ +# # +# Copyright (C) 2016 Jack-Benny Persson # +# # +# This program is free software; you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation; either version 2 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program; if not, write to the Free Software # +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # +# # +################################################################################ + +############################################################################### +# # +# Nagios plugin to test IMAP servers with STARTTLS and test to login with # +# username and password # +# # +############################################################################### + +PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin" + +VERSION="Version 0.1" +AUTHOR="(c) 2016 Jack-Benny Persson (jack-benny@cyberinfo.se)" + +# Exit codes +STATE_OK=0 +STATE_WARNING=1 +STATE_CRITICAL=2 +STATE_UNKNOWN=3 + +shopt -s extglob + +# Function to login/logout from IMAP-servers +login() { +sleep 2 +echo "a login $Username $Password" +sleep 1 +echo "a logout" +} + +# Print version information +print_version() +{ + printf "\n\n$0 - $VERSION\n" +} + +#Print help information +print_help() +{ + print_version + printf "$AUTHOR\n" + printf "Test/monitor a IMAP server with STARTTLS and login with a username/password\n" +/bin/cat < /dev/null +if [ $? != 0 ]; then + echo "There is a problem with your OpenSSL program" + exit 3 +fi + + +# Parse command line options +while [[ -n "$1" ]]; do + case "$1" in + + -h | --help) + print_help + exit $STATE_OK + ;; + + -V | --version) + print_version + exit $STATE_OK + ;; + + -p | --port) + Port="$2" + shift 2 + ;; + + -H | --hostname) + Host="$2" + shift 2 + ;; + + -U | --username) + Username="$2" + shift 2 + ;; + + -P | --password) + Password="$2" + shift 2 + ;; + -\?) + print_help + exit $STATE_OK + ;; + + *) + printf "\nInvalid option '$1'" + print_help + exit $STATE_UNKNOWN + ;; + esac +done + +# Check if everything is set +if [[ -z "$Host" ]]; then + printf "\nHostname not set" + print_help + exit $STATE_UNKNOWN +fi + +if [[ -z "$Port" ]]; then + printf "\nPort not set" + print_help + exit $STATE_UNKNOWN +fi + +if [[ -z "$Username" ]]; then + printf "\nUsername not set" + print_help + exit $STATE_UNKNOWN +fi + +if [[ -z "$Password" ]]; then + printf "\nPassword not set" + print_help + exit $STATE_UNKNOWN +fi + +# Main program/logic +out=`login | openssl s_client -crlf -connect $Host:$Port -starttls imap 2>&1 | grep "a OK Logged in"` +if [ $? == 0 ]; then + echo "IMAP OK: $out" + exit $STATE_OK +else + echo "IMAP CRITICAL: Can't login to IMAP server $Host:$Port" + exit $STATE_CRITICAL +fi + +# If we ended down here, it's an unknown mystery +exit $STATE_UNKNOWN