ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/examples/network_traffic.c
Revision: 1.5
Committed: Sun Nov 23 13:51:10 2003 UTC (20 years, 11 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.4: +14 -11 lines
Log Message:
Add statgrab_init() call to the examples. If people are going to copy them
I guess we should try and make them correct :-)

Also tidied up whitespace in the examples.

File Contents

# User Rev Content
1 pajs 1.1 /*
2     * i-scream central monitoring system
3 tdb 1.3 * http://www.i-scream.org
4     * Copyright (C) 2000-2003 i-scream
5 pajs 1.1 *
6     * This program is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU General Public License
8     * as published by the Free Software Foundation; either version 2
9     * of the License, or (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19     */
20    
21     /* A very basic example of how to get the network statistics from the system
22     * and diaply them. Also it adds up all the traffic to create and print out
23     * a total
24     * Takes several arguments :
25     * -d <number> Takes the number of seconds to wait to get traffic sent since last call
26 tdb 1.5 * Note, this is not network traffic per second. Its the traffic since last
27     * call. If you want it traffic per second averaged over time since last call
28 pajs 1.1 * You need to divide against the time since last time this was called. The time
29     * between 2 calls is stored in systime in the network_stat_t structure.
30     * -b Display in bytes
31     * -k Display in kilobytes
32 tdb 1.5 * -m Display in megabytes
33 pajs 1.1 */
34    
35     #include <stdio.h>
36     #include <statgrab.h>
37     #include <stdlib.h>
38     #include <unistd.h>
39    
40     int main(int argc, char **argv){
41    
42     extern char *optarg;
43     extern int optind;
44     int c;
45    
46     /* We default to 1 second updates and displaying in bytes*/
47     int delay = 1;
48     char units = 'b';
49    
50     network_stat_t *network_stats;
51     int num_network_stats;
52    
53     /* Parse command line options */
54 ats 1.4 while ((c = getopt(argc, argv, "d:bkm")) != -1){
55 pajs 1.1 switch (c){
56     case 'd':
57     delay = atoi(optarg);
58     break;
59     case 'b':
60     units = 'b';
61     break;
62     case 'k':
63     units = 'k';
64     break;
65     case 'm':
66     units = 'm';
67     break;
68     }
69     }
70    
71 tdb 1.5 /* Initialise statgrab */
72     statgrab_init();
73    
74     /* We are not interested in the amount of traffic ever transmitted, just differences.
75 pajs 1.1 * Because of this, we do nothing for the very first call.
76     */
77 tdb 1.5
78 pajs 1.1 network_stats = get_network_stats_diff(&num_network_stats);
79     if (network_stats == NULL){
80     perror("Error. Failed to get network stats");
81     return 1;
82     }
83    
84     /* Clear the screen ready for display the network stats */
85     printf("\033[2J");
86 tdb 1.5
87 pajs 1.1 /* Keep getting the network stats */
88     while ( (network_stats = get_network_stats_diff(&num_network_stats)) != NULL){
89     int x;
90     int line_number = 2;
91    
92     long long total_tx=0;
93     long long total_rx=0;
94    
95     for(x = 0; x < num_network_stats; x++){
96     /* Print at location 2, linenumber the interface name */
97     printf("\033[%d;2H%-25s : %-10s", line_number++, "Network Interface Name", network_stats->interface_name);
98     /* Print out at the correct location the traffic in the requsted units passed at command time */
99     switch(units){
100     case 'b':
101     printf("\033[%d;2H%-25s : %8lld b", line_number++, "Network Interface Rx", network_stats->rx);
102     printf("\033[%d;2H%-25s : %8lld b", line_number++, "Network Interface Tx", network_stats->tx);
103     break;
104     case 'k':
105     printf("\033[%d;2H%-25s : %5lld k", line_number++, "Network Interface Rx", (network_stats->rx / 1024));
106 tdb 1.5 printf("\033[%d;2H%-25s : %5lld", line_number++, "Network Interface Tx", (network_stats->tx / 1024));
107     break;
108 pajs 1.1 case 'm':
109 pajs 1.2 printf("\033[%d;2H%-25s : %5.2f m", line_number++, "Network Interface Rx", network_stats->rx / (1024.00*1024.00));
110 pajs 1.1 printf("\033[%d;2H%-25s : %5.2f m", line_number++, "Network Interface Tx", network_stats->tx / (1024.00*1024.00));
111     }
112     printf("\033[%d;2H%-25s : %ld ", line_number++, "Network Interface systime", (long) network_stats->systime);
113 tdb 1.5
114 pajs 1.1 /* Add a blank line between interfaces */
115     line_number++;
116    
117     /* Add up this interface to the total so we can display a "total" network io" */
118     total_tx+=network_stats->tx;
119     total_rx+=network_stats->rx;
120    
121     /* Move the pointer onto the next interface. Since this returns a static buffer, we dont need
122     * to keep track of the orginal pointer to free later */
123     network_stats++;
124     }
125 tdb 1.5
126 pajs 1.1 printf("\033[%d;2H%-25s : %-10s", line_number++, "Network Interface Name", "Total Network IO");
127     switch(units){
128     case 'b':
129     printf("\033[%d;2H%-25s : %8lld b", line_number++, "Network Total Rx", total_rx);
130     printf("\033[%d;2H%-25s : %8lld b", line_number++, "Network Total Tx", total_tx);
131     break;
132     case 'k':
133     printf("\033[%d;2H%-25s : %5lld k", line_number++, "Network Total Rx", (total_rx / 1024));
134     printf("\033[%d;2H%-25s : %5lld k", line_number++, "Network Total Tx", (total_tx / 1024));
135     break;
136     case 'm':
137     printf("\033[%d;2H%-25s : %5.2f m", line_number++, "Network Total Rx", (total_rx / (1024.00*1024.00)));
138     printf("\033[%d;2H%-25s : %5.2f m", line_number++, "Network Total Tx", (total_tx / (1024.00*1024.00)));
139     break;
140     }
141 tdb 1.5
142 pajs 1.1 fflush(stdout);
143    
144     sleep(delay);
145    
146     }
147    
148     return 0;
149    
150     }