munin cgi 动态生成 图片

张映 发表于 2014-12-31

分类目录: 服务器相关

标签:, ,

今天又弄了一下munin,发现有些图片出不来.

http://xxxxxx.com/static/dynazoom.html?cgiurl_graph=/munin-cgi/munin-cgi-graph&plugin_name=xxxxxx

加载图片的地方报404错误。

1,安装munin

请参考:linux munin 服务器监控 安装配置

2,安装munin-nginx

# yum install munin-nginx spawn-fcgi

安装完了以后,在4个地方,会产生重要文件

[root@node1 www]# ll /etc/nginx/conf.d/ |grep munin
-rw-r--r-- 1 root root 1427 12月 30 14:11 munin.conf          //nginx的配置文件

[root@node1 www]# ll /var/www/cgi-bin/
总用量 24
-rwxr-xr-x 1 root munin 14530 11月 27 03:37 munin-cgi-graph    //perl cgi程序
-rwxr-xr-x 1 root munin 4245 11月 27 03:37 munin-cgi-html

[root@node1 www]# ll /etc/init.d/ |grep munin-fcgi
-rwxr-xr-x 1 root root 1123 12月 30 19:22 munin-fcgi-graph     //调用munin-cgi-graph的系统脚本
-rwxr-xr-x 1 root root 1037 12月 29 15:59 munin-fcgi-html

[root@node1 www]# whereis spawn-fcgi    //perl程序的承载器
spawn-fcgi: /usr/bin/spawn-fcgi /usr/share/man/man1/spawn-fcgi.1.gz

在这里注意,/etc/init.d/munin-fcgi-graph,这个文件启动munin-cgi-graph,老是有问题,所以从官方网站找到了启动脚本。

3,修改munin.conf的主配置文件

[root@node1 munin]# cat munin.conf |awk '{if($0 !~ /^$/ && $0 !~ /#/) {print $0}}'
dbdir /var/lib/munin
htmldir /var/www/html/munin
logdir /var/log/munin
rundir /var/run/munin
tmpldir /etc/munin/templates
includedir /etc/munin/conf.d
graph_strategy cgi       //在此处将cron改为cgi
cgiurl_graph /munin-cgi/munin-cgi-graph
html_strategy cron
[munin]
 address 127.0.0.1
 use_node_name yes

4,修改nginx的munin虚拟机

[root@node1 www]# cat /etc/nginx/conf.d/munin.conf |awk '{if($0 !~ /^$/ && $0 !~ /#/) {print $0}}'
server {
 listen 80;
 server_name munin.51yip.com; //自定义
 root /var/www/html/munin; //munin html的根目录
 index index.html index.htm;

 location ^~ /munin-cgi/munin-cgi-graph/ {
 fastcgi_split_path_info ^(/munin-cgi/munin-cgi-graph)(.*);
 fastcgi_param PATH_INFO $fastcgi_path_info;
 fastcgi_pass unix:/var/run/munin/spawn-fcgi-munin-graph.sock;
 include fastcgi_params;
 }
}

5,启动脚本

[root@node1 www]# cat /etc/init.d/spawn-fcgi-munin-graph |awk '{if($0 !~ /^$/ && $0 !~ /#/) {print $0}}'
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=spawn-fcgi-munin-graph
PID_FILE=/var/run/munin/$NAME.pid
SOCK_FILE=/var/run/munin/$NAME.sock
SOCK_USER=tank    //nginx的启动用户
FCGI_USER=munin
FCGI_GROUP=munin
FCGI_WORKERS=2
DAEMON=/usr/bin/spawn-fcgi
DAEMON_OPTS="-s $SOCK_FILE -F $FCGI_WORKERS -U $SOCK_USER -u $FCGI_USER -g $FCGI_GROUP -P $PID_FILE -- /var/www/cgi-bin/munin-cgi-graph"
if [ ! -x $DAEMON ]; then
 echo "File not found or is not executable: $DAEMON!"
 exit 0
fi
status() {
 if [ ! -r $PID_FILE ]; then
 return 1
 fi

 for FCGI_PID in `cat $PID_FILE`; do
 if [ -z "${FCGI_PID}" ]; then
 return 1
 fi

 FCGI_RUNNING=`ps -p ${FCGI_PID} | grep ${FCGI_PID}`
 if [ -z "${FCGI_RUNNING}" ]; then
 return 1
 fi
 done;

 return 0
}

start() {
 if status; then
 echo "FCGI is already running!"
 exit 1
 else
 $DAEMON $DAEMON_OPTS
 fi
}
stop () {
 if ! status; then
 echo "No PID-file at $PID_FILE found or PID not valid. Maybe not running"
 exit 1
 fi

 for PID_RUNNING in `cat $PID_FILE`; do
 kill -9 $PID_RUNNING
 done

 rm -f $PID_FILE

 rm -f $SOCK_FILE
}
case "$1" in
 start)
 echo "Starting $NAME: "
 start
 echo "... DONE"
 ;;
 stop)
 echo "Stopping $NAME: "
 stop
 echo "... DONE"
 ;;
 force-reload|restart)
 echo "Stopping $NAME: "
 stop
 echo "Starting $NAME: "
 start
 echo "... DONE"
 ;;

 status)
 if status; then
 echo "FCGI is RUNNING"
 else
 echo "FCGI is NOT RUNNING"
 fi
 ;;

 *)
 echo "Usage: $0 {start|stop|force-reload|restart|status}"
 exit 1
 ;;
esac
exit 0

注意,其实上述脚本就是围绕以下命令写的,也可以说上述脚本与以下命令等同。

# spawn-fcgi -s /var/run/munin/spawn-fcgi-munin-graph.sock -F 2 -U tank -u munin -g munin /var/www/cgi-bin/munin-cgi-graph

6,权限设置

# chown tank:munin -R /var/www/cgi-bin/       //tank为nginx启动用户

这一步,非常非常重要,这一步不做的话,图片出不来,nginx会报以下错误,因为这个小问题,费了我好几个小时。

2014/12/29 18:20:56 [error] 12263#0: *1 connect() to unix:/var/run/munin/spawn-fcgi-munin-graph.sock failed (111: Connection refused) while connecting to upstream,

7,重启相关服务

# /etc/init.d/nginx restart

# /etc/init.d/munin-node restart

# chmod +x /etc/init.d/spawn-fcgi-munin-graph 

# /etc/init.d/spawn-fcgi-munin-graph start

等一会后,久违的图片出来。

munin cgi 动态生成图片

munin cgi 动态生成图片



转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/server/1685.html