1

I am trying to create a bash script to check that mounts are in place and if not both log the information to a file as well as send out a notification. I am very novice at scripting so some of the ins and outs are elusive to me.

Because we are doing this script to monitor a very finicky piece of software I was hoping to do as much as possible in line rather than have something that creates a config file or what not because I know that if anything is changed as a setting the vendor will say that is the cause of all the issues. Below is the code of what I have come up with so far. I will admit I am not totally confident that anything in the code is going to work so if you see any glaring errors I've made I would love to know about them.

#!/bin/bash
LOGFILE="/tmp/logs/mount.log
TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`

while sleep 10m;
do
    status=$(for mnt in /reachengine /reachdata/mongo /reachbkups /mnt/AsperaShares /mnt/Editing /mnt/VOD-World/Movies_in_ProRes51_Archive /mnt/Production /mnt/ReachEngine /mnt/ITBackup /mnt/reach '/mnt/Ready for Air' '/mnt/Ready for Reach'; do mountpoint -q "$mnt" || echo "$TIMESTAMP $mnt missing"; done)
    [ "$status" ] && echo "$status" >> $LOGFILE
    [ "$status" ] && echo "$status" | mail -s "Missing mount" ####@###.##
done
Tripp Kinetics
  • 5,178
  • 2
  • 23
  • 37
Daniel Cox
  • 33
  • 1
  • 6
  • In what way does that code fail? – Tripp Kinetics Apr 05 '18 at 18:57
  • I am actually asking the question be fore I run it to try and make sure it is good before I put it on the extremely finicky server and run it there. Even if there is absolutely nothing that this should effect the vendor of the software on that system will try to use it as a reason their software doesn't work so I would rather double, triple, and quadruple check and have it reviewed before trying it on the system. – Daniel Cox Apr 05 '18 at 19:21
  • You badly need a test environment. – Tripp Kinetics Apr 05 '18 at 19:22
  • There are 6 months of banging our head against the wall with this vendor about that very topic. What they said would work was an option to expensive to have actually be viable. – Daniel Cox Apr 05 '18 at 19:29
  • Can you clone the server and test with the clone? – Tripp Kinetics Apr 05 '18 at 19:52
  • We would need to clone 3 servers at least and the software is not supported in a virtual environment and any software installed to allow for the clone to happen would be pointed to as a problem. The system is raided so shutting the system down physically copying isn't possible. – Daniel Cox Apr 05 '18 at 20:24

1 Answers1

1
#!/bin/bash

logfile="/tmp/logs/mount.log"               # add 1 quote
timestamp=$( date "+%Y-%m-%d %H:%M:%S" )
mounts=(
    /reachengine 
    /reachdata/mongo 
    /reachbkups 
    /mnt/AsperaShares 
    /mnt/Editing 
    /mnt/VOD-World/Movies_in_ProRes51_Archive 
    /mnt/Production 
    /mnt/ReachEngine 
    /mnt/ITBackup 
    /mnt/reach 
    '/mnt/Ready for Air' 
    '/mnt/Ready for Reach'
)

while sleep 10m; do
    status=$(
        for mnt in ${mounts[@]}; do 
            mountpoint -q "$mnt" || echo "$timestamp $mnt missing"
        done
    )
    if [ "$status" ]; then
        echo "$status" >> $logfile
        echo "$status" | mail -s "Missing mount" ####@###.##
    fi
done 

suggestions with:

  • correction close double quote on logfile=
  • lowercase variables
  • $( ) instead backticks
  • array to scan mounts, easier to maintain
  • better indentation

just a quick reading, not a test
take what you like

kyodev
  • 573
  • 2
  • 14
  • Your quick read is likely significantly more experienced than my poring over it. I do like the defining of the array early as it is a lot more readable, in fact, it is all more readable. Is the lowercase variables necessary or is it just how most people do it? Also since I have not done it before do you know if I have to create the log file before I start this or will outputting to a file that doesn't exist create it? – Daniel Cox Apr 05 '18 at 22:15
  • for logfile, you can do like that, you **add** to an existing file `>>` If the file does not exist, it will be created. I you want to create a new file every start, you have to erase it before loop – kyodev Apr 05 '18 at 22:41
  • the lowercase is prefered, the upercase variable "reserved" for system or environment – kyodev Apr 05 '18 at 22:42
  • Well hopefully this won't be needing to be maintained in the future because it is more to show evidence that the issue the vendor is claiming is not in fact happening. However just because this one should be short-lived doesn't mean I shouldn't get in the habit of doing it right and for long-term, – Daniel Cox Apr 06 '18 at 15:41