[Ganglia监控扩展]监控nginx的连接数

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