marian78
Patron
- Joined
- Jun 30, 2011
- Messages
- 210
hi,
i want ask for help with my script, that email me, if somebody upload/download/delete files over FTP. For now i testing it, but want some critique, that i can tune up it.
How it works:
1. in Freenas box - FTP advanced tab, set this to make transfer log:
2. next i wrote small script, that use "tail" for monitoring transfer log. If in transfer log is new line, script looking into this line and search for known names of ftp folders. If it find, add to this folder name email adress and posted this line to this email.
I have two groups of ftp folders - by send to email adress.
If script find folder but no email is defined for that folder, email is set to default.
Here is my script, that i will set as startup script after testing (FreeNAS-9.10-STABLE-201604261518 (881b70d)) and tuning (for now is setup as disabled cron jod and manually run) :
i want ask for help with my script, that email me, if somebody upload/download/delete files over FTP. For now i testing it, but want some critique, that i can tune up it.
How it works:
1. in Freenas box - FTP advanced tab, set this to make transfer log:
Code:
#FTP transfer file log - add to advanced ftp service this: TransferLog /mnt/volume0/logs/transfer.log LogFormat default "%a %h %l %u %t \\"%r\\" %s %b" LogFormat auth "%v [%P] %h %t \\"%r\\" %s" LogFormat write "%h %l %u %t \\"%r\\" %s %b"
2. next i wrote small script, that use "tail" for monitoring transfer log. If in transfer log is new line, script looking into this line and search for known names of ftp folders. If it find, add to this folder name email adress and posted this line to this email.
I have two groups of ftp folders - by send to email adress.
If script find folder but no email is defined for that folder, email is set to default.
Here is my script, that i will set as startup script after testing (FreeNAS-9.10-STABLE-201604261518 (881b70d)) and tuning (for now is setup as disabled cron jod and manually run) :
Code:
#!/bin/sh #FTP transfer file log - add to advanced ftp service this: # TransferLog /mnt/volume0/logs/transfer.log # LogFormat default "%a %h %l %u %t \\"%r\\" %s %b" # LogFormat auth "%v [%P] %h %t \\"%r\\" %s" # LogFormat write "%h %l %u %t \\"%r\\" %s %b" #case sensitive #names of ftp dirs on ftp root are equal to ftp user names users="dir1 dir2 dir3 dir4 dir5 dir6 dir7" #dirs of ftpusers1 that email to tomail1 ftpusers1="dir1 dir2 dir3" #dirs of ftpusers2 that email to tomail2 ftpusers2="dir4 dir5 dir6" #ftpusers1 email adress tomail1="test1@yourdomain.com" #ftpusers2 email adress tomail2="test2@yourdomain.com" #default email if not exist any "users" in "ftpuser1" or "ftpuser2" tomail3="test3@yourdomain.com" #email from frommail="from@yourdomain.com" #subject prefix subjectpre="my ftp server detect transport on:" #ftp root dir rootdir="/mnt/volume0/" #FTP transfer file log - add to advanced ftp service this: logfile="/mnt/volume0/logs/transfer.log" #temp file tmpfile="/mnt/volume0/scripts/ftpsendmail.tmp" #start #delete and backup transfer log after start script (start with clean transfer log file) if test -f "$logfile";then cp -p "$logfile" "$logfile".`date +%H.%M.%S_%d.%m.%Y.bak` rm "$logfile"; fi #wait for transferlog creation while ! test -s "$logfile"; do sleep 60 done while true; do #find changes in transferlog tail -F -n 0 $logfile | while read line; do #find ftpuser from line in transerlog and send email for user in $users; do if echo "$line" | grep -q $rootdir${user}; then #assotiate mailto - "ftp user" vs "mail to" if echo "$ftpusers1" | grep -q ${user}; then tomail="$tomail1"; else if echo "$ftpusers2" | grep -q ${user}; then tomail="$tomail2"; else tomail="$tomail3"; fi fi #temp file delete if test -f "$tmpfile";then rm "$tmpfile"; fi #prepare mail to temp file ( echo "To: $tomail" echo "From: $frommail" echo "Subject: $subjectpre ${user}" echo "Content-Type: text/html" echo "MIME-Version: 1.0" echo -e "\r\n" echo "<pre style=\"font-size:14px\">" echo "$line" ) > $tmpfile #send mail sendmail -t < $tmpfile; #delete temp file rm "$tmpfile" fi done done done
Last edited: