The script gets the port interface rate and deposits it in MySQL

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.