指令碼擷取網口介面速率並存入mysql

來源:互聯網
上載者:User

標籤:mysql   sleep   介面   流量   

指令碼如下:


netflow.sh

#!/bin/bash#while true;do                ##死迴圈R1=`cat /sys/class/net/eth0/statistics/rx_bytes`  ##擷取eth0的收到流量位元組數T1=`cat /sys/class/net/eth0/statistics/tx_bytes`  ##擷取eht0發送的流量位元組數#sleep 5R2=`cat /sys/class/net/eth0/statistics/rx_bytes`  ##擷取5s後收到的流量位元組數T2=`cat /sys/class/net/eth0/statistics/tx_bytes`  ##擷取5s後發送出去的流量數#TBPS=`expr $T2 - $T1`            ##相減RBPS=`expr $R2 - $R1`TKBPS=`expr $TBPS / 10240`        ##換算為KB,由於這裡應該是除以1024 再處於8 然後再處於2s,所以這裡直接就是使用10240,為了簡化而已。RKBPS=`expr $RBPS / 10240`TIME=`date`                        ##目前時間sql="insert into linux.netflow(TXBPS,RXBPS,TIME)values($TKBPS,$RKBPS,‘$TIME‘)"mysql -uroot -p123456 -e "$sql"        ##將資料插入mysql表中#mysql -uroot -p123456 -e "select * from linux.netflow"done



netflow.sql   ##建立mysql表(ID,TXBPS,RXBPS,TIME)

create database linux;use linux;create table netflow(ID int auto_increment primary key,TXBPS float,RXBPS float,TIME char(10));



使用過程如下:

①執行netflow.sql建立mysql表

[[email protected] ~]#  mysql  -uroot  -p < netflow.sql

②netflow.sh後台運行

[[email protected] ~]#  ./netflow.sh &    ##&是自己放到後台執行,指令碼會不斷的擷取介面速率,然後儲存在mysql中


③編輯php網頁,實現web 方式查看流量資訊

<?php$conn=mysql_connect("localhost","root","123456");if($conn){        $sql="select * from linux.netflow limit 10";        $result=mysql_query("$sql");        echo "<center>";        echo "<table border=1>";        echo "<captive>eth0_speed</captive>";          echo "<tr>";                echo "<th>ID</th>";                echo "<th>TIME</th>";                echo "<th>RXBPS</th>";                echo "<th>TXBPS</th>";          echo "</tr>";        while($filter=mysql_fetch_assoc($result)){          echo "<tr>";                echo "<td>$filter[ID]</td>";                echo "<td>$filter[TIME]</td>";                echo "<td>$filter[RXBPS]</td>";                echo "<td>$filter[TXBPS]</td>";          echo "</tr>";        }        echo "</table>";        echo "</center>";}else{        echo failure;}?>



650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6B/7A/wKiom1UuYhiSEp8nAAQHdRR03VI137.jpg" title="5.png" width="400" height="161" border="0" hspace="0" vspace="0" style="width:400px;height:161px;" alt="wKiom1UuYhiSEp8nAAQHdRR03VI137.jpg" />


頁面較為簡單,只是為了示範而已,後續可以進一步強化,可以通過繪圖等形式更加直觀展示。

本文出自 “Lu2Yu” 部落格,請務必保留此出處http://lu2yu.blog.51cto.com/10009517/1633099

指令碼擷取網口介面速率並存入mysql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.