1 |
< |
/* |
1 |
> |
/* |
2 |
|
* i-scream central monitoring system |
3 |
|
* http://www.i-scream.org |
4 |
< |
* Copyright (C) 2000-2003 i-scream |
4 |
> |
* Copyright (C) 2000-2004 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. |
6 |
> |
* This library is free software; you can redistribute it and/or |
7 |
> |
* modify it under the terms of the GNU Lesser General Public |
8 |
> |
* License as published by the Free Software Foundation; either |
9 |
> |
* version 2.1 of the License, or (at your option) any later version. |
10 |
|
* |
11 |
< |
* This program is distributed in the hope that it will be useful, |
11 |
> |
* This library 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. |
13 |
> |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
> |
* Lesser 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. |
16 |
> |
* You should have received a copy of the GNU Lesser General Public |
17 |
> |
* License along with this library; if not, write to the Free Software |
18 |
> |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
19 |
> |
* 02111-1307 USA |
20 |
> |
* |
21 |
> |
* $Id$ |
22 |
|
*/ |
23 |
|
|
24 |
|
#ifdef HAVE_CONFIG_H |
28 |
|
#include <stdio.h> |
29 |
|
#include <stdlib.h> |
30 |
|
#include <string.h> |
31 |
+ |
#include <time.h> |
32 |
|
#include "statgrab.h" |
33 |
|
|
34 |
|
#ifdef SOLARIS |
35 |
|
#include <sys/mnttab.h> |
36 |
|
#include <sys/statvfs.h> |
37 |
|
#include <kstat.h> |
34 |
– |
#include <sys/types.h> |
35 |
– |
#include <dirent.h> |
36 |
– |
#include <limits.h> |
37 |
– |
#include <unistd.h> |
38 |
|
#define VALID_FS_TYPES {"ufs", "tmpfs"} |
39 |
|
#endif |
40 |
|
|
41 |
< |
#ifdef LINUX |
42 |
< |
#include <sys/vfs.h> |
41 |
> |
#if defined(LINUX) || defined(CYGWIN) |
42 |
|
#include <mntent.h> |
43 |
+ |
#include <sys/vfs.h> |
44 |
|
#include "tools.h" |
45 |
– |
#define VALID_FS_TYPES {"ext2", "ext3", "xfs", "reiserfs", "vfat", "tmpfs"} |
45 |
|
#endif |
46 |
|
|
47 |
< |
#ifdef FREEBSD |
47 |
> |
#ifdef LINUX |
48 |
> |
#define VALID_FS_TYPES {"adfs", "affs", "befs", "bfs", "efs", "ext2", \ |
49 |
> |
"ext3", "vxfs", "hfs", "hfsplus", "hpfs", "jffs", \ |
50 |
> |
"jffs2", "minix", "msdos", "ntfs", "qnx4", "ramfs", \ |
51 |
> |
"rootfs", "reiserfs", "sysv", "v7", "udf", "ufs", \ |
52 |
> |
"umsdos", "vfat", "xfs", "jfs"} |
53 |
> |
#endif |
54 |
> |
|
55 |
> |
#ifdef CYGWIN |
56 |
> |
#define VALID_FS_TYPES {"user"} |
57 |
> |
#endif |
58 |
> |
|
59 |
> |
#ifdef ALLBSD |
60 |
|
#include <sys/param.h> |
61 |
|
#include <sys/ucred.h> |
62 |
|
#include <sys/mount.h> |
63 |
+ |
#endif |
64 |
+ |
#ifdef FREEBSD |
65 |
|
#include <sys/dkstat.h> |
66 |
|
#include <devstat.h> |
67 |
< |
#define VALID_FS_TYPES {"ufs", "mfs"} |
67 |
> |
#define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \ |
68 |
> |
"ufs", "mfs"} |
69 |
|
#endif |
70 |
< |
#define START_VAL 1 |
71 |
< |
|
72 |
< |
/* Solaris kernel stores the disk names in the old BSD format |
73 |
< |
* and we would prefer it in th modern format. |
74 |
< |
*/ |
75 |
< |
#ifdef SOLARIS |
62 |
< |
/* Lines from the path_to_inst file */ |
63 |
< |
typedef struct { |
64 |
< |
/* unlikely to get a line that long */ |
65 |
< |
char line[256]; |
66 |
< |
}file_line_t; |
67 |
< |
|
68 |
< |
/* Linked list of the mappings from the older bsd-based sunos names |
69 |
< |
* to the modern solaris names |
70 |
< |
*/ |
71 |
< |
struct disk_mapping_ent_t{ |
72 |
< |
char *sunos_name; |
73 |
< |
char *solaris_name; |
74 |
< |
struct disk_mapping_ent_t *next; |
75 |
< |
}; |
76 |
< |
typedef struct disk_mapping_ent_t disk_mapping_t; |
70 |
> |
#if defined(NETBSD) || defined(OPENBSD) |
71 |
> |
#include <sys/param.h> |
72 |
> |
#include <sys/sysctl.h> |
73 |
> |
#include <sys/disk.h> |
74 |
> |
#define VALID_FS_TYPES {"ffs", "mfs", "msdos", "lfs", "adosfs", "ext2fs", \ |
75 |
> |
"ntfs"} |
76 |
|
#endif |
77 |
|
|
78 |
+ |
#define START_VAL 1 |
79 |
+ |
|
80 |
|
char *copy_string(char *orig_ptr, const char *newtext){ |
81 |
|
|
82 |
|
/* Maybe free if not NULL, and strdup rather than realloc and strcpy? */ |
101 |
|
} |
102 |
|
} |
103 |
|
|
104 |
+ |
int is_valid_fs_type(const char *type) { |
105 |
+ |
const char *types[] = VALID_FS_TYPES; |
106 |
+ |
int i; |
107 |
+ |
|
108 |
+ |
for (i = 0; i < (sizeof types / sizeof *types); i++) { |
109 |
+ |
if (strcmp(types[i], type) == 0) { |
110 |
+ |
return 1; |
111 |
+ |
} |
112 |
+ |
} |
113 |
+ |
return 0; |
114 |
+ |
} |
115 |
+ |
|
116 |
|
disk_stat_t *get_disk_stats(int *entries){ |
117 |
|
|
118 |
|
static disk_stat_t *disk_stats; |
119 |
|
static int watermark=-1; |
120 |
|
|
121 |
< |
char *fs_types[] = VALID_FS_TYPES; |
109 |
< |
int x, valid_type; |
121 |
> |
int valid_type; |
122 |
|
int num_disks=0; |
123 |
< |
#if defined(LINUX) || defined (SOLARIS) |
123 |
> |
#if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN) |
124 |
|
FILE *f; |
125 |
|
#endif |
126 |
|
|
130 |
|
struct mnttab mp; |
131 |
|
struct statvfs fs; |
132 |
|
#endif |
133 |
< |
#ifdef LINUX |
133 |
> |
#if defined(LINUX) || defined(CYGWIN) |
134 |
|
struct mntent *mp; |
135 |
|
struct statfs fs; |
136 |
|
#endif |
137 |
< |
#ifdef FREEBSD |
137 |
> |
#ifdef ALLBSD |
138 |
|
int nummnt; |
139 |
|
struct statfs *mp; |
140 |
|
#endif |
147 |
|
watermark=START_VAL; |
148 |
|
init_disk_stat(0, watermark-1, disk_stats); |
149 |
|
} |
150 |
< |
#ifdef FREEBSD |
150 |
> |
#ifdef ALLBSD |
151 |
|
nummnt=getmntinfo(&mp , MNT_LOCAL); |
152 |
|
if (nummnt<=0){ |
153 |
|
return NULL; |
154 |
|
} |
155 |
|
for(;nummnt--; mp++){ |
156 |
< |
valid_type=0; |
145 |
< |
for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){ |
146 |
< |
if(strcmp(mp->f_fstypename, fs_types[x]) ==0){ |
147 |
< |
valid_type=1; |
148 |
< |
break; |
149 |
< |
} |
150 |
< |
} |
156 |
> |
valid_type = is_valid_fs_type(mp->f_fstypename); |
157 |
|
#endif |
158 |
|
|
159 |
< |
#ifdef LINUX |
159 |
> |
#if defined(LINUX) || defined(CYGWIN) |
160 |
|
if ((f=setmntent("/etc/mtab", "r" ))==NULL){ |
161 |
|
return NULL; |
162 |
|
} |
166 |
|
continue; |
167 |
|
} |
168 |
|
|
169 |
< |
valid_type=0; |
164 |
< |
for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){ |
165 |
< |
if(strcmp(mp->mnt_type, fs_types[x]) ==0){ |
166 |
< |
valid_type=1; |
167 |
< |
break; |
168 |
< |
} |
169 |
< |
} |
169 |
> |
valid_type = is_valid_fs_type(mp->mnt_type); |
170 |
|
#endif |
171 |
|
|
172 |
|
#ifdef SOLARIS |
177 |
|
if ((statvfs(mp.mnt_mountp, &fs)) !=0){ |
178 |
|
continue; |
179 |
|
} |
180 |
< |
valid_type=0; |
181 |
< |
for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){ |
182 |
< |
if(strcmp(mp.mnt_fstype, fs_types[x]) ==0){ |
183 |
< |
valid_type=1; |
184 |
< |
break; |
185 |
< |
} |
186 |
< |
} |
180 |
> |
valid_type = is_valid_fs_type(mp.mnt_fstype); |
181 |
|
#endif |
182 |
|
|
183 |
|
if(valid_type){ |
193 |
|
} |
194 |
|
|
195 |
|
disk_ptr=disk_stats+num_disks; |
196 |
< |
#ifdef FREEBSD |
196 |
> |
#ifdef ALLBSD |
197 |
|
if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){ |
198 |
|
return NULL; |
199 |
|
} |
215 |
|
/* Freebsd doesn't have a "available" inodes */ |
216 |
|
disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes; |
217 |
|
#endif |
218 |
< |
#ifdef LINUX |
218 |
> |
#if defined(LINUX) || defined(CYGWIN) |
219 |
|
if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){ |
220 |
|
return NULL; |
221 |
|
} |
269 |
|
|
270 |
|
*entries=num_disks; |
271 |
|
|
272 |
< |
/* If this fails, there is very little i can do about it, so i'll ignore it :) */ |
273 |
< |
#if defined(LINUX) || defined(SOLARIS) |
272 |
> |
/* If this fails, there is very little i can do about it, so |
273 |
> |
I'll ignore it :) */ |
274 |
> |
#if defined(LINUX) || defined(CYGWIN) |
275 |
> |
endmntent(f); |
276 |
> |
#endif |
277 |
> |
#if defined(SOLARIS) |
278 |
|
fclose(f); |
279 |
|
#endif |
280 |
|
|
290 |
|
} |
291 |
|
} |
292 |
|
|
295 |
– |
#ifdef SOLARIS |
296 |
– |
char *drive_map(char *sunos_name){ |
297 |
– |
file_line_t *file_lines = NULL; |
298 |
– |
file_line_t *f_ptr; |
299 |
– |
static disk_mapping_t *disk_mapping = NULL; |
300 |
– |
disk_mapping_t *d_ptr; |
301 |
– |
int x = 0; |
302 |
– |
int y = 256; |
303 |
– |
FILE *f; |
304 |
– |
DIR *dsk; |
305 |
– |
struct dirent *dsk_ent; |
306 |
– |
char linkpointer[PATH_MAX]; |
307 |
– |
char link_path[PATH_MAX]; |
308 |
– |
char *p, *r, *q; |
309 |
– |
|
310 |
– |
int dev_num; |
311 |
– |
char dev_name[51]; |
312 |
– |
|
313 |
– |
if(disk_mapping == NULL){ |
314 |
– |
/* Read in the path_to_inst file into memory */ |
315 |
– |
file_lines = malloc(sizeof(file_line_t) * y); |
316 |
– |
f_ptr = file_lines; |
317 |
– |
if(file_lines == NULL) return NULL; |
318 |
– |
f = fopen("/etc/path_to_inst", "r"); |
319 |
– |
if (f == NULL){ |
320 |
– |
free(file_lines); |
321 |
– |
return NULL; |
322 |
– |
} |
323 |
– |
for(x=0;fgets(f_ptr->line, sizeof(f_ptr->line), f);x++){ |
324 |
– |
if ((x+1) >= y){ |
325 |
– |
y = y*2; |
326 |
– |
file_lines = realloc(file_lines, sizeof(file_line_t) * y); |
327 |
– |
if (file_lines == NULL) return NULL; |
328 |
– |
f_ptr=file_lines+x; |
329 |
– |
} |
330 |
– |
f_ptr++; |
331 |
– |
} |
332 |
– |
f_ptr = NULL; |
333 |
– |
fclose(f); |
334 |
– |
|
335 |
– |
dsk = opendir("/dev/dsk"); |
336 |
– |
if (dsk == NULL){ |
337 |
– |
free(file_lines); |
338 |
– |
return NULL; |
339 |
– |
} |
340 |
– |
|
341 |
– |
while((dsk_ent = readdir(dsk)) != NULL){ |
342 |
– |
snprintf(link_path, sizeof(link_path), "/dev/dsk/%s", dsk_ent->d_name); |
343 |
– |
/* Ignore the non symlinks, e.g. ".." and "." */ |
344 |
– |
if((readlink(link_path, linkpointer, PATH_MAX)) == -1) continue; |
345 |
– |
/* We are only intrested in the string past the ../../devices/ part */ |
346 |
– |
p = strstr(linkpointer, "devices"); |
347 |
– |
p+=8; |
348 |
– |
|
349 |
– |
/* We are not intrested in the :a etc slices */ |
350 |
– |
q = strrchr(p, ':'); |
351 |
– |
*q = '\0'; |
352 |
– |
|
353 |
– |
/* char *p should not contain the info we need to look up in the |
354 |
– |
* path_to_inst file. |
355 |
– |
*/ |
356 |
– |
|
357 |
– |
for(f_ptr = file_lines; f_ptr != NULL; f_ptr++){ |
358 |
– |
r = strstr(f_ptr->line, p); |
359 |
– |
if (r != NULL) break; |
360 |
– |
} |
361 |
– |
|
362 |
– |
if(r == NULL){ |
363 |
– |
free(file_lines); |
364 |
– |
closedir(dsk); |
365 |
– |
return NULL; |
366 |
– |
} |
367 |
– |
|
368 |
– |
/* f_ptr should be pointing to the line of path_to_inst we are intrested in now */ |
369 |
– |
|
370 |
– |
sscanf(r, "%*s %d \"%50s", &dev_num, dev_name); |
371 |
– |
q = strrchr(dev_name, '"'); |
372 |
– |
if (q == NULL){ |
373 |
– |
free(file_lines); |
374 |
– |
closedir(dsk); |
375 |
– |
return NULL; |
376 |
– |
} |
377 |
– |
*q = '\0'; |
378 |
– |
|
379 |
– |
/* Nasty... Add the number to the end of the string.. This means |
380 |
– |
* dev_name now contains the sunos name.. E.g. ssd1 |
381 |
– |
*/ |
382 |
– |
snprintf(dev_name, 50, "%s%d", dev_name, dev_num); |
383 |
– |
if (disk_mapping == NULL){ |
384 |
– |
disk_mapping = malloc(sizeof(disk_mapping_t)); |
385 |
– |
d_ptr = disk_mapping; |
386 |
– |
}else{ |
387 |
– |
d_ptr->next = malloc(sizeof(disk_mapping_t)); |
388 |
– |
d_ptr = d_ptr->next; |
389 |
– |
} |
390 |
– |
|
391 |
– |
if (d_ptr == NULL){ |
392 |
– |
free(file_lines); |
393 |
– |
closedir(dsk); |
394 |
– |
return NULL; |
395 |
– |
} |
396 |
– |
|
397 |
– |
if( ((d_ptr->sunos_name = strdup(dev_name)) == NULL) || |
398 |
– |
((d_ptr->solaris_name = strdup(dsk_ent->d_name))==NULL) ){ |
399 |
– |
free(file_lines); |
400 |
– |
closedir(dsk); |
401 |
– |
return NULL; |
402 |
– |
} |
403 |
– |
} |
404 |
– |
free(file_lines); |
405 |
– |
closedir(dsk); |
406 |
– |
} |
407 |
– |
|
408 |
– |
for(d_ptr = disk_mapping; d_ptr !=NULL; d_ptr=d_ptr->next){ |
409 |
– |
if(!strcmp(sunos_name, d_ptr->sunos_name)){ |
410 |
– |
return (strdup(d_ptr->solaris_name)); |
411 |
– |
} |
412 |
– |
} |
413 |
– |
|
414 |
– |
/* If we really fail to find it.. Return the original name back */ |
415 |
– |
return strdup(sunos_name); |
416 |
– |
} |
417 |
– |
|
418 |
– |
#endif |
419 |
– |
|
293 |
|
diskio_stat_t *diskio_stat_malloc(int needed_entries, int *cur_entries, diskio_stat_t *diskio_stats){ |
294 |
|
|
295 |
|
if(diskio_stats==NULL){ |
329 |
|
diskio_stat_t *get_diskio_stats(int *entries){ |
330 |
|
|
331 |
|
static int sizeof_diskio_stats=0; |
332 |
+ |
#ifndef LINUX |
333 |
|
diskio_stat_t *diskio_stats_ptr; |
334 |
+ |
#endif |
335 |
|
|
336 |
|
#ifdef SOLARIS |
337 |
|
kstat_ctl_t *kc; |
342 |
|
FILE *f; |
343 |
|
char *line_ptr; |
344 |
|
int major, minor; |
470 |
– |
char dev_letter; |
345 |
|
int has_pp_stats = 1; |
346 |
|
static partition *parts = NULL; |
347 |
|
static int alloc_parts = 0; |
348 |
|
int i, n; |
349 |
|
time_t now; |
350 |
+ |
const char *format; |
351 |
|
#endif |
352 |
|
#ifdef FREEBSD |
353 |
|
static struct statinfo stats; |
358 |
|
long sel_gen; |
359 |
|
struct devstat *dev_ptr; |
360 |
|
#endif |
361 |
+ |
#ifdef NETBSD |
362 |
+ |
struct disk_sysctl *stats; |
363 |
+ |
#endif |
364 |
+ |
#ifdef OPENBSD |
365 |
+ |
int diskcount; |
366 |
+ |
char *disknames, *name, *bufpp; |
367 |
+ |
char **dk_name; |
368 |
+ |
struct diskstats *stats; |
369 |
+ |
#endif |
370 |
+ |
#ifdef NETBSD |
371 |
+ |
#define MIBSIZE 3 |
372 |
+ |
#endif |
373 |
+ |
#ifdef OPENBSD |
374 |
+ |
#define MIBSIZE 2 |
375 |
+ |
#endif |
376 |
+ |
#if defined(NETBSD) || defined(OPENBSD) |
377 |
+ |
int num_disks, i; |
378 |
+ |
int mib[MIBSIZE]; |
379 |
+ |
size_t size; |
380 |
+ |
#endif |
381 |
+ |
|
382 |
|
num_diskio=0; |
383 |
|
|
384 |
+ |
#ifdef OPENBSD |
385 |
+ |
mib[0] = CTL_HW; |
386 |
+ |
mib[1] = HW_DISKCOUNT; |
387 |
+ |
|
388 |
+ |
size = sizeof(diskcount); |
389 |
+ |
if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) { |
390 |
+ |
return NULL; |
391 |
+ |
} |
392 |
+ |
|
393 |
+ |
mib[0] = CTL_HW; |
394 |
+ |
mib[1] = HW_DISKNAMES; |
395 |
+ |
|
396 |
+ |
if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) { |
397 |
+ |
return NULL; |
398 |
+ |
} |
399 |
+ |
|
400 |
+ |
disknames = malloc(size); |
401 |
+ |
if (disknames == NULL) { |
402 |
+ |
return NULL; |
403 |
+ |
} |
404 |
+ |
|
405 |
+ |
if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) { |
406 |
+ |
return NULL; |
407 |
+ |
} |
408 |
+ |
|
409 |
+ |
dk_name = calloc(diskcount, sizeof(char *)); |
410 |
+ |
bufpp = disknames; |
411 |
+ |
for (i = 0; i < diskcount && (name = strsep(&bufpp, ",")) != NULL; i++) { |
412 |
+ |
dk_name[i] = name; |
413 |
+ |
} |
414 |
+ |
#endif |
415 |
+ |
|
416 |
+ |
#if defined(NETBSD) || defined(OPENBSD) |
417 |
+ |
mib[0] = CTL_HW; |
418 |
+ |
mib[1] = HW_DISKSTATS; |
419 |
+ |
#ifdef NETBSD |
420 |
+ |
mib[2] = sizeof(struct disk_sysctl); |
421 |
+ |
#endif |
422 |
+ |
|
423 |
+ |
if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) { |
424 |
+ |
return NULL; |
425 |
+ |
} |
426 |
+ |
|
427 |
+ |
#ifdef NETBSD |
428 |
+ |
num_disks = size / sizeof(struct disk_sysctl); |
429 |
+ |
#else |
430 |
+ |
num_disks = size / sizeof(struct diskstats); |
431 |
+ |
#endif |
432 |
+ |
|
433 |
+ |
stats = malloc(size); |
434 |
+ |
if (stats == NULL) { |
435 |
+ |
return NULL; |
436 |
+ |
} |
437 |
+ |
|
438 |
+ |
if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) { |
439 |
+ |
return NULL; |
440 |
+ |
} |
441 |
+ |
|
442 |
+ |
for (i = 0; i < num_disks; i++) { |
443 |
+ |
u_int64_t rbytes, wbytes; |
444 |
+ |
|
445 |
+ |
#ifdef NETBSD |
446 |
+ |
#ifdef HAVE_DK_RBYTES |
447 |
+ |
rbytes = stats[i].dk_rbytes; |
448 |
+ |
wbytes = stats[i].dk_wbytes; |
449 |
+ |
#else |
450 |
+ |
/* Before 1.7, NetBSD merged reads and writes. */ |
451 |
+ |
rbytes = wbytes = stats[i].dk_bytes; |
452 |
+ |
#endif |
453 |
+ |
#else |
454 |
+ |
rbytes = wbytes = stats[i].ds_bytes; |
455 |
+ |
#endif |
456 |
+ |
|
457 |
+ |
/* Don't keep stats for disks that have never been used. */ |
458 |
+ |
if (rbytes == 0 && wbytes == 0) { |
459 |
+ |
continue; |
460 |
+ |
} |
461 |
+ |
|
462 |
+ |
diskio_stats = diskio_stat_malloc(num_diskio + 1, |
463 |
+ |
&sizeof_diskio_stats, |
464 |
+ |
diskio_stats); |
465 |
+ |
if (diskio_stats == NULL) { |
466 |
+ |
return NULL; |
467 |
+ |
} |
468 |
+ |
diskio_stats_ptr = diskio_stats + num_diskio; |
469 |
+ |
|
470 |
+ |
diskio_stats_ptr->read_bytes = rbytes; |
471 |
+ |
diskio_stats_ptr->write_bytes = wbytes; |
472 |
+ |
if (diskio_stats_ptr->disk_name != NULL) { |
473 |
+ |
free(diskio_stats_ptr->disk_name); |
474 |
+ |
} |
475 |
+ |
#ifdef NETBSD |
476 |
+ |
diskio_stats_ptr->disk_name = strdup(stats[i].dk_name); |
477 |
+ |
#else |
478 |
+ |
diskio_stats_ptr->disk_name = strdup(dk_name[i]); |
479 |
+ |
#endif |
480 |
+ |
diskio_stats_ptr->systime = time(NULL); |
481 |
+ |
|
482 |
+ |
num_diskio++; |
483 |
+ |
} |
484 |
+ |
|
485 |
+ |
free(stats); |
486 |
+ |
#ifdef OPENBSD |
487 |
+ |
free(dk_name); |
488 |
+ |
free(disknames); |
489 |
+ |
#endif |
490 |
+ |
#endif |
491 |
+ |
|
492 |
|
#ifdef FREEBSD |
493 |
|
if (!stats_init) { |
494 |
|
stats.dinfo=malloc(sizeof(struct devinfo)); |
495 |
|
if(stats.dinfo==NULL) return NULL; |
496 |
+ |
bzero(stats.dinfo, sizeof(struct devinfo)); |
497 |
|
stats_init = 1; |
498 |
|
} |
499 |
+ |
#ifdef FREEBSD5 |
500 |
+ |
if ((devstat_getdevs(NULL, &stats)) < 0) return NULL; |
501 |
+ |
/* Not aware of a get all devices, so i said 999. If we ever |
502 |
+ |
* find a machine with more than 999 disks, then i'll change |
503 |
+ |
* this number :) |
504 |
+ |
*/ |
505 |
+ |
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; |
506 |
+ |
#else |
507 |
|
if ((getdevs(&stats)) < 0) return NULL; |
508 |
|
/* Not aware of a get all devices, so i said 999. If we ever |
509 |
|
* find a machine with more than 999 disks, then i'll change |
510 |
|
* this number :) |
511 |
|
*/ |
512 |
|
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; |
513 |
+ |
#endif |
514 |
|
|
515 |
|
for(counter=0;counter<stats.dinfo->numdevs;counter++){ |
516 |
|
dev_ptr=&stats.dinfo->devices[dev_sel[counter].position]; |
519 |
|
* devices.. like mem, proc.. and also doesn't report floppy |
520 |
|
* drives etc unless they are doing stuff :) |
521 |
|
*/ |
522 |
+ |
#ifdef FREEBSD5 |
523 |
+ |
if((dev_ptr->bytes[DEVSTAT_READ]==0) && (dev_ptr->bytes[DEVSTAT_WRITE]==0)) continue; |
524 |
+ |
#else |
525 |
|
if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue; |
526 |
+ |
#endif |
527 |
|
if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){ |
528 |
|
return NULL; |
529 |
|
} |
530 |
|
diskio_stats_ptr=diskio_stats+num_diskio; |
531 |
< |
|
531 |
> |
|
532 |
> |
#ifdef FREEBSD5 |
533 |
> |
diskio_stats_ptr->read_bytes=dev_ptr->bytes[DEVSTAT_READ]; |
534 |
> |
diskio_stats_ptr->write_bytes=dev_ptr->bytes[DEVSTAT_WRITE]; |
535 |
> |
#else |
536 |
|
diskio_stats_ptr->read_bytes=dev_ptr->bytes_read; |
537 |
|
diskio_stats_ptr->write_bytes=dev_ptr->bytes_written; |
538 |
+ |
#endif |
539 |
|
if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name); |
540 |
|
asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number); |
541 |
|
diskio_stats_ptr->systime=time(NULL); |
571 |
|
|
572 |
|
if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name); |
573 |
|
|
574 |
< |
diskio_stats_ptr->disk_name=drive_map(ksp->ks_name); |
574 |
> |
diskio_stats_ptr->disk_name=strdup((char *) get_svr_from_bsd(ksp->ks_name)); |
575 |
|
diskio_stats_ptr->systime=time(NULL); |
576 |
|
num_diskio++; |
577 |
|
} |
585 |
|
n = 0; |
586 |
|
|
587 |
|
/* Read /proc/partitions to find what devices exist. Recent 2.4 kernels |
588 |
< |
have statistics in here too, so we can use those directly. */ |
588 |
> |
have statistics in here too, so we can use those directly. |
589 |
> |
2.6 kernels have /proc/diskstats instead with almost (but not quite) |
590 |
> |
the same format. */ |
591 |
|
|
592 |
< |
f = fopen("/proc/partitions", "r"); |
592 |
> |
f = fopen("/proc/diskstats", "r"); |
593 |
> |
format = " %d %d %19s %*d %*d %lld %*d %*d %*d %lld"; |
594 |
> |
if (f == NULL) { |
595 |
> |
f = fopen("/proc/partitions", "r"); |
596 |
> |
format = " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld"; |
597 |
> |
} |
598 |
|
if (f == NULL) goto out; |
599 |
|
now = time(NULL); |
600 |
|
|
603 |
|
char *s; |
604 |
|
long long rsect, wsect; |
605 |
|
|
606 |
< |
int nr = sscanf(line_ptr, |
577 |
< |
" %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld", |
606 |
> |
int nr = sscanf(line_ptr, format, |
607 |
|
&major, &minor, name, &rsect, &wsect); |
608 |
|
if (nr < 3) continue; |
580 |
– |
if (nr < 5) { |
581 |
– |
has_pp_stats = 0; |
582 |
– |
rsect = 0; |
583 |
– |
wsect = 0; |
584 |
– |
} |
609 |
|
|
610 |
|
/* Skip device names ending in numbers, since they're |
611 |
|
partitions. */ |
614 |
|
--s; |
615 |
|
if (*s >= '0' && *s <= '9') continue; |
616 |
|
|
617 |
+ |
if (nr < 5) { |
618 |
+ |
has_pp_stats = 0; |
619 |
+ |
rsect = 0; |
620 |
+ |
wsect = 0; |
621 |
+ |
} |
622 |
+ |
|
623 |
|
diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats, |
624 |
|
diskio_stats); |
625 |
|
if (diskio_stats == NULL) goto out; |
644 |
|
n++; |
645 |
|
} |
646 |
|
|
647 |
+ |
fclose(f); |
648 |
+ |
f = NULL; |
649 |
+ |
|
650 |
|
if (!has_pp_stats) { |
651 |
< |
/* This is an older kernel without stats in /proc/partitions. |
652 |
< |
Read what we can from /proc/stat instead. */ |
651 |
> |
/* This is an older kernel where /proc/partitions doesn't |
652 |
> |
contain stats. Read what we can from /proc/stat instead, and |
653 |
> |
fill in the appropriate bits of the list allocated above. */ |
654 |
|
|
655 |
|
f = fopen("/proc/stat", "r"); |
656 |
|
if (f == NULL) goto out; |
717 |
|
num_diskio = n; |
718 |
|
out: |
719 |
|
if (f != NULL) fclose(f); |
720 |
+ |
#endif |
721 |
|
|
722 |
+ |
#ifdef CYGWIN |
723 |
+ |
return NULL; |
724 |
|
#endif |
725 |
+ |
|
726 |
|
*entries=num_diskio; |
727 |
|
|
728 |
|
return diskio_stats; |
729 |
|
} |
730 |
|
|
731 |
|
diskio_stat_t *get_diskio_stats_diff(int *entries){ |
732 |
< |
static diskio_stat_t *diskio_stats_diff=NULL; |
733 |
< |
static int sizeof_diskio_stats_diff=0; |
734 |
< |
diskio_stat_t *diskio_stats_diff_ptr, *diskio_stats_ptr; |
735 |
< |
int disks, x, y; |
732 |
> |
static diskio_stat_t *diff = NULL; |
733 |
> |
static int diff_count = 0; |
734 |
> |
diskio_stat_t *src, *dest; |
735 |
> |
int i, j, new_count; |
736 |
|
|
737 |
< |
if(diskio_stats==NULL){ |
738 |
< |
diskio_stats_ptr=get_diskio_stats(&disks); |
739 |
< |
*entries=disks; |
702 |
< |
return diskio_stats_ptr; |
737 |
> |
if (diskio_stats == NULL) { |
738 |
> |
/* No previous stats, so we can't calculate a difference. */ |
739 |
> |
return get_diskio_stats(entries); |
740 |
|
} |
741 |
|
|
742 |
< |
diskio_stats_diff=diskio_stat_malloc(num_diskio, &sizeof_diskio_stats_diff, diskio_stats_diff); |
743 |
< |
if(diskio_stats_diff==NULL){ |
742 |
> |
/* Resize the results array to match the previous stats. */ |
743 |
> |
diff = diskio_stat_malloc(num_diskio, &diff_count, diff); |
744 |
> |
if (diff == NULL) { |
745 |
|
return NULL; |
746 |
|
} |
747 |
|
|
748 |
< |
diskio_stats_diff_ptr=diskio_stats_diff; |
749 |
< |
diskio_stats_ptr=diskio_stats; |
748 |
> |
/* Copy the previous stats into the result. */ |
749 |
> |
for (i = 0; i < diff_count; i++) { |
750 |
> |
src = &diskio_stats[i]; |
751 |
> |
dest = &diff[i]; |
752 |
|
|
753 |
< |
for(disks=0;disks<num_diskio;disks++){ |
754 |
< |
if(diskio_stats_diff_ptr->disk_name!=NULL){ |
715 |
< |
free(diskio_stats_diff_ptr->disk_name); |
753 |
> |
if (dest->disk_name != NULL) { |
754 |
> |
free(dest->disk_name); |
755 |
|
} |
756 |
< |
diskio_stats_diff_ptr->disk_name=strdup(diskio_stats_ptr->disk_name); |
757 |
< |
diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes; |
758 |
< |
diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes; |
759 |
< |
diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime; |
756 |
> |
dest->disk_name = strdup(src->disk_name); |
757 |
> |
dest->read_bytes = src->read_bytes; |
758 |
> |
dest->write_bytes = src->write_bytes; |
759 |
> |
dest->systime = src->systime; |
760 |
> |
} |
761 |
|
|
762 |
< |
diskio_stats_diff_ptr++; |
763 |
< |
diskio_stats_ptr++; |
762 |
> |
/* Get a new set of stats. */ |
763 |
> |
if (get_diskio_stats(&new_count) == NULL) { |
764 |
> |
return NULL; |
765 |
|
} |
766 |
|
|
767 |
< |
diskio_stats_ptr=get_diskio_stats(&disks); |
768 |
< |
diskio_stats_diff_ptr=diskio_stats_diff; |
767 |
> |
/* For each previous stat... */ |
768 |
> |
for (i = 0; i < diff_count; i++) { |
769 |
> |
dest = &diff[i]; |
770 |
|
|
771 |
< |
for(x=0;x<sizeof_diskio_stats_diff;x++){ |
772 |
< |
|
773 |
< |
if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){ |
774 |
< |
diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes; |
775 |
< |
diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes; |
776 |
< |
diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime; |
777 |
< |
}else{ |
736 |
< |
diskio_stats_ptr=diskio_stats; |
737 |
< |
for(y=0;y<disks;y++){ |
738 |
< |
if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){ |
739 |
< |
diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes; |
740 |
< |
diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes; |
741 |
< |
diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime; |
742 |
< |
|
743 |
< |
break; |
744 |
< |
} |
745 |
< |
|
746 |
< |
diskio_stats_ptr++; |
771 |
> |
/* ... find the corresponding new stat ... */ |
772 |
> |
for (j = 0; j < new_count; j++) { |
773 |
> |
/* Try the new stat in the same position first, |
774 |
> |
since that's most likely to be it. */ |
775 |
> |
src = &diskio_stats[(i + j) % new_count]; |
776 |
> |
if (strcmp(src->disk_name, dest->disk_name) == 0) { |
777 |
> |
break; |
778 |
|
} |
779 |
|
} |
780 |
+ |
if (j == new_count) { |
781 |
+ |
/* No match found. */ |
782 |
+ |
continue; |
783 |
+ |
} |
784 |
|
|
785 |
< |
diskio_stats_ptr++; |
786 |
< |
diskio_stats_diff_ptr++; |
787 |
< |
|
785 |
> |
/* ... and subtract the previous stat from it to get the |
786 |
> |
difference. */ |
787 |
> |
dest->read_bytes = src->read_bytes - dest->read_bytes; |
788 |
> |
dest->write_bytes = src->write_bytes - dest->write_bytes; |
789 |
> |
dest->systime = src->systime - dest->systime; |
790 |
|
} |
791 |
< |
|
792 |
< |
*entries=sizeof_diskio_stats_diff; |
793 |
< |
return diskio_stats_diff; |
791 |
> |
|
792 |
> |
*entries = diff_count; |
793 |
> |
return diff; |
794 |
|
} |
795 |
+ |
|