ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/saidar/saidar.c
Revision: 1.7
Committed: Sat Oct 18 14:00:21 2003 UTC (21 years, 1 month ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.6: +12 -1 lines
Log Message:
Fix an annoying long hostname problem.

File Contents

# Content
1 /*
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 #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
38 #ifdef HAVE_NCURSES_H
39 #include <ncurses.h>
40 #else
41 #include <curses.h>
42 #endif
43
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 /* Size before it will start overwriting "uptime" */
209 char hostname[15];
210 char *ptr;
211
212 move(0,12);
213 strncpy(hostname, stats.general_stats->hostname, sizeof(hostname));
214 ptr=strchr(hostname, '.');
215 /* Some hosts give back a FQDN for hostname. To avoid this, we'll
216 * just blank out everything after the first "."
217 */
218 if (ptr != NULL){
219 *ptr = '\0';
220 }
221 printw("%s", hostname);
222 move(0,36);
223 printw("%s", hr_uptime(stats.general_stats->uptime));
224 epoc_time=time(NULL);
225 tm_time = localtime(&epoc_time);
226 strftime(cur_time, 20, "%Y-%m-%d %T", tm_time);
227 move(0,61);
228 printw("%s", cur_time);
229
230 /* Load */
231 move(2,12);
232 printw("%6.2f", stats.load_stats->min1);
233 move(3,12);
234 printw("%6.2f", stats.load_stats->min5);
235 move(4,12);
236 printw("%6.2f", stats.load_stats->min15);
237
238 /* CPU */
239 move(2,33);
240 printw("%6.2f%%", stats.cpu_percents->idle);
241 move(3,33);
242 printw("%6.2f%%", (stats.cpu_percents->kernel + stats.cpu_percents->iowait + stats.cpu_percents->swap));
243 move(4,33);
244 printw("%6.2f%%", (stats.cpu_percents->user + stats.cpu_percents->nice));
245
246 /* Process */
247 move(2, 54);
248 printw("%5d", stats.process_stats->running);
249 move(2,74);
250 printw("%5d", stats.process_stats->zombie);
251 move(3, 54);
252 printw("%5d", stats.process_stats->sleeping);
253 move(3, 74);
254 printw("%5d", stats.process_stats->total);
255 move(4, 54);
256 printw("%5d", stats.process_stats->stopped);
257 move(4,74);
258 printw("%5d", stats.user_stats->num_entries);
259
260 /* Mem */
261 move(6, 12);
262 printw("%7s", size_conv(stats.mem_stats->total));
263 move(7, 12);
264 printw("%7s", size_conv(stats.mem_stats->used));
265 move(8, 12);
266 printw("%7s", size_conv(stats.mem_stats->free));
267
268 /* Swap */
269 move(6, 32);
270 printw("%8s", size_conv(stats.swap_stats->total));
271 move(7, 32);
272 printw("%8s", size_conv(stats.swap_stats->used));
273 move(8, 32);
274 printw("%8s", size_conv(stats.swap_stats->free));
275
276 /* VM */
277 move(6, 54);
278 printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total));
279 move(7, 54);
280 printw("%5.2f%%", (100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total));
281 move(8, 54);
282 printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used+stats.swap_stats->used)/(stats.mem_stats->total+stats.swap_stats->total)));
283
284 /* Paging */
285 move(6, 74);
286 printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pagein / stats.page_stats->systime): stats.page_stats->pages_pagein);
287 move(7, 74);
288 printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pageout / stats.page_stats->systime) : stats.page_stats->pages_pageout);
289
290 /* Disk IO */
291 diskio_stat_ptr = stats.diskio_stats;
292 r=0;
293 w=0;
294 for(counter=0;counter<stats.diskio_entries;counter++){
295 move(11+counter, 0);
296 printw("%s", diskio_stat_ptr->disk_name);
297 move(11+counter, 12);
298 rt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->read_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->read_bytes;
299 printw("%7s", size_conv(rt));
300 r+=rt;
301 move(11+counter, 26);
302 wt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->write_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->write_bytes;
303 printw("%7s", size_conv(wt));
304 w+=wt;
305 diskio_stat_ptr++;
306 }
307 move(12+counter, 0);
308 printw("Total");
309 move(12+counter, 12);
310 printw("%7s", size_conv(r));
311 move(12+counter, 26);
312 printw("%7s", size_conv(w));
313
314 /* Network */
315 network_stat_ptr = stats.network_stats;
316 for(counter=0;counter<stats.network_entries;counter++){
317 move(11+counter, 42);
318 printw("%s", network_stat_ptr->interface_name);
319 move(11+counter, 62);
320 rt = (network_stat_ptr->systime)? (network_stat_ptr->rx / network_stat_ptr->systime): network_stat_ptr->rx;
321 printw("%7s", size_conv(rt));
322 move(11+counter, 72);
323 wt = (network_stat_ptr->systime)? (network_stat_ptr->tx / network_stat_ptr->systime): network_stat_ptr->tx;
324 printw("%7s", size_conv(wt));
325 network_stat_ptr++;
326 }
327
328 /* Disk */
329 disk_stat_ptr = stats.disk_stats;
330 for(counter=0;counter<stats.disk_entries;counter++){
331 move(13+stats.network_entries+counter, 42);
332 printw("%s", disk_stat_ptr->mnt_point);
333 move(13+stats.network_entries+counter, 62);
334 printw("%7s", size_conv(disk_stat_ptr->avail));
335 move(13+stats.network_entries+counter, 73);
336 printw("%5.2f%%", 100.00 * ((float) (disk_stat_ptr->size - disk_stat_ptr->avail) / (float) disk_stat_ptr->size));
337 disk_stat_ptr++;
338 }
339
340 refresh();
341 }
342
343 void sig_winch_handler(int sig){
344 clear();
345 display_headings();
346 display_data();
347 signal(SIGWINCH, sig_winch_handler);
348 }
349
350 int get_stats(){
351 if((stats.cpu_percents = cpu_percent_usage()) == NULL) return 0;
352 if((stats.mem_stats = get_memory_stats()) == NULL) return 0;
353 if((stats.swap_stats = get_swap_stats()) == NULL) return 0;
354 if((stats.load_stats = get_load_stats()) == NULL) return 0;
355 if((stats.process_stats = get_process_stats()) == NULL) return 0;
356 if((stats.page_stats = get_page_stats_diff()) == NULL) return 0;
357 if((stats.network_stats = get_network_stats_diff(&(stats.network_entries))) == NULL) return 0;
358 if((stats.diskio_stats = get_diskio_stats_diff(&(stats.diskio_entries))) == NULL) return 0;
359 if((stats.disk_stats = get_disk_stats(&(stats.disk_entries))) == NULL) return 0;
360 if((stats.general_stats = get_general_stats()) == NULL) return 0;
361 if((stats.user_stats = get_user_stats()) == NULL) return 0;
362
363 return 1;
364 }
365
366 void version_num(char *progname){
367 fprintf(stderr, "%s version %s\n", progname, PACKAGE_VERSION);
368 fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
369 exit(1);
370 }
371
372 void usage(char *progname){
373 fprintf(stderr, "Usage: %s [-d delay] [-v] [-h]\n\n", progname);
374 fprintf(stderr, " -d Sets the update time in seconds\n");
375 fprintf(stderr, " -v Prints version number\n");
376 fprintf(stderr, " -h Displays this help information.\n");
377 fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
378 exit(1);
379
380 }
381
382 int main(int argc, char **argv){
383
384 extern char *optarg;
385 extern int optind;
386 int c;
387
388 WINDOW *window;
389
390 int stdin_fileno;
391 fd_set infds;
392 struct timeval timeout;
393
394 extern int errno;
395 char ch;
396
397 int delay=2;
398
399 while ((c = getopt(argc, argv, "vhd:")) != EOF){
400 switch (c){
401 case 'd':
402 delay = atoi(optarg);
403 if (delay == 0){
404 fprintf(stderr, "Time must be 1 second or greater\n");
405 exit(1);
406 }
407 delay--;
408 break;
409 case 'v':
410 version_num(argv[0]);
411 break;
412 case 'h':
413 default:
414 usage(argv[0]);
415 return 1;
416 break;
417
418 }
419 }
420
421 signal(SIGWINCH, sig_winch_handler);
422 initscr();
423 nonl();
424 cbreak();
425 noecho();
426 window=newwin(0, 0, 0, 0);
427 clear();
428
429 if(!get_stats()){
430 fprintf(stderr, "Failed to get all the stats. Please check correct permissions\n");
431 endwin();
432 return 1;
433 }
434
435 display_headings();
436 stdin_fileno=fileno(stdin);
437
438 for(;;){
439
440 FD_ZERO(&infds);
441 FD_SET(stdin_fileno, &infds);
442 timeout.tv_sec = delay;
443 timeout.tv_usec = 0;
444
445 if((select((stdin_fileno+1), &infds, NULL, NULL, &timeout)) == -1){
446 if(errno!=EINTR){
447 perror("select failed");
448 exit(1);
449 }
450 }
451
452 if(FD_ISSET(stdin_fileno, &infds)){
453 ch=getch();
454 if (ch == 'q'){
455 endwin();
456 return 0;
457 }
458 }
459
460 get_stats();
461
462 display_data();
463
464 /* To keep the numbers slightly accurate we do not want them updating more
465 * frequently than once a second.
466 */
467 sleep(1);
468
469 }
470
471
472
473 endwin();
474 return 0;
475 }