Linux Traffic Statistics Applet

Source: Internet
Author: User

The source code is as follows:

//2015/7/2 10:30:35//gino#include <stdio.h>#include <sys/stat.h>#include <sys/types.h>#include <fcntl.h>#include <stdlib.h>#include <errno.h>#include <string.h>#define Flow_rx_file "/sys/class/net/eth0/statistics/rx_bytes"#define Flow_tx_file "/sys/class/net/eth0/statistics/tx_bytes"#define Flow_statistics "/tmp/statistics"#define DATA_BUF_SIZE 1024x768intMainintargcChar**ARGV) {intFLOWRX_FD, FLOWTX_FD, FLOWST_FD, data_size;CharRx_buf[data_buf_size], tx_buf[data_buf_size]; FILE *flowst_pin = NULL;if(flowrx_fd = open (Flow_rx_file, o_rdonly)) = =-1){fprintf(stderr,"Rx_open:%s\n", Strerror (errno));Exit(exit_failure); }if(flowtx_fd = open (Flow_tx_file, o_rdonly)) = =-1){fprintf(stderr,"Tx_open:%s\n", Strerror (errno));Exit(exit_failure); }if(flowst_fd = open (Flow_statistics, o_rdwr| O_creat, s_irusr| S_IWUSR)) = =-1){fprintf(stderr,"St_open:%s\n", Strerror (errno));Exit(exit_failure); }//Convert file descriptor to file pointerFlowst_pin = Fdopen (FLOWST_FD,"w+"); while(1) {bzero (Rx_buf,sizeof(RX_BUF)); Bzero (Tx_buf,sizeof(TX_BUF));if(data_size = Read (Flowrx_fd,rx_buf,sizeof(RX_BUF))) == -1){fprintf(stderr,"READ_RX:%s\n", Strerror (errno)); } rx_buf[data_size-1] =' + ';if(data_size = Read (Flowtx_fd,tx_buf,sizeof(TX_BUF))) == -1){fprintf(stderr,"Read_tx:%s\n", Strerror (errno)); } tx_buf[data_size-1] =' + ';//josn Format {"FirstName": "Brett", "LastName": "McLaughlin", "email": "AAAA"}            fprintf(Flowst_pin,"{\" up_flow\ ": \"%s\ ", \" down_flow\ ": \"%s\ "}\n", Rx_buf, TX_BUF);            Fflush (Flowst_pin); Lseek (FLOWRX_FD,0, Seek_set); Lseek (FLOWTX_FD,0, Seek_set); Sleep2);//Change the file size to save the data written to the file to reach the overwrite effectFtruncate (FLOWST_FD,0); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux Traffic Statistics Applet

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.