<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[需要实践]使用PHP获取网卡实时流量带宽使用情况,shell脚本之获取网卡实时流量。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Thu, 05 Jun 2014 03:18:44 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：PHP探针里有一个：服务器实时数据（CPU型号 [4核] ...）&nbsp;&nbsp;网络使用状况（eth1 :&nbsp;&nbsp;已接收 : 1.03612 GB&nbsp;&nbsp;已发送 : 0.20958 GB） 这个是肿实现的？<br/><br/>[root@localhost ~]$ cat /proc/net/dev<br/>Inter-&#124;&nbsp;&nbsp; Receive&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;&nbsp;&nbsp;Transmit<br/>face &#124;bytes&nbsp;&nbsp;&nbsp;&nbsp;packets errs drop fifo frame compressed multicast&#124;bytes&nbsp;&nbsp;&nbsp;&nbsp;packets errs drop fifo colls carrier compressed<br/>&nbsp;&nbsp;&nbsp;&nbsp;lo:1068205690 1288942839&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 1068205690 1288942839&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0<br/>&nbsp;&nbsp;eth0:91581844 334143895&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0 145541676 4205113078 3435231517&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0<br/>proc/net/dev 文件保存了网卡总流量信息，通过间隔一段间隔，将入网卡与出记录加起来。减去之前就得到实际速率。 <br/>php实现代码：<br/><br/>&lt;?php<br/>$dev = $argv[1]?$argv[1]:&quot;eth0&quot;;<br/> <br/>while(true)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result = file_get_contents(&quot;/proc/net/dev&quot;);<br/> <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;preg_match(&quot;/&#123;$dev&#125;:(&#92;d+)&#92;s+&#92;d+&#92;s+&#92;d+&#92;s+&#92;d+&#92;s+&#92;d+&#92;s+&#92;d+&#92;s+&#92;d+&#92;s+&#92;d+&#92;s+(&#92;d+)/&quot;, $result, $preg_arr);<br/> <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$lastre = $thisre;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$lasttr = $thistr;<br/> <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$thisre = $preg_arr[1];<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$thistr = $preg_arr[2];<br/> <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$re = hbw($thisre - $lastre);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$tr = hbw($thistr - $lasttr);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;&#92;33[2J&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&#92;33[1;1H%-12sss&#92;n&quot;, &#039;Device&#039;, &#039;In&#039;, &#039;Out&#039;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&#92;33[2;1H%-12sss&#92;n&quot;, $dev, $re, $tr);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(1);<br/>&#125;<br/> <br/> <br/>function hbw($size) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$size *= 8;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($size &gt; 1024 * 1024 * 1024) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$size = round($size / 1073741824 * 100) / 100 . &#039; Gbps&#039;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; elseif($size &gt; 1024 * 1024) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$size = round($size / 1048576 * 100) / 100 . &#039; Mbps&#039;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; elseif($size &gt; 1024) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$size = round($size / 1024 * 100) / 100 . &#039; Kbps&#039;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; else &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$size = $size . &#039; Bbps&#039;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $size;<br/>&#125;<br/>?&gt;<br/><br/><br/><br/>php怎么得到cpu 内存 网络流量的使用率<br/><br/><br/>&lt;?php <br/>$str = shell_exec(&#039;more /proc/stat&#039;); <br/>$pattern = &quot;/(cpu[0-9]?)[&#92;s]+([0-9]+)[&#92;s]+([0-9]+)[&#92;s]+([0-9]+)[&#92;s]+([0-9]+)[&#92;s]+([0-9]+)[&#92;s]+([0-9]+)[&#92;s]+([0-9]+)/&quot;; <br/>preg_match_all($pattern, $str, $out); <br/>echo &quot;共有&quot;.count($out[1]).&quot;个CPU，每个CPU利用率如下：&lt;br&gt;&quot;; <br/>for($n=0;$n&lt;count($out[1]);$n++) <br/>&#123; <br/>echo $out[1][$n].&quot;=&quot;.(100*($out[1][$n]+$out[2][$n]+$out[3][$n])/($out[4][$n]+$out[5][$n]+$out[6][$n]+$out[7][$n])).&quot;%&lt;br&gt;&quot;; <br/>&#125; <br/>?&gt; <br/>////////////////////////////////////////////////////////////////////// <br/>&lt;?php <br/>$str = shell_exec(&#039;more /proc/meminfo&#039;); <br/>$pattern = &quot;/(.+):&#92;s*([0-9]+)/&quot;; <br/>preg_match_all($pattern, $str, $out); <br/>echo &quot;物理内存总量：&quot;.$out[2][0].&quot;&lt;br&gt;&quot;; <br/>echo &quot;已使用的内存：&quot;.$out[2][1].&quot;&lt;br&gt;&quot;; <br/>echo &quot;-----------------------------------------&lt;br&gt;&quot;; <br/>echo &quot;内存使用率：&quot;.(100*($out[2][0]-$out[2][1])/$out[2][0]).&quot;%&lt;br&gt;&quot;; <br/>?&gt;<br/>//////////////////////////////////////////////////////////////////////<br/>&lt;?php<br/>$str = shell_exec(&#039;more /proc/net/dev&#039;);<br/>$pattern = &quot;/(eth[0-9]+):&#92;s*([0-9]+)&#92;s+([0-9]+)&#92;s+([0-9]+)&#92;s+([0-9]+)&#92;s+([0-9]+)&#92;s+([0-9]+)&#92;s+([0-9]+)&#92;s+([0-9]+)&#92;s+([0-9]+)&#92;s+([0-9]+)/&quot;;<br/>preg_match_all($pattern, $str, $out); <br/>echo &quot;共有&quot;.count($out[1]).&quot;个网络接口，每个网络接口利用率如下：&lt;br&gt;&quot;; <br/>for($n=0;$n&lt;count($out[1]);$n++) <br/>&#123; <br/>echo $out[1][$n].&quot;：收到 &quot;.$out[3][$n].&quot; 个数据包，发送 &quot;.$out[11][$n].&quot; 个数据包&lt;br&gt;&quot;; <br/>&#125; <br/>?&gt;<br/><br/>来自：http://idsips.blog.163.com/blog/static/48001272201312092813563/<br/><br/><br/>shell脚本之获取网卡实时流量：<br/>最近一直在搞服务器监控系统，分享一下本人采集网卡实时流量的shell 脚本，写得不好的地方还请指出，共同进步一下。<br/><textarea name="code" class="C" rows="15" cols="100">
#!/bin/sh&nbsp;&nbsp;
#!/bin/sh&nbsp;&nbsp;
#Collect Network Connection Flow&nbsp;&nbsp;
#version 2.0&nbsp;&nbsp;
#yangqijun 2012&nbsp;&nbsp;
eth=$1&nbsp;&nbsp;
type=$2&nbsp;&nbsp;
if [ $type -eq 1 ]&nbsp;&nbsp;
then&nbsp;&nbsp;
 result=$(ifconfig $eth &#124; grep -e &#039;RX bytes&#039;&#124;awk &#039;&#123;print $2&#125;&#039;&#124;awk -F&#039;:&#039; &#039;&#123;print $2&#125;&#039;);&nbsp;&nbsp;
 sleep 5;&nbsp;&nbsp;
 result2=$(ifconfig $eth &#124; grep -e &#039;RX bytes&#039;&#124;awk &#039;&#123;print $2&#125;&#039;&#124;awk -F&#039;:&#039; &#039;&#123;print $2&#125;&#039;);&nbsp;&nbsp;
else&nbsp;&nbsp;
&nbsp;&nbsp;result=0&nbsp;&nbsp;
&nbsp;&nbsp;result=$(ifconfig $eth &#124; grep -e &#039;RX bytes&#039;&#124;awk &#039;&#123;print $6&#125;&#039;&#124;awk -F&#039;:&#039; &#039;&#123;print $2&#125;&#039;);&nbsp;&nbsp;
&nbsp;&nbsp;sleep 5;&nbsp;&nbsp;
&nbsp;&nbsp;result2=$(ifconfig $eth &#124; grep -e &#039;RX bytes&#039;&#124;awk &#039;&#123;print $6&#125;&#039;&#124;awk -F&#039;:&#039; &#039;&#123;print $2&#125;&#039;);&nbsp;&nbsp;
fi&nbsp;&nbsp;
if [ -z &quot;$result2&quot; ]&nbsp;&nbsp;
then&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result2=0&nbsp;&nbsp;
fi&nbsp;&nbsp;
if [ -z &quot;$result&quot; ]; then&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result=0&nbsp;&nbsp;
fi&nbsp;&nbsp;
if [ $result2 -ge $result ]&nbsp;&nbsp;
then&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;diffentvalue=$((($result2-$result)/5));&nbsp;&nbsp;
else&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;diffentvalue=$((($result2+4*1024*1024*1024-$result)/5));&nbsp;&nbsp;
fi&nbsp;&nbsp;
diffentvalue=$&#123;diffentvalue#-&#125;&nbsp;&nbsp;
if [ $diffentvalue -ge 1073741824 ]&nbsp;&nbsp;
then&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; resultend=$(($diffentvalue/1073741824));&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; unit=G;&nbsp;&nbsp;
elif [ $diffentvalue -ge 1048576 ]&nbsp;&nbsp;
then&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; resultend=$(($diffentvalue/1048576));&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; unit=M;&nbsp;&nbsp;
elif [ $diffentvalue -ge 1024 ]&nbsp;&nbsp;
then&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; resultend=$(($diffentvalue/1024));&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; unit=k;&nbsp;&nbsp;
else&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; resultend=$diffentvalue ;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; unit=B;&nbsp;&nbsp;
fi&nbsp;&nbsp;
#echo $&#123;resultend#-&#125;&quot;$unit&quot;&nbsp;&nbsp;
echo $resultend&quot;$unit&quot;&nbsp;&nbsp;
</textarea><br/><br/>使用方式:<br/>sh /root/monitor_flow eth0 1<br/><br/>1 表示 进流量（RX）<br/>2 表示 出流量 （TX）<br/><br/>[root@ZWCLC6X-7198 ~]# sh /root/monitor_flow eth1 2<br/>1k<br/><br/><br/>以上代码仅供参考。<br/>代码来自：http://blog.csdn.net/youngqj/article/details/7254224
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [需要实践]使用PHP获取网卡实时流量带宽使用情况,shell脚本之获取网卡实时流量。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>