Ganglia监控支持多种脚本的功能扩展,本文介绍使用python扩展对nginx连接数进行监控,可以到网上下载监控脚本:https://github.com/csakatoku/ganglia-pymodule_nginx_status。这个监控脚本主要是借助nginx自身的监控模块来实现,nginx需要开启--with-http_stub_status_module模块,如果没有开启,需要重新编译nginx(重新编译后需要kill -QUIT才能生效)。
第一步,配置nginx,开启nginx_status监控:
在nginx.conf中添加如下配置:
server {
listen 12345;#监听的端口
server_name 127.0.0.1;#当前机器的ip或域名
location /nginx_status {
stub_status on;
access_log off;
#allow xx.xx.xx.xx;#允许访问的IP地址
#deny all;
allow all;
}
}
第二步,配置ganglia客户端,收集nginx_status数据:
在/etc/ganglia目录下新建conf.d和python_modules目录,把下载的扩展文件分别复制到这两个目录下,修改配置文件nginx_status.pyconf里的nginx监控访问路径和nginx启动文件的路径;接下来需要修改gmond.conf文件,添加如下两段:
在modules下添加python支持:
module {
name = "python_module"
path = "modpython.so"#这里需要确定ganglia安装目录下的lib64里是否有这个文件
params = "/etc/ganglia/python_modules"
}
在文件最后导入python配置:
include ("/etc/ganglia/conf.d/*.pyconf")
重启gmond服务:service gmond restart,这样就开始收集这台机器上的nginx监控数据。
第三步,配置监控前端页面:
如果只需要查看单台机器的监控图表,直接在这台机器的节点里就可以看到,如下:
如果需要看整个集群的监控画面,则需要做一些配置,如下:
在gmetad运行的机器上把下载到的graph.d下的文件复制到ganglia web页面目录下的graph.d目录下面。
然后修改conf.php,找到$optional_graphs,去掉注释,修改为 $optional_graphs = array('nginx_accepts_ratio','nginx_scoreboard'),保存,刷新监控页面即可看到监控图(这个就不帖图了)。
第四步,定制自己的监控画面:
虽然插件提供了比较详细的监控画面,但我比较关注的是active、reading、writing和waiting这几个数据,并且在一张图形中显示。所以需要自己定制一个画图程序,在graph.d文件夹下新建一个叫nginx_report.php的文件,输入如下的内容(function的名称必须是graph_加上文件名):
1 <?php
2
3 function graph_nginx_report ( &$rrdtool_graph ) {
4
5 global $context,
6 $cpu_system_color,
7 $mem_swapped_color,
8 $mem_shared_color,
9 $mem_cached_color,
10 $mem_buffered_color,
11 $mem_used_color,
12 $hostname,
13 $range,
14 $rrd_dir,
15 $size,
16 $strip_domainname,
17 $graphreport_stats;
18
19 if ($strip_domainname) {
20 $hostname = strip_domainname($hostname);
21 }
22
23 $rrdtool_graph['height'] += ($size == 'medium') ? 28 : 0;
24 if ( $graphreport_stats ) {
25 $rrdtool_graph['height'] += ($size == 'medium') ? 52 : 0;
26 $rmspace = '\\g';
27 } else {
28 $rmspace = '';
29 }
30
31 $title = 'Nginx';
32 if ($context != 'host') {
33 $rrdtool_graph['title'] = $title;
34 } else {
35 $rrdtool_graph['title'] = "$hostname $title last $range";
36 }
37
38 $rrdtool_graph['lower-limit'] = '0';
39 $rrdtool_graph['vertical-label'] = 'num/sec';
40 $rrdtool_graph['extras'] = '--base 1024';
41 $rrdtool_graph['extras'] .= ($graphreport_stats == true) ? ' --font LEGEND:7' : '';
42
43 if ($size == 'small') {
44 $eol1 = '\\l';
45 $space1 = ' ';
46 $space2 = ' ';
47 } else if ($size == 'medium' || $size == 'default') {
48 $eol1 = '';
49 $space1 = ' ';
50 $space2 = '';
51 } else if ($size == 'large') {
52 $eol1 = '';
53 $space1 = ' ';
54 $space2 = ' ';
55 }
56
57 $series = "DEF:'nginx_reading'='${rrd_dir}/nginx_reading.rrd':'sum':AVERAGE "
58 . "DEF:'nginx_writing'='${rrd_dir}/nginx_writing.rrd':'sum':AVERAGE "
59 . "DEF:'nginx_waiting'='${rrd_dir}/nginx_waiting.rrd':'sum':AVERAGE "
60 . "DEF:'nginx_active'='${rrd_dir}/nginx_active_connections.rrd':'sum':AVERAGE "
61 . "LINE2:'nginx_reading'#$mem_cached_color:'Reading${rmspace}' ";
62
63 if ( $graphreport_stats ) {
64 $series .= "CDEF:nginxreading_pos=nginx_reading,0,INF,LIMIT "
65 . "VDEF:nginxreading_last=nginxreading_pos,LAST "
66 . "VDEF:nginxreading_min=nginxreading_pos,MINIMUM "
67 . "VDEF:nginxreading_avg=nginxreading_pos,AVERAGE "
68 . "VDEF:nginxreading_max=nginxreading_pos,MAXIMUM "
69 . "GPRINT:'nginxreading_last':' ${space1}Now\:%6.1lf%s' "
70 . "GPRINT:'nginxreading_min':'${space1}Min\:%6.1lf%s${eol1}' "
71 . "GPRINT:'nginxreading_avg':'${space2}Avg\:%6.1lf%s' "
72 . "GPRINT:'nginxreading_max':'${space1}Max\:%6.1lf%s\\l' ";
73 }
74
75 $series .= "LINE2:'nginx_writing'#$mem_used_color:'Writing${rmspace}' ";
76
77 if ( $graphreport_stats ) {
78 $series .= "CDEF:nginxwriting_pos=nginx_writing,0,INF,LIMIT "
79 . "VDEF:nginxwriting_last=nginxwriting_pos,LAST "
80 . "VDEF:nginxwriting_min=nginxwriting_pos,MINIMUM "
81 . "VDEF:nginxwriting_avg=nginxwriting_pos,AVERAGE "
82 . "VDEF:nginxwriting_max=nginxwriting_pos,MAXIMUM "
83 . "GPRINT:'nginxwriting_last':'${space1}Now\:%6.1lf%s' "
84 . "GPRINT:'nginxwriting_min':'${space1}Min\:%6.1lf%s${eol1}' "
85 . "GPRINT:'nginxwriting_avg':'${space2}Avg\:%6.1lf%s' "
86 . "GPRINT:'nginxwriting_max':'${space1}Max\:%6.1lf%s\\l' ";
87 }
88
89 $series .= "LINE2:'nginx_waiting'#$mem_swapped_color:'Waiting${rmspace}' ";
90
91 if ( $graphreport_stats ) {
92 $series .= "CDEF:nginxwaiting_pos=nginx_waiting,0,INF,LIMIT "
93 . "VDEF:nginxwaiting_last=nginxwaiting_pos,LAST "
94 . "VDEF:nginxwaiting_min=nginxwaiting_pos,MINIMUM "
95 . "VDEF:nginxwaiting_avg=nginxwaiting_pos,AVERAGE "
96 . "VDEF:nginxwaiting_max=nginxwaiting_pos,MAXIMUM "
97 . "GPRINT:'nginxwaiting_last':'${space1}Now\:%6.1lf%s' "
98 . "GPRINT:'nginxwaiting_min':'${space1}Min\:%6.1lf%s${eol1}' "
99 . "GPRINT:'nginxwaiting_avg':'${space2}Avg\:%6.1lf%s' "
100 . "GPRINT:'nginxwaiting_max':'${space1}Max\:%6.1lf%s\\l' ";
101 }
102
103
104
105 $series .= "LINE2:'nginx_active'#$cpu_system_color:'Active${rmspace}' ";
106
107 if ( $graphreport_stats ) {
108 $series .= "CDEF:nginxactive_pos=nginx_active,0,INF,LIMIT "
109 . "VDEF:nginxactive_last=nginxactive_pos,LAST "
110 . "VDEF:nginxactive_min=nginxactive_pos,MINIMUM "
111 . "VDEF:nginxactive_avg=nginxactive_pos,AVERAGE "
112 . "VDEF:nginxactive_max=nginxactive_pos,MAXIMUM "
113 . "GPRINT:'nginxactive_last':'${space1}Now\:%6.1lf%s' "
114 . "GPRINT:'nginxactive_min':'${space1}Min\:%6.1lf%s${eol1}' "
115 . "GPRINT:'nginxactive_avg':'${space2}Avg\:%6.1lf%s' "
116 . "GPRINT:'nginxactive_max':'${space1}Max\:%6.1lf%s\\l' ";
117 }
118
119
120 $rrdtool_graph['series'] = $series;
121
122 return $rrdtool_graph;
123 }
124 ?>
完成后保存文件,在conf.php中$optional_graphs数组里添加一个元素'nginx'(文件名的_report.php之前的部分),刷新监控页面即可看到整个集群的nginx连接数信息。
如上,便完成了在ganglia中集成nginx监控的所有工作。
-->枪声依旧<--