1

I am looking for a good way monitor and log mounts on a CentOS 6.5 box. Since I am new to Linux shell scripting I am somewhat at a loss as to if there is something that is already around and proven which I could just plug in or is there a good method I should direct my research toward to build my own.

In the end what I am hoping to have running is a check of each of the 9 mounts on the server to confirm they are up and working. If there is an issue I would like to log the information to a file, possibly email out the info, and check the next mount. 5-10 minutes later I would like to run it again. I know that probably this isn't needed but we are trying to gather evidence if there is an issue or show to a vendor that what they are saying is the issue is not a problem.

Daniel Cox
  • 33
  • 1
  • 6
  • 2
    You should ask a specific question for a particular problem. Since Stack Overflow hides the Close reason from you: *"Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question."* – jww Apr 03 '18 at 23:32

1 Answers1

2

This shell script will test each mountpoint and send mail to root if any of them is not mounted:

#!/bin/bash
while sleep 10m;
do
    status=$(for mnt in /mnt/disk1 /mnt/disk2 /mnt/disk3; do mountpoint -q "$mnt" || echo "$mnt missing"; done)
    [ "$status" ] && echo "$status" | mail root -s "Missing mount"
done

My intention here is not to give a complete turn-key solution but, instead, to give you a starting point for your research. To make this fit your precise needs, you will need to learn about bash and shell scripts, cron jobs, and other of Unix's very useful tools.

How it works

  • #!/bin/bash

    This announces that this is a bash script.

  • while sleep 10m; do

    This repeats the commands in the loop once every 10 minutes.

  • status=$(for mnt in /mnt/disk1 /mnt/disk2 /mnt/disk3; do mountpoint -q "$mnt" || echo "$mnt missing"; done)

    This cycles through mount points /mnt/disk1, /mnt/disk2, and /mnt/disk3 and tests that each one is mounted. If it isn't, a message is created and stored in the shell variable status.

    You will want to replace /mnt/disk1 /mnt/disk2 /mnt/disk3 with your list of mount points, whatever they are.

    This uses the command mountpoint which is standard on modern linux versions. It is part of the util-linux package. It might be missing on old installations.

  • [ "$status" ] && echo "$status" | mail root -s "Missing mount"

    If status contains any messages, they will be mailed to root with the subject line Missing mount.

    There are a few different versions of the mail command. You may need to adjust the argument list to work with the version on your system.

  • done

    This marks the end of the while loop.

Notes

The above script uses a while loop that runs the tests every ten minutes. If you are familiar with the cron system, you may want to use that to run the commands every 10 minutes instead of the while loop.

John1024
  • 109,961
  • 14
  • 137
  • 171
  • This is Exactly what I was hoping for. It gives me ways to do what I am looking for and ideas of how to modify it for my own uses. I may make multiple entires so that each one tests one mount point that way I can tailor the output message so that I know which mount is an issue. I figure I could also add a section for writing the output to a file so I have something on the system to track it also. Thank you very much this was a perfect and extremely informative. Edit: Just noticed this already identifies the mount point. – Daniel Cox Apr 04 '18 at 16:59
  • Working on the script and I think it is looking good (I may want to post it to catch glaring errors before letting it loose on the server). I have a question though, would \40 work as a space in this command or is there another way I need to do it? Also would the mnt in "status=$(for mnt in" need a $ to identify it for the future stuff, if not is there a reason for that? – Daniel Cox Apr 04 '18 at 17:42
  • @DanielCox Yes, we use the variable name, `mnt`, when we assign a value to the variable. We use `$mnt` when we want to reference that value. In bash, one can use blanks or tabs or any combination of the two for spaces. – John1024 Apr 04 '18 at 18:33
  • So having a mount like "/mnt/Ready for Air" in place of "/mnt/disk1" from your example will not cause any issues? I would have figured that it would think that the space means it should be going on to the next item. – Daniel Cox Apr 04 '18 at 19:45
  • @DanielCox Thanks for clarifying that: Just put any such mount point names in quotes like this: `for mnt in '/mnt/Ready for Air' /mnt/disk2 '/mnt/Ready for Sea'; do` – John1024 Apr 04 '18 at 21:57