ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/saidar/saidar.c
Revision: 1.3
Committed: Thu Oct 9 16:08:16 2003 UTC (21 years, 1 month ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.2: +25 -0 lines
Log Message:
Added some usage flags, and a -v for version and -h for help.

File Contents

# User Rev Content
1 tdb 1.2 /*
2     * i-scream central monitoring system
3     * http://www.i-scream.org
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     #ifdef HAVE_CONFIG_H
22     #include "config.h"
23     #endif
24    
25 pajs 1.1 #include <stdio.h>
26     #include <string.h>
27     #include <sys/ioctl.h>
28     #include <unistd.h>
29     #include <stdlib.h>
30     #include <sys/termios.h>
31     #include <signal.h>
32     #include <errno.h>
33     #include <statgrab.h>
34     #include <sys/times.h>
35     #include <limits.h>
36     #include <time.h>
37 tdb 1.2
38     #ifdef HAVE_NCURSES_H
39     #include <ncurses.h>
40     #else
41 pajs 1.1 #include <curses.h>
42 tdb 1.2 #endif
43 pajs 1.1
44     typedef struct{
45     cpu_percent_t *cpu_percents;
46     mem_stat_t *mem_stats;
47     swap_stat_t *swap_stats;
48     load_stat_t *load_stats;
49     process_stat_t *process_stats;
50     page_stat_t *page_stats;
51    
52     network_stat_t *network_stats;
53     int network_entries;
54    
55     diskio_stat_t *diskio_stats;
56     int diskio_entries;
57    
58     disk_stat_t *disk_stats;
59     int disk_entries;
60    
61     general_stat_t *general_stats;
62     user_stat_t *user_stats;
63     }stats_t;
64    
65     stats_t stats;
66    
67     char *size_conv(long long number){
68     char type[] = {'B', 'K', 'M', 'G', 'T'};
69     int x=0;
70     static char string[10];
71    
72     for(;x<5;x++){
73     if( (number/1024) < (100)) {
74     break;
75     }
76     number = (number/1024);
77     }
78    
79     snprintf(string, 10, "%lld%c", number, type[x]);
80     return string;
81    
82     }
83    
84     char *hr_uptime(time_t time){
85     int day = 0, hour = 0, min = 0;
86     static char uptime_str[25];
87     int sec = (int) time;
88    
89     day = sec / (24*60*60);
90     sec = sec % (24*60*60);
91     hour = sec / (60*60);
92     sec = sec % (60*60);
93     min = sec / 60;
94     sec = sec % 60;
95    
96     if(day){
97     snprintf(uptime_str, 25, "%dd %02d:%02d:%02d", day, hour, min, sec);
98     }else{
99     snprintf(uptime_str, 25, "%02d:%02d:%02d", hour, min, sec);
100     }
101     return uptime_str;
102     }
103    
104     void display_headings(){
105     move(0,0);
106     printw("Hostname :");
107     move(0,27);
108     printw("Uptime : ");
109     move(0,54);
110     printw("Date : ");
111    
112     /* Load */
113     move(2,0);
114     printw("Load 1 :");
115     move(3,0);
116     printw("Load 5 :");
117     move(4,0);
118     printw("Load 15 :");
119    
120     /* CPU */
121     move(2,21);
122     printw("CPU Idle :");
123     move(3,21);
124     printw("CPU System:");
125     move(4,21);
126     printw("CPU User :");
127    
128     /* Process */
129     move(2, 42);
130     printw("Running :");
131     move(3, 42);
132     printw("Sleeping :");
133     move(4, 42);
134     printw("Stopped :");
135     move(2, 62);
136     printw("Zombie :");
137     move(3, 62);
138     printw("Total :");
139     move(4, 62);
140     printw("No. Users :");
141    
142     /* Mem */
143     move(6, 0);
144     printw("Mem Total :");
145     move(7, 0);
146     printw("Mem Used :");
147     move(8, 0);
148     printw("Mem Free :");
149    
150     /* Swap */
151     move(6, 21);
152     printw("Swap Total:");
153     move(7, 21);
154     printw("Swap Used :");
155     move(8, 21);
156     printw("Swap Free :");
157    
158     /* VM */
159     move(6, 42);
160     printw("Mem Used :");
161     move(7, 42);
162     printw("Swap Used :");
163     move(8, 42);
164     printw("Total Used:");
165    
166     /* Paging */
167     move(6, 62);
168     printw("Paging in :");
169     move(7, 62);
170     printw("Paging out:");
171    
172     /* Disk IO */
173     move(10,0);
174     printw("Disk Name");
175     move(10,15);
176     printw("Read");
177     move(10,28);
178     printw("Write");
179    
180     /* Network IO */
181     move(10, 42);
182     printw("Network Interface");
183     move(10, 67);
184     printw("rx");
185     move(10,77);
186     printw("tx");
187    
188     move(12+stats.network_entries, 42);
189     printw("Mount Point");
190     move(12+stats.network_entries, 65);
191     printw("Free");
192     move(12+stats.network_entries, 75);
193     printw("Used");
194    
195     refresh();
196     }
197    
198     void display_data(){
199     char cur_time[20];
200     struct tm *tm_time;
201     time_t epoc_time;
202     int counter;
203     long long r,w;
204     long long rt, wt;
205     diskio_stat_t *diskio_stat_ptr;
206     network_stat_t *network_stat_ptr;
207     disk_stat_t *disk_stat_ptr;
208    
209     move(0,12);
210     printw("%s", stats.general_stats->hostname);
211     move(0,36);
212     printw("%s", hr_uptime(stats.general_stats->uptime));
213     epoc_time=time(NULL);
214     tm_time = localtime(&epoc_time);
215     strftime(cur_time, 20, "%Y-%m-%d %T", tm_time);
216     move(0,61);
217     printw("%s", cur_time);
218    
219     /* Load */
220     move(2,12);
221     printw("%6.2f", stats.load_stats->min1);
222     move(3,12);
223     printw("%6.2f", stats.load_stats->min5);
224     move(4,12);
225     printw("%6.2f", stats.load_stats->min15);
226    
227     /* CPU */
228     move(2,33);
229     printw("%6.2f%%", stats.cpu_percents->idle);
230     move(3,33);
231     printw("%6.2f%%", (stats.cpu_percents->kernel + stats.cpu_percents->iowait + stats.cpu_percents->swap));
232     move(4,33);
233     printw("%6.2f%%", (stats.cpu_percents->user + stats.cpu_percents->nice));
234    
235     /* Process */
236     move(2, 54);
237     printw("%5d", stats.process_stats->running);
238     move(2,74);
239     printw("%5d", stats.process_stats->zombie);
240     move(3, 54);
241     printw("%5d", stats.process_stats->sleeping);
242     move(3, 74);
243     printw("%5d", stats.process_stats->total);
244     move(4, 54);
245     printw("%5d", stats.process_stats->stopped);
246     move(4,74);
247     printw("%5d", stats.user_stats->num_entries);
248    
249     /* Mem */
250    
251     move(6, 12);
252     printw("%7s", size_conv(stats.mem_stats->total));
253     move(7, 12);
254     printw("%7s", size_conv(stats.mem_stats->used));
255     move(8, 12);
256     printw("%7s", size_conv(stats.mem_stats->free));
257    
258     /* Swap */
259     move(6, 32);
260     printw("%8s", size_conv(stats.swap_stats->total));
261     move(7, 32);
262     printw("%8s", size_conv(stats.swap_stats->used));
263     move(8, 32);
264     printw("%8s", size_conv(stats.swap_stats->free));
265    
266     /* VM */
267     move(6, 54);
268     printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total));
269     move(7, 54);
270     printw("%5.2f%%", (100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total));
271     move(8, 54);
272     printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used+stats.swap_stats->used)/(stats.mem_stats->total+stats.swap_stats->total)));
273    
274     /* Paging */
275     move(6, 74);
276     printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pagein / stats.page_stats->systime): stats.page_stats->pages_pagein);
277     move(7, 74);
278     printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pageout / stats.page_stats->systime) : stats.page_stats->pages_pageout);
279    
280     /* Disk IO */
281     diskio_stat_ptr = stats.diskio_stats;
282     r=0;
283     w=0;
284     for(counter=0;counter<stats.diskio_entries;counter++){
285     move(11+counter, 0);
286     printw("%s", diskio_stat_ptr->disk_name);
287     move(11+counter, 12);
288     rt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->read_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->read_bytes;
289     printw("%7s", size_conv(rt));
290     r+=rt;
291     move(11+counter, 26);
292     wt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->write_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->write_bytes;
293     printw("%7s", size_conv(wt));
294     w+=wt;
295     diskio_stat_ptr++;
296     }
297     move(12+counter, 0);
298     printw("Total");
299     move(12+counter, 12);
300     printw("%7s", size_conv(r));
301     move(12+counter, 26);
302     printw("%7s", size_conv(w));
303    
304     /* Network */
305     network_stat_ptr = stats.network_stats;
306     for(counter=0;counter<stats.network_entries;counter++){
307     move(11+counter, 42);
308     printw("%s", network_stat_ptr->interface_name);
309     move(11+counter, 62);
310     rt = (network_stat_ptr->systime)? (network_stat_ptr->rx / network_stat_ptr->systime): network_stat_ptr->rx;
311     printw("%7s", size_conv(rt));
312     move(11+counter, 72);
313     wt = (network_stat_ptr->systime)? (network_stat_ptr->tx / network_stat_ptr->systime): network_stat_ptr->tx;
314     printw("%7s", size_conv(wt));
315     network_stat_ptr++;
316     }
317    
318     disk_stat_ptr = stats.disk_stats;
319     for(counter=0;counter<stats.disk_entries;counter++){
320     move(13+stats.network_entries+counter, 42);
321     printw("%s", disk_stat_ptr->mnt_point);
322     move(13+stats.network_entries+counter, 62);
323     printw("%7s", size_conv(disk_stat_ptr->avail));
324     move(13+stats.network_entries+counter, 73);
325     printw("%5.2f%%", 100.00 * ((float)disk_stat_ptr->used / (float)disk_stat_ptr->size));
326     disk_stat_ptr++;
327     }
328    
329     refresh();
330     }
331    
332     void sig_winch_handler(int sig){
333     clear();
334     display_headings();
335     display_data();
336     signal(SIGWINCH, sig_winch_handler);
337     }
338    
339     int get_stats(){
340     if((stats.cpu_percents = cpu_percent_usage()) == NULL) return 0;
341     if((stats.mem_stats = get_memory_stats()) == NULL) return 0;
342     if((stats.swap_stats = get_swap_stats()) == NULL) return 0;
343     if((stats.load_stats = get_load_stats()) == NULL) return 0;
344     if((stats.process_stats = get_process_stats()) == NULL) return 0;
345     if((stats.page_stats = get_page_stats_diff()) == NULL) return 0;
346     if((stats.network_stats = get_network_stats_diff(&(stats.network_entries))) == NULL) return 0;
347     if((stats.diskio_stats = get_diskio_stats_diff(&(stats.diskio_entries))) == NULL) return 0;
348     if((stats.disk_stats = get_disk_stats(&(stats.disk_entries))) == NULL) return 0;
349     if((stats.general_stats = get_general_stats()) == NULL) return 0;
350     if((stats.user_stats = get_user_stats()) == NULL) return 0;
351    
352     return 1;
353     }
354    
355 pajs 1.3 void version_num(char *progname){
356     fprintf(stderr, "%s version %s\n", PACKAGE_VERSION);
357     fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
358     exit(1);
359     }
360    
361     void usage(char *progname){
362     fprintf(stderr, "Usage: %s [-d delay] [-v] [-h]\n\n", progname);
363     fprintf(stderr, " -d Sets the update time in seconds\n");
364     fprintf(stderr, " -v Prints version number\n");
365     fprintf(stderr, " -h Displays this help information.\n");
366     fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
367     exit(1);
368    
369     }
370    
371 pajs 1.1 int main(int argc, char **argv){
372    
373     extern char *optarg;
374     extern int optind;
375     int c;
376    
377     WINDOW *window;
378    
379     int stdin_fileno;
380     fd_set infds;
381     struct timeval timeout;
382    
383     extern int errno;
384     char ch;
385    
386     int delay=2;
387    
388     while ((c = getopt(argc, argv, "d:")) != EOF){
389     switch (c){
390     case 'd':
391     delay = atoi(optarg);
392     if (delay == 0){
393     fprintf(stderr, "Time must be 1 second or greater\n");
394     exit(1);
395     }
396     delay--;
397     break;
398 pajs 1.3 case 'v':
399     version_num(argv[0]);
400     break;
401     case 'h':
402     default:
403     usage(argv[0]);
404     return 1;
405     break;
406    
407 pajs 1.1 }
408     }
409    
410     signal(SIGWINCH, sig_winch_handler);
411     initscr();
412     nonl();
413     cbreak();
414     noecho();
415     window=newwin(0, 0, 0, 0);
416     clear();
417    
418     if(!get_stats()){
419     fprintf(stderr, "Failed to get all the stats. Please check correct permissions\n");
420     endwin();
421     return 1;
422     }
423    
424     display_headings();
425     stdin_fileno=fileno(stdin);
426    
427     for(;;){
428    
429     FD_ZERO(&infds);
430     FD_SET(stdin_fileno, &infds);
431     timeout.tv_sec = delay;
432     timeout.tv_usec = 0;
433    
434     if((select((stdin_fileno+1), &infds, NULL, NULL, &timeout)) == -1){
435     if(errno!=EINTR){
436     perror("select failed");
437     exit(1);
438     }
439     }
440    
441     if(FD_ISSET(stdin_fileno, &infds)){
442     ch=getch();
443     if (ch == 'q'){
444     endwin();
445     return 0;
446     }
447     }
448    
449     get_stats();
450    
451     display_data();
452    
453     /* To keep the numbers slightly accurate we do not want them updating more
454     * frequently than once a second.
455     */
456     sleep(1);
457    
458     }
459    
460    
461    
462     endwin();
463     return 0;
464     }