Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm in need of a script which should work as below.

A script where it would validate if that specific mountpoint is currently mounted on the server. It would search for mountpoint name in /etc/fstab and then validate either from df -h output or /proc/mounts to check if its mounted on the server (if you have any better way to validate thats also fine).

After reboot, in case it does not gets mounted it would trigger an email.

For one server its okay but this is going to be used to validate more than 1000 servers so a script would be better solution.

So the script would be executed on one server and it would check for mountpoint status in another 1000 server.

Mountpoint in the server is in the name of /mount1, /mount2, /mount3 etc. We need to validate this specific mountpoint name only, we can ignore other OS related FS.

What I have so far:

#!/bin/bash

# Grep for word mountpoint name ie "mount" 
awk '{print $2}' /etc/fstab | grep -i "mount" > mntpoint.txt
exec 3< mntpoint.txt

while read mount <&3
do
# Search for present mountpoint in file /prod/mounts.
# I'm using /proc/mounts here to validate

grep -iw $mount /proc/mounts > /dev/null

if [ $? -eq 0 ]
then
    echo $mount "is mounted"
else
    echo $mount "is not mounted needs manual intervention"
fi
done
share|improve this question
2  
What have you already tried? In general, we don't just write out huge systems on demand, but help you in writing one when you get stuck doing so. –  Chris Down Sep 12 '13 at 19:20
3  
Hi, this is not a "We write your code for free!" webpage. Perhaps you were looking for rent-acoder.com? Please attempt to write the script yourself and post here if you have a specific problem we can help you with. Have a look at our help center pages for information on what kinds of questions one can ask here. –  terdon Sep 12 '13 at 19:21
    
Sure Chris,I understand...This is what I am trying in one of the server. It would let me know which mountpoint is currently missing. But same script has to be executed from one server and it should login to each server and provide output. So is it that it has to be copied to destination server, run it there and send the output back to original server ? –  Venom Sep 12 '13 at 21:29
    
The script which I am trying to copy above doesnot follow indentation !! sorry about it ..is there better way to put my script in this post –  Venom Sep 12 '13 at 21:35

4 Answers 4

I would recommend trying this in python. The built-in os.path module has a very simple ismount feature.

$ cat ismount.py 
import os
mp = '/mount1'
if os.path.ismount(mp):
    print('{0} is mounted'.format(mp))
else:
    print('{0} is NOT mounted'.format(mp))
$ python ismount.py 
/mount1 is NOT mounted
share|improve this answer

Try something like this. First create a list of server IPs (this assumes that you have password-less ssh set up and can connect as root to all severs) then, run this little script (copy paste into the command line):

while read ip;
do
    echo "connecting to $ip";
    ssh root@$ip "until mount | grep -w \"$MOUNT\" >/dev/null;
     do echo mounting \"$MOUNT\"; mount \"$MOUNT\"; sleep 1; done && 
     echo Mounted on $ip"
done < ips.txt

This expects a file called ips.txt with one IP per line. It will ssh into the IP, and while the mount $MOUNT is not mounted, it will attempt to do so. Make sure to replace $MOUNT with the mount point you are interested in, it should appear exactly as it does in /etc/fstab. For example:

while read ip;
do
    echo "connecting to $ip";
    ssh root@$ip "until mount | grep -w \"/mnt/data\" >/dev/null;
     do echo mounting \"/mnt/data\"; mount \"/mnt/data\"; sleep 1; done && 
     echo Mounted on $ip"
done < ips.txt
share|improve this answer
#!/bin/bash

A=``awk '{print $2}' /etc/fstab | grep -i "^/" | egrep -v '/etc/fstab|proc|sys|shm|pts`'`

`for i in $A; do
grep $i /proc/mounts > /dev/null`

`if [ $? -eq 0 ]; then
    echo $i "is mounted"
else
    echo $i "is not mounted needs manual intervention"
fi
done`
share|improve this answer
    
This will return false positives if the path is a substring of a larger mountpoint. –  Chris Down Dec 31 '13 at 5:28

The script provided by LG works fine when you change the grep in the for-loop to:

grep "$i " /proc/mounts.

In that case it won't return false positives.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.