OpenResty query Linux VPS host traffic

Source: Internet
Author: User

This is the case. I bought vps, and the traffic per month is not much. I just want to see how much traffic is used. But I don't want to go to the background of the host, and I still don't want to use those monitoring software, so I want to get a script and read it out through openresty.

Retrieve traffic data

The operating system of vps is Centos6, which can be obtained by using shell commands or reading a file.

You can read the following two files. venet0 is the network card name.

[Root @ CT1391 ~] # Cat/sys/class/net/venet0/statistics/rx_bytes
2300558468
[Root @ CT1391 ~] # Cat/sys/class/net/venet0/statistics/tx_bytes
2111210364
In lua, we can directly read the file and get the traffic value of the NIC. The rest is to format and output the file.

Lua script

When lua reads and displays the Nic data, it also requires a monthly reset action (generally, it restarts the NIC and no other method is found. The trouble is to record the data at the beginning of each month and calculate the data for the current month by yourself ).

Nginx configuration

Server {
Listen 80;
Server_name localhost;
Lua_code_cache off;

Location/status {
Default_type text/html;
Charset UTF-8;
Content_by_lua_file/etc/nginx/lua/tstatus. lua;
    }
}
During deployment, remember to enable lua_code_cache.

Tstatus. lua

Local io = require "io"
Local math = require "math"

-- Change to your Nic and total number
Local fname = "eth2"
Local month_flow = "250G"

Local function get_value (v)
Local file = io. open ("/sys/class/net/" .. fname .. "/statistics/" .. v .. "_ bytes ")
Local value = file: read ()
File: close ()
Return value
End

Local function round (num, dip)
Return tonumber (string. format ("%."... (dip or 0) .. "f", num ))
End

Local function flow_format (v)
Local v = tonumber (v)
If v <1024 then
Return v .. "byte"
Elseif v <1024*1024 then
Return round (v/1024.0, 2) .. "Kb"
Elseif v <1024*1024*1024 then
Return round (v/1024.0/1024.0, 2)... "M"
Elseif v <1024*1024*1024*1024 then
Return round (v/1024.0/1024.0/1024.0, 2) .. "G"
End
End

Local rx = get_value ("rx ")
Local tx = get_value ("tx ")
Local total = rx + tx

Ngx. say ("Your total traffic available this month is"... month_flow... "<br> ")
Ngx. say ("RX:"... rx... "->"... flow_format (rx)... "<br> ")
Ngx. say ("TX:" .. tx... "->" .. flow_format (tx) .. "<br> ")
Ngx. say ("Total:"... total... "->" .. flow_format (total ))
Ngx. exit (200)
Test

Using curl for testing can also be used normally in the browser. If you want to look tall, you can create a pie chart or a dashboard, and then use json to get the traffic value.

[Root @ orangleliu lzz] # curl http: // 192.168.59.104/status
Your total traffic available this month is 250 GB <br>
RX: 3412800496-> 3.18 GB <br>
TX: 61802720-> 58.94 M <br>
Total: 3474603216-> 3.24 GB
Let's start with this. In 1st day of next month, we first recorded the accumulated traffic and made a process of persistence and computing for the current month ..

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.