Bash脚本 – Munin插件不工作

我写了一个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 

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

 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 

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

我已经通过telnettesting了来自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. 

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

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

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

 [script_file_mask_*] user USER_FOR_YOR_SCRIPT env.VARIABLE some_variable 

在你的情况看来,脚本只是没有读紧日志文件。 所以添加

 [lighttpd_*] user root 

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

我不知道你是否曾经设法解决这个问题,但我曾经和我想过分享我的解决scheme。

Rushbuild议以root身份运行它,但是真正的错误似乎与你select的域名(1xx,2xx,3xx,…)有关。 按照这个wiki页面 :

插件中的每个数据源必须由字段名称标识。 以下介绍该字段的名称:
*字符必须是[a-zA-Z0-9_], 而第一个字符必须是[a-zA-Z_]

这就是为什么你只看到5xx在你的图表,没有结果。 在创buildrdd文件时,munin用下划线(如_xx)replace了数字,这意味着数据被5个字段中的每一个覆盖。 简单的解决方法是添加一个字母到你的字段名称,如下所示:

 graph_category lighttpd T1xx.label 1xx T2xx.label 2xx T3xx.label 3xx T4xx.label 4xx T5xx.label 5xx EOM 

  echo "T1xx.value "$CODE_1xx echo "T2xx.value "$CODE_2xx echo "T3xx.value "$CODE_3xx echo "T4xx.value "$CODE_4xx echo "T5xx.value "$CODE_5xx else echo "T1xx.value U" echo "T2xx.value U" echo "T3xx.value U" echo "T4xx.value U" echo "T5xx.value U" fi 

像这样我得到了你的脚本完美的工作。

检查/etc/munin/munin-node.conf中 munin运行的用户名,以及该用户是否可以读取lighttpd日志文件。