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