User Tools

Site Tools


sftp_monitoring_script

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sftp_monitoring_script [2020/05/07 15:23] (current)
The SuperUser created
Line 1: Line 1:
 +Create "​myfile"​ full of random data to have an upload file
 +<​code>​head -c 10M </​dev/​urandom >​myfile</​code>​
 +Create "​serverlist"​ with servers to test, one line per server, no blank lines or comments\\
 +Create "​mail.template"​ with your body header. ​ This file is put at the top of the email if needed
 +
 +Here is the script:
 +<​code>​
 +#!/bin/bash
 +
 +FILE=/​home/​filemon/​myfile
 +LOG=/​home/​filemon/​log/​sftpmon.log
 +T_FILE=`mktemp`
 +
 +while read server
 +do
 +        result=`scp -B -q $FILE $server:/ >/​dev/​null 2>&​1;​ echo $?`
 +        if [ $result -eq 0 ]
 +        then
 +                echo "​`date` $server successful"​ >> $LOG
 +        else
 +                echo "​`date` $server failed"​ >> $LOG
 +                echo $server >> $T_FILE
 +        fi
 +done < /​home/​filemon/​serverlist
 +
 +if [ `cat $T_FILE | wc -l ` -gt 0 ]
 +then
 +        cat /​home/​filemon/​mail.template $T_FILE | mailx -s "SFTP server issue" [email protected]
 +fi
 +rm $T_FILE
 +</​code>​
 +
 +Run this in cron with flock to prevent multiple instances
 +<​code>​*/​5 * * * * flock -n /​home/​filemon/​sftpmon.lck -c /​home/​filemon/​sftpmon.sh</​code>​
 +
 +Put the log file into logrotate to keep it under control.
 +
  
sftp_monitoring_script.txt ยท Last modified: 2020/05/07 15:23 by The SuperUser