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){ |
79 |
> |
static void disk_stat_init(disk_stat_t *d) { |
80 |
> |
d->device_name = NULL; |
81 |
> |
d->fs_type = NULL; |
82 |
> |
d->mnt_point = NULL; |
83 |
> |
} |
84 |
|
|
85 |
< |
/* Maybe free if not NULL, and strdup rather than realloc and strcpy? */ |
86 |
< |
orig_ptr=realloc(orig_ptr, (1+strlen(newtext))); |
87 |
< |
if(orig_ptr==NULL){ |
88 |
< |
return NULL; |
85 |
< |
} |
86 |
< |
strcpy(orig_ptr, newtext); |
87 |
< |
|
88 |
< |
return orig_ptr; |
85 |
> |
static void disk_stat_destroy(disk_stat_t *d) { |
86 |
> |
free(d->device_name); |
87 |
> |
free(d->fs_type); |
88 |
> |
free(d->mnt_point); |
89 |
|
} |
90 |
|
|
91 |
+ |
int is_valid_fs_type(const char *type) { |
92 |
+ |
const char *types[] = VALID_FS_TYPES; |
93 |
+ |
int i; |
94 |
|
|
95 |
< |
void init_disk_stat(int start, int end, disk_stat_t *disk_stats){ |
96 |
< |
|
97 |
< |
for(disk_stats+=start; start<=end; start++){ |
98 |
< |
disk_stats->device_name=NULL; |
96 |
< |
disk_stats->fs_type=NULL; |
97 |
< |
disk_stats->mnt_point=NULL; |
98 |
< |
|
99 |
< |
disk_stats++; |
95 |
> |
for (i = 0; i < (sizeof types / sizeof *types); i++) { |
96 |
> |
if (strcmp(types[i], type) == 0) { |
97 |
> |
return 1; |
98 |
> |
} |
99 |
|
} |
100 |
+ |
return 0; |
101 |
|
} |
102 |
|
|
103 |
|
disk_stat_t *get_disk_stats(int *entries){ |
104 |
+ |
VECTOR_DECLARE_STATIC(disk_stats, disk_stat_t, 10, |
105 |
+ |
disk_stat_init, disk_stat_destroy); |
106 |
|
|
107 |
< |
static disk_stat_t *disk_stats; |
106 |
< |
static int watermark=-1; |
107 |
< |
|
108 |
< |
char *fs_types[] = VALID_FS_TYPES; |
109 |
< |
int x, valid_type; |
107 |
> |
int 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 |
123 |
< |
#ifdef FREEBSD |
123 |
> |
#ifdef ALLBSD |
124 |
|
int nummnt; |
125 |
|
struct statfs *mp; |
126 |
|
#endif |
127 |
|
|
128 |
< |
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 |
128 |
> |
#ifdef ALLBSD |
129 |
|
nummnt=getmntinfo(&mp , MNT_LOCAL); |
130 |
|
if (nummnt<=0){ |
131 |
|
return NULL; |
132 |
|
} |
133 |
|
for(;nummnt--; mp++){ |
134 |
< |
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 |
< |
} |
134 |
> |
valid_type = is_valid_fs_type(mp->f_fstypename); |
135 |
|
#endif |
136 |
|
|
137 |
< |
#ifdef LINUX |
137 |
> |
#if defined(LINUX) || defined(CYGWIN) |
138 |
|
if ((f=setmntent("/etc/mtab", "r" ))==NULL){ |
139 |
|
return NULL; |
140 |
|
} |
144 |
|
continue; |
145 |
|
} |
146 |
|
|
147 |
< |
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 |
< |
} |
147 |
> |
valid_type = is_valid_fs_type(mp->mnt_type); |
148 |
|
#endif |
149 |
|
|
150 |
|
#ifdef SOLARIS |
155 |
|
if ((statvfs(mp.mnt_mountp, &fs)) !=0){ |
156 |
|
continue; |
157 |
|
} |
158 |
< |
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 |
< |
} |
158 |
> |
valid_type = is_valid_fs_type(mp.mnt_fstype); |
159 |
|
#endif |
160 |
|
|
161 |
|
if(valid_type){ |
162 |
< |
if(num_disks>watermark-1){ |
163 |
< |
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); |
162 |
> |
if (VECTOR_RESIZE(disk_stats, num_disks + 1) < 0) { |
163 |
> |
return NULL; |
164 |
|
} |
200 |
– |
|
165 |
|
disk_ptr=disk_stats+num_disks; |
166 |
< |
#ifdef FREEBSD |
167 |
< |
if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){ |
166 |
> |
|
167 |
> |
#ifdef ALLBSD |
168 |
> |
if (update_string(&disk_ptr->device_name, mp->f_mntfromname) == NULL) { |
169 |
|
return NULL; |
170 |
|
} |
171 |
< |
|
207 |
< |
if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->f_fstypename))==NULL){ |
171 |
> |
if (update_string(&disk_ptr->fs_type, mp->f_fstypename) == NULL) { |
172 |
|
return NULL; |
173 |
|
} |
174 |
< |
|
211 |
< |
if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->f_mntonname))==NULL){ |
174 |
> |
if (update_string(&disk_ptr->mnt_point, mp->f_mntonname) == NULL) { |
175 |
|
return NULL; |
176 |
|
} |
177 |
|
|
184 |
|
/* Freebsd doesn't have a "available" inodes */ |
185 |
|
disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes; |
186 |
|
#endif |
187 |
< |
#ifdef LINUX |
188 |
< |
if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){ |
187 |
> |
#if defined(LINUX) || defined(CYGWIN) |
188 |
> |
if (update_string(&disk_ptr->device_name, mp->mnt_fsname) == NULL) { |
189 |
|
return NULL; |
190 |
|
} |
191 |
|
|
192 |
< |
if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->mnt_type))==NULL){ |
192 |
> |
if (update_string(&disk_ptr->fs_type, mp->mnt_type) == NULL) { |
193 |
|
return NULL; |
194 |
|
} |
195 |
|
|
196 |
< |
if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->mnt_dir))==NULL){ |
196 |
> |
if (update_string(&disk_ptr->mnt_point, mp->mnt_dir) == NULL) { |
197 |
|
return NULL; |
198 |
|
} |
199 |
|
disk_ptr->size = (long long)fs.f_bsize * (long long)fs.f_blocks; |
207 |
|
#endif |
208 |
|
|
209 |
|
#ifdef SOLARIS |
247 |
– |
/* Memory leak in event of realloc failing */ |
210 |
|
/* Maybe make this char[bigenough] and do strncpy's and put a null in the end? |
211 |
|
* Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob |
212 |
|
* be upwards of a k each |
213 |
|
*/ |
214 |
< |
if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp.mnt_special))==NULL){ |
214 |
> |
if (update_string(&disk_ptr->device_name, mp.mnt_special) == NULL) { |
215 |
|
return NULL; |
216 |
|
} |
217 |
|
|
218 |
< |
if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp.mnt_fstype))==NULL){ |
218 |
> |
if (update_string(&disk_ptr->fs_type, mp.mnt_fstype) == NULL) { |
219 |
|
return NULL; |
220 |
|
} |
221 |
|
|
222 |
< |
if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp.mnt_mountp))==NULL){ |
222 |
> |
if (update_string(&disk_ptr->mnt_point, mp.mnt_mountp) == NULL) { |
223 |
|
return NULL; |
224 |
|
} |
225 |
|
|
237 |
|
|
238 |
|
*entries=num_disks; |
239 |
|
|
240 |
< |
/* If this fails, there is very little i can do about it, so i'll ignore it :) */ |
241 |
< |
#if defined(LINUX) || defined(SOLARIS) |
240 |
> |
/* If this fails, there is very little i can do about it, so |
241 |
> |
I'll ignore it :) */ |
242 |
> |
#if defined(LINUX) || defined(CYGWIN) |
243 |
> |
endmntent(f); |
244 |
> |
#endif |
245 |
> |
#if defined(SOLARIS) |
246 |
|
fclose(f); |
247 |
|
#endif |
248 |
|
|
249 |
|
return disk_stats; |
250 |
|
|
251 |
|
} |
286 |
– |
void diskio_stat_init(int start, int end, diskio_stat_t *diskio_stats){ |
252 |
|
|
253 |
< |
for(diskio_stats+=start; start<end; start++){ |
254 |
< |
diskio_stats->disk_name=NULL; |
290 |
< |
|
291 |
< |
diskio_stats++; |
292 |
< |
} |
253 |
> |
static void diskio_stat_init(diskio_stat_t *d) { |
254 |
> |
d->disk_name = NULL; |
255 |
|
} |
256 |
|
|
257 |
< |
#ifdef SOLARIS |
258 |
< |
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); |
257 |
> |
static void diskio_stat_destroy(diskio_stat_t *d) { |
258 |
> |
free(d->disk_name); |
259 |
|
} |
260 |
|
|
261 |
< |
#endif |
261 |
> |
VECTOR_DECLARE_STATIC(diskio_stats, diskio_stat_t, 10, |
262 |
> |
diskio_stat_init, diskio_stat_destroy); |
263 |
|
|
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 |
– |
|
264 |
|
#ifdef LINUX |
265 |
|
typedef struct { |
266 |
|
int major; |
269 |
|
#endif |
270 |
|
|
271 |
|
diskio_stat_t *get_diskio_stats(int *entries){ |
272 |
< |
|
273 |
< |
static int sizeof_diskio_stats=0; |
272 |
> |
int num_diskio; |
273 |
> |
#ifndef LINUX |
274 |
|
diskio_stat_t *diskio_stats_ptr; |
275 |
+ |
#endif |
276 |
|
|
277 |
|
#ifdef SOLARIS |
278 |
|
kstat_ctl_t *kc; |
283 |
|
FILE *f; |
284 |
|
char *line_ptr; |
285 |
|
int major, minor; |
470 |
– |
char dev_letter; |
286 |
|
int has_pp_stats = 1; |
287 |
< |
static partition *parts = NULL; |
473 |
< |
static int alloc_parts = 0; |
287 |
> |
VECTOR_DECLARE_STATIC(parts, partition, 16, NULL, NULL); |
288 |
|
int i, n; |
289 |
|
time_t now; |
290 |
+ |
const char *format; |
291 |
|
#endif |
292 |
< |
#ifdef FREEBSD |
292 |
> |
#if defined(FREEBSD) || defined(DFBSD) |
293 |
|
static struct statinfo stats; |
294 |
|
static int stats_init = 0; |
295 |
|
int counter; |
298 |
|
long sel_gen; |
299 |
|
struct devstat *dev_ptr; |
300 |
|
#endif |
301 |
+ |
#ifdef NETBSD |
302 |
+ |
struct disk_sysctl *stats; |
303 |
+ |
#endif |
304 |
+ |
#ifdef OPENBSD |
305 |
+ |
int diskcount; |
306 |
+ |
char *disknames, *name, *bufpp; |
307 |
+ |
char **dk_name; |
308 |
+ |
struct diskstats *stats; |
309 |
+ |
#endif |
310 |
+ |
#ifdef NETBSD |
311 |
+ |
#define MIBSIZE 3 |
312 |
+ |
#endif |
313 |
+ |
#ifdef OPENBSD |
314 |
+ |
#define MIBSIZE 2 |
315 |
+ |
#endif |
316 |
+ |
#if defined(NETBSD) || defined(OPENBSD) |
317 |
+ |
int num_disks, i; |
318 |
+ |
int mib[MIBSIZE]; |
319 |
+ |
size_t size; |
320 |
+ |
#endif |
321 |
+ |
|
322 |
|
num_diskio=0; |
323 |
|
|
324 |
< |
#ifdef FREEBSD |
324 |
> |
#ifdef OPENBSD |
325 |
> |
mib[0] = CTL_HW; |
326 |
> |
mib[1] = HW_DISKCOUNT; |
327 |
> |
|
328 |
> |
size = sizeof(diskcount); |
329 |
> |
if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) { |
330 |
> |
return NULL; |
331 |
> |
} |
332 |
> |
|
333 |
> |
mib[0] = CTL_HW; |
334 |
> |
mib[1] = HW_DISKNAMES; |
335 |
> |
|
336 |
> |
if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) { |
337 |
> |
return NULL; |
338 |
> |
} |
339 |
> |
|
340 |
> |
disknames = malloc(size); |
341 |
> |
if (disknames == NULL) { |
342 |
> |
return NULL; |
343 |
> |
} |
344 |
> |
|
345 |
> |
if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) { |
346 |
> |
return NULL; |
347 |
> |
} |
348 |
> |
|
349 |
> |
dk_name = calloc(diskcount, sizeof(char *)); |
350 |
> |
bufpp = disknames; |
351 |
> |
for (i = 0; i < diskcount && (name = strsep(&bufpp, ",")) != NULL; i++) { |
352 |
> |
dk_name[i] = name; |
353 |
> |
} |
354 |
> |
#endif |
355 |
> |
|
356 |
> |
#if defined(NETBSD) || defined(OPENBSD) |
357 |
> |
mib[0] = CTL_HW; |
358 |
> |
mib[1] = HW_DISKSTATS; |
359 |
> |
#ifdef NETBSD |
360 |
> |
mib[2] = sizeof(struct disk_sysctl); |
361 |
> |
#endif |
362 |
> |
|
363 |
> |
if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) { |
364 |
> |
return NULL; |
365 |
> |
} |
366 |
> |
|
367 |
> |
#ifdef NETBSD |
368 |
> |
num_disks = size / sizeof(struct disk_sysctl); |
369 |
> |
#else |
370 |
> |
num_disks = size / sizeof(struct diskstats); |
371 |
> |
#endif |
372 |
> |
|
373 |
> |
stats = malloc(size); |
374 |
> |
if (stats == NULL) { |
375 |
> |
return NULL; |
376 |
> |
} |
377 |
> |
|
378 |
> |
if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) { |
379 |
> |
return NULL; |
380 |
> |
} |
381 |
> |
|
382 |
> |
for (i = 0; i < num_disks; i++) { |
383 |
> |
u_int64_t rbytes, wbytes; |
384 |
> |
|
385 |
> |
#ifdef NETBSD |
386 |
> |
#ifdef HAVE_DK_RBYTES |
387 |
> |
rbytes = stats[i].dk_rbytes; |
388 |
> |
wbytes = stats[i].dk_wbytes; |
389 |
> |
#else |
390 |
> |
/* Before 1.7, NetBSD merged reads and writes. */ |
391 |
> |
rbytes = wbytes = stats[i].dk_bytes; |
392 |
> |
#endif |
393 |
> |
#else |
394 |
> |
rbytes = wbytes = stats[i].ds_bytes; |
395 |
> |
#endif |
396 |
> |
|
397 |
> |
/* Don't keep stats for disks that have never been used. */ |
398 |
> |
if (rbytes == 0 && wbytes == 0) { |
399 |
> |
continue; |
400 |
> |
} |
401 |
> |
|
402 |
> |
if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) { |
403 |
> |
return NULL; |
404 |
> |
} |
405 |
> |
diskio_stats_ptr = diskio_stats + num_diskio; |
406 |
> |
|
407 |
> |
diskio_stats_ptr->read_bytes = rbytes; |
408 |
> |
diskio_stats_ptr->write_bytes = wbytes; |
409 |
> |
if (diskio_stats_ptr->disk_name != NULL) { |
410 |
> |
free(diskio_stats_ptr->disk_name); |
411 |
> |
} |
412 |
> |
#ifdef NETBSD |
413 |
> |
diskio_stats_ptr->disk_name = strdup(stats[i].dk_name); |
414 |
> |
#else |
415 |
> |
diskio_stats_ptr->disk_name = strdup(dk_name[i]); |
416 |
> |
#endif |
417 |
> |
diskio_stats_ptr->systime = time(NULL); |
418 |
> |
|
419 |
> |
num_diskio++; |
420 |
> |
} |
421 |
> |
|
422 |
> |
free(stats); |
423 |
> |
#ifdef OPENBSD |
424 |
> |
free(dk_name); |
425 |
> |
free(disknames); |
426 |
> |
#endif |
427 |
> |
#endif |
428 |
> |
|
429 |
> |
#if defined(FREEBSD) || defined(DFBSD) |
430 |
|
if (!stats_init) { |
431 |
|
stats.dinfo=malloc(sizeof(struct devinfo)); |
491 |
– |
bzero(stats.dinfo, sizeof(struct devinfo)); |
432 |
|
if(stats.dinfo==NULL) return NULL; |
433 |
+ |
bzero(stats.dinfo, sizeof(struct devinfo)); |
434 |
|
stats_init = 1; |
435 |
|
} |
436 |
+ |
#ifdef FREEBSD5 |
437 |
+ |
if ((devstat_getdevs(NULL, &stats)) < 0) return NULL; |
438 |
+ |
/* Not aware of a get all devices, so i said 999. If we ever |
439 |
+ |
* find a machine with more than 999 disks, then i'll change |
440 |
+ |
* this number :) |
441 |
+ |
*/ |
442 |
+ |
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; |
443 |
+ |
#else |
444 |
|
if ((getdevs(&stats)) < 0) return NULL; |
445 |
|
/* Not aware of a get all devices, so i said 999. If we ever |
446 |
|
* find a machine with more than 999 disks, then i'll change |
447 |
|
* this number :) |
448 |
|
*/ |
449 |
|
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; |
450 |
+ |
#endif |
451 |
|
|
452 |
|
for(counter=0;counter<stats.dinfo->numdevs;counter++){ |
453 |
|
dev_ptr=&stats.dinfo->devices[dev_sel[counter].position]; |
456 |
|
* devices.. like mem, proc.. and also doesn't report floppy |
457 |
|
* drives etc unless they are doing stuff :) |
458 |
|
*/ |
459 |
+ |
#ifdef FREEBSD5 |
460 |
+ |
if((dev_ptr->bytes[DEVSTAT_READ]==0) && (dev_ptr->bytes[DEVSTAT_WRITE]==0)) continue; |
461 |
+ |
#else |
462 |
|
if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue; |
463 |
< |
if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){ |
463 |
> |
#endif |
464 |
> |
|
465 |
> |
if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) { |
466 |
|
return NULL; |
467 |
|
} |
468 |
|
diskio_stats_ptr=diskio_stats+num_diskio; |
469 |
< |
|
469 |
> |
|
470 |
> |
#ifdef FREEBSD5 |
471 |
> |
diskio_stats_ptr->read_bytes=dev_ptr->bytes[DEVSTAT_READ]; |
472 |
> |
diskio_stats_ptr->write_bytes=dev_ptr->bytes[DEVSTAT_WRITE]; |
473 |
> |
#else |
474 |
|
diskio_stats_ptr->read_bytes=dev_ptr->bytes_read; |
475 |
|
diskio_stats_ptr->write_bytes=dev_ptr->bytes_written; |
476 |
+ |
#endif |
477 |
|
if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name); |
478 |
|
asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number); |
479 |
|
diskio_stats_ptr->systime=time(NULL); |
497 |
|
if((kstat_read(kc, ksp, &kios))==-1){ |
498 |
|
} |
499 |
|
|
500 |
< |
if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){ |
500 |
> |
if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) { |
501 |
|
kstat_close(kc); |
502 |
|
return NULL; |
503 |
|
} |
509 |
|
|
510 |
|
if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name); |
511 |
|
|
512 |
< |
diskio_stats_ptr->disk_name=drive_map(ksp->ks_name); |
512 |
> |
diskio_stats_ptr->disk_name=strdup((char *) get_svr_from_bsd(ksp->ks_name)); |
513 |
|
diskio_stats_ptr->systime=time(NULL); |
514 |
|
num_diskio++; |
515 |
|
} |
523 |
|
n = 0; |
524 |
|
|
525 |
|
/* Read /proc/partitions to find what devices exist. Recent 2.4 kernels |
526 |
< |
have statistics in here too, so we can use those directly. */ |
526 |
> |
have statistics in here too, so we can use those directly. |
527 |
> |
2.6 kernels have /proc/diskstats instead with almost (but not quite) |
528 |
> |
the same format. */ |
529 |
|
|
530 |
< |
f = fopen("/proc/partitions", "r"); |
530 |
> |
f = fopen("/proc/diskstats", "r"); |
531 |
> |
format = " %d %d %99s %*d %*d %lld %*d %*d %*d %lld"; |
532 |
> |
if (f == NULL) { |
533 |
> |
f = fopen("/proc/partitions", "r"); |
534 |
> |
format = " %d %d %*d %99s %*d %*d %lld %*d %*d %*d %lld"; |
535 |
> |
} |
536 |
|
if (f == NULL) goto out; |
537 |
|
now = time(NULL); |
538 |
|
|
539 |
|
while ((line_ptr = f_read_line(f, "")) != NULL) { |
540 |
< |
char name[20]; |
540 |
> |
char name[100]; |
541 |
|
char *s; |
542 |
|
long long rsect, wsect; |
543 |
|
|
544 |
< |
int nr = sscanf(line_ptr, |
578 |
< |
" %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld", |
544 |
> |
int nr = sscanf(line_ptr, format, |
545 |
|
&major, &minor, name, &rsect, &wsect); |
546 |
|
if (nr < 3) continue; |
581 |
– |
if (nr < 5) { |
582 |
– |
has_pp_stats = 0; |
583 |
– |
rsect = 0; |
584 |
– |
wsect = 0; |
585 |
– |
} |
547 |
|
|
548 |
|
/* Skip device names ending in numbers, since they're |
549 |
|
partitions. */ |
552 |
|
--s; |
553 |
|
if (*s >= '0' && *s <= '9') continue; |
554 |
|
|
555 |
< |
diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats, |
556 |
< |
diskio_stats); |
557 |
< |
if (diskio_stats == NULL) goto out; |
558 |
< |
if (n >= alloc_parts) { |
598 |
< |
alloc_parts += 16; |
599 |
< |
parts = realloc(parts, alloc_parts * sizeof *parts); |
600 |
< |
if (parts == NULL) { |
601 |
< |
alloc_parts = 0; |
602 |
< |
goto out; |
603 |
< |
} |
555 |
> |
if (nr < 5) { |
556 |
> |
has_pp_stats = 0; |
557 |
> |
rsect = 0; |
558 |
> |
wsect = 0; |
559 |
|
} |
560 |
|
|
561 |
+ |
if (VECTOR_RESIZE(diskio_stats, n + 1) < 0) { |
562 |
+ |
goto out; |
563 |
+ |
} |
564 |
+ |
if (VECTOR_RESIZE(parts, n + 1) < 0) { |
565 |
+ |
goto out; |
566 |
+ |
} |
567 |
+ |
|
568 |
|
if (diskio_stats[n].disk_name != NULL) |
569 |
|
free(diskio_stats[n].disk_name); |
570 |
|
diskio_stats[n].disk_name = strdup(name); |
577 |
|
n++; |
578 |
|
} |
579 |
|
|
580 |
+ |
fclose(f); |
581 |
+ |
f = NULL; |
582 |
+ |
|
583 |
|
if (!has_pp_stats) { |
584 |
< |
/* This is an older kernel without stats in /proc/partitions. |
585 |
< |
Read what we can from /proc/stat instead. */ |
584 |
> |
/* This is an older kernel where /proc/partitions doesn't |
585 |
> |
contain stats. Read what we can from /proc/stat instead, and |
586 |
> |
fill in the appropriate bits of the list allocated above. */ |
587 |
|
|
588 |
|
f = fopen("/proc/stat", "r"); |
589 |
|
if (f == NULL) goto out; |
650 |
|
num_diskio = n; |
651 |
|
out: |
652 |
|
if (f != NULL) fclose(f); |
653 |
+ |
#endif |
654 |
|
|
655 |
+ |
#ifdef CYGWIN |
656 |
+ |
return NULL; |
657 |
|
#endif |
658 |
+ |
|
659 |
|
*entries=num_diskio; |
660 |
|
|
661 |
|
return diskio_stats; |
662 |
|
} |
663 |
|
|
664 |
|
diskio_stat_t *get_diskio_stats_diff(int *entries){ |
665 |
< |
static diskio_stat_t *diskio_stats_diff=NULL; |
666 |
< |
static int sizeof_diskio_stats_diff=0; |
667 |
< |
diskio_stat_t *diskio_stats_diff_ptr, *diskio_stats_ptr; |
668 |
< |
int disks, x, y; |
665 |
> |
VECTOR_DECLARE_STATIC(diff, diskio_stat_t, 1, |
666 |
> |
diskio_stat_init, diskio_stat_destroy); |
667 |
> |
diskio_stat_t *src = NULL, *dest; |
668 |
> |
int i, j, diff_count, new_count; |
669 |
|
|
670 |
< |
if(diskio_stats==NULL){ |
671 |
< |
diskio_stats_ptr=get_diskio_stats(&disks); |
672 |
< |
*entries=disks; |
703 |
< |
return diskio_stats_ptr; |
670 |
> |
if (diskio_stats == NULL) { |
671 |
> |
/* No previous stats, so we can't calculate a difference. */ |
672 |
> |
return get_diskio_stats(entries); |
673 |
|
} |
674 |
|
|
675 |
< |
diskio_stats_diff=diskio_stat_malloc(num_diskio, &sizeof_diskio_stats_diff, diskio_stats_diff); |
676 |
< |
if(diskio_stats_diff==NULL){ |
675 |
> |
/* Resize the results array to match the previous stats. */ |
676 |
> |
diff_count = VECTOR_SIZE(diskio_stats); |
677 |
> |
if (VECTOR_RESIZE(diff, diff_count) < 0) { |
678 |
|
return NULL; |
679 |
|
} |
680 |
|
|
681 |
< |
diskio_stats_diff_ptr=diskio_stats_diff; |
682 |
< |
diskio_stats_ptr=diskio_stats; |
681 |
> |
/* Copy the previous stats into the result. */ |
682 |
> |
for (i = 0; i < diff_count; i++) { |
683 |
> |
src = &diskio_stats[i]; |
684 |
> |
dest = &diff[i]; |
685 |
|
|
686 |
< |
for(disks=0;disks<num_diskio;disks++){ |
687 |
< |
if(diskio_stats_diff_ptr->disk_name!=NULL){ |
716 |
< |
free(diskio_stats_diff_ptr->disk_name); |
686 |
> |
if (dest->disk_name != NULL) { |
687 |
> |
free(dest->disk_name); |
688 |
|
} |
689 |
< |
diskio_stats_diff_ptr->disk_name=strdup(diskio_stats_ptr->disk_name); |
690 |
< |
diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes; |
691 |
< |
diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes; |
692 |
< |
diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime; |
689 |
> |
dest->disk_name = strdup(src->disk_name); |
690 |
> |
dest->read_bytes = src->read_bytes; |
691 |
> |
dest->write_bytes = src->write_bytes; |
692 |
> |
dest->systime = src->systime; |
693 |
> |
} |
694 |
|
|
695 |
< |
diskio_stats_diff_ptr++; |
696 |
< |
diskio_stats_ptr++; |
695 |
> |
/* Get a new set of stats. */ |
696 |
> |
if (get_diskio_stats(&new_count) == NULL) { |
697 |
> |
return NULL; |
698 |
|
} |
699 |
|
|
700 |
< |
diskio_stats_ptr=get_diskio_stats(&disks); |
701 |
< |
diskio_stats_diff_ptr=diskio_stats_diff; |
700 |
> |
/* For each previous stat... */ |
701 |
> |
for (i = 0; i < diff_count; i++) { |
702 |
> |
dest = &diff[i]; |
703 |
|
|
704 |
< |
for(x=0;x<sizeof_diskio_stats_diff;x++){ |
705 |
< |
|
706 |
< |
if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){ |
707 |
< |
diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes; |
708 |
< |
diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes; |
709 |
< |
diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime; |
710 |
< |
}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++; |
704 |
> |
/* ... find the corresponding new stat ... */ |
705 |
> |
for (j = 0; j < new_count; j++) { |
706 |
> |
/* Try the new stat in the same position first, |
707 |
> |
since that's most likely to be it. */ |
708 |
> |
src = &diskio_stats[(i + j) % new_count]; |
709 |
> |
if (strcmp(src->disk_name, dest->disk_name) == 0) { |
710 |
> |
break; |
711 |
|
} |
712 |
|
} |
713 |
+ |
if (j == new_count) { |
714 |
+ |
/* No match found. */ |
715 |
+ |
continue; |
716 |
+ |
} |
717 |
|
|
718 |
< |
diskio_stats_ptr++; |
719 |
< |
diskio_stats_diff_ptr++; |
720 |
< |
|
718 |
> |
/* ... and subtract the previous stat from it to get the |
719 |
> |
difference. */ |
720 |
> |
dest->read_bytes = src->read_bytes - dest->read_bytes; |
721 |
> |
dest->write_bytes = src->write_bytes - dest->write_bytes; |
722 |
> |
dest->systime = src->systime - dest->systime; |
723 |
|
} |
724 |
< |
|
725 |
< |
*entries=sizeof_diskio_stats_diff; |
726 |
< |
return diskio_stats_diff; |
724 |
> |
|
725 |
> |
*entries = diff_count; |
726 |
> |
return diff; |
727 |
|
} |
728 |
+ |
|