Bash-Scripting - Munin Plugin don't work

Posted by FTV Admin on Server Fault See other posts from Server Fault or by FTV Admin
Published on 2011-12-19T16:07:51Z Indexed on 2012/07/07 21:17 UTC
Read the original article Hit count: 360

Filed under:
|
|
|

i have written a munin-plugin to count the http-statuscodes of lighttpd. The script:

#!/bin/bash

######################################
# Munin-Script: Lighttpd-Statuscodes #
######################################

##Config
# path to  lighttpd access.log
LIGHTTPD_ACCESS_LOG_PATH="/var/log/lighttpd/access.log"
# rows to parse in logfile (higher value incrase time to run plugin. if value to low you may get bad counting)
LOG_ROWS="200000"
#
#munin
case $1 in
   autoconf) # check config
        AVAILABLE=`ls $LIGHTTPD_ACCESS_LOG_PATH`
        if [ "$AVAILABLE" = "$LIGHTTPD_ACCESS_LOG_PATH" ]; then
           echo "yes"
        else
           echo "No: "$AVAILABLE
           echo "Please check your config!"
        fi
        exit 0;;
   config) # graph config
        cat <<'EOM'
graph_title Lighhtpd Statuscodes
graph_vlabel http-statuscodes / min
graph_category lighttpd
1xx.label 1xx
2xx.label 2xx
3xx.label 3xx
4xx.label 4xx
5xx.label 5xx
EOM
        exit 0;;
esac

## calculate
AVAILABLE=`ls $LIGHTTPD_ACCESS_LOG_PATH`
if [ "$AVAILABLE" = "$LIGHTTPD_ACCESS_LOG_PATH" ]; then
   TIME_NOW=`date`
   CODE_1xx="0"
   CODE_2xx="0"
   CODE_3xx="0"
   CODE_4xx="0"
   CODE_5xx="0"
   for i in 1 2 3 4 5; do
        TIME5=`date +%d/%b/%Y:%k:%M --date "$TIME_NOW -"$i"min"`
        CODE_1xx=$(( $CODE_1xx + `tail -n $LOG_ROWS $LIGHTTPD_ACCESS_LOG_PATH | grep "$TIME5" | grep 'HTTP/1.1" 1' | grep -c " "` ))
        CODE_2xx=$(( $CODE_2xx + `tail -n $LOG_ROWS $LIGHTTPD_ACCESS_LOG_PATH | grep "$TIME5" | grep 'HTTP/1.1" 2' | grep -c " "` ))
        CODE_3xx=$(( $CODE_3xx + `tail -n $LOG_ROWS $LIGHTTPD_ACCESS_LOG_PATH | grep "$TIME5" | grep 'HTTP/1.1" 3' | grep -c " "` ))
        CODE_4xx=$(( $CODE_4xx + `tail -n $LOG_ROWS $LIGHTTPD_ACCESS_LOG_PATH | grep "$TIME5" | grep 'HTTP/1.1" 4' | grep -c " "` ))
        CODE_5xx=$(( $CODE_5xx + `tail -n $LOG_ROWS $LIGHTTPD_ACCESS_LOG_PATH | grep "$TIME5" | grep 'HTTP/1.1" 5' | grep -c " "` ))
   done
        CODE_1xx=$(( $CODE_1xx / 5 ))
        CODE_2xx=$(( $CODE_2xx / 5 ))
        CODE_3xx=$(( $CODE_3xx / 5 ))
        CODE_4xx=$(( $CODE_4xx / 5 ))
        CODE_5xx=$(( $CODE_5xx / 5 ))

        echo "1xx.value "$CODE_1xx
        echo "2xx.value "$CODE_2xx
        echo "3xx.value "$CODE_3xx
        echo "4xx.value "$CODE_4xx
        echo "5xx.value "$CODE_5xx
else
        echo "1xx.value U"
        echo "2xx.value U"
        echo "3xx.value U"
        echo "4xx.value U"
        echo "5xx.value U"
fi

If i run the script on local machine it runs perfectly:

root@server1 /etc/munin/plugins # ll
lrwxrwxrwx 1 root root   45 2011-12-19 15:23 lighttpd_statuscodes -> /usr/share/munin/plugins/lighttpd_statuscodes*
root@server1 /etc/munin/plugins # ./lighttpd_statuscodes autoconf
yes
root@server1 /etc/munin/plugins # ./lighttpd_statuscodes config
graph_title Lighhtpd Statuscodes
graph_vlabel http-statuscodes / min
graph_category lighttpd
1xx.label 1xx
2xx.label 2xx
3xx.label 3xx
4xx.label 4xx
5xx.label 5xx 
root@server1 /etc/munin/plugins #./lighttpd_statuscodes
1xx.value 0
2xx.value 5834
3xx.value 1892
4xx.value 0
5xx.value 0 

But Munin shows no graph: http://s1.directupload.net/images/111219/3psgq3vb.jpg

I have tested the Plugin from munin-server via telnet:

root@munin-server /etc/munin/plugins/ # telnet 123.123.123.123 4949
Trying 123.123.123.123...
Connected to 123.123.123.123.
Escape character is '^]'.
# munin node at server1.cluster1
fetch lighttpd_statuscodes
1xx.value U
2xx.value U
3xx.value U
4xx.value U
5xx.value U
.
Connection closed by foreign host.

You can see in the script that value = U only printed, when the script can't check the lighttpd's access.log. But why can't script do it, when running via munin, and when running on local machine all is ok?

Is there a bug in my bash-script? I have no Idea. Thanks for helping!

© Server Fault or respective owner

Related posts about bash

Related posts about lighttpd