-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-watcher.sh
executable file
·64 lines (57 loc) · 2.24 KB
/
file-watcher.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
# Send a metric to statsd from bash
#
# Useful for:
# deploy scripts (http://codeascraft.etsy.com/2010/12/08/track-every-release/)
# init scripts
# sending metrics via crontab one-liners
# sprinkling in existing bash scripts.
#
# netcat options:
# -w timeout If a connection and stdin are idle for more than timeout seconds, then the connection is silently closed.
# -u Use UDP instead of the default option of TCP.
#
#echo "deploys.test.myservice:1|c" | nc -w 1 -u 001.graphite 8125
# ------------ CONFIGURATION ---------
INOTIFYWAIT=`which inotifywait`
LOGGER=`which logger`
LOGGER_CONFIG="--id --priority daemon.notice --tag fs-watcher"
NC=`which nc`
NC_CONFIG="-w 1 -u 001.graphite 8125"
# ------ FUNCTIONS ----------------
message() {
case "${event}" in
CREATE)
${LOGGER} ${LOGGER_CONFIG} "the file ${file} inside ${dir} has been created"
echo "file-watcher.file.create:1|c" | ${NC} ${NC_CONFIG}
;;
CREATE:ISDIR)
${LOGGER} ${LOGGER_CONFIG} "the directory ${dir}${file} has been created"
echo "file-watcher.directory.create:1|c" | ${NC} ${NC_CONFIG}
;;
DELETE:ISDIR)
${LOGGER} ${LOGGER_CONFIG} "the directory ${dir}${file} has been deleted"
echo "file-watcher.directory.delete:1|c" | ${NC} ${NC_CONFIG}
;;
DELETE)
${LOGGER} ${LOGGER_CONFIG} "the file ${file} inside ${dir} has been deleted"
echo "file-watcher.file.delete:1|c" | ${NC} ${NC_CONFIG}
;;
MODIFY)
${LOGGER} ${LOGGER_CONFIG} "the file ${file} inside ${dir} has been modified"
echo "file-watcher.file.modify:1|c" | ${NC} ${NC_CONFIG}
;;
MOVED_FROM)
${LOGGER} ${LOGGER_CONFIG} "the file ${file} has been moved FROM ${dir} directory"
echo "file-watcher.file.moved_from:1|c" | ${NC} ${NC_CONFIG}
;;
MOVED_TO)
${LOGGER} ${LOGGER_CONFIG} "the file ${file} has been moved TO ${dir} directory"
echo "file-watcher.file.moved_to:1|c" | ${NC} ${NC_CONFIG}
;;
esac
}
# ------- MAIN ------------
${INOTIFYWAIT} -mr --format '%w %f %:e' -e create -e modify -e delete -e moved_to -e moved_from $1 | while read dir file event; do
message
done