1 |
|
/* |
2 |
< |
* i-scream central monitoring system |
2 |
> |
* i-scream libstatgrab |
3 |
|
* http://www.i-scream.org |
4 |
|
* Copyright (C) 2000-2004 i-scream |
5 |
|
* |
30 |
|
#include <string.h> |
31 |
|
#include <time.h> |
32 |
|
#include "statgrab.h" |
33 |
+ |
#include "vector.h" |
34 |
+ |
#include "tools.h" |
35 |
|
|
36 |
|
#ifdef SOLARIS |
37 |
|
#include <sys/mnttab.h> |
38 |
|
#include <sys/statvfs.h> |
39 |
|
#include <kstat.h> |
40 |
< |
#define VALID_FS_TYPES {"ufs", "tmpfs"} |
40 |
> |
#define VALID_FS_TYPES {"ufs", "tmpfs", "vxfs"} |
41 |
|
#endif |
42 |
|
|
43 |
|
#if defined(LINUX) || defined(CYGWIN) |
44 |
|
#include <mntent.h> |
45 |
|
#include <sys/vfs.h> |
44 |
– |
#include "tools.h" |
46 |
|
#endif |
47 |
|
|
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"} |
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 |
66 |
|
#include <sys/dkstat.h> |
67 |
|
#include <devstat.h> |
68 |
|
#define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \ |
69 |
< |
"ufs", "mfs"} |
69 |
> |
"ufs", "mfs"} |
70 |
|
#endif |
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"} |
76 |
> |
"ntfs"} |
77 |
|
#endif |
78 |
|
|
79 |
< |
#define START_VAL 1 |
79 |
> |
#ifdef HPUX |
80 |
> |
#include <sys/param.h> |
81 |
> |
#include <sys/pstat.h> |
82 |
> |
#include <sys/types.h> |
83 |
> |
#include <sys/stat.h> |
84 |
> |
#include <sys/vfs.h> |
85 |
> |
#include <mntent.h> |
86 |
> |
#include <dirent.h> |
87 |
> |
#include <stdio.h> |
88 |
> |
#include <time.h> |
89 |
> |
#define VALID_FS_TYPES {"vxfs", "hfs"} |
90 |
> |
#define DISK_BATCH 30 |
91 |
> |
#endif |
92 |
|
|
93 |
< |
char *copy_string(char *orig_ptr, const char *newtext){ |
94 |
< |
|
95 |
< |
/* Maybe free if not NULL, and strdup rather than realloc and strcpy? */ |
96 |
< |
orig_ptr=realloc(orig_ptr, (1+strlen(newtext))); |
84 |
< |
if(orig_ptr==NULL){ |
85 |
< |
return NULL; |
86 |
< |
} |
87 |
< |
strcpy(orig_ptr, newtext); |
88 |
< |
|
89 |
< |
return orig_ptr; |
93 |
> |
static void disk_stat_init(sg_fs_stats *d) { |
94 |
> |
d->device_name = NULL; |
95 |
> |
d->fs_type = NULL; |
96 |
> |
d->mnt_point = NULL; |
97 |
|
} |
98 |
|
|
99 |
< |
|
100 |
< |
void init_disk_stat(int start, int end, disk_stat_t *disk_stats){ |
101 |
< |
|
102 |
< |
for(disk_stats+=start; start<=end; start++){ |
96 |
< |
disk_stats->device_name=NULL; |
97 |
< |
disk_stats->fs_type=NULL; |
98 |
< |
disk_stats->mnt_point=NULL; |
99 |
< |
|
100 |
< |
disk_stats++; |
101 |
< |
} |
99 |
> |
static void disk_stat_destroy(sg_fs_stats *d) { |
100 |
> |
free(d->device_name); |
101 |
> |
free(d->fs_type); |
102 |
> |
free(d->mnt_point); |
103 |
|
} |
104 |
|
|
105 |
< |
int is_valid_fs_type(const char *type) { |
105 |
> |
static int is_valid_fs_type(const char *type) { |
106 |
|
const char *types[] = VALID_FS_TYPES; |
107 |
|
int i; |
108 |
|
|
114 |
|
return 0; |
115 |
|
} |
116 |
|
|
117 |
< |
disk_stat_t *get_disk_stats(int *entries){ |
117 |
> |
sg_fs_stats *sg_get_fs_stats(int *entries){ |
118 |
> |
VECTOR_DECLARE_STATIC(disk_stats, sg_fs_stats, 10, |
119 |
> |
disk_stat_init, disk_stat_destroy); |
120 |
|
|
118 |
– |
static disk_stat_t *disk_stats; |
119 |
– |
static int watermark=-1; |
120 |
– |
|
121 |
|
int valid_type; |
122 |
|
int num_disks=0; |
123 |
< |
#if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN) |
123 |
> |
#if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN) || defined(HPUX) |
124 |
|
FILE *f; |
125 |
|
#endif |
126 |
|
|
127 |
< |
disk_stat_t *disk_ptr; |
127 |
> |
sg_fs_stats *disk_ptr; |
128 |
|
|
129 |
|
#ifdef SOLARIS |
130 |
|
struct mnttab mp; |
131 |
|
struct statvfs fs; |
132 |
|
#endif |
133 |
< |
#if defined(LINUX) || defined(CYGWIN) |
133 |
> |
#if defined(LINUX) || defined(CYGWIN) || defined(HPUX) |
134 |
|
struct mntent *mp; |
135 |
|
struct statfs fs; |
136 |
|
#endif |
137 |
|
#ifdef ALLBSD |
138 |
|
int nummnt; |
139 |
+ |
#ifdef HAVE_STATVFS |
140 |
+ |
struct statvfs *mp; |
141 |
+ |
#else |
142 |
|
struct statfs *mp; |
143 |
|
#endif |
144 |
+ |
#endif |
145 |
|
|
142 |
– |
if(watermark==-1){ |
143 |
– |
disk_stats=malloc(START_VAL * sizeof(disk_stat_t)); |
144 |
– |
if(disk_stats==NULL){ |
145 |
– |
return NULL; |
146 |
– |
} |
147 |
– |
watermark=START_VAL; |
148 |
– |
init_disk_stat(0, watermark-1, disk_stats); |
149 |
– |
} |
146 |
|
#ifdef ALLBSD |
147 |
|
nummnt=getmntinfo(&mp , MNT_LOCAL); |
148 |
|
if (nummnt<=0){ |
149 |
+ |
sg_set_error_with_errno(SG_ERROR_GETMNTINFO, NULL); |
150 |
|
return NULL; |
151 |
|
} |
152 |
|
for(;nummnt--; mp++){ |
153 |
|
valid_type = is_valid_fs_type(mp->f_fstypename); |
154 |
|
#endif |
155 |
|
|
156 |
< |
#if defined(LINUX) || defined(CYGWIN) |
156 |
> |
#if defined(LINUX) || defined(CYGWIN) || defined(HPUX) |
157 |
> |
#ifdef MNT_MNTTAB |
158 |
> |
if ((f=setmntent(MNT_MNTTAB, "r" ))==NULL){ |
159 |
> |
#else |
160 |
|
if ((f=setmntent("/etc/mtab", "r" ))==NULL){ |
161 |
+ |
#endif |
162 |
+ |
sg_set_error(SG_ERROR_SETMNTENT, NULL); |
163 |
|
return NULL; |
164 |
|
} |
165 |
|
|
173 |
|
|
174 |
|
#ifdef SOLARIS |
175 |
|
if ((f=fopen("/etc/mnttab", "r" ))==NULL){ |
176 |
+ |
sg_set_error_with_errno(SG_ERROR_OPEN, "/etc/mnttab"); |
177 |
|
return NULL; |
178 |
|
} |
179 |
|
while((getmntent(f, &mp)) == 0){ |
184 |
|
#endif |
185 |
|
|
186 |
|
if(valid_type){ |
187 |
< |
if(num_disks>watermark-1){ |
188 |
< |
disk_ptr=disk_stats; |
186 |
< |
if((disk_stats=realloc(disk_stats, (watermark*2 * sizeof(disk_stat_t))))==NULL){ |
187 |
< |
disk_stats=disk_ptr; |
188 |
< |
return NULL; |
189 |
< |
} |
190 |
< |
|
191 |
< |
watermark=watermark*2; |
192 |
< |
init_disk_stat(num_disks, watermark-1, disk_stats); |
187 |
> |
if (VECTOR_RESIZE(disk_stats, num_disks + 1) < 0) { |
188 |
> |
return NULL; |
189 |
|
} |
194 |
– |
|
190 |
|
disk_ptr=disk_stats+num_disks; |
191 |
+ |
|
192 |
|
#ifdef ALLBSD |
193 |
< |
if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){ |
193 |
> |
if (sg_update_string(&disk_ptr->device_name, mp->f_mntfromname) < 0) { |
194 |
|
return NULL; |
195 |
|
} |
196 |
< |
|
201 |
< |
if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->f_fstypename))==NULL){ |
196 |
> |
if (sg_update_string(&disk_ptr->fs_type, mp->f_fstypename) < 0) { |
197 |
|
return NULL; |
198 |
|
} |
199 |
< |
|
205 |
< |
if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->f_mntonname))==NULL){ |
199 |
> |
if (sg_update_string(&disk_ptr->mnt_point, mp->f_mntonname) < 0) { |
200 |
|
return NULL; |
201 |
|
} |
202 |
|
|
209 |
|
/* Freebsd doesn't have a "available" inodes */ |
210 |
|
disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes; |
211 |
|
#endif |
212 |
< |
#if defined(LINUX) || defined(CYGWIN) |
213 |
< |
if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){ |
212 |
> |
#if defined(LINUX) || defined(CYGWIN) || defined(HPUX) |
213 |
> |
if (sg_update_string(&disk_ptr->device_name, mp->mnt_fsname) < 0) { |
214 |
|
return NULL; |
215 |
|
} |
216 |
|
|
217 |
< |
if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->mnt_type))==NULL){ |
217 |
> |
if (sg_update_string(&disk_ptr->fs_type, mp->mnt_type) < 0) { |
218 |
|
return NULL; |
219 |
|
} |
220 |
|
|
221 |
< |
if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->mnt_dir))==NULL){ |
221 |
> |
if (sg_update_string(&disk_ptr->mnt_point, mp->mnt_dir) < 0) { |
222 |
|
return NULL; |
223 |
|
} |
224 |
|
disk_ptr->size = (long long)fs.f_bsize * (long long)fs.f_blocks; |
232 |
|
#endif |
233 |
|
|
234 |
|
#ifdef SOLARIS |
241 |
– |
/* Memory leak in event of realloc failing */ |
235 |
|
/* Maybe make this char[bigenough] and do strncpy's and put a null in the end? |
236 |
|
* Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob |
237 |
|
* be upwards of a k each |
238 |
|
*/ |
239 |
< |
if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp.mnt_special))==NULL){ |
239 |
> |
if (sg_update_string(&disk_ptr->device_name, mp.mnt_special) < 0) { |
240 |
|
return NULL; |
241 |
|
} |
242 |
|
|
243 |
< |
if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp.mnt_fstype))==NULL){ |
244 |
< |
return NULL; |
245 |
< |
} |
243 |
> |
if (sg_update_string(&disk_ptr->fs_type, mp.mnt_fstype) < 0) { |
244 |
> |
return NULL; |
245 |
> |
} |
246 |
|
|
247 |
< |
if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp.mnt_mountp))==NULL){ |
248 |
< |
return NULL; |
249 |
< |
} |
247 |
> |
if (sg_update_string(&disk_ptr->mnt_point, mp.mnt_mountp) < 0) { |
248 |
> |
return NULL; |
249 |
> |
} |
250 |
|
|
251 |
|
disk_ptr->size = (long long)fs.f_frsize * (long long)fs.f_blocks; |
252 |
|
disk_ptr->avail = (long long)fs.f_frsize * (long long)fs.f_bavail; |
263 |
|
*entries=num_disks; |
264 |
|
|
265 |
|
/* If this fails, there is very little i can do about it, so |
266 |
< |
I'll ignore it :) */ |
267 |
< |
#if defined(LINUX) || defined(CYGWIN) |
266 |
> |
I'll ignore it :) */ |
267 |
> |
#if defined(LINUX) || defined(CYGWIN) || defined(HPUX) |
268 |
|
endmntent(f); |
269 |
|
#endif |
270 |
|
#if defined(SOLARIS) |
274 |
|
return disk_stats; |
275 |
|
|
276 |
|
} |
284 |
– |
void diskio_stat_init(int start, int end, diskio_stat_t *diskio_stats){ |
277 |
|
|
278 |
< |
for(diskio_stats+=start; start<end; start++){ |
279 |
< |
diskio_stats->disk_name=NULL; |
280 |
< |
|
281 |
< |
diskio_stats++; |
282 |
< |
} |
278 |
> |
int sg_fs_compare_device_name(const void *va, const void *vb) { |
279 |
> |
const sg_fs_stats *a = (const sg_fs_stats *)va; |
280 |
> |
const sg_fs_stats *b = (const sg_fs_stats *)vb; |
281 |
> |
|
282 |
> |
return strcmp(a->device_name, b->device_name); |
283 |
|
} |
284 |
|
|
285 |
< |
diskio_stat_t *diskio_stat_malloc(int needed_entries, int *cur_entries, diskio_stat_t *diskio_stats){ |
285 |
> |
int sg_fs_compare_mnt_point(const void *va, const void *vb) { |
286 |
> |
const sg_fs_stats *a = (const sg_fs_stats *)va; |
287 |
> |
const sg_fs_stats *b = (const sg_fs_stats *)vb; |
288 |
|
|
289 |
< |
if(diskio_stats==NULL){ |
289 |
> |
return strcmp(a->mnt_point, b->mnt_point); |
290 |
> |
} |
291 |
|
|
292 |
< |
if((diskio_stats=malloc(needed_entries * sizeof(diskio_stat_t)))==NULL){ |
293 |
< |
return NULL; |
294 |
< |
} |
300 |
< |
diskio_stat_init(0, needed_entries, diskio_stats); |
301 |
< |
*cur_entries=needed_entries; |
292 |
> |
static void diskio_stat_init(sg_disk_io_stats *d) { |
293 |
> |
d->disk_name = NULL; |
294 |
> |
} |
295 |
|
|
296 |
< |
return diskio_stats; |
297 |
< |
} |
305 |
< |
|
306 |
< |
|
307 |
< |
if(*cur_entries<needed_entries){ |
308 |
< |
diskio_stats=realloc(diskio_stats, (sizeof(diskio_stat_t)*needed_entries)); |
309 |
< |
if(diskio_stats==NULL){ |
310 |
< |
return NULL; |
311 |
< |
} |
312 |
< |
diskio_stat_init(*cur_entries, needed_entries, diskio_stats); |
313 |
< |
*cur_entries=needed_entries; |
314 |
< |
} |
315 |
< |
|
316 |
< |
return diskio_stats; |
296 |
> |
static void diskio_stat_destroy(sg_disk_io_stats *d) { |
297 |
> |
free(d->disk_name); |
298 |
|
} |
299 |
|
|
300 |
< |
static diskio_stat_t *diskio_stats=NULL; |
301 |
< |
static int num_diskio=0; |
300 |
> |
VECTOR_DECLARE_STATIC(diskio_stats, sg_disk_io_stats, 10, |
301 |
> |
diskio_stat_init, diskio_stat_destroy); |
302 |
|
|
303 |
|
#ifdef LINUX |
304 |
|
typedef struct { |
307 |
|
} partition; |
308 |
|
#endif |
309 |
|
|
310 |
< |
diskio_stat_t *get_diskio_stats(int *entries){ |
311 |
< |
|
331 |
< |
static int sizeof_diskio_stats=0; |
310 |
> |
sg_disk_io_stats *sg_get_disk_io_stats(int *entries){ |
311 |
> |
int num_diskio; |
312 |
|
#ifndef LINUX |
313 |
< |
diskio_stat_t *diskio_stats_ptr; |
313 |
> |
sg_disk_io_stats *diskio_stats_ptr; |
314 |
|
#endif |
315 |
|
|
316 |
+ |
#ifdef HPUX |
317 |
+ |
long long rbytes = 0, wbytes = 0; |
318 |
+ |
struct dirent *dinfo = NULL; |
319 |
+ |
struct stat lstatinfo; |
320 |
+ |
struct pst_diskinfo pstat_diskinfo[DISK_BATCH]; |
321 |
+ |
char fullpathbuf[1024] = {0}; |
322 |
+ |
dev_t diskid; |
323 |
+ |
DIR *dh = NULL; |
324 |
+ |
int diskidx = 0; |
325 |
+ |
int num, i; |
326 |
+ |
#endif |
327 |
|
#ifdef SOLARIS |
328 |
< |
kstat_ctl_t *kc; |
329 |
< |
kstat_t *ksp; |
328 |
> |
kstat_ctl_t *kc; |
329 |
> |
kstat_t *ksp; |
330 |
|
kstat_io_t kios; |
331 |
|
#endif |
332 |
|
#ifdef LINUX |
334 |
|
char *line_ptr; |
335 |
|
int major, minor; |
336 |
|
int has_pp_stats = 1; |
337 |
< |
static partition *parts = NULL; |
347 |
< |
static int alloc_parts = 0; |
337 |
> |
VECTOR_DECLARE_STATIC(parts, partition, 16, NULL, NULL); |
338 |
|
int i, n; |
339 |
|
time_t now; |
340 |
|
const char *format; |
371 |
|
|
372 |
|
num_diskio=0; |
373 |
|
|
374 |
+ |
#ifdef HPUX |
375 |
+ |
while (1) { |
376 |
+ |
num = pstat_getdisk(pstat_diskinfo, sizeof pstat_diskinfo[0], |
377 |
+ |
DISK_BATCH, diskidx); |
378 |
+ |
if (num == -1) { |
379 |
+ |
sg_set_error_with_errno(SG_ERROR_PSTAT, |
380 |
+ |
"pstat_getdisk"); |
381 |
+ |
return NULL; |
382 |
+ |
} else if (num == 0) { |
383 |
+ |
break; |
384 |
+ |
} |
385 |
+ |
|
386 |
+ |
for (i = 0; i < num; i++) { |
387 |
+ |
struct pst_diskinfo *di = &pstat_diskinfo[i]; |
388 |
+ |
|
389 |
+ |
/* Skip "disabled" disks. */ |
390 |
+ |
if (di->psd_status == 0) { |
391 |
+ |
continue; |
392 |
+ |
} |
393 |
+ |
|
394 |
+ |
/* We can't seperate the reads from the writes, we'll |
395 |
+ |
just give the same to each. */ |
396 |
+ |
rbytes = wbytes = di->psd_dkwds / 2; |
397 |
+ |
|
398 |
+ |
/* Skip unused disks. */ |
399 |
+ |
if (rbytes == 0 && wbytes == 0) { |
400 |
+ |
continue; |
401 |
+ |
} |
402 |
+ |
|
403 |
+ |
if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) { |
404 |
+ |
return NULL; |
405 |
+ |
} |
406 |
+ |
|
407 |
+ |
diskio_stats_ptr = diskio_stats + num_diskio; |
408 |
+ |
|
409 |
+ |
diskio_stats_ptr->read_bytes = rbytes; |
410 |
+ |
diskio_stats_ptr->write_bytes = wbytes; |
411 |
+ |
|
412 |
+ |
diskio_stats_ptr->systime = time(NULL); |
413 |
+ |
|
414 |
+ |
num_diskio++; |
415 |
+ |
|
416 |
+ |
/* FIXME This should use a static cache, like the Linux |
417 |
+ |
* code below. */ |
418 |
+ |
if (diskio_stats_ptr->disk_name == NULL) { |
419 |
+ |
dh = opendir("/dev/dsk"); |
420 |
+ |
if (dh == NULL) { |
421 |
+ |
continue; |
422 |
+ |
} |
423 |
+ |
|
424 |
+ |
diskid = (di->psd_dev.psd_major << 24) | di->psd_dev.psd_minor; |
425 |
+ |
while (1) { |
426 |
+ |
dinfo = readdir(dh); |
427 |
+ |
if (dinfo == NULL) { |
428 |
+ |
break; |
429 |
+ |
} |
430 |
+ |
snprintf(fullpathbuf, sizeof(fullpathbuf), "/dev/dsk/%s", dinfo->d_name); |
431 |
+ |
if (lstat(fullpathbuf, &lstatinfo) < 0) { |
432 |
+ |
continue; |
433 |
+ |
} |
434 |
+ |
|
435 |
+ |
if (lstatinfo.st_rdev == diskid) { |
436 |
+ |
if (sg_update_string(&diskio_stats_ptr->disk_name, dinfo->d_name) < 0) { |
437 |
+ |
return NULL; |
438 |
+ |
} |
439 |
+ |
break; |
440 |
+ |
} |
441 |
+ |
} |
442 |
+ |
closedir(dh); |
443 |
+ |
|
444 |
+ |
if (diskio_stats_ptr->disk_name == NULL) { |
445 |
+ |
if (sg_update_string(&diskio_stats_ptr->disk_name, di->psd_hw_path.psh_name) < 0) { |
446 |
+ |
return NULL; |
447 |
+ |
} |
448 |
+ |
} |
449 |
+ |
} |
450 |
+ |
} |
451 |
+ |
diskidx = pstat_diskinfo[num - 1].psd_idx + 1; |
452 |
+ |
} |
453 |
+ |
#endif |
454 |
|
#ifdef OPENBSD |
455 |
|
mib[0] = CTL_HW; |
456 |
|
mib[1] = HW_DISKCOUNT; |
457 |
|
|
458 |
|
size = sizeof(diskcount); |
459 |
|
if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) { |
460 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKCOUNT"); |
461 |
|
return NULL; |
462 |
|
} |
463 |
|
|
465 |
|
mib[1] = HW_DISKNAMES; |
466 |
|
|
467 |
|
if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) { |
468 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES"); |
469 |
|
return NULL; |
470 |
|
} |
471 |
|
|
472 |
< |
disknames = malloc(size); |
472 |
> |
disknames = sg_malloc(size); |
473 |
|
if (disknames == NULL) { |
474 |
|
return NULL; |
475 |
|
} |
476 |
|
|
477 |
|
if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) { |
478 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES"); |
479 |
|
return NULL; |
480 |
|
} |
481 |
|
|
482 |
< |
dk_name = calloc(diskcount, sizeof(char *)); |
482 |
> |
dk_name = sg_malloc(diskcount * sizeof(char *)); |
483 |
|
bufpp = disknames; |
484 |
|
for (i = 0; i < diskcount && (name = strsep(&bufpp, ",")) != NULL; i++) { |
485 |
|
dk_name[i] = name; |
494 |
|
#endif |
495 |
|
|
496 |
|
if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) { |
497 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS"); |
498 |
|
return NULL; |
499 |
|
} |
500 |
|
|
504 |
|
num_disks = size / sizeof(struct diskstats); |
505 |
|
#endif |
506 |
|
|
507 |
< |
stats = malloc(size); |
507 |
> |
stats = sg_malloc(size); |
508 |
|
if (stats == NULL) { |
509 |
|
return NULL; |
510 |
|
} |
511 |
|
|
512 |
|
if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) { |
513 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS"); |
514 |
|
return NULL; |
515 |
|
} |
516 |
|
|
517 |
|
for (i = 0; i < num_disks; i++) { |
518 |
+ |
const char *name; |
519 |
|
u_int64_t rbytes, wbytes; |
520 |
|
|
521 |
|
#ifdef NETBSD |
523 |
|
rbytes = stats[i].dk_rbytes; |
524 |
|
wbytes = stats[i].dk_wbytes; |
525 |
|
#else |
526 |
< |
/* Before 1.7, NetBSD merged reads and writes. */ |
526 |
> |
/* Before 2.0, NetBSD merged reads and writes. */ |
527 |
|
rbytes = wbytes = stats[i].dk_bytes; |
528 |
|
#endif |
529 |
|
#else |
530 |
+ |
#ifdef HAVE_DS_RBYTES |
531 |
+ |
rbytes = stats[i].ds_rbytes; |
532 |
+ |
wbytes = stats[i].ds_wbytes; |
533 |
+ |
#else |
534 |
+ |
/* Before 3.5, OpenBSD merged reads and writes */ |
535 |
|
rbytes = wbytes = stats[i].ds_bytes; |
536 |
|
#endif |
537 |
+ |
#endif |
538 |
|
|
539 |
|
/* Don't keep stats for disks that have never been used. */ |
540 |
|
if (rbytes == 0 && wbytes == 0) { |
541 |
|
continue; |
542 |
|
} |
543 |
|
|
544 |
< |
diskio_stats = diskio_stat_malloc(num_diskio + 1, |
463 |
< |
&sizeof_diskio_stats, |
464 |
< |
diskio_stats); |
465 |
< |
if (diskio_stats == NULL) { |
544 |
> |
if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) { |
545 |
|
return NULL; |
546 |
|
} |
547 |
|
diskio_stats_ptr = diskio_stats + num_diskio; |
548 |
|
|
549 |
|
diskio_stats_ptr->read_bytes = rbytes; |
550 |
|
diskio_stats_ptr->write_bytes = wbytes; |
472 |
– |
if (diskio_stats_ptr->disk_name != NULL) { |
473 |
– |
free(diskio_stats_ptr->disk_name); |
474 |
– |
} |
551 |
|
#ifdef NETBSD |
552 |
< |
diskio_stats_ptr->disk_name = strdup(stats[i].dk_name); |
552 |
> |
name = stats[i].dk_name; |
553 |
|
#else |
554 |
< |
diskio_stats_ptr->disk_name = strdup(dk_name[i]); |
554 |
> |
name = dk_name[i]; |
555 |
|
#endif |
556 |
+ |
if (sg_update_string(&diskio_stats_ptr->disk_name, name) < 0) { |
557 |
+ |
return NULL; |
558 |
+ |
} |
559 |
|
diskio_stats_ptr->systime = time(NULL); |
560 |
|
|
561 |
|
num_diskio++; |
570 |
|
|
571 |
|
#if defined(FREEBSD) || defined(DFBSD) |
572 |
|
if (!stats_init) { |
573 |
< |
stats.dinfo=malloc(sizeof(struct devinfo)); |
573 |
> |
stats.dinfo=sg_malloc(sizeof(struct devinfo)); |
574 |
|
if(stats.dinfo==NULL) return NULL; |
575 |
|
bzero(stats.dinfo, sizeof(struct devinfo)); |
576 |
|
stats_init = 1; |
577 |
|
} |
578 |
|
#ifdef FREEBSD5 |
579 |
< |
if ((devstat_getdevs(NULL, &stats)) < 0) return NULL; |
579 |
> |
if ((devstat_getdevs(NULL, &stats)) < 0) { |
580 |
> |
/* FIXME devstat functions return a string error in |
581 |
> |
devstat_errbuf */ |
582 |
> |
sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL); |
583 |
> |
return NULL; |
584 |
> |
} |
585 |
|
/* Not aware of a get all devices, so i said 999. If we ever |
586 |
|
* find a machine with more than 999 disks, then i'll change |
587 |
|
* this number :) |
588 |
|
*/ |
589 |
< |
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; |
589 |
> |
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) { |
590 |
> |
sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL); |
591 |
> |
return NULL; |
592 |
> |
} |
593 |
|
#else |
594 |
< |
if ((getdevs(&stats)) < 0) return NULL; |
594 |
> |
if ((getdevs(&stats)) < 0) { |
595 |
> |
sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL); |
596 |
> |
return NULL; |
597 |
> |
} |
598 |
|
/* Not aware of a get all devices, so i said 999. If we ever |
599 |
|
* find a machine with more than 999 disks, then i'll change |
600 |
|
* this number :) |
601 |
|
*/ |
602 |
< |
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; |
602 |
> |
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) { |
603 |
> |
sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL); |
604 |
> |
return NULL; |
605 |
> |
} |
606 |
|
#endif |
607 |
|
|
608 |
|
for(counter=0;counter<stats.dinfo->numdevs;counter++){ |
617 |
|
#else |
618 |
|
if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue; |
619 |
|
#endif |
620 |
< |
if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){ |
620 |
> |
|
621 |
> |
if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) { |
622 |
|
return NULL; |
623 |
|
} |
624 |
|
diskio_stats_ptr=diskio_stats+num_diskio; |
631 |
|
diskio_stats_ptr->write_bytes=dev_ptr->bytes_written; |
632 |
|
#endif |
633 |
|
if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name); |
634 |
< |
asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number); |
634 |
> |
if (asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number) == -1) { |
635 |
> |
sg_set_error_with_errno(SG_ERROR_ASPRINTF, NULL); |
636 |
> |
return NULL; |
637 |
> |
} |
638 |
|
diskio_stats_ptr->systime=time(NULL); |
639 |
|
|
640 |
|
num_diskio++; |
644 |
|
#endif |
645 |
|
#ifdef SOLARIS |
646 |
|
if ((kc = kstat_open()) == NULL) { |
647 |
< |
return NULL; |
648 |
< |
} |
647 |
> |
sg_set_error(SG_ERROR_KSTAT_OPEN, NULL); |
648 |
> |
return NULL; |
649 |
> |
} |
650 |
|
|
651 |
|
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
652 |
< |
if (!strcmp(ksp->ks_class, "disk")) { |
652 |
> |
if (!strcmp(ksp->ks_class, "disk")) { |
653 |
|
|
654 |
|
if(ksp->ks_type != KSTAT_TYPE_IO) continue; |
655 |
|
/* We dont want metadevices appearins as num_diskio */ |
656 |
|
if(strcmp(ksp->ks_module, "md")==0) continue; |
657 |
< |
if((kstat_read(kc, ksp, &kios))==-1){ |
657 |
> |
if((kstat_read(kc, ksp, &kios))==-1){ |
658 |
|
} |
659 |
|
|
660 |
< |
if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){ |
660 |
> |
if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) { |
661 |
|
kstat_close(kc); |
662 |
|
return NULL; |
663 |
|
} |
664 |
|
diskio_stats_ptr=diskio_stats+num_diskio; |
665 |
|
|
666 |
|
diskio_stats_ptr->read_bytes=kios.nread; |
569 |
– |
|
667 |
|
diskio_stats_ptr->write_bytes=kios.nwritten; |
668 |
< |
|
669 |
< |
if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name); |
670 |
< |
|
671 |
< |
diskio_stats_ptr->disk_name=strdup((char *) get_svr_from_bsd(ksp->ks_name)); |
668 |
> |
if (sg_update_string(&diskio_stats_ptr->disk_name, |
669 |
> |
sg_get_svr_from_bsd(ksp->ks_name)) < 0) { |
670 |
> |
return NULL; |
671 |
> |
} |
672 |
|
diskio_stats_ptr->systime=time(NULL); |
673 |
+ |
|
674 |
|
num_diskio++; |
675 |
|
} |
676 |
|
} |
688 |
|
the same format. */ |
689 |
|
|
690 |
|
f = fopen("/proc/diskstats", "r"); |
691 |
< |
format = " %d %d %19s %*d %*d %lld %*d %*d %*d %lld"; |
691 |
> |
format = " %d %d %99s %*d %*d %lld %*d %*d %*d %lld"; |
692 |
|
if (f == NULL) { |
693 |
|
f = fopen("/proc/partitions", "r"); |
694 |
< |
format = " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld"; |
694 |
> |
format = " %d %d %*d %99s %*d %*d %lld %*d %*d %*d %lld"; |
695 |
|
} |
696 |
|
if (f == NULL) goto out; |
697 |
|
now = time(NULL); |
698 |
|
|
699 |
< |
while ((line_ptr = f_read_line(f, "")) != NULL) { |
700 |
< |
char name[20]; |
699 |
> |
while ((line_ptr = sg_f_read_line(f, "")) != NULL) { |
700 |
> |
char name[100]; |
701 |
|
char *s; |
702 |
|
long long rsect, wsect; |
703 |
|
|
718 |
|
wsect = 0; |
719 |
|
} |
720 |
|
|
721 |
< |
diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats, |
722 |
< |
diskio_stats); |
625 |
< |
if (diskio_stats == NULL) goto out; |
626 |
< |
if (n >= alloc_parts) { |
627 |
< |
alloc_parts += 16; |
628 |
< |
parts = realloc(parts, alloc_parts * sizeof *parts); |
629 |
< |
if (parts == NULL) { |
630 |
< |
alloc_parts = 0; |
631 |
< |
goto out; |
632 |
< |
} |
721 |
> |
if (VECTOR_RESIZE(diskio_stats, n + 1) < 0) { |
722 |
> |
goto out; |
723 |
|
} |
724 |
+ |
if (VECTOR_RESIZE(parts, n + 1) < 0) { |
725 |
+ |
goto out; |
726 |
+ |
} |
727 |
|
|
728 |
< |
if (diskio_stats[n].disk_name != NULL) |
729 |
< |
free(diskio_stats[n].disk_name); |
730 |
< |
diskio_stats[n].disk_name = strdup(name); |
728 |
> |
if (sg_update_string(&diskio_stats[n].disk_name, name) < 0) { |
729 |
> |
goto out; |
730 |
> |
} |
731 |
|
diskio_stats[n].read_bytes = rsect * 512; |
732 |
|
diskio_stats[n].write_bytes = wsect * 512; |
733 |
|
diskio_stats[n].systime = now; |
749 |
|
if (f == NULL) goto out; |
750 |
|
now = time(NULL); |
751 |
|
|
752 |
< |
line_ptr = f_read_line(f, "disk_io:"); |
752 |
> |
line_ptr = sg_f_read_line(f, "disk_io:"); |
753 |
|
if (line_ptr == NULL) goto out; |
754 |
|
|
755 |
|
while((line_ptr=strchr(line_ptr, ' '))!=NULL){ |
813 |
|
#endif |
814 |
|
|
815 |
|
#ifdef CYGWIN |
816 |
+ |
sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin"); |
817 |
|
return NULL; |
818 |
|
#endif |
819 |
|
|
822 |
|
return diskio_stats; |
823 |
|
} |
824 |
|
|
825 |
< |
diskio_stat_t *get_diskio_stats_diff(int *entries){ |
826 |
< |
static diskio_stat_t *diff = NULL; |
827 |
< |
static int diff_count = 0; |
828 |
< |
diskio_stat_t *src, *dest; |
829 |
< |
int i, j, new_count; |
825 |
> |
sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entries){ |
826 |
> |
VECTOR_DECLARE_STATIC(diff, sg_disk_io_stats, 1, |
827 |
> |
diskio_stat_init, diskio_stat_destroy); |
828 |
> |
sg_disk_io_stats *src = NULL, *dest; |
829 |
> |
int i, j, diff_count, new_count; |
830 |
|
|
831 |
|
if (diskio_stats == NULL) { |
832 |
|
/* No previous stats, so we can't calculate a difference. */ |
833 |
< |
return get_diskio_stats(entries); |
833 |
> |
return sg_get_disk_io_stats(entries); |
834 |
|
} |
835 |
|
|
836 |
|
/* Resize the results array to match the previous stats. */ |
837 |
< |
diff = diskio_stat_malloc(num_diskio, &diff_count, diff); |
838 |
< |
if (diff == NULL) { |
837 |
> |
diff_count = VECTOR_SIZE(diskio_stats); |
838 |
> |
if (VECTOR_RESIZE(diff, diff_count) < 0) { |
839 |
|
return NULL; |
840 |
|
} |
841 |
|
|
844 |
|
src = &diskio_stats[i]; |
845 |
|
dest = &diff[i]; |
846 |
|
|
847 |
< |
if (dest->disk_name != NULL) { |
848 |
< |
free(dest->disk_name); |
847 |
> |
if (sg_update_string(&dest->disk_name, src->disk_name) < 0) { |
848 |
> |
return NULL; |
849 |
|
} |
756 |
– |
dest->disk_name = strdup(src->disk_name); |
850 |
|
dest->read_bytes = src->read_bytes; |
851 |
|
dest->write_bytes = src->write_bytes; |
852 |
|
dest->systime = src->systime; |
853 |
|
} |
854 |
|
|
855 |
|
/* Get a new set of stats. */ |
856 |
< |
if (get_diskio_stats(&new_count) == NULL) { |
856 |
> |
if (sg_get_disk_io_stats(&new_count) == NULL) { |
857 |
|
return NULL; |
858 |
|
} |
859 |
|
|
884 |
|
|
885 |
|
*entries = diff_count; |
886 |
|
return diff; |
887 |
+ |
} |
888 |
+ |
|
889 |
+ |
int sg_disk_io_compare_name(const void *va, const void *vb) { |
890 |
+ |
const sg_disk_io_stats *a = (const sg_disk_io_stats *)va; |
891 |
+ |
const sg_disk_io_stats *b = (const sg_disk_io_stats *)vb; |
892 |
+ |
|
893 |
+ |
return strcmp(a->disk_name, b->disk_name); |
894 |
|
} |
895 |
|
|