<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Ops运维 | Mr`kang Blog｜小豆子]]></title> 
<link>https://www.cnmysql.com/index.php</link> 
<description><![CDATA[关注服务器运维技术、DB优化、Docker虚拟化、自动化运维、工作积累]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Ops运维 | Mr`kang Blog｜小豆子]]></copyright>
<item>
<link>https://www.cnmysql.com/post//</link>
<title><![CDATA[Nginx监控利器Ngxtop]]></title> 
<author>kangyang &lt;kangyang315@qq.com&gt;</author>
<category><![CDATA[WEB技术]]></category>
<pubDate>Thu, 14 Aug 2014 03:12:09 +0000</pubDate> 
<guid>https://www.cnmysql.com/post//</guid> 
<description>
<![CDATA[ 
	<span style="font-size: 12px;"><span style="font-family: 宋体;">nginx监控利器ngxtop<br/>Nginx网站服务器在生产环境中运行的时候需要进行实时监控。实际上，诸如Nagios, Zabbix, Munin 的网络监控软件是支持 Nginx 监控的。<br/>如果你不需要以上软件提供的综合性报告或者长期数据统计功能，只是需要一种快速简便的办法去监控 Nginx 服务器的请求的话，我建议你采用一个叫 ngxtop 的命令行工具。<br/>你马上就会发现 ngxtop 从界面和名称都借鉴了著名的top命令。ngxtop 是通过分析 Nginx 或者其他的日志文件，使用类似 top 命令的界面实时展示出来的。你可以说你知道的其他高端监控工具，但是在简洁这方面 ngxtop 无疑是最好的。简单就意味着不可替代。<br/><br/>安装部署ngxtop<br/>1、安装pip<br/>网址：<a href="https://pypi.python.org/packages/source/p/pip/" target="_blank">https://pypi.python.org/packages/source/p/pip/</a><br/>下载<br/><div class="code">wget https://pypi.python.org/packages/source/p/pip/pip-1.5.6.tar.gz --no-check-certificate </div><br/>#由于下载pip是基于https协议的,故需要在wget url后面加上--no-check-certificate,否则不能下载<br/><div class="code">tar zxvf pip-1.5.6.tar.gz<br/>cd pip-1.5.6<br/>python setup.py install</div><br/><br/>2、安装ngxtop<br/><div class="code">pip install ngxtop</div><br/>3、ngxtop 使用<br/>基本语法：<br/><div class="code">ngxtop &#91;options&#93;<br/>ngxtop &#91;options&#93; (print&#124;top&#124;avg&#124;sum) &lt;var&gt;<br/>ngxtop info<br/><br/>-l : 指定日志文件的完整路径 (Nginx 或 Apache2)<br/>-f : 日志格式<br/>--no-follow: 处理当前已经写入的日志文件，而不是实时处理新添加到日志文件的日志<br/>-t : 更新频率<br/>-n : 显示行号<br/>-o : 排序规则(默认是访问计数)<br/>-a ..., --a ...: 添加表达式(一般是聚合表达式如： sum, avg, min, max 等)到输出中。<br/>-v: 输出详细信息<br/>-i : 只处理符合规则的记录<br/><br/>内置变量<br/>bodybytessend<br/>http_referer<br/>httpuseragent<br/>remote_addr<br/>remote_user<br/>request<br/>status<br/>time_local<br/><br/><br/>#ngxtop&nbsp;&nbsp;&nbsp;&nbsp;----列出10个nginx服务器，按请求数量排序<br/>#ngxtop -n 20 ----显示前20个最频繁的请求<br/>#ngxtop info&nbsp;&nbsp;----获取nginx基本信息<br/>#ngxtop print request http_user_agent remote_addr&nbsp;&nbsp;---以自定义显示的变量，简单列出需要显示的变量。使用 &quot;print&quot; 命令显示自定义请求。<br/>#ngxtop top remote_addr&nbsp;&nbsp; ----显示请求最多的客户端IP地址<br/>#ngxtop -i &#039;status == 404&#039; print request status&nbsp;&nbsp;&nbsp;&nbsp;----显示状态码是404的请求<br/><br/>除了Nginx，ngtop 还可以处理其他的日志文件，比如 Apache 的访问文件。使用以下命令监控 Apache 服务器:<br/>#tail -f /var/log/httpd/access.log &#124; ngxtop -f common<br/></div><br/><br/><br/>安装过程遇到的错误：<br/>（1）执行ngxtop会报如下错误：ImportError: No module named _sqlite3<br/># /usr/local/bin/ngxtop --help<br/>Traceback (most recent call last):<br/>&nbsp;&nbsp;File "/usr/local/bin/ngxtop", line 7, in <module><br/>&nbsp;&nbsp;&nbsp;&nbsp;from ngxtop.ngxtop import main<br/>&nbsp;&nbsp;File "/usr/local/lib/python2.7/site-packages/ngxtop/ngxtop.py", line 60, in <module><br/>&nbsp;&nbsp;&nbsp;&nbsp;import sqlite3<br/>&nbsp;&nbsp;File "/usr/local/lib/python2.7/sqlite3/__init__.py", line 24, in <module><br/>&nbsp;&nbsp;&nbsp;&nbsp;from dbapi2 import *<br/>&nbsp;&nbsp;File "/usr/local/lib/python2.7/sqlite3/dbapi2.py", line 27, in <module><br/>&nbsp;&nbsp;&nbsp;&nbsp;from _sqlite3 import *<br/>ImportError: No module named _sqlite3<br/><br/>解决方案：编辑Python-2.7.3/Modules/_sqlite/connection.c加入如下44-57行，然后重新编译python2.7。configure && make && make install<br/>[root@ Python-2.7.3]# vim Modules/_sqlite/connection.c <br/>&nbsp;&nbsp;38 #if SQLITE_VERSION_NUMBER >= 3003008<br/>&nbsp;&nbsp;39 #ifndef SQLITE_OMIT_LOAD_EXTENSION<br/>&nbsp;&nbsp;40 #define HAVE_LOAD_EXTENSION<br/>&nbsp;&nbsp;41 #endif<br/>&nbsp;&nbsp;42 #endif<br/>&nbsp;&nbsp;43 <br/>&nbsp;&nbsp;44 /*** add by lai for **/<br/>&nbsp;&nbsp;45 #ifdef SQLITE_INT64_TYPE<br/>&nbsp;&nbsp;46 typedef SQLITE_INT64_TYPE sqlite_int64;<br/>&nbsp;&nbsp;47 typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;<br/>&nbsp;&nbsp;48 #elif defined(_MSC_VER) &#124;&#124; defined(__BORLANDC__)<br/>&nbsp;&nbsp;49 typedef __int64 sqlite_int64;<br/>&nbsp;&nbsp;50 typedef unsigned __int64 sqlite_uint64;<br/>&nbsp;&nbsp;51 #else<br/>&nbsp;&nbsp;52 typedef long long int sqlite_int64;<br/>&nbsp;&nbsp;53 typedef unsigned long long int sqlite_uint64;<br/>&nbsp;&nbsp;54 #endif<br/>&nbsp;&nbsp;55 typedef sqlite_int64 sqlite3_int64;<br/>&nbsp;&nbsp;56 typedef sqlite_uint64 sqlite3_uint64;<br/>&nbsp;&nbsp;57 /*** end ***/<br/>&nbsp;&nbsp;58 <br/>&nbsp;&nbsp;59 <br/>&nbsp;&nbsp;60 static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level);<br/>&nbsp;&nbsp;61 static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);<br/>&nbsp;&nbsp;62 <br/><br/><br/>（2）[root@ Python-2.7.3]# ngxtop -n 20<br/>Error: Access log file or format was not set and nginx config file cannot be detected. Perhaps nginx is not in your PATH?<br/><br/>解决办法：<br/>&nbsp;&nbsp;&nbsp;&nbsp;1.软连接<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ln -s /usr/local/nginx/nginx /usr/bin/nginx<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.修改环境变量加入到path里面<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vi /etc/profile<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PATH=$JAVA_HOME/bin:/usr/local/php/bin:/usr/local/nginx:$PATH<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;source /etc/profile<br/>&nbsp;&nbsp;&nbsp;&nbsp;3.指定配置文件<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ngxtop -c /usr/local/nginx/nginx.conf<br/></span></span><br/>Tags - <a href="https://www.cnmysql.com/tags/nginx/" rel="tag">nginx</a> , <a href="https://www.cnmysql.com/tags/ngxtop/" rel="tag">ngxtop</a> , <a href="https://www.cnmysql.com/tags/pip/" rel="tag">pip</a>
]]>
</description>
</item><item>
<link>https://www.cnmysql.com/post//#blogcomment</link>
<title><![CDATA[[评论] Nginx监控利器Ngxtop]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.cnmysql.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>