Bash-Scripting - Munin 插件不起作用

Arn*_*exa 5 munin bash lighttpd shell-scripting

我写了一个 munin-plugin 来计算 lighttpd 的 http-statuscodes。剧本:

#!/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
Run Code Online (Sandbox Code Playgroud)

如果我在本地机器上运行脚本,它运行完美:

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 
Run Code Online (Sandbox Code Playgroud)

但穆宁没有显示图表:http : //s1.directupload.net/images/111219/3psgq3vb.jpg

我已经通过 telnet 从 munin-server 测试了插件:

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.
Run Code Online (Sandbox Code Playgroud)

您可以在脚本中看到 value = U 仅打印,当脚本无法检查 lighttpd 的 access.log 时。但是为什么脚本不能做到这一点,通过 munin 运行时,以及在本地机器上运行时一切正常?

我的 bash 脚本中有错误吗?我不知道。感谢您的帮助!

rus*_*ush 3

不要在仅运行直接脚本的情况下检查 munin 脚本。这是错误的方式。有一个特殊的 perl 脚本munin-run,它执行脚本的方式与 munin 更新期间运行的方式完全相同,您将能够找到所有错误。可能您需要为脚本定义特殊设置。您可以/etc/munin/plugin-conf.d/munin-node通过以下方式在文件中执行此操作:

[script_file_mask_*]
user USER_FOR_YOR_SCRIPT
env.VARIABLE some_variable
Run Code Online (Sandbox Code Playgroud)

在你的情况下,脚本似乎没有严格读取日志文件。所以添加

[lighttpd_*]
user root
Run Code Online (Sandbox Code Playgroud)

/etc/munin/plugin-conf.d/munin-node重新启动 munin-node。应该有帮助。