DSM-G600, DNS-3xx and NSA-220 Hack Forum

Unfortunately no one can be told what fun_plug is - you have to see it for yourself.

You are not logged in.

Announcement

#1 2012-06-15 11:57:19

scaramanga
Member
Registered: 2010-08-04
Posts: 251

Transmission Watchdog

Since I have Transmission running 24/7 and after a long while of running it uses A LOT of memory I wrote a script to restart it whenever it's using too much memory.

What the script does in detail:
0. If transmission-daemon isn't running - start it.
1. If transmission-daemon is running but uses too much memory - restart it. The amount of memory is configurable, and expressed in percent (50, 90, etc.).
2. The restart procedure is a bit more elaborate than calling /ffp/bin/transmission restart - unlike that, the watchdog script waits till transmission-daemon stops before starting it. The wait period is configurable, and if set too short can time-out. If the time-out occurs, transmission-daemon won't be restarted. But since, typically, you'll run this script periodically, as a cron job, the next time it runs it will restart it.
3. If you configured mutt, you can set the script to send an email to an address of your choice detailing what happened (start/restart/timeout). By default this is turned off. You can change that, and the email in the settings section at the beginning of the script.
4. If you run into problems, change the debug setting from false to true and run from the command-line to get more verbose output to help figure out what's wrong.
5. It's easy to make changes for different ffp installation via the setting section.

Important:I tested those features and using this script on my DNS-323 running Firmware 1.10 and ffp 0.5.

/ffp/bin/transmission-watchdog.sh

Code:

#!/bin/sh

############
# Settings #
############

# if Transmission consumes more memory (percent) than this it will be restarted
maxTransmissionMemoryUse=90

# These settings control how long will this script wait for Transmission stop to complete
maxTransmissionStopRetryCount=5
maxTransmissionStopRetryWait=60

# Change this to turn on/off emailing what action the script took.
# make sure you configure mutt/msmtp before turning on this feature.
shouldEmail=false
#shouldEmail=true

# Where the email will be sent to
myEmail="my_email@gmail.com"


# Change this to true for verbose output
debug=false
#debug=true


# Set path to Fun_Plug files.
# Fun_Plug 3.0 or 4.0
# ffpPath=/mnt/HD_a2/fun_plug.d
# Fun_Plug 5.0
ffpPath=/ffp
ffpStartPath=${ffpPath}/start

topCmd="${ffpPath}/bin/top"
grepCmd="${ffpPath}/bin/grep"
echoCmd="${ffpPath}/bin/echo"
awkCmd="${ffpPath}/bin/awk"
sedCmd="${ffpPath}/bin/sed"
sleepCmd="${ffpPath}/bin/sleep"
muttCmd="/usr/bin/mutt"
transmissionStart="${ffpStartPath}/transmission.sh"


#########################
# Transmission-Watchdog #
#########################

grepTransmission=$(${topCmd} -b -n 1 | ${grepCmd} transmission-daemon | ${grepCmd} -v grep)
grepRes=$?

# if transmission is running
if [ ${grepRes} -eq 0 ];  then
    ${debug} && ${echoCmd} -n "Transmission is running. "

    transmissionMemoryUse=$(${echoCmd} $grepTransmission | ${awkCmd} '{print $6;}' | ${sedCmd} 's/%//')
    ${debug} && ${echoCmd} "It's using ${transmissionMemoryUse}% of system memory. Allowed: ${maxTransmissionMemoryUse}%"

    if [ ${transmissionMemoryUse} -gt ${maxTransmissionMemoryUse} ]; then
        ${debug} && ${echoCmd} -n "Stopping Transmission: "
        ${transmissionStart} stop

        retryCount=${maxTransmissionStopRetryCount}
        while [ ${retryCount} -gt 0 ]
        do
            ${topCmd} -b -n 1 | ${grepCmd} transmission-daemon | ${grepCmd} -v grep > /dev/null
            if [ $? -ne 0 ]; then
                ${debug} && ${echoCmd} " Done!"
                break;
            fi

            ${debug} && ${echoCmd} -n "."
            retryCount=$((${retryCount} - 1))
            ${sleepCmd} ${maxTransmissionStopRetryWait}
        done

        if [ ${retryCount} -eq 0 ]; then
            timeOut=$((${maxTransmissionStopRetryWait} * ${maxTransmissionStopRetryCount}))
            ${debug} && ${echoCmd} " Timed-out after ${timeOut} seconds!"
            ${shouldEmail} && ${echoCmd} -e "Transmission Restarted due to excessive memory usage.\nActual ${transmissionMemoryUse}%, allowed: ${maxTransmissionMemoryUse}%. But Transmission stop took too long to complete (more than ${timeOut} seconds).\nEither something's wrong or you need to increase the timeout period." | ${muttCmd} -n -F /ffp/usr/local/share/mutt/Muttrc -s "Transmission restart failed" ${myEmail}
            exit 127
        fi

        ${debug} && ${echoCmd} -n "Starting Transmission: "
        ${transmissionStart} start
        ${shouldEmail} && ${echoCmd} -e "Transmission Restarted due to excessive memory usage.\nActual ${transmissionMemoryUse}%, allowed: ${maxTransmissionMemoryUse}%." | ${muttCmd} -n -F /ffp/usr/local/share/mutt/Muttrc -s "Transmission restarted" ${myEmail}
    fi
else
    ${debug} && ${echoCmd} -n "Transmission isn't running. Start it: "
    ${transmissionStart} start
    ${shouldEmail} && ${echoCmd} -e "Transmission wasn't running so it was started." | ${muttCmd} -n -F /ffp/usr/local/share/mutt/Muttrc -s "Transmission started" ${myEmail}
fi

For the sake of completeness, here's the script I use to set-up a cron job for running the transmission-watchdog.sh script periodically.
It's sort of a generic script, to start/stop cron jobs. You should place it in /ffp/start and make sure it has execute permissions.
After that, either restart the device or run the command "/ffp/start/editcron_transmission.sh start".
Currently, I set it to run the transmission-watchdog.sh script every two hours.

/ffp/start/editcron_transmission.sh

Code:

#!/bin/sh

# PROVIDE: EDITCRON_TRANSMISSION
# REQUIRE: LOGIN

#
# Schedule job
# Important: Always run "/ffp/start/editcron_transmission.sh stop" BEFORE making changes to job.
#            Otherwise, the old job will remain scheduled until you restart.
#
schedule="0 */2 * * *"
job="/ffp/bin/transmission-watchdog.sh"

#
# Commands
#
grepcmd="/bin/grep"
crontabcmd="/bin/crontab"
rmcmd="/bin/rm"
echocmd="/bin/echo"

#
# Tempporary files
#
TMPDIR="/tmp"
TMPCRONTXT="${TMPDIR}/crontab.txt"
TMPOTHERCRONTXT="${TMPDIR}/othercrontab.txt"

#
# FFP Start Functions
#
. /ffp/etc/ffp.subr
start_cmd="editcron_start"
stop_cmd="editcron_stop"
status_cmd="editcron_status"

editcron_start()
{
    # grab existing crontab
    ${crontabcmd} -l > ${TMPCRONTXT}

    # check if already scheduled
    cronjobs=$(${grepcmd} "${job}" ${TMPCRONTXT})
    if test -n "${cronjobs}"; then
        ${echocmd} "${job} already scheduled:"
        ${echocmd} "${cronjobs}"
    else
        # add the cron job and install the new one
        ${echocmd} "Scheduling ${schedule} ${job}"
        ${echocmd} "${schedule} ${job}" >> ${TMPCRONTXT}
        ${crontabcmd} ${TMPCRONTXT}
    fi

    # clean up
    ${rmcmd} ${TMPCRONTXT}
}

editcron_stop()
{
    # grab existing crontab
    ${crontabcmd} -l > ${TMPCRONTXT}

    # check if already scheduled
    cronjobs=$(${grepcmd} "${job}" ${TMPCRONTXT})
    if test -z "${cronjobs}"; then
        ${echocmd} "${job} not scheduled"
    else
        # remove the cron job(s) and install the new one
        ${grepcmd} -v "${job}" ${TMPCRONTXT} > ${TMPOTHERCRONTXT}
        ${crontabcmd} ${TMPOTHERCRONTXT}
        ${echocmd} "${job} not longer scheduled"

        # clean up
        ${rmcmd} ${TMPOTHERCRONTXT}
    fi

    # clean up
    ${rmcmd} ${TMPCRONTXT}
}

editcron_status()
{
    # grab existing crontab
    ${crontabcmd} -l > ${TMPCRONTXT}
    cronjobs=$(${grepcmd} "${job}" ${TMPCRONTXT})

    # check if already scheduled
    if test -n "${cronjobs}"; then
        ${echocmd} "${job} is scheduled:"
        ${echocmd} "${cronjobs}"
    else
        ${echocmd} "${job} not scheduled"
    fi

    # clean up
    ${rmcmd} ${TMPCRONTXT}
}

run_rc_command "$1"

Last edited by scaramanga (2012-06-15 13:16:59)


DNS-323 HW Rev. C1 FW 1.10 fun-plug 0.5
2 x WD10EARS-00Y5B1 in Standard mode (LCC set to 5 min; Aligned to 4K)
Transmission with Transmission Remote GUI

Offline

 

#2 2012-06-17 03:38:16

bound4h
Member
Registered: 2010-04-12
Posts: 209

Re: Transmission Watchdog

Thanks man

Offline

 

#3 2012-06-17 15:18:27

scaramanga
Member
Registered: 2010-08-04
Posts: 251

Re: Transmission Watchdog

I posted a how-to configure msmtp/mutt if you're interested in emailing from scripts, such as this one here.


DNS-323 HW Rev. C1 FW 1.10 fun-plug 0.5
2 x WD10EARS-00Y5B1 in Standard mode (LCC set to 5 min; Aligned to 4K)
Transmission with Transmission Remote GUI

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2010 PunBB