Solved

Linux script to check IMAP status

Posted on 2009-05-18
7
1,035 Views
Last Modified: 2012-05-07
Hi,
I'm looking for a bash script (example) that executes some commands depending on if a certain (IMAP) server is on-line.

What is the best way to do this?

For example: ping the server and depending on the response execute some commands.
Thank in advance.
Onno
0
Comment
Question by:ostraaten
  • 3
  • 2
  • 2
7 Comments
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 50 total points
ID: 24415516
I'd use the Nagios IMAP check as it is very comprehensive.

# ./check_imap -h
check_imap v1991 (nagios-plugins 1.4.13)
Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>
Copyright (c) 1999-2008 Nagios Plugin Development Team
        <nagiosplug-devel@lists.sourceforge.net>

This plugin tests IMAP connections with the specified host (or unix socket).

Usage:check_imap -H host -p port [-w <warning time>] [-c <critical time>] [-s <send string>]
[-e <expect string>] [-q <quit string>][-m <maximum bytes>] [-d <delay>]
[-t <timeout seconds>] [-r <refuse state>] [-M <mismatch state>] [-v] [-4|-6] [-j]
[-D <days to cert expiry>] [-S <use SSL>] [-E]

Options:
 -h, --help
    Print detailed help screen
 -V, --version
    Print version information
 -H, --hostname=ADDRESS
    Host name, IP Address, or unix socket (must be an absolute path)
 -p, --port=INTEGER
    Port number (default: none)
 -4, --use-ipv4
    Use IPv4 connection
 -6, --use-ipv6
    Use IPv6 connection
 -E, --escape
    Can use \n, \r, \t or \ in send or quit string. Must come before send or quit option
    Default: nothing added to send, \r\n added to end of quit
 -s, --send=STRING
    String to send to the server
 -e, --expect=STRING
    String to expect in server response (may be repeated)
 -A, --all
    All expect strings need to occur in server response. Default is any
 -q, --quit=STRING
    String to send server to initiate a clean close of the connection
 -r, --refuse=ok|warn|crit
    Accept tcp refusals with states ok, warn, crit (default: crit)
 -M, --mismatch=ok|warn|crit
    Accept expected string mismatches with states ok, warn, crit (default: warn)
 -j, --jail
    Hide output from TCP socket
 -m, --maxbytes=INTEGER
    Close connection once more than this number of bytes are received
 -d, --delay=INTEGER
    Seconds to wait between sending string and polling for response
 -D, --certificate=INTEGER
    Minimum number of days a certificate has to be valid.
 -S, --ssl
    Use SSL for the connection.
 -w, --warning=DOUBLE
    Response time to result in warning status (seconds)
 -c, --critical=DOUBLE
    Response time to result in critical status (seconds)
 -t, --timeout=INTEGER
    Seconds before connection times out (default: 10)
 -v, --verbose
    Show details for command-line debugging (Nagios may truncate output)
0
 

Author Comment

by:ostraaten
ID: 24415848
That is too advanced a solution for me, this is just to automate some stuff on my laptop.

A script that just checks if a server can be reached and if it can reach the server it should execute some commands.

0
 
LVL 48

Expert Comment

by:Tintin
ID: 24416159
Checking whether a particular port is responding or not is straight forward, but once you add in the requirement to execute commands and check the response/status, then it becomes  a lot more complicated.

Do you have a specific example of the IMAP commands you want to run?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:ostraaten
ID: 24416339
No not at this point.

I think a simple ping can suffice, if the server can be reached it should run a script, it can be any script.
0
 

Accepted Solution

by:
ipv6ee earned 450 total points
ID: 24417471
Hi,

i have attached  my script that i use for HTTP and others i modified it for IMAP server and it will mail the status of imap server to u(root) if its running or not.
You can chage port according to urs need i.e 993 to 995 in the script
You can add more services status like ntp everthing will be same.

Cheers
#!/bin/bash
 
 

###### IMAPs SECTION ###########

netstat -ntulp | grep 993 > /dev/null 
 

IMAPs="$?"
 

if [ $IMAPs -eq  0 ];then
 

echo 	"On $(date '+%F at %r') IMAPs is up and listening" | mail -s "IMAPs server status" root
 

else 
 

echo "On $(date '+%F %r') IMAP is not running" | mail -s "IMAPs server is down and needs yours attention immediatly" root
 
 
 

fi

Open in new window

0
 

Author Comment

by:ostraaten
ID: 24419141
The script turned out to be not exactly what I needed as it tests for a local IMAP server, I want to check if a remote (IMAP) server can be reached. Basically I want to check if I'm connected to the network.  

But I could change the script a bit easily to my needs by using a ping command.
#!/bin/bash

 

ping -c 1 myimapserver | grep "1 received" > /dev/null

 

RESULT="$?"

 

if [ $RESULT -eq  0 ];then

 

echo    "On $(date '+%F at %r') server found" 

 

else 

 

echo "On $(date '+%F %r') server is not found" 

 

 

fi

Open in new window

0
 

Expert Comment

by:ipv6ee
ID: 24419363
if u put IMAP script in crontab suppose to run every 10 minutes it will mail to the email-id u specify the status of IMAP every 10 minutes so it will work on remotely too :-)
 nd same thing u can do with urs own script too.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Installing Git and chefdk via bat script 8 56
issue with beginner powershell script. 5 54
Configure Robocopy to excluding folders 6 45
networking details on centos 6.6 4 49
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now