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.

Dani AI

Generated

A few practical ways to close the gaps in the thread: already has the right high-level flow, and correctly pointed out fstab/sudo as fixes when mounts require root. Two robust options usually work best: 1) avoid mounting entirely and query the SMB server for the file timestamp, or 2) allow a tightly scoped mount/umount for the monitoring account (fstab + credentials or a restricted sudoers entry). Each avoids running an all-powerful script as root and keeps the plugin reliable.

Querying SMB without a mount is the simplest and avoids permission headaches. A small example using smbclient (do not embed plaintext passwords on the command line) and a credentials file:

smbclient //HOST/SHARE -U 'USER' -c "allinfo path/to/file"

Credentials file format (600 perms):

username=USER
password=PASS

Parsing text output can be brittle; prefer extracting a numeric mtime (epoch) if possible, or fetch the file to /tmp and stat it locally.

If mounting is required, use /etc/fstab with explicit options so a non-root process can mount the share, for example noauto,x-systemd.automount,_netdev,credentials=/root/.smbcreds,uid=backup,file_mode=0444,dir_mode=0444. Alternatively, give the monitor only the mount/umount commands via sudoers:

monitoringuser ALL=(root) NOPASSWD: /bin/mount /mnt/folder, /bin/umount /mnt/folder

Limit scope where possible and keep the credential file protected (chmod 600).

Make the script defensive: enable set -euo pipefail, verify the mount with mountpoint -q /mnt/folder before statting, test file existence first, trap to unmount on EXIT, avoid lazy unmount unless intended, and use standard monitoring exit codes:

OK=0; WARNING=1; CRITICAL=2; UNKNOWN=3

These steps address permission, parsing, cleanup, and correct status reporting while keeping the check safe to run from a monitoring account.

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.