The script is as follows:
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 $T 2 - $T 1 ' # #相减RBPS = ' expr $R 2 - $R 1 ' tkbps= ' expr $TBPS / 10240 ' # #换算为KB, as this should be divided by 1024 again in 8 and then in 2s, So here is the use of 10240, in order to simplify just. 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 (1 0));
Use the following procedure:
① execute netflow.sql create MySQL table
[Email protected] ~]# Mysql-uroot-p < Netflow.sql
②netflow.sh Background Run
[[email protected] ~]#./netflow.sh & ##& is executed in the background, the script will continue to get the interface rate, and then save in MySQL
③ Edit PHP Web page to view traffic information in Web mode
<?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= "height=" 161 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:400px;height:161px; "alt=" Wkiom1uuyhisep8naaqhdrr03vi137.jpg "/>
The page is simple, just for demonstration, the follow-up can be further enhanced, you can use the form of drawing more visual display.
This article is from the "Lu2yu" blog, make sure to keep this source http://lu2yu.blog.51cto.com/10009517/1633099
The script gets the port interface rate and deposits it in MySQL