0

I an running df -k command for my application server, and it is having mount issue.

So I need to trigger a mail saying that the server is having mount issue.

My basic question, I will write a shell script which will run df -k command and identify if the command takes long time to complete the command, then I need to trigger a mail.

How can I do this ?

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146

2 Answers2

0

It's not clear what exactly you're trying to archive; but timing how long a command takes is fairly easy, for example:

#!/bin/sh

start=$(date +%s)
out=$(sleep 6)
took=$(($(date +%s) - $start))

if [ $took -gt 5 ]; then
    echo "$out" | mail -s "Command took too long" test@example.com
fi

Edit

This required the command to finish; if you want to have a timeout, I'd recommend using Python. It is possible with shell scripting, but IMHO this is much easier.

#!/usr/bin/env python

import subprocess, smtplib
from email.mime.text import MIMEText

proc = subprocess.Popen(['sleep', '100'])
try:
    proc.wait(5)  # 5 is the timeout in seconds
except subprocess.TimeoutExpired:
    proc.kill()

    # Send email
    msg = MIMEText("Command took too long\r\n")
    msg['Subject'] = 'Command took too long'
    msg['From'] = 'test@example.com'
    msg['To'] = 'test@example.com'
    s = smtplib.SMTP('localhost')
    s.sendmail('test@example.com', ['test@example.com'], msg.as_string())
    s.quit()
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • Thanks a lot Carpetsmoker.But this will work only when the command completed successfully,am i right?Mount issue is soming thing like some mount points are created in created to share the folder in the remote server.If the host server not able to connect to that remote server , the df -k command will keep loading without allowing us to type the next command.We manually need to use CTRL+C to kill the command. – Gopal Krishnan Jul 22 '14 at 07:53
  • @GopalKrishnan That's correct, I added an example with a timeout. – Martin Tournoij Jul 22 '14 at 08:21
  • Hi Can you please let me know how to create a pythol file.what is the extension of hte file and how to run the file? – Gopal Krishnan Jul 22 '14 at 08:26
  • The usual extension is `.py`. And you can just use `python file.py`, or make it executable and use `./file.py`... – Martin Tournoij Jul 22 '14 at 08:28
  • File "./mount_check_Allservers.sh", line 6, in proc = subprocess.Popen(['sh weblogic@web-preapp5 df -k', '100']) File "/usr/lib/python2.6/subprocess.py", line 621, in __init__ errread, errwrite) File "/usr/lib/python2.6/subprocess.py", line 1126, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory – Gopal Krishnan Jul 22 '14 at 08:30
  • The `args` argument isn't executed by the shell. [See the docs](https://docs.python.org/2.6/library/subprocess.html). Perhaps you could add `shell=True`... Perhaps it would be even better to call a coworker who has more experience to look with you, since you seem to be *very* inexperienced in this area... – Martin Tournoij Jul 22 '14 at 08:34
0

This question lists ways to detect stale NFS mounts: Is there a good way to detect a stale NFS mount

Select one solution and run it before the df -k.

Alternatively, you can redirect stderr to stdout and then grep for the error pattern:

df -k 2>&1 | grep ...error...
if [[ $? -ne 0 ]]; then
    ...send mail...
fi
Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Thanks Aaron.But this does'nt work.I used ssh web@web-slapp5 df -k | grep not.But i dont get any output.It got stucked in the df -k command itself. – Gopal Krishnan Jul 22 '14 at 08:00
  • Eventually, the `df` command will time out. And the code above needs to go into a single script which you have to invoke with `ssh`. Your code will run `df -k` remotely and then grep locally. If you want to avoid the wait, you need to look at the solutions behind the link. – Aaron Digulla Jul 22 '14 at 08:51