Following on from my post, 'HOW-TO: Chrooted SFTP only access' I wanted to find a way to get an email notification of when a client had uploaded a file to my work's SFTP area. I had Sendmail already installed.
Each client has their own subdirectory under /home/sftp/ and I wanted to monitor them all and trigger an email when a new file was created.
Ok, I don't know how much use this will be to others but I am just going to throw down what I did to achieve what I wanted. These are personal notes and use them at your peril.
Code:
sudo apt-get install iwatch
installs iWatch, written in Perl and based on inotify, a file change notification system, a kernel feature that allows applications to request the monitoring of a set of files against a list of events.
Code:
sudo touch /etc/init.d/sftp-iwatch
creates an empty file. I edited the file and typed
Code:
iwatch -r -e create -c "(w;ps -ef)|/home/david/file_notify_email.sh %f '$($(which date))'" /home/sftp >> /dev/null 2>&1 &
which basically executes a script /home/david/file_notify_email.sh when a new file is created under /home/sftp/.
/home/david/file_notify_email.sh
Code:
#!/bin/bash
# 20110723 David Humble
FILE="$1"
CREATED="$2"
DATE=$($(which date))
FROM="aadfiler"
SUBJECT="File upload notification"
EMAIL="technical@xxx.co.uk"
EMAILMESSAGE="/var/upload.txt"
echo "Date: $DATE" > /var/upload.txt
echo "To: $EMAIL" >> /var/upload.txt
echo "From: $FROM">>/var/upload.txt
echo "Subject: $SUBJECT">>/var/upload.txt
echo "New file $FILE created at $CREATED on aadfiler SFTP Server.">>/var/upload.txt
echo ".">>/var/upload.txt
cat /var/upload.txt | /usr/sbin/sendmail -t
Code:
sudo chmod +x /etc/init.d/sftp-iwatch
sudo chmod +x /home/david/file_notify_email.sh
makes both files executable.
Code:
cd /etc/init.d
update-rc.d sftp-iwatch defaults
Adds sftp-iwatch to the servers startup processes.
now you can manually start sftp-iwatch by typing
Code:
sudo /etc/init.d/sftp-iwatch
Now whenever a client uploads a file to any subdirectory of /home/sftp/ I get a notification thus:
Quote:
Date: Wed Aug 3 16:27:02 BST 2011
To: technical@xxx.co.uk
From: aadfiler@xxx.local
Subject: File upload notification
New file /home/sftp/clientsname/upload/AAD013 GE eNewsletter Day 3.csv created at Sat Jul 23 13:36:37 BST 2011 on aadfiler SFTP Server.
References
http://iwatch.sourceforge.net/index.html
Categories: How-To, Linux, OS, Open-SSH, SFTP server, Chrooted SFTP only access
Tags: kde, ubuntu server, Linux, open-ssh, chroot, sftp, File upload email notification, iwatch, notify, dnotify