Ticket #127: transmission_watchdog

File transmission_watchdog, 6.8 KB (added by oleo, 3 years ago)

For running with cron ever 30 minutes or so

Line 
1#!/bin/sh
2
3. /opt/etc/transmission.conf
4
5DEBUG=
6
7##
8##############################################
9
10##########################
11## Support functions
12warning ()
13{
14    logger -t transmission_watchdog -p warning $*
15}
16
17notice ()
18{
19    logger -t transmission_watchdog -p notice $*
20}
21
22debug ()
23{
24    if [ -n "$DEBUG"  ]; then
25        echo $*
26    fi
27}
28
29##########################
30#
31# Checks
32#
33
34# check the configuration items
35if [ ! -d $SOURCE ]; then
36    warning "$SOURCE does not exist."
37    exit 1
38fi
39
40if [ ! -d $TARGET ]; then
41    warning "$TARGET does not exist."
42    exit 1
43fi
44
45if [ ! -d $WORK ]; then
46    warning "$WORK does not exist."
47    exit 1
48fi
49
50if [  -n "$DEBUG"  ]; then
51    echo "SOURCE: $SOURCE"
52    echo "WORK: $WORK"
53    echo "TARGET: $TARGET"
54    echo "USER: $USER"
55fi
56
57if [ -f $WORK/.watchdog ]; then
58    exit 0
59fi
60
61touch $WORK/.watchdog
62
63
64
65_exit ()
66{
67    rm $WORK/.watchdog
68}
69
70trap _exit EXIT
71
72_write_info ()
73{
74    echo "STARTTIME=\"${STARTTIME}\"
75ENDTIME=\"${ENDTIME}\"
76STATUS=\"${STATUS}\"
77SCRAPE=\"${SCRAPE}\"
78UPLOADED=\"${UPLOADED}\"
79URL=\"${URL}\"
80NOTE=\"${NOTE}\"
81TORRENTNAME=\"${TORRENTNAME}\"" > "${TORRENT%/*}/.info"
82}
83
84
85
86move_to_target ()
87{
88    debug "Move to target"
89    DIRNAME="${TORRENT%/*}"
90    DEST="${TARGET}/${DIRNAME##*/}"
91
92    mkdir -p "${DEST}"
93    chmod 777 "${DEST}"
94    chgrp ${GROUP} "${DEST}"
95    chown ${USER} "${DEST}"
96   
97    cd "${TORRENT%/*}"
98   
99    chgrp ${GROUP} *
100    chown ${USER} *
101    chmod ug+rw *
102
103    # Auto purge old logs
104    [ -e current.log ] && rm current.log
105    [ -e current_error.log ] && rm current_error.log
106   
107    notice "Moving files to: ${DEST}"
108    mv * .info .status "${DEST}"
109    chmod 775 "${DEST}"
110
111    STATUS="Ok"
112}
113
114cleanup_work ()
115{
116    debug "Clean up"
117    # cleanup
118    debug "Removing work dir: ${TORRENT%/*}"
119    rm -rf "${TORRENT%/*}"
120}
121
122# Seed recently completed torrent
123auto_seed_completed()
124{
125    if [ "${AUTOSEED}" = "YES" ]; then
126        DIRNAME="${TORRENT%/*}"
127        DEST="${TARGET}/${DIRNAME##*/}"
128        NEWNAME="${DEST}/${TORRENT##*/}"
129        notice "Auto seeding ${NEWNAME}.seeding"
130        mv "${NEWNAME}" "${NEWNAME}.seeding"
131        TORRENT="${NEWNAME}.seeding"
132        _update_active
133    fi
134}
135
136send_report ()
137{
138    debug "Mail from: $MAILFROM"
139    debug "Mail to: $MAILTO"
140
141    if [  -n "${MAILFROM}" -a -n "${MAILTO}" ]; then
142        echo "From: <${MAILFROM}>
143To: <${MAILTO}>
144Subject: ${TORRENTNAME} torrent finished ($STATUS)
145------------------
146Name          : ${TORRENTNAME}
147Torrent       : ${TORRENT}
148Started       : ${STARTTIME}
149Completed     : ${ENDTIME}
150Stored in     : ${TARGET}
151Status        : ${STATUS}
152"  | ${MAILER:-mini_sendmail} ${MAILOPT}${MAILFROM} ${MAILTO}
153        notice "Mail sent to : ${MAILTO}"
154    fi
155}
156
157
158# Update progress
159_update_progress()
160{
161    [ -f ${PIDFILE} ] || return
162    kill -USR1 `cat ${PIDFILE}`
163    sleep 5
164    for TORRENT in ${WORK}/*/*.torrent ${TARGET}/*/*.torrent.seeding ; do
165       INFO="${TORRENT%/*}/.info" 
166       if [ -f "${INFO}" ]; then
167           . "${INFO}"
168           LOG="${TORRENT%/*}/.status"
169           if [ -f "${LOG}" ]; then
170               .  "${LOG}"
171           else
172               STATUS=".status not found for ${TORRENT}"
173           fi
174       else
175           _clean_info
176       fi
177       _write_info
178    done
179}
180
181
182check_tracker_error ()
183{
184    return
185
186    [ -f "${TORRENT%/*}/current.log" ] || return
187
188    tail -10 "${TORRENT%/*}/current.log" | grep -q "Error from tracker"  2>/dev/null
189if [ $? -eq 0 ]; then   
190    debug "Error from tracker"
191    warning "Error from tracker"
192    . "${TORRENT%/*}/.info"
193    sleep 2
194    mv "${TORRENT}" "${TORRENT}.suspended"
195    notice "Torrent ${TORRENT}suspended"
196    STATUS="Error from tracker"
197    _write_info
198   
199    exit 1
200fi
201}
202
203
204# writes active torrents listing into file and notifies daemon to reload it
205_update_active()
206{
207    if [ -n "`ls ${WORK}/*/*.torrent 2>/dev/null | head -1`" ] ; then
208       ls -1  ${WORK}/*/*.torrent > ${ACTIVE}   
209    fi
210    if [ -n "`ls ${TARGET}/*/*.seeding 2>/dev/null | head -1`" ] ; then
211        ls -1  ${TARGET}/*/*.seeding >> ${ACTIVE}
212    fi
213    [ -f ${PIDFILE} ] && kill -HUP `cat ${PIDFILE}`
214}
215
216_stop_torrent()
217{
218    grep -v "${TORRENT}"  ${ACTIVE} > ${ACTIVE}.tmp
219    mv ${ACTIVE}.tmp ${ACTIVE}
220    kill -HUP `cat ${PIDFILE}`
221}
222
223
224# Torrent exists and runs
225check_if_complete()
226{
227
228    debug "check_if_complete  TORRENT:${TORRENT}"
229    [ -f "${TORRENT%.seeding}.seeding" ] &&  return
230
231
232# Kill btget when torrent is complete
233    grep -q "Seeding" "${TORRENT%/*}/.status"
234    if [ $? -eq 0 ]; then
235        _stop_torrent
236        . "${TORRENT%/*}/.info"
237        STATUS="completed"
238        ENDTIME=`date +"${DATE_FORMAT}"`
239        _write_info
240        move_to_target
241        cleanup_work
242        send_report
243        auto_seed_completed
244    fi
245}
246
247##############################################################
248# MAIN PROGRAM
249#
250
251if [ -f "${WORK}/.paused" ]; then
252    notice "Torrents paused"
253    debug "Torrents paused"
254    exit 3
255fi
256
257#check if Transmission daemon is running
258if [ -f ${PIDFILE} ] ; then
259   PID=`cat ${PIDFILE}`
260   grep -q transmissiond /proc/${PID}/cmdline 2> /dev/null
261   if [ $? != 0 ]; then
262      is_running=no
263   else
264      is_running=yes
265   fi
266else
267   is_running=no
268fi
269
270if [ ${is_running} = "no" ]; then
271    [ -f ${PIDFILE} ] && rm ${PIDFILE}
272    export HOME
273    _update_active
274    ulimit -c 60000
275    transmissiond -p ${LISTENING_PORT} \
276        -w ${WATCHDOG} \
277        -u ${UPLOAD_SPEED} \
278        -d ${DOWNLOAD_SPEED} \
279        -i ${PIDFILE}   ${ACTIVE}
280    warning "Transmission daemon restarted!"
281    sleep 5
282    exit 1
283fi
284
285
286ACTIVE_TORRENTS=0
287# We will check all torrents for running state
288_update_progress
289for TORRENT in ${WORK}/*/*.torrent ${TARGET}/*/*.torrent.seeding ; do
290    INFO="${TORRENT%/*}/.info"
291    if [ -f "${INFO}" ]; then
292        . "${INFO}"
293        check_if_complete
294        check_tracker_error
295    else
296        debug "${INFO} not found!"
297    fi
298    ACTIVE_TORRENTS=$((${ACTIVE_TORRENTS}+1))
299done
300
301# Check if any torrent file exists in run env.
302TORRENT=`ls -1 $WORK/*/*.torrent 2>/dev/null | head -1`
303
304# Start up new  torrent if work is empty
305if [ -z "$TORRENT" ]; then
306    max_active_torrents=10
307    if [ ${ACTIVE_TORRENTS} = ${max_active_torrents} ]; then
308        notice "Max active torrents (${max_active_torrents}) reached."
309        exit 4
310    fi
311    # check if there is a torrent to process
312    FILE=`ls -1 $SOURCE/*.torrent 2>/dev/null | head -1`
313    if [ -z "$FILE" ]; then
314        notice "No torrents to process"
315        debug "Nothing to do"
316        exit 2
317    fi
318   
319    TORRENTNAME="${FILE%%.torrent*}"
320    TORRENTNAME="${TORRENTNAME##*/}"
321
322   
323    mkdir -p "$WORK/$TORRENTNAME"
324    chmod 777 "$WORK/$TORRENTNAME"
325   
326    debug "Moving [$FILE] to [$WORK/$TORRENTNAME]"
327    mv "$FILE" "$WORK/$TORRENTNAME"
328   
329    TORRENT="$WORK/$TORRENTNAME/$TORRENTNAME.torrent"
330    debug "Torrent: $TORRENT"
331
332
333    _update_active
334   
335    STARTTIME=`date +"${DATE_FORMAT}"`
336    DONE=0
337   
338    notice "Starting torrent $TORRENT"
339   
340    STATUS="started" 
341    ENDTIME=
342    SCRAPE=
343    UPLOADED=
344    URL=
345    NOTE=
346    _write_info
347
348
349    exit 0
350fi
351
352
353
354
355
356