1 |
|
/* |
2 |
|
* i-scream central monitoring system |
3 |
< |
* http://www.i-scream.org.uk |
4 |
< |
* Copyright (C) 2000-2002 i-scream |
3 |
> |
* http://www.i-scream.org |
4 |
> |
* Copyright (C) 2000-2003 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 |
296 |
|
static diskio_stat_t *diskio_stats=NULL; |
297 |
|
static int num_diskio=0; |
298 |
|
|
299 |
+ |
#ifdef LINUX |
300 |
+ |
typedef struct { |
301 |
+ |
int major; |
302 |
+ |
int minor; |
303 |
+ |
} partition; |
304 |
+ |
#endif |
305 |
+ |
|
306 |
|
diskio_stat_t *get_diskio_stats(int *entries){ |
307 |
|
|
308 |
|
static int sizeof_diskio_stats=0; |
318 |
|
char *line_ptr; |
319 |
|
int major, minor; |
320 |
|
char dev_letter; |
321 |
+ |
int has_pp_stats = 1; |
322 |
+ |
static partition *parts = NULL; |
323 |
+ |
static int alloc_parts = 0; |
324 |
+ |
int i, n; |
325 |
+ |
time_t now; |
326 |
|
#endif |
327 |
|
#ifdef FREEBSD |
328 |
< |
struct statinfo stats; |
328 |
> |
static struct statinfo stats; |
329 |
> |
static int stats_init = 0; |
330 |
|
int counter; |
331 |
|
struct device_selection *dev_sel = NULL; |
332 |
|
int n_selected, n_selections; |
336 |
|
num_diskio=0; |
337 |
|
|
338 |
|
#ifdef FREEBSD |
339 |
< |
stats.dinfo=malloc(sizeof(struct devinfo)); |
340 |
< |
if(stats.dinfo==NULL) return NULL; |
339 |
> |
if (!stats_init) { |
340 |
> |
stats.dinfo=malloc(sizeof(struct devinfo)); |
341 |
> |
bzero(stats.dinfo, sizeof(struct devinfo)); |
342 |
> |
if(stats.dinfo==NULL) return NULL; |
343 |
> |
stats_init = 1; |
344 |
> |
} |
345 |
> |
#ifdef FREEBSD5 |
346 |
> |
if ((devstat_getdevs(NULL, &stats)) < 0) return NULL; |
347 |
> |
/* Not aware of a get all devices, so i said 999. If we ever * find a machine with more than 999 disks, then i'll change * this number :) */ |
348 |
> |
if (devstat_selectdevs(&dev_sel, &n_selected, &n_selections, &sel_gen, stats.dinfo->generation, stats.dinfo->devices, stats.dinfo->numdevs, NULL, 0, NULL, 0, DS_SELECT_ONLY, 999, 1) < 0) return NULL; |
349 |
> |
#else |
350 |
|
if ((getdevs(&stats)) < 0) return NULL; |
351 |
|
/* Not aware of a get all devices, so i said 999. If we ever |
352 |
|
* find a machine with more than 999 disks, then i'll change |
353 |
|
* this number :) |
354 |
|
*/ |
355 |
|
if (selectdevs(&dev_sel, &n_selected, &n_selections, &sel_gen, stats.dinfo->generation, stats.dinfo->devices, stats.dinfo->numdevs, NULL, 0, NULL, 0, DS_SELECT_ONLY, 999, 1) < 0) return NULL; |
356 |
+ |
#endif |
357 |
|
|
358 |
|
for(counter=0;counter<stats.dinfo->numdevs;counter++){ |
359 |
|
dev_ptr=&stats.dinfo->devices[dev_sel[counter].position]; |
362 |
|
* devices.. like mem, proc.. and also doesn't report floppy |
363 |
|
* drives etc unless they are doing stuff :) |
364 |
|
*/ |
365 |
+ |
#ifdef FREEBSD5 |
366 |
+ |
if((dev_ptr->bytes[DEVSTAT_READ]==0) && (dev_ptr->bytes[DEVSTAT_WRITE]==0)) continue; |
367 |
+ |
#else |
368 |
|
if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue; |
369 |
+ |
#endif |
370 |
|
if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){ |
371 |
|
return NULL; |
372 |
|
} |
373 |
|
diskio_stats_ptr=diskio_stats+num_diskio; |
374 |
< |
|
374 |
> |
|
375 |
> |
#ifdef FREEBSD5 |
376 |
> |
diskio_stats_ptr->read_bytes=dev_ptr->bytes[DEVSTAT_READ]; |
377 |
> |
diskio_stats_ptr->write_bytes=dev_ptr->bytes[DEVSTAT_WRITE]; |
378 |
> |
#else |
379 |
|
diskio_stats_ptr->read_bytes=dev_ptr->bytes_read; |
380 |
|
diskio_stats_ptr->write_bytes=dev_ptr->bytes_written; |
381 |
+ |
#endif |
382 |
|
if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name); |
383 |
|
asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number); |
384 |
|
diskio_stats_ptr->systime=time(NULL); |
386 |
|
num_diskio++; |
387 |
|
} |
388 |
|
free(dev_sel); |
357 |
– |
free(stats.dinfo); |
389 |
|
|
390 |
|
#endif |
391 |
|
#ifdef SOLARIS |
424 |
|
#endif |
425 |
|
|
426 |
|
#ifdef LINUX |
427 |
< |
f=fopen("/proc/stat", "r"); |
428 |
< |
if(f==NULL){ |
429 |
< |
*entries=0; |
430 |
< |
fclose(f); |
431 |
< |
return NULL; |
432 |
< |
} |
433 |
< |
if((line_ptr=f_read_line(f, "disk_io:"))==NULL){ |
434 |
< |
*entries=0; |
435 |
< |
fclose(f); |
436 |
< |
return NULL; |
437 |
< |
} |
438 |
< |
while((line_ptr=strchr(line_ptr, ' '))!=NULL){ |
439 |
< |
line_ptr++; |
440 |
< |
if(*line_ptr=='\0'){ |
441 |
< |
break; |
427 |
> |
num_diskio = 0; |
428 |
> |
n = 0; |
429 |
> |
|
430 |
> |
/* Read /proc/partitions to find what devices exist. Recent 2.4 kernels |
431 |
> |
have statistics in here too, so we can use those directly. */ |
432 |
> |
|
433 |
> |
f = fopen("/proc/partitions", "r"); |
434 |
> |
if (f == NULL) goto out; |
435 |
> |
now = time(NULL); |
436 |
> |
|
437 |
> |
while ((line_ptr = f_read_line(f, "")) != NULL) { |
438 |
> |
char name[20]; |
439 |
> |
char *s; |
440 |
> |
long long rsect, wsect; |
441 |
> |
|
442 |
> |
int nr = sscanf(line_ptr, |
443 |
> |
" %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld", |
444 |
> |
&major, &minor, name, &rsect, &wsect); |
445 |
> |
if (nr < 3) continue; |
446 |
> |
if (nr < 5) { |
447 |
> |
has_pp_stats = 0; |
448 |
> |
rsect = 0; |
449 |
> |
wsect = 0; |
450 |
|
} |
412 |
– |
if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){ |
413 |
– |
fclose(f); |
414 |
– |
*entries=0; |
415 |
– |
return NULL; |
416 |
– |
} |
417 |
– |
diskio_stats_ptr=diskio_stats+num_diskio; |
451 |
|
|
452 |
+ |
/* Skip device names ending in numbers, since they're |
453 |
+ |
partitions. */ |
454 |
+ |
s = name; |
455 |
+ |
while (*s != '\0') s++; |
456 |
+ |
--s; |
457 |
+ |
if (*s >= '0' && *s <= '9') continue; |
458 |
|
|
459 |
< |
if((sscanf(line_ptr, "(%d,%d):(%*d, %*d, %lld, %*d, %lld)", \ |
460 |
< |
&major, \ |
461 |
< |
&minor, \ |
462 |
< |
&diskio_stats_ptr->read_bytes, \ |
463 |
< |
&diskio_stats_ptr->write_bytes))!=4) { |
464 |
< |
continue; |
459 |
> |
diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats, |
460 |
> |
diskio_stats); |
461 |
> |
if (diskio_stats == NULL) goto out; |
462 |
> |
if (n >= alloc_parts) { |
463 |
> |
alloc_parts += 16; |
464 |
> |
parts = realloc(parts, alloc_parts * sizeof *parts); |
465 |
> |
if (parts == NULL) { |
466 |
> |
alloc_parts = 0; |
467 |
> |
goto out; |
468 |
> |
} |
469 |
|
} |
470 |
|
|
471 |
< |
/* We read the number of blocks. Blocks are stored in 512 bytes */ |
472 |
< |
diskio_stats_ptr->read_bytes=diskio_stats_ptr->read_bytes*512; |
473 |
< |
diskio_stats_ptr->write_bytes=diskio_stats_ptr->write_bytes*512; |
471 |
> |
if (diskio_stats[n].disk_name != NULL) |
472 |
> |
free(diskio_stats[n].disk_name); |
473 |
> |
diskio_stats[n].disk_name = strdup(name); |
474 |
> |
diskio_stats[n].read_bytes = rsect * 512; |
475 |
> |
diskio_stats[n].write_bytes = wsect * 512; |
476 |
> |
diskio_stats[n].systime = now; |
477 |
> |
parts[n].major = major; |
478 |
> |
parts[n].minor = minor; |
479 |
|
|
480 |
< |
if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name); |
480 |
> |
n++; |
481 |
> |
} |
482 |
|
|
483 |
< |
switch(major){ |
484 |
< |
case 2: |
485 |
< |
if(minor==0){ |
437 |
< |
diskio_stats_ptr->disk_name=strdup("fd0"); |
438 |
< |
} |
439 |
< |
break; |
483 |
> |
if (!has_pp_stats) { |
484 |
> |
/* This is an older kernel without stats in /proc/partitions. |
485 |
> |
Read what we can from /proc/stat instead. */ |
486 |
|
|
487 |
< |
case 3: |
488 |
< |
if(minor==0){ |
489 |
< |
diskio_stats_ptr->disk_name=strdup("hda"); |
490 |
< |
}else{ |
491 |
< |
diskio_stats_ptr->disk_name=strdup("hdb"); |
492 |
< |
} |
493 |
< |
break; |
487 |
> |
f = fopen("/proc/stat", "r"); |
488 |
> |
if (f == NULL) goto out; |
489 |
> |
now = time(NULL); |
490 |
> |
|
491 |
> |
line_ptr = f_read_line(f, "disk_io:"); |
492 |
> |
if (line_ptr == NULL) goto out; |
493 |
> |
|
494 |
> |
while((line_ptr=strchr(line_ptr, ' '))!=NULL){ |
495 |
> |
long long rsect, wsect; |
496 |
|
|
497 |
+ |
if (*++line_ptr == '\0') break; |
498 |
+ |
|
499 |
+ |
if((sscanf(line_ptr, |
500 |
+ |
"(%d,%d):(%*d, %*d, %lld, %*d, %lld)", |
501 |
+ |
&major, &minor, &rsect, &wsect)) != 4) { |
502 |
+ |
continue; |
503 |
+ |
} |
504 |
+ |
|
505 |
+ |
/* Find the corresponding device from earlier. |
506 |
+ |
Just to add to the fun, "minor" is actually the disk |
507 |
+ |
number, not the device minor, so we need to figure |
508 |
+ |
out the real minor number based on the major! |
509 |
+ |
This list is not exhaustive; if you're running |
510 |
+ |
an older kernel you probably don't have fancy |
511 |
+ |
I2O hardware anyway... */ |
512 |
+ |
switch (major) { |
513 |
+ |
case 3: |
514 |
+ |
case 21: |
515 |
|
case 22: |
516 |
< |
if(minor==0){ |
517 |
< |
diskio_stats_ptr->disk_name=strdup("hdc"); |
518 |
< |
}else{ |
519 |
< |
diskio_stats_ptr->disk_name=strdup("hdd"); |
520 |
< |
} |
516 |
> |
case 33: |
517 |
> |
case 34: |
518 |
> |
case 36: |
519 |
> |
case 56: |
520 |
> |
case 57: |
521 |
> |
case 88: |
522 |
> |
case 89: |
523 |
> |
case 90: |
524 |
> |
case 91: |
525 |
> |
minor *= 64; |
526 |
|
break; |
527 |
< |
case 8: |
528 |
< |
dev_letter='a'+(minor/16); |
458 |
< |
diskio_stats_ptr->disk_name=malloc(4); |
459 |
< |
snprintf(diskio_stats_ptr->disk_name, 4, "sd%c", dev_letter); |
527 |
> |
case 9: |
528 |
> |
case 43: |
529 |
|
break; |
530 |
|
default: |
531 |
< |
/* I have no idea what it is then :) */ |
463 |
< |
diskio_stats_ptr->disk_name=malloc(16); |
464 |
< |
snprintf(diskio_stats_ptr->disk_name, 16, "%d %d", major, minor); |
531 |
> |
minor *= 16; |
532 |
|
break; |
533 |
< |
} |
533 |
> |
} |
534 |
> |
for (i = 0; i < n; i++) { |
535 |
> |
if (major == parts[i].major |
536 |
> |
&& minor == parts[i].minor) |
537 |
> |
break; |
538 |
> |
} |
539 |
> |
if (i == n) continue; |
540 |
|
|
541 |
< |
diskio_stats_ptr->systime=time(NULL); |
542 |
< |
num_diskio++; |
541 |
> |
/* We read the number of blocks. Blocks are stored in |
542 |
> |
512 bytes */ |
543 |
> |
diskio_stats[i].read_bytes = rsect * 512; |
544 |
> |
diskio_stats[i].write_bytes = wsect * 512; |
545 |
> |
diskio_stats[i].systime = now; |
546 |
> |
} |
547 |
|
} |
548 |
|
|
549 |
< |
fclose(f); |
549 |
> |
num_diskio = n; |
550 |
> |
out: |
551 |
> |
if (f != NULL) fclose(f); |
552 |
|
|
553 |
|
#endif |
554 |
|
*entries=num_diskio; |