25 |
|
#include <stdio.h> |
26 |
|
#include <stdlib.h> |
27 |
|
#include <string.h> |
28 |
+ |
#include <time.h> |
29 |
|
#include "statgrab.h" |
30 |
|
|
31 |
|
#ifdef SOLARIS |
35 |
|
#define VALID_FS_TYPES {"ufs", "tmpfs"} |
36 |
|
#endif |
37 |
|
|
38 |
< |
#ifdef LINUX |
38 |
< |
#include <time.h> |
39 |
< |
#include <sys/vfs.h> |
38 |
> |
#if defined(LINUX) || defined(CYGWIN) |
39 |
|
#include <mntent.h> |
40 |
+ |
#include <sys/vfs.h> |
41 |
|
#include "tools.h" |
42 |
+ |
#endif |
43 |
+ |
|
44 |
+ |
#ifdef LINUX |
45 |
|
#define VALID_FS_TYPES {"adfs", "affs", "befs", "bfs", "efs", "ext2", \ |
46 |
|
"ext3", "vxfs", "hfs", "hfsplus", "hpfs", "jffs", \ |
47 |
|
"jffs2", "minix", "msdos", "ntfs", "qnx4", "ramfs", \ |
49 |
|
"umsdos", "vfat", "xfs", "jfs"} |
50 |
|
#endif |
51 |
|
|
52 |
+ |
#ifdef CYGWIN |
53 |
+ |
#define VALID_FS_TYPES {"user"} |
54 |
+ |
#endif |
55 |
+ |
|
56 |
|
#ifdef ALLBSD |
57 |
|
#include <sys/param.h> |
58 |
|
#include <sys/ucred.h> |
59 |
|
#include <sys/mount.h> |
53 |
– |
#define VALID_FS_TYPES {"ufs", "mfs", "ffs"} |
60 |
|
#endif |
61 |
|
#ifdef FREEBSD |
62 |
|
#include <sys/dkstat.h> |
63 |
|
#include <devstat.h> |
64 |
+ |
#define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \ |
65 |
+ |
"ufs", "mfs"} |
66 |
|
#endif |
67 |
+ |
#ifdef NETBSD |
68 |
+ |
#include <sys/param.h> |
69 |
+ |
#include <sys/sysctl.h> |
70 |
+ |
#include <sys/disk.h> |
71 |
+ |
#define VALID_FS_TYPES {"ffs", "mfs", "msdos", "lfs", "adosfs", "ext2fs", \ |
72 |
+ |
"ntfs"} |
73 |
+ |
#endif |
74 |
|
|
75 |
|
#define START_VAL 1 |
76 |
|
|
106 |
|
char *fs_types[] = VALID_FS_TYPES; |
107 |
|
int x, valid_type; |
108 |
|
int num_disks=0; |
109 |
< |
#if defined(LINUX) || defined (SOLARIS) |
109 |
> |
#if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN) |
110 |
|
FILE *f; |
111 |
|
#endif |
112 |
|
|
116 |
|
struct mnttab mp; |
117 |
|
struct statvfs fs; |
118 |
|
#endif |
119 |
< |
#ifdef LINUX |
119 |
> |
#if defined(LINUX) || defined(CYGWIN) |
120 |
|
struct mntent *mp; |
121 |
|
struct statfs fs; |
122 |
|
#endif |
148 |
|
} |
149 |
|
#endif |
150 |
|
|
151 |
< |
#ifdef LINUX |
151 |
> |
#if defined(LINUX) || defined(CYGWIN) |
152 |
|
if ((f=setmntent("/etc/mtab", "r" ))==NULL){ |
153 |
|
return NULL; |
154 |
|
} |
219 |
|
/* Freebsd doesn't have a "available" inodes */ |
220 |
|
disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes; |
221 |
|
#endif |
222 |
< |
#ifdef LINUX |
222 |
> |
#if defined(LINUX) || defined(CYGWIN) |
223 |
|
if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){ |
224 |
|
return NULL; |
225 |
|
} |
273 |
|
|
274 |
|
*entries=num_disks; |
275 |
|
|
276 |
< |
/* If this fails, there is very little i can do about it, so i'll ignore it :) */ |
277 |
< |
#if defined(LINUX) || defined(SOLARIS) |
276 |
> |
/* If this fails, there is very little i can do about it, so |
277 |
> |
I'll ignore it :) */ |
278 |
> |
#if defined(LINUX) || defined(CYGWIN) |
279 |
> |
endmntent(f); |
280 |
> |
#endif |
281 |
> |
#if defined(SOLARIS) |
282 |
|
fclose(f); |
283 |
|
#endif |
284 |
|
|
333 |
|
diskio_stat_t *get_diskio_stats(int *entries){ |
334 |
|
|
335 |
|
static int sizeof_diskio_stats=0; |
336 |
+ |
#ifndef LINUX |
337 |
|
diskio_stat_t *diskio_stats_ptr; |
338 |
+ |
#endif |
339 |
|
|
340 |
|
#ifdef SOLARIS |
341 |
|
kstat_ctl_t *kc; |
346 |
|
FILE *f; |
347 |
|
char *line_ptr; |
348 |
|
int major, minor; |
328 |
– |
char dev_letter; |
349 |
|
int has_pp_stats = 1; |
350 |
|
static partition *parts = NULL; |
351 |
|
static int alloc_parts = 0; |
352 |
|
int i, n; |
353 |
|
time_t now; |
354 |
+ |
const char *format; |
355 |
|
#endif |
356 |
|
#ifdef FREEBSD |
357 |
|
static struct statinfo stats; |
363 |
|
struct devstat *dev_ptr; |
364 |
|
#endif |
365 |
|
#ifdef NETBSD |
366 |
< |
/* FIXME get_diskio_stats NYI on NetBSD. |
367 |
< |
* See vmstat/dkstats.c in NetBSD source for examples. |
368 |
< |
*/ |
366 |
> |
struct disk_sysctl *stats; |
367 |
> |
int num_disks, i; |
368 |
> |
int mib[3]; |
369 |
> |
size_t size; |
370 |
|
#endif |
371 |
+ |
|
372 |
|
num_diskio=0; |
373 |
|
|
374 |
+ |
#ifdef NETBSD |
375 |
+ |
mib[0] = CTL_HW; |
376 |
+ |
mib[1] = HW_DISKSTATS; |
377 |
+ |
mib[2] = sizeof(struct disk_sysctl); |
378 |
+ |
|
379 |
+ |
if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) { |
380 |
+ |
return NULL; |
381 |
+ |
} |
382 |
+ |
num_disks = size / sizeof(struct disk_sysctl); |
383 |
+ |
|
384 |
+ |
stats = malloc(size); |
385 |
+ |
if (stats == NULL) { |
386 |
+ |
return NULL; |
387 |
+ |
} |
388 |
+ |
|
389 |
+ |
if (sysctl(mib, 3, stats, &size, NULL, 0) < 0) { |
390 |
+ |
return NULL; |
391 |
+ |
} |
392 |
+ |
|
393 |
+ |
for (i = 0; i < num_disks; i++) { |
394 |
+ |
u_int64_t rbytes, wbytes; |
395 |
+ |
|
396 |
+ |
#ifdef HAVE_DK_RBYTES |
397 |
+ |
rbytes = stats[i].dk_rbytes; |
398 |
+ |
wbytes = stats[i].dk_wbytes; |
399 |
+ |
#else |
400 |
+ |
/* Before 1.7, NetBSD merged reads and writes. */ |
401 |
+ |
rbytes = wbytes = stats[i].dk_bytes; |
402 |
+ |
#endif |
403 |
+ |
|
404 |
+ |
/* Don't keep stats for disks that have never been used. */ |
405 |
+ |
if (rbytes == 0 && wbytes == 0) { |
406 |
+ |
continue; |
407 |
+ |
} |
408 |
+ |
|
409 |
+ |
diskio_stats = diskio_stat_malloc(num_diskio + 1, |
410 |
+ |
&sizeof_diskio_stats, |
411 |
+ |
diskio_stats); |
412 |
+ |
if (diskio_stats == NULL) { |
413 |
+ |
return NULL; |
414 |
+ |
} |
415 |
+ |
diskio_stats_ptr = diskio_stats + num_diskio; |
416 |
+ |
|
417 |
+ |
diskio_stats_ptr->read_bytes = rbytes; |
418 |
+ |
diskio_stats_ptr->write_bytes = wbytes; |
419 |
+ |
if (diskio_stats_ptr->disk_name != NULL) { |
420 |
+ |
free(diskio_stats_ptr->disk_name); |
421 |
+ |
} |
422 |
+ |
diskio_stats_ptr->disk_name = strdup(stats[i].dk_name); |
423 |
+ |
diskio_stats_ptr->systime = time(NULL); |
424 |
+ |
|
425 |
+ |
num_diskio++; |
426 |
+ |
} |
427 |
+ |
|
428 |
+ |
free(stats); |
429 |
+ |
#endif |
430 |
+ |
|
431 |
|
#ifdef FREEBSD |
432 |
|
if (!stats_init) { |
433 |
|
stats.dinfo=malloc(sizeof(struct devinfo)); |
354 |
– |
bzero(stats.dinfo, sizeof(struct devinfo)); |
434 |
|
if(stats.dinfo==NULL) return NULL; |
435 |
+ |
bzero(stats.dinfo, sizeof(struct devinfo)); |
436 |
|
stats_init = 1; |
437 |
|
} |
438 |
|
#ifdef FREEBSD5 |
524 |
|
n = 0; |
525 |
|
|
526 |
|
/* Read /proc/partitions to find what devices exist. Recent 2.4 kernels |
527 |
< |
have statistics in here too, so we can use those directly. */ |
527 |
> |
have statistics in here too, so we can use those directly. |
528 |
> |
2.6 kernels have /proc/diskstats instead with almost (but not quite) |
529 |
> |
the same format. */ |
530 |
|
|
531 |
< |
f = fopen("/proc/partitions", "r"); |
531 |
> |
f = fopen("/proc/diskstats", "r"); |
532 |
> |
format = " %d %d %19s %*d %*d %lld %*d %*d %*d %lld"; |
533 |
> |
if (f == NULL) { |
534 |
> |
f = fopen("/proc/partitions", "r"); |
535 |
> |
format = " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld"; |
536 |
> |
} |
537 |
|
if (f == NULL) goto out; |
538 |
|
now = time(NULL); |
539 |
|
|
542 |
|
char *s; |
543 |
|
long long rsect, wsect; |
544 |
|
|
545 |
< |
int nr = sscanf(line_ptr, |
459 |
< |
" %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld", |
545 |
> |
int nr = sscanf(line_ptr, format, |
546 |
|
&major, &minor, name, &rsect, &wsect); |
547 |
|
if (nr < 3) continue; |
462 |
– |
if (nr < 5) { |
463 |
– |
has_pp_stats = 0; |
464 |
– |
rsect = 0; |
465 |
– |
wsect = 0; |
466 |
– |
} |
548 |
|
|
549 |
|
/* Skip device names ending in numbers, since they're |
550 |
|
partitions. */ |
552 |
|
while (*s != '\0') s++; |
553 |
|
--s; |
554 |
|
if (*s >= '0' && *s <= '9') continue; |
555 |
+ |
|
556 |
+ |
if (nr < 5) { |
557 |
+ |
has_pp_stats = 0; |
558 |
+ |
rsect = 0; |
559 |
+ |
wsect = 0; |
560 |
+ |
} |
561 |
|
|
562 |
|
diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats, |
563 |
|
diskio_stats); |