ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/examples/disk_traffic.c
Revision: 1.1
Committed: Thu Jun 5 15:38:44 2003 UTC (21 years, 6 months ago) by pajs
Content type: text/plain
Branch: MAIN
Log Message:
Example for disk traffic.

File Contents

# User Rev Content
1 pajs 1.1 /*
2     * i-scream central monitoring system
3     * http://www.i-scream.org.uk
4     * Copyright (C) 2000-2002 i-scream
5     *
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 disk 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     * Note, this is not disk traffic per second. Its the traffic since last
27     * call. If you want it traffic per second averaged over time since last call
28     * 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 disk_stat_t structure.
30     * -b Display in bytes
31     * -k Display in kilobytes
32     * -m Display in megabytes
33     */
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     diskio_stat_t *diskio_stats;
51     int num_diskio_stats;
52    
53     /* Parse command line options */
54     while ((c = getopt(argc, argv, "d:bkm")) != EOF){
55     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     /* We are not intrested in the amount of traffic ever transmitted, just differences.
72     * Because of this, we do nothing for the very first call.
73     */
74    
75     diskio_stats = get_diskio_stats_diff(&num_diskio_stats);
76     if (diskio_stats == NULL){
77     perror("Error. Failed to get disk stats");
78     return 1;
79     }
80    
81     /* Clear the screen ready for display the disk stats */
82     printf("\033[2J");
83    
84     /* Keep getting the disk stats */
85     while ( (diskio_stats = get_diskio_stats_diff(&num_diskio_stats)) != NULL){
86     int x;
87     int line_number = 2;
88    
89     long long total_write=0;
90     long long total_read=0;
91    
92     for(x = 0; x < num_diskio_stats; x++){
93     /* Print at location 2, linenumber the interface name */
94     printf("\033[%d;2H%-25s : %-10s", line_number++, "Disk Name", diskio_stats->disk_name);
95     /* Print out at the correct location the traffic in the requsted units passed at command time */
96     switch(units){
97     case 'b':
98     printf("\033[%d;2H%-25s : %8lld b", line_number++, "Disk read", diskio_stats->read_bytes);
99     printf("\033[%d;2H%-25s : %8lld b", line_number++, "Disk write", diskio_stats->write_bytes);
100     break;
101     case 'k':
102     printf("\033[%d;2H%-25s : %5lld k", line_number++, "Disk read", (diskio_stats->read_bytes / 1024));
103     printf("\033[%d;2H%-25s : %5lld", line_number++, "Disk write", (diskio_stats->write_bytes / 1024));
104     break;
105     case 'm':
106     printf("\033[%d;2H%-25s : %5.2f m", line_number++, "Disk read", diskio_stats->read_bytes / (1024.00*1024.00));
107     printf("\033[%d;2H%-25s : %5.2f m", line_number++, "Disk write", diskio_stats->write_bytes / (1024.00*1024.00));
108     }
109     printf("\033[%d;2H%-25s : %ld ", line_number++, "Disk systime", (long) diskio_stats->systime);
110    
111     /* Add a blank line between interfaces */
112     line_number++;
113    
114     /* Add up this interface to the total so we can display a "total" disk io" */
115     total_write+=diskio_stats->write_bytes;
116     total_read+=diskio_stats->read_bytes;
117    
118     /* Move the pointer onto the next interface. Since this returns a static buffer, we dont need
119     * to keep track of the orginal pointer to free later */
120     diskio_stats++;
121     }
122    
123     printf("\033[%d;2H%-25s : %-10s", line_number++, "Disk Name", "Total Disk IO");
124     switch(units){
125     case 'b':
126     printf("\033[%d;2H%-25s : %8lld b", line_number++, "Disk Total read", total_read);
127     printf("\033[%d;2H%-25s : %8lld b", line_number++, "Disk Total write", total_write);
128     break;
129     case 'k':
130     printf("\033[%d;2H%-25s : %5lld k", line_number++, "Disk Total read", (total_read / 1024));
131     printf("\033[%d;2H%-25s : %5lld k", line_number++, "Disk Total write", (total_write / 1024));
132     break;
133     case 'm':
134     printf("\033[%d;2H%-25s : %5.2f m", line_number++, "Disk Total read", (total_read / (1024.00*1024.00)));
135     printf("\033[%d;2H%-25s : %5.2f m", line_number++, "Disk Total write", (total_write / (1024.00*1024.00)));
136     break;
137     }
138    
139     fflush(stdout);
140    
141     sleep(delay);
142    
143     }
144    
145     return 0;
146    
147     }