qBitTorrent Monitoring Script help #bash

Other platforms, generic questions.
Post Reply
annomatik

qBitTorrent Monitoring Script help #bash

Post by annomatik »

Hi! My qBitTorrent installation makes all kinds of troubles, as a first step, I'd like to introduce it to my monitoring (Pandora FMS).

My qBitTorrent is running on Win 10, but my script is using the Web-API to access it via Debian, so I'm posting this in the "Generic" section.

I have written a small script to count, how many torrents are currently in error, in stalled and in checking. Unfortunatelly, for about 1700 Torrents, this script takes 11 minutes to run, so for regular monitoring, this is way to slow (I'd like to have it running below 5 seconds if possible).

This is the script:

Code: Select all

#!/bin/bash

#
# Pandora Module that counts, how many torrents are in what state
#
# 2018-09-04 count how many torrents are in which state
#
#

# ----------------------------------------------
#
# config
#
# $1=URL to qBitTorrent
# $2=Username
# $3=Password

qbt="$1"
user="$2"
pass="$3"


# ----------------------------------------------


function print_kv {

        # $1 = key
        # $2 = type (e.g. generic_proc)
        # $3 = value
        # $4 = description
        # $5 = module_group (e.g. Application)

        echo "<module>"
        echo -e "\t<name><![CDATA[$1]]></name>"
        echo -e "\t<type><![CDATA[$2]]></type>"
        echo -e "\t<data><![CDATA[$3]]></data>"
        echo -e "\t<description><![CDATA[$4]]></description>"
        echo -e "\t<module_group><![CDATA[$5]]></module_group>"
        echo "</module>"

        return

}


function calc_size_hr {

        size_hr=$1

        if [ $size_hr -ge 1099511627776 ]; then
                        size_hr=$(awk 'BEGIN {printf "%.2fT", '$size_hr'/1099511627776}')
        elif [ $size_hr -ge 1073741824 ]; then
                        size_hr=$(awk 'BEGIN {printf "%.2fG", '$size_hr'/1073741824}')
        elif [ $size_hr -ge 1048576 ]; then
                        size_hr=$(awk 'BEGIN {printf "%.2fM", '$size_hr'/1048576}')
        elif [ $size_hr -ge 1024 ]; then
                        size_hr=$(awk 'BEGIN {printf "%.2fK", '$size_hr'/1024}')
        else
                        size_hr="${size_hr}B"
        fi

        echo -n $size_hr

}



cookie_raw=$(curl -i --header "Referer: $qbt" --data "username=$user&password=$pass" $qbt/login 2>/dev/null | grep set-cookie)
cookie=$(echo $cookie_raw | tr -d \; | cut -d\  -f2)

if [[ "x$cookie" == "x" ]]; then

        print_kv "qBitTorrent API Status" "generic_proc" "0" "Cannot get Cookie. qBitTorrent API offline?" "Application"

else

        print_kv "qBitTorrent API Status" "generic_proc" "1" "qBitTorrent API is online." "Application"

        in_error=0
        in_stalled=0
        in_checking=0
        downloading=0

        curl "$qbt/query/torrents?filter=inactive" --cookie "$cookie" 2>/dev/null | tr ',' "\n" | while read a; do

                key=$(echo $a | cut -d\: -f1|tr -d '"')
                value=$(echo $a | cut -d\: -f2|tr -d '"')

                if [[ "$key" == "state" ]]; then
                        state=$value

                        case "$state" in
                                "error")
                                        ((in_error++))
                                        echo "in_error set to $in_error"
                                        ;;
                                "stalledDL")
                                        ((in_stalled++))
                                        echo "in_stalled set to $in_stalled"
                                        ;;
                                "stalledUP")
                                        ((in_stalled++))
                                        echo "in_stalled set to $in_stalled"
                                        ;;
                                "checkingDL")
                                        ((in_checking++))
                                        echo "in_checking set to $in_checking"
                                        ;;
                                "checkingUP")
                                        ((in_checking++))
                                        echo "in_checking set to $in_checking"
                                        ;;
                        esac;
                fi

        done

        echo "in_error=$in_error"
        echo "in_stalled=$in_stalled"
        echo "in_checking=$in_checking"

        curl "$qbt/query/torrents?filter=downloading" --cookie "$cookie" 2>/dev/null | tr ',' "\n" | while read a; do
                if [[ "$key" == "state" ]]; then
                        if [[ "$value" == "downloading" ]]; then

                                ((downloading++))
                                echo "downloading set to $downloading"

                        fi
                fi
        done

        echo "downloading=$downloading"

fi
This is the output (not getting the error / checking / downloading values yet in the right scope, but it's a start):

Code: Select all

$time ./qt-count <url> <user> <pass>
<module>
        <name><![CDATA[qBitTorrent API Status]]></name>
        <type><![CDATA[generic_proc]]></type>
        <data><![CDATA[1]]></data>
        <description><![CDATA[qBitTorrent API is online.]]></description>
        <module_group><![CDATA[Application]]></module_group>
</module>
in_error set to 1
in_stalled set to 1
in_error set to 2
in_error set to 3
in_error set to 4
in_error set to 5
in_error set to 6
in_error set to 7
in_error set to 8
in_error set to 9
in_error set to 10
in_error set to 11
in_error set to 12
in_error set to 13
in_error set to 14
in_error set to 15
in_error set to 16
in_error set to 17
in_stalled set to 2
in_error set to 18
in_error set to 19
in_stalled set to 3
in_stalled set to 4
in_error set to 20
in_error set to 21
in_error set to 22
in_error set to 23
in_stalled set to 5
in_error set to 24
in_error set to 25
in_error set to 26
in_error set to 27
in_error set to 28
in_error set to 29
in_error set to 30
in_error set to 31
in_error set to 32
in_error set to 33
in_error=0
in_stalled=0
in_checking=0
downloading=0

real    8m6.943s
user    10m56.825s
sys     3m34.814s
So my quesion... How do I make this script run (much) faster (on the same hardware :-))? Any hints, suggestions and tips welcome.

Thanks!
annomatik

Re: qBitTorrent Monitoring Script help #bash

Post by annomatik »

anyone?
Post Reply