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 |
+ |
#include "vector.h" |
34 |
|
|
35 |
|
#ifdef SOLARIS |
36 |
|
#include <sys/mnttab.h> |
37 |
|
#include <sys/statvfs.h> |
38 |
|
#include <kstat.h> |
34 |
– |
#include <sys/types.h> |
35 |
– |
#include <dirent.h> |
36 |
– |
#include <limits.h> |
37 |
– |
#include <unistd.h> |
39 |
|
#define VALID_FS_TYPES {"ufs", "tmpfs"} |
40 |
|
#endif |
41 |
|
|
42 |
< |
#ifdef LINUX |
42 |
< |
#include <sys/vfs.h> |
42 |
> |
#if defined(LINUX) || defined(CYGWIN) |
43 |
|
#include <mntent.h> |
44 |
+ |
#include <sys/vfs.h> |
45 |
|
#include "tools.h" |
45 |
– |
#define VALID_FS_TYPES {"ext2", "ext3", "xfs", "reiserfs", "vfat", "tmpfs"} |
46 |
|
#endif |
47 |
|
|
48 |
< |
#ifdef FREEBSD |
48 |
> |
#ifdef LINUX |
49 |
> |
#define VALID_FS_TYPES {"adfs", "affs", "befs", "bfs", "efs", "ext2", \ |
50 |
> |
"ext3", "vxfs", "hfs", "hfsplus", "hpfs", "jffs", \ |
51 |
> |
"jffs2", "minix", "msdos", "ntfs", "qnx4", "ramfs", \ |
52 |
> |
"rootfs", "reiserfs", "sysv", "v7", "udf", "ufs", \ |
53 |
> |
"umsdos", "vfat", "xfs", "jfs"} |
54 |
> |
#endif |
55 |
> |
|
56 |
> |
#ifdef CYGWIN |
57 |
> |
#define VALID_FS_TYPES {"user"} |
58 |
> |
#endif |
59 |
> |
|
60 |
> |
#ifdef ALLBSD |
61 |
|
#include <sys/param.h> |
62 |
|
#include <sys/ucred.h> |
63 |
|
#include <sys/mount.h> |
64 |
+ |
#endif |
65 |
+ |
#if defined(FREEBSD) || defined(DFBSD) |
66 |
|
#include <sys/dkstat.h> |
67 |
|
#include <devstat.h> |
68 |
< |
#define VALID_FS_TYPES {"ufs", "mfs"} |
68 |
> |
#define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \ |
69 |
> |
"ufs", "mfs"} |
70 |
|
#endif |
71 |
< |
#define START_VAL 1 |
72 |
< |
|
73 |
< |
/* Solaris kernel stores the disk names in the old BSD format |
74 |
< |
* and we would prefer it in th modern format. |
75 |
< |
*/ |
76 |
< |
#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; |
71 |
> |
#if defined(NETBSD) || defined(OPENBSD) |
72 |
> |
#include <sys/param.h> |
73 |
> |
#include <sys/sysctl.h> |
74 |
> |
#include <sys/disk.h> |
75 |
> |
#define VALID_FS_TYPES {"ffs", "mfs", "msdos", "lfs", "adosfs", "ext2fs", \ |
76 |
> |
"ntfs"} |
77 |
|
#endif |
78 |
|
|
79 |
|
char *copy_string(char *orig_ptr, const char *newtext){ |
88 |
|
return orig_ptr; |
89 |
|
} |
90 |
|
|
91 |
+ |
static void disk_stat_init(disk_stat_t *d) { |
92 |
+ |
d->device_name = NULL; |
93 |
+ |
d->fs_type = NULL; |
94 |
+ |
d->mnt_point = NULL; |
95 |
+ |
} |
96 |
|
|
97 |
< |
void init_disk_stat(int start, int end, disk_stat_t *disk_stats){ |
97 |
> |
static void disk_stat_destroy(disk_stat_t *d) { |
98 |
> |
free(d->device_name); |
99 |
> |
free(d->fs_type); |
100 |
> |
free(d->mnt_point); |
101 |
> |
} |
102 |
|
|
103 |
< |
for(disk_stats+=start; start<=end; start++){ |
104 |
< |
disk_stats->device_name=NULL; |
105 |
< |
disk_stats->fs_type=NULL; |
106 |
< |
disk_stats->mnt_point=NULL; |
107 |
< |
|
108 |
< |
disk_stats++; |
103 |
> |
int is_valid_fs_type(const char *type) { |
104 |
> |
const char *types[] = VALID_FS_TYPES; |
105 |
> |
int i; |
106 |
> |
|
107 |
> |
for (i = 0; i < (sizeof types / sizeof *types); i++) { |
108 |
> |
if (strcmp(types[i], type) == 0) { |
109 |
> |
return 1; |
110 |
> |
} |
111 |
|
} |
112 |
+ |
return 0; |
113 |
|
} |
114 |
|
|
115 |
|
disk_stat_t *get_disk_stats(int *entries){ |
116 |
+ |
VECTOR_DECLARE_STATIC(disk_stats, disk_stat_t, 10, |
117 |
+ |
disk_stat_init, disk_stat_destroy); |
118 |
|
|
119 |
< |
static disk_stat_t *disk_stats; |
106 |
< |
static int watermark=-1; |
107 |
< |
|
108 |
< |
char *fs_types[] = VALID_FS_TYPES; |
109 |
< |
int x, valid_type; |
119 |
> |
int valid_type; |
120 |
|
int num_disks=0; |
121 |
< |
#if defined(LINUX) || defined (SOLARIS) |
121 |
> |
#if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN) |
122 |
|
FILE *f; |
123 |
|
#endif |
124 |
|
|
128 |
|
struct mnttab mp; |
129 |
|
struct statvfs fs; |
130 |
|
#endif |
131 |
< |
#ifdef LINUX |
131 |
> |
#if defined(LINUX) || defined(CYGWIN) |
132 |
|
struct mntent *mp; |
133 |
|
struct statfs fs; |
134 |
|
#endif |
135 |
< |
#ifdef FREEBSD |
135 |
> |
#ifdef ALLBSD |
136 |
|
int nummnt; |
137 |
|
struct statfs *mp; |
138 |
|
#endif |
139 |
|
|
140 |
< |
if(watermark==-1){ |
131 |
< |
disk_stats=malloc(START_VAL * sizeof(disk_stat_t)); |
132 |
< |
if(disk_stats==NULL){ |
133 |
< |
return NULL; |
134 |
< |
} |
135 |
< |
watermark=START_VAL; |
136 |
< |
init_disk_stat(0, watermark-1, disk_stats); |
137 |
< |
} |
138 |
< |
#ifdef FREEBSD |
140 |
> |
#ifdef ALLBSD |
141 |
|
nummnt=getmntinfo(&mp , MNT_LOCAL); |
142 |
|
if (nummnt<=0){ |
143 |
|
return NULL; |
144 |
|
} |
145 |
|
for(;nummnt--; mp++){ |
146 |
< |
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 |
< |
} |
146 |
> |
valid_type = is_valid_fs_type(mp->f_fstypename); |
147 |
|
#endif |
148 |
|
|
149 |
< |
#ifdef LINUX |
149 |
> |
#if defined(LINUX) || defined(CYGWIN) |
150 |
|
if ((f=setmntent("/etc/mtab", "r" ))==NULL){ |
151 |
|
return NULL; |
152 |
|
} |
156 |
|
continue; |
157 |
|
} |
158 |
|
|
159 |
< |
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 |
< |
} |
159 |
> |
valid_type = is_valid_fs_type(mp->mnt_type); |
160 |
|
#endif |
161 |
|
|
162 |
|
#ifdef SOLARIS |
167 |
|
if ((statvfs(mp.mnt_mountp, &fs)) !=0){ |
168 |
|
continue; |
169 |
|
} |
170 |
< |
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 |
< |
} |
170 |
> |
valid_type = is_valid_fs_type(mp.mnt_fstype); |
171 |
|
#endif |
172 |
|
|
173 |
|
if(valid_type){ |
174 |
< |
if(num_disks>watermark-1){ |
175 |
< |
disk_ptr=disk_stats; |
192 |
< |
if((disk_stats=realloc(disk_stats, (watermark*2 * sizeof(disk_stat_t))))==NULL){ |
193 |
< |
disk_stats=disk_ptr; |
194 |
< |
return NULL; |
195 |
< |
} |
196 |
< |
|
197 |
< |
watermark=watermark*2; |
198 |
< |
init_disk_stat(num_disks, watermark-1, disk_stats); |
174 |
> |
if (VECTOR_RESIZE(disk_stats, num_disks + 1) < 0) { |
175 |
> |
return NULL; |
176 |
|
} |
200 |
– |
|
177 |
|
disk_ptr=disk_stats+num_disks; |
178 |
< |
#ifdef FREEBSD |
178 |
> |
|
179 |
> |
#ifdef ALLBSD |
180 |
|
if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){ |
181 |
|
return NULL; |
182 |
|
} |
198 |
|
/* Freebsd doesn't have a "available" inodes */ |
199 |
|
disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes; |
200 |
|
#endif |
201 |
< |
#ifdef LINUX |
201 |
> |
#if defined(LINUX) || defined(CYGWIN) |
202 |
|
if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){ |
203 |
|
return NULL; |
204 |
|
} |
252 |
|
|
253 |
|
*entries=num_disks; |
254 |
|
|
255 |
< |
/* If this fails, there is very little i can do about it, so i'll ignore it :) */ |
256 |
< |
#if defined(LINUX) || defined(SOLARIS) |
255 |
> |
/* If this fails, there is very little i can do about it, so |
256 |
> |
I'll ignore it :) */ |
257 |
> |
#if defined(LINUX) || defined(CYGWIN) |
258 |
> |
endmntent(f); |
259 |
> |
#endif |
260 |
> |
#if defined(SOLARIS) |
261 |
|
fclose(f); |
262 |
|
#endif |
263 |
|
|
264 |
|
return disk_stats; |
265 |
|
|
266 |
|
} |
286 |
– |
void diskio_stat_init(int start, int end, diskio_stat_t *diskio_stats){ |
267 |
|
|
268 |
< |
for(diskio_stats+=start; start<end; start++){ |
269 |
< |
diskio_stats->disk_name=NULL; |
290 |
< |
|
291 |
< |
diskio_stats++; |
292 |
< |
} |
268 |
> |
static void diskio_stat_init(diskio_stat_t *d) { |
269 |
> |
d->disk_name = NULL; |
270 |
|
} |
271 |
|
|
272 |
< |
#ifdef SOLARIS |
273 |
< |
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); |
272 |
> |
static void diskio_stat_destroy(diskio_stat_t *d) { |
273 |
> |
free(d->disk_name); |
274 |
|
} |
275 |
|
|
276 |
< |
#endif |
276 |
> |
VECTOR_DECLARE_STATIC(diskio_stats, diskio_stat_t, 10, |
277 |
> |
diskio_stat_init, diskio_stat_destroy); |
278 |
|
|
420 |
– |
diskio_stat_t *diskio_stat_malloc(int needed_entries, int *cur_entries, diskio_stat_t *diskio_stats){ |
421 |
– |
|
422 |
– |
if(diskio_stats==NULL){ |
423 |
– |
|
424 |
– |
if((diskio_stats=malloc(needed_entries * sizeof(diskio_stat_t)))==NULL){ |
425 |
– |
return NULL; |
426 |
– |
} |
427 |
– |
diskio_stat_init(0, needed_entries, diskio_stats); |
428 |
– |
*cur_entries=needed_entries; |
429 |
– |
|
430 |
– |
return diskio_stats; |
431 |
– |
} |
432 |
– |
|
433 |
– |
|
434 |
– |
if(*cur_entries<needed_entries){ |
435 |
– |
diskio_stats=realloc(diskio_stats, (sizeof(diskio_stat_t)*needed_entries)); |
436 |
– |
if(diskio_stats==NULL){ |
437 |
– |
return NULL; |
438 |
– |
} |
439 |
– |
diskio_stat_init(*cur_entries, needed_entries, diskio_stats); |
440 |
– |
*cur_entries=needed_entries; |
441 |
– |
} |
442 |
– |
|
443 |
– |
return diskio_stats; |
444 |
– |
} |
445 |
– |
|
446 |
– |
static diskio_stat_t *diskio_stats=NULL; |
447 |
– |
static int num_diskio=0; |
448 |
– |
|
279 |
|
#ifdef LINUX |
280 |
|
typedef struct { |
281 |
|
int major; |
284 |
|
#endif |
285 |
|
|
286 |
|
diskio_stat_t *get_diskio_stats(int *entries){ |
287 |
< |
|
288 |
< |
static int sizeof_diskio_stats=0; |
287 |
> |
int num_diskio; |
288 |
> |
#ifndef LINUX |
289 |
|
diskio_stat_t *diskio_stats_ptr; |
290 |
+ |
#endif |
291 |
|
|
292 |
|
#ifdef SOLARIS |
293 |
|
kstat_ctl_t *kc; |
298 |
|
FILE *f; |
299 |
|
char *line_ptr; |
300 |
|
int major, minor; |
470 |
– |
char dev_letter; |
301 |
|
int has_pp_stats = 1; |
302 |
|
static partition *parts = NULL; |
303 |
|
static int alloc_parts = 0; |
304 |
|
int i, n; |
305 |
|
time_t now; |
306 |
+ |
const char *format; |
307 |
|
#endif |
308 |
< |
#ifdef FREEBSD |
308 |
> |
#if defined(FREEBSD) || defined(DFBSD) |
309 |
|
static struct statinfo stats; |
310 |
|
static int stats_init = 0; |
311 |
|
int counter; |
314 |
|
long sel_gen; |
315 |
|
struct devstat *dev_ptr; |
316 |
|
#endif |
317 |
+ |
#ifdef NETBSD |
318 |
+ |
struct disk_sysctl *stats; |
319 |
+ |
#endif |
320 |
+ |
#ifdef OPENBSD |
321 |
+ |
int diskcount; |
322 |
+ |
char *disknames, *name, *bufpp; |
323 |
+ |
char **dk_name; |
324 |
+ |
struct diskstats *stats; |
325 |
+ |
#endif |
326 |
+ |
#ifdef NETBSD |
327 |
+ |
#define MIBSIZE 3 |
328 |
+ |
#endif |
329 |
+ |
#ifdef OPENBSD |
330 |
+ |
#define MIBSIZE 2 |
331 |
+ |
#endif |
332 |
+ |
#if defined(NETBSD) || defined(OPENBSD) |
333 |
+ |
int num_disks, i; |
334 |
+ |
int mib[MIBSIZE]; |
335 |
+ |
size_t size; |
336 |
+ |
#endif |
337 |
+ |
|
338 |
|
num_diskio=0; |
339 |
|
|
340 |
< |
#ifdef FREEBSD |
340 |
> |
#ifdef OPENBSD |
341 |
> |
mib[0] = CTL_HW; |
342 |
> |
mib[1] = HW_DISKCOUNT; |
343 |
> |
|
344 |
> |
size = sizeof(diskcount); |
345 |
> |
if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) { |
346 |
> |
return NULL; |
347 |
> |
} |
348 |
> |
|
349 |
> |
mib[0] = CTL_HW; |
350 |
> |
mib[1] = HW_DISKNAMES; |
351 |
> |
|
352 |
> |
if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) { |
353 |
> |
return NULL; |
354 |
> |
} |
355 |
> |
|
356 |
> |
disknames = malloc(size); |
357 |
> |
if (disknames == NULL) { |
358 |
> |
return NULL; |
359 |
> |
} |
360 |
> |
|
361 |
> |
if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) { |
362 |
> |
return NULL; |
363 |
> |
} |
364 |
> |
|
365 |
> |
dk_name = calloc(diskcount, sizeof(char *)); |
366 |
> |
bufpp = disknames; |
367 |
> |
for (i = 0; i < diskcount && (name = strsep(&bufpp, ",")) != NULL; i++) { |
368 |
> |
dk_name[i] = name; |
369 |
> |
} |
370 |
> |
#endif |
371 |
> |
|
372 |
> |
#if defined(NETBSD) || defined(OPENBSD) |
373 |
> |
mib[0] = CTL_HW; |
374 |
> |
mib[1] = HW_DISKSTATS; |
375 |
> |
#ifdef NETBSD |
376 |
> |
mib[2] = sizeof(struct disk_sysctl); |
377 |
> |
#endif |
378 |
> |
|
379 |
> |
if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) { |
380 |
> |
return NULL; |
381 |
> |
} |
382 |
> |
|
383 |
> |
#ifdef NETBSD |
384 |
> |
num_disks = size / sizeof(struct disk_sysctl); |
385 |
> |
#else |
386 |
> |
num_disks = size / sizeof(struct diskstats); |
387 |
> |
#endif |
388 |
> |
|
389 |
> |
stats = malloc(size); |
390 |
> |
if (stats == NULL) { |
391 |
> |
return NULL; |
392 |
> |
} |
393 |
> |
|
394 |
> |
if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) { |
395 |
> |
return NULL; |
396 |
> |
} |
397 |
> |
|
398 |
> |
for (i = 0; i < num_disks; i++) { |
399 |
> |
u_int64_t rbytes, wbytes; |
400 |
> |
|
401 |
> |
#ifdef NETBSD |
402 |
> |
#ifdef HAVE_DK_RBYTES |
403 |
> |
rbytes = stats[i].dk_rbytes; |
404 |
> |
wbytes = stats[i].dk_wbytes; |
405 |
> |
#else |
406 |
> |
/* Before 1.7, NetBSD merged reads and writes. */ |
407 |
> |
rbytes = wbytes = stats[i].dk_bytes; |
408 |
> |
#endif |
409 |
> |
#else |
410 |
> |
rbytes = wbytes = stats[i].ds_bytes; |
411 |
> |
#endif |
412 |
> |
|
413 |
> |
/* Don't keep stats for disks that have never been used. */ |
414 |
> |
if (rbytes == 0 && wbytes == 0) { |
415 |
> |
continue; |
416 |
> |
} |
417 |
> |
|
418 |
> |
if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) { |
419 |
> |
return NULL; |
420 |
> |
} |
421 |
> |
diskio_stats_ptr = diskio_stats + num_diskio; |
422 |
> |
|
423 |
> |
diskio_stats_ptr->read_bytes = rbytes; |
424 |
> |
diskio_stats_ptr->write_bytes = wbytes; |
425 |
> |
if (diskio_stats_ptr->disk_name != NULL) { |
426 |
> |
free(diskio_stats_ptr->disk_name); |
427 |
> |
} |
428 |
> |
#ifdef NETBSD |
429 |
> |
diskio_stats_ptr->disk_name = strdup(stats[i].dk_name); |
430 |
> |
#else |
431 |
> |
diskio_stats_ptr->disk_name = strdup(dk_name[i]); |
432 |
> |
#endif |
433 |
> |
diskio_stats_ptr->systime = time(NULL); |
434 |
> |
|
435 |
> |
num_diskio++; |
436 |
> |
} |
437 |
> |
|
438 |
> |
free(stats); |
439 |
> |
#ifdef OPENBSD |
440 |
> |
free(dk_name); |
441 |
> |
free(disknames); |
442 |
> |
#endif |
443 |
> |
#endif |
444 |
> |
|
445 |
> |
#if defined(FREEBSD) || defined(DFBSD) |
446 |
|
if (!stats_init) { |
447 |
|
stats.dinfo=malloc(sizeof(struct devinfo)); |
448 |
|
if(stats.dinfo==NULL) return NULL; |
449 |
+ |
bzero(stats.dinfo, sizeof(struct devinfo)); |
450 |
|
stats_init = 1; |
451 |
|
} |
452 |
+ |
#ifdef FREEBSD5 |
453 |
+ |
if ((devstat_getdevs(NULL, &stats)) < 0) return NULL; |
454 |
+ |
/* Not aware of a get all devices, so i said 999. If we ever |
455 |
+ |
* find a machine with more than 999 disks, then i'll change |
456 |
+ |
* this number :) |
457 |
+ |
*/ |
458 |
+ |
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; |
459 |
+ |
#else |
460 |
|
if ((getdevs(&stats)) < 0) return NULL; |
461 |
|
/* Not aware of a get all devices, so i said 999. If we ever |
462 |
|
* find a machine with more than 999 disks, then i'll change |
463 |
|
* this number :) |
464 |
|
*/ |
465 |
|
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; |
466 |
+ |
#endif |
467 |
|
|
468 |
|
for(counter=0;counter<stats.dinfo->numdevs;counter++){ |
469 |
|
dev_ptr=&stats.dinfo->devices[dev_sel[counter].position]; |
472 |
|
* devices.. like mem, proc.. and also doesn't report floppy |
473 |
|
* drives etc unless they are doing stuff :) |
474 |
|
*/ |
475 |
+ |
#ifdef FREEBSD5 |
476 |
+ |
if((dev_ptr->bytes[DEVSTAT_READ]==0) && (dev_ptr->bytes[DEVSTAT_WRITE]==0)) continue; |
477 |
+ |
#else |
478 |
|
if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue; |
479 |
< |
if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){ |
479 |
> |
#endif |
480 |
> |
|
481 |
> |
if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) { |
482 |
|
return NULL; |
483 |
|
} |
484 |
|
diskio_stats_ptr=diskio_stats+num_diskio; |
485 |
< |
|
485 |
> |
|
486 |
> |
#ifdef FREEBSD5 |
487 |
> |
diskio_stats_ptr->read_bytes=dev_ptr->bytes[DEVSTAT_READ]; |
488 |
> |
diskio_stats_ptr->write_bytes=dev_ptr->bytes[DEVSTAT_WRITE]; |
489 |
> |
#else |
490 |
|
diskio_stats_ptr->read_bytes=dev_ptr->bytes_read; |
491 |
|
diskio_stats_ptr->write_bytes=dev_ptr->bytes_written; |
492 |
+ |
#endif |
493 |
|
if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name); |
494 |
|
asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number); |
495 |
|
diskio_stats_ptr->systime=time(NULL); |
497 |
|
num_diskio++; |
498 |
|
} |
499 |
|
free(dev_sel); |
523 |
– |
free(stats.dinfo); |
500 |
|
|
501 |
|
#endif |
502 |
|
#ifdef SOLARIS |
513 |
|
if((kstat_read(kc, ksp, &kios))==-1){ |
514 |
|
} |
515 |
|
|
516 |
< |
if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){ |
516 |
> |
if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) { |
517 |
|
kstat_close(kc); |
518 |
|
return NULL; |
519 |
|
} |
525 |
|
|
526 |
|
if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name); |
527 |
|
|
528 |
< |
diskio_stats_ptr->disk_name=drive_map(ksp->ks_name); |
528 |
> |
diskio_stats_ptr->disk_name=strdup((char *) get_svr_from_bsd(ksp->ks_name)); |
529 |
|
diskio_stats_ptr->systime=time(NULL); |
530 |
|
num_diskio++; |
531 |
|
} |
539 |
|
n = 0; |
540 |
|
|
541 |
|
/* Read /proc/partitions to find what devices exist. Recent 2.4 kernels |
542 |
< |
have statistics in here too, so we can use those directly. */ |
542 |
> |
have statistics in here too, so we can use those directly. |
543 |
> |
2.6 kernels have /proc/diskstats instead with almost (but not quite) |
544 |
> |
the same format. */ |
545 |
|
|
546 |
< |
f = fopen("/proc/partitions", "r"); |
546 |
> |
f = fopen("/proc/diskstats", "r"); |
547 |
> |
format = " %d %d %99s %*d %*d %lld %*d %*d %*d %lld"; |
548 |
> |
if (f == NULL) { |
549 |
> |
f = fopen("/proc/partitions", "r"); |
550 |
> |
format = " %d %d %*d %99s %*d %*d %lld %*d %*d %*d %lld"; |
551 |
> |
} |
552 |
|
if (f == NULL) goto out; |
553 |
|
now = time(NULL); |
554 |
|
|
555 |
|
while ((line_ptr = f_read_line(f, "")) != NULL) { |
556 |
< |
char name[20]; |
556 |
> |
char name[100]; |
557 |
|
char *s; |
558 |
|
long long rsect, wsect; |
559 |
|
|
560 |
< |
int nr = sscanf(line_ptr, |
578 |
< |
" %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld", |
560 |
> |
int nr = sscanf(line_ptr, format, |
561 |
|
&major, &minor, name, &rsect, &wsect); |
562 |
|
if (nr < 3) continue; |
581 |
– |
if (nr < 5) { |
582 |
– |
has_pp_stats = 0; |
583 |
– |
rsect = 0; |
584 |
– |
wsect = 0; |
585 |
– |
} |
563 |
|
|
564 |
|
/* Skip device names ending in numbers, since they're |
565 |
|
partitions. */ |
568 |
|
--s; |
569 |
|
if (*s >= '0' && *s <= '9') continue; |
570 |
|
|
571 |
< |
diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats, |
572 |
< |
diskio_stats); |
573 |
< |
if (diskio_stats == NULL) goto out; |
571 |
> |
if (nr < 5) { |
572 |
> |
has_pp_stats = 0; |
573 |
> |
rsect = 0; |
574 |
> |
wsect = 0; |
575 |
> |
} |
576 |
> |
|
577 |
> |
if (VECTOR_RESIZE(diskio_stats, n + 1) < 0) { |
578 |
> |
goto out; |
579 |
> |
} |
580 |
|
if (n >= alloc_parts) { |
581 |
|
alloc_parts += 16; |
582 |
|
parts = realloc(parts, alloc_parts * sizeof *parts); |
598 |
|
n++; |
599 |
|
} |
600 |
|
|
601 |
+ |
fclose(f); |
602 |
+ |
f = NULL; |
603 |
+ |
|
604 |
|
if (!has_pp_stats) { |
605 |
< |
/* This is an older kernel without stats in /proc/partitions. |
606 |
< |
Read what we can from /proc/stat instead. */ |
605 |
> |
/* This is an older kernel where /proc/partitions doesn't |
606 |
> |
contain stats. Read what we can from /proc/stat instead, and |
607 |
> |
fill in the appropriate bits of the list allocated above. */ |
608 |
|
|
609 |
|
f = fopen("/proc/stat", "r"); |
610 |
|
if (f == NULL) goto out; |
671 |
|
num_diskio = n; |
672 |
|
out: |
673 |
|
if (f != NULL) fclose(f); |
674 |
+ |
#endif |
675 |
|
|
676 |
+ |
#ifdef CYGWIN |
677 |
+ |
return NULL; |
678 |
|
#endif |
679 |
+ |
|
680 |
|
*entries=num_diskio; |
681 |
|
|
682 |
|
return diskio_stats; |
683 |
|
} |
684 |
|
|
685 |
|
diskio_stat_t *get_diskio_stats_diff(int *entries){ |
686 |
< |
static diskio_stat_t *diskio_stats_diff=NULL; |
687 |
< |
static int sizeof_diskio_stats_diff=0; |
688 |
< |
diskio_stat_t *diskio_stats_diff_ptr, *diskio_stats_ptr; |
689 |
< |
int disks, x, y; |
686 |
> |
VECTOR_DECLARE_STATIC(diff, diskio_stat_t, 1, |
687 |
> |
diskio_stat_init, diskio_stat_destroy); |
688 |
> |
diskio_stat_t *src = NULL, *dest; |
689 |
> |
int i, j, diff_count, new_count; |
690 |
|
|
691 |
< |
if(diskio_stats==NULL){ |
692 |
< |
diskio_stats_ptr=get_diskio_stats(&disks); |
693 |
< |
*entries=disks; |
703 |
< |
return diskio_stats_ptr; |
691 |
> |
if (diskio_stats == NULL) { |
692 |
> |
/* No previous stats, so we can't calculate a difference. */ |
693 |
> |
return get_diskio_stats(entries); |
694 |
|
} |
695 |
|
|
696 |
< |
diskio_stats_diff=diskio_stat_malloc(num_diskio, &sizeof_diskio_stats_diff, diskio_stats_diff); |
697 |
< |
if(diskio_stats_diff==NULL){ |
696 |
> |
/* Resize the results array to match the previous stats. */ |
697 |
> |
diff_count = VECTOR_SIZE(diskio_stats); |
698 |
> |
if (VECTOR_RESIZE(diff, diff_count) < 0) { |
699 |
|
return NULL; |
700 |
|
} |
701 |
|
|
702 |
< |
diskio_stats_diff_ptr=diskio_stats_diff; |
703 |
< |
diskio_stats_ptr=diskio_stats; |
702 |
> |
/* Copy the previous stats into the result. */ |
703 |
> |
for (i = 0; i < diff_count; i++) { |
704 |
> |
src = &diskio_stats[i]; |
705 |
> |
dest = &diff[i]; |
706 |
|
|
707 |
< |
for(disks=0;disks<num_diskio;disks++){ |
708 |
< |
if(diskio_stats_diff_ptr->disk_name!=NULL){ |
716 |
< |
free(diskio_stats_diff_ptr->disk_name); |
707 |
> |
if (dest->disk_name != NULL) { |
708 |
> |
free(dest->disk_name); |
709 |
|
} |
710 |
< |
diskio_stats_diff_ptr->disk_name=strdup(diskio_stats_ptr->disk_name); |
711 |
< |
diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes; |
712 |
< |
diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes; |
713 |
< |
diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime; |
710 |
> |
dest->disk_name = strdup(src->disk_name); |
711 |
> |
dest->read_bytes = src->read_bytes; |
712 |
> |
dest->write_bytes = src->write_bytes; |
713 |
> |
dest->systime = src->systime; |
714 |
> |
} |
715 |
|
|
716 |
< |
diskio_stats_diff_ptr++; |
717 |
< |
diskio_stats_ptr++; |
716 |
> |
/* Get a new set of stats. */ |
717 |
> |
if (get_diskio_stats(&new_count) == NULL) { |
718 |
> |
return NULL; |
719 |
|
} |
720 |
|
|
721 |
< |
diskio_stats_ptr=get_diskio_stats(&disks); |
722 |
< |
diskio_stats_diff_ptr=diskio_stats_diff; |
721 |
> |
/* For each previous stat... */ |
722 |
> |
for (i = 0; i < diff_count; i++) { |
723 |
> |
dest = &diff[i]; |
724 |
|
|
725 |
< |
for(x=0;x<sizeof_diskio_stats_diff;x++){ |
726 |
< |
|
727 |
< |
if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){ |
728 |
< |
diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes; |
729 |
< |
diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes; |
730 |
< |
diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime; |
731 |
< |
}else{ |
737 |
< |
diskio_stats_ptr=diskio_stats; |
738 |
< |
for(y=0;y<disks;y++){ |
739 |
< |
if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){ |
740 |
< |
diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes; |
741 |
< |
diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes; |
742 |
< |
diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime; |
743 |
< |
|
744 |
< |
break; |
745 |
< |
} |
746 |
< |
|
747 |
< |
diskio_stats_ptr++; |
725 |
> |
/* ... find the corresponding new stat ... */ |
726 |
> |
for (j = 0; j < new_count; j++) { |
727 |
> |
/* Try the new stat in the same position first, |
728 |
> |
since that's most likely to be it. */ |
729 |
> |
src = &diskio_stats[(i + j) % new_count]; |
730 |
> |
if (strcmp(src->disk_name, dest->disk_name) == 0) { |
731 |
> |
break; |
732 |
|
} |
733 |
|
} |
734 |
+ |
if (j == new_count) { |
735 |
+ |
/* No match found. */ |
736 |
+ |
continue; |
737 |
+ |
} |
738 |
|
|
739 |
< |
diskio_stats_ptr++; |
740 |
< |
diskio_stats_diff_ptr++; |
741 |
< |
|
739 |
> |
/* ... and subtract the previous stat from it to get the |
740 |
> |
difference. */ |
741 |
> |
dest->read_bytes = src->read_bytes - dest->read_bytes; |
742 |
> |
dest->write_bytes = src->write_bytes - dest->write_bytes; |
743 |
> |
dest->systime = src->systime - dest->systime; |
744 |
|
} |
745 |
< |
|
746 |
< |
*entries=sizeof_diskio_stats_diff; |
747 |
< |
return diskio_stats_diff; |
745 |
> |
|
746 |
> |
*entries = diff_count; |
747 |
> |
return diff; |
748 |
|
} |
749 |
+ |
|