#!/bin/bash # a script to grab translation statistics from po files and send them to Zabbix # po files are expected to be found in # $polocation//LC_MESSAGES/.po # project must be passed as the first positional parameter and is appended to # polocation # domain can be passed as the second positional parameter. default domain is # "frontend" # item key syntax: gettext.strings[language,type,domain] # item key example: gettext.strings[uk,translated,frontend] # expects "msgfmt --statistics" output to be in the format: # 439 translated messages, 75 fuzzy translations, 376 untranslated messages # if any type is missing, 0 is sent for that type instead # lots of possibilities to extend, including dynamic project polocation=/srv/www/htdocs/pootle/po project=$1 domain=${2:-frontend} zsender=/usr/local/bin/zabbix_sender zserver=127.0.0.1 zhost="Zabbix.org" zkey=gettext.strings [[ -x $zsender ]] || { echo "zabbix_sender not found at $zsender" exit 1 } # unfortunately, msgfmt outputs statistics to stderr, so we can't capture errors # separately svn up "$polocation/$project" for langdir in $(find "$polocation/$project" -maxdepth 1 -mindepth 1 -type d\ ! -wholename '*/.svn*'); do statline=$(msgfmt --statistics -o /dev/null\ $langdir/LC_MESSAGES/$domain.po 2>&1) for type in translated fuzzy untranslated; do # a bit of an ugly hack with prepending a space to get different # types correctly amount=$(echo " $statline" | sed -n "s/.* \([0-9]\+\)\ ${type}.*/\1/p") key="$zkey[$(basename $langdir),$type,$domain]" # normal version sender_output=$($zsender -z $zserver -s "$zhost" -k $key -o ${amount:-0} -vv 2>&1) # historic version - we expect to have timestamp passed as the third parameter # ! disable svn up above #sender_output=$(echo "\"$zhost\" $key $3 ${amount:-0}" | $zsender -z $zserver -i - -T -vv 2>&1) # echo "$sender_output" [[ "$sender_output" =~ "Sending failed"|"Failed "[1-9] ]] && { # if sending fails for any reason (including missing # items), we try to transmit the error to the Zabbix # server $zsender -z $zserver -s "$zhost" -k gettext.errors\ -o "$(echo -e "Language directory: $langdir\nmsgfmt output:\ $statline\nType: $type\nExtracted amount: ${amount:-0}\nKey: $key\nZabbix sender output:\n")$sender_output" -vv >> /tmp/zabbix.gettext.errors 2>&1 # newline before sender output doesn't seem to be properly transmitted } done # exit done