First, it has been a LONG time since Ive been here. Good memories from when I was young.

Anyways

In a monitoring system I have, I had a plugin that remotely checked a SMB share and if the file hasnt been modified in 1 day, it should return a warning. if it is 2 days or more, it should return a critical.

Returning the warning and the critical isnt the issue, its the entire logic of the script itself.

I mean, I dont think it should be too difficult but Im not used to shell scripting. Also, the downfall exists, of having to check if the mount is correct, etc.

The logic is basically

inputvariable warningdays
inputvariable criticaldays
inputvariable smbpath
mount smbpath to final folder /mnt/folder
if mount is unsuccessful
return unknown
if mount is successful
if /mnt/folder/file does not exist
return unknown
if /mnt/folder/file does exist
if /mnt/folder/file has been written to less than warningdays
return ok
if /mnt/folder/file has been written to more than warningdays and less than criticaldays
return warning
if /mnt/folder/file has been written to more than criticaldays
return critical
if previousoperations are done
unmount
if unmount is unsuccessful
return unknown
finish

Could someone tell me the basics of the shell script?

Thank you.

Recommended Answers

All 5 Replies

Bash would be fine

#!/bin/sh
# How many seconds before file is deemed "older"
# 86400 = 1 day
# 172800 = 2 days
FILENAME=$1
WARNTIME=$2
CRITTIME=$3
# Get current and file times
CURTIME=$(date +%s)
mount -t cifs //192.168.3.222/SHARENAME/FOLDER /mnt/FOLDER -o ro,username=USER,vers=1.0,password=PASS,nodev,uid=backup,file_mode=0444,dir_mode=0444
FILETIME=$(stat /mnt/FOLDER/$FILENAME -c %Y)
umount -l /mnt/FOLDER
TIMEDIFF=$(expr $CURTIME - $FILETIME)
# Check if file older
if [ $TIMEDIFF -lt $WARNTIME ]; then
echo "OK"
exit 1
elif [ $TIMEDIFF -gt $WARNTIME -and $TIMEDIFF -lt $CRITTIME ] ; then
echo "WARNING"
exit 1
elif [ $TIMEDIFF -gt $CRITTIME ] ; then
echo "CRITICAL"
exit 2
fi
echo "UNKNOWN"
exit 3

I believe the issue is that "mount" isnt working. Running this as root works so the script isnt the issue. I know the username that runs the script.

First, it has been a LONG time since Ive been here. Good memories from when I was young.

This question is out of my wheelhouse, but just thought I'd reach out and say welcome back!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.