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 |
|
* |
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> |
43 |
|
#if defined(LINUX) || defined(CYGWIN) |
44 |
|
#include <mntent.h> |
45 |
|
#include <sys/vfs.h> |
45 |
– |
#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 |
< |
char *copy_string(char *orig_ptr, const char *newtext){ |
80 |
< |
|
81 |
< |
/* Maybe free if not NULL, and strdup rather than realloc and strcpy? */ |
82 |
< |
orig_ptr=realloc(orig_ptr, (1+strlen(newtext))); |
83 |
< |
if(orig_ptr==NULL){ |
84 |
< |
return NULL; |
85 |
< |
} |
86 |
< |
strcpy(orig_ptr, newtext); |
87 |
< |
|
88 |
< |
return orig_ptr; |
89 |
< |
} |
90 |
< |
|
91 |
< |
static void disk_stat_init(disk_stat_t *d) { |
79 |
> |
static void disk_stat_init(sg_fs_stats *d) { |
80 |
|
d->device_name = NULL; |
81 |
|
d->fs_type = NULL; |
82 |
|
d->mnt_point = NULL; |
83 |
|
} |
84 |
|
|
85 |
< |
static void disk_stat_destroy(disk_stat_t *d) { |
85 |
> |
static void disk_stat_destroy(sg_fs_stats *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) { |
91 |
> |
static int is_valid_fs_type(const char *type) { |
92 |
|
const char *types[] = VALID_FS_TYPES; |
93 |
|
int i; |
94 |
|
|
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); |
103 |
> |
sg_fs_stats *sg_get_fs_stats(int *entries){ |
104 |
> |
VECTOR_DECLARE_STATIC(disk_stats, sg_fs_stats, 10, |
105 |
> |
disk_stat_init, disk_stat_destroy); |
106 |
|
|
107 |
|
int valid_type; |
108 |
|
int num_disks=0; |
110 |
|
FILE *f; |
111 |
|
#endif |
112 |
|
|
113 |
< |
disk_stat_t *disk_ptr; |
113 |
> |
sg_fs_stats *disk_ptr; |
114 |
|
|
115 |
|
#ifdef SOLARIS |
116 |
|
struct mnttab mp; |
165 |
|
disk_ptr=disk_stats+num_disks; |
166 |
|
|
167 |
|
#ifdef ALLBSD |
168 |
< |
if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){ |
168 |
> |
if (sg_update_string(&disk_ptr->device_name, mp->f_mntfromname) < 0) { |
169 |
|
return NULL; |
170 |
|
} |
171 |
< |
|
184 |
< |
if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->f_fstypename))==NULL){ |
171 |
> |
if (sg_update_string(&disk_ptr->fs_type, mp->f_fstypename) < 0) { |
172 |
|
return NULL; |
173 |
|
} |
174 |
< |
|
188 |
< |
if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->f_mntonname))==NULL){ |
174 |
> |
if (sg_update_string(&disk_ptr->mnt_point, mp->f_mntonname) < 0) { |
175 |
|
return NULL; |
176 |
|
} |
177 |
|
|
185 |
|
disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes; |
186 |
|
#endif |
187 |
|
#if defined(LINUX) || defined(CYGWIN) |
188 |
< |
if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){ |
188 |
> |
if (sg_update_string(&disk_ptr->device_name, mp->mnt_fsname) < 0) { |
189 |
|
return NULL; |
190 |
|
} |
191 |
|
|
192 |
< |
if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->mnt_type))==NULL){ |
192 |
> |
if (sg_update_string(&disk_ptr->fs_type, mp->mnt_type) < 0) { |
193 |
|
return NULL; |
194 |
|
} |
195 |
|
|
196 |
< |
if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->mnt_dir))==NULL){ |
196 |
> |
if (sg_update_string(&disk_ptr->mnt_point, mp->mnt_dir) < 0) { |
197 |
|
return NULL; |
198 |
|
} |
199 |
|
disk_ptr->size = (long long)fs.f_bsize * (long long)fs.f_blocks; |
207 |
|
#endif |
208 |
|
|
209 |
|
#ifdef SOLARIS |
224 |
– |
/* 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 (sg_update_string(&disk_ptr->device_name, mp.mnt_special) < 0) { |
215 |
|
return NULL; |
216 |
|
} |
217 |
|
|
218 |
< |
if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp.mnt_fstype))==NULL){ |
219 |
< |
return NULL; |
220 |
< |
} |
218 |
> |
if (sg_update_string(&disk_ptr->fs_type, mp.mnt_fstype) < 0) { |
219 |
> |
return NULL; |
220 |
> |
} |
221 |
|
|
222 |
< |
if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp.mnt_mountp))==NULL){ |
223 |
< |
return NULL; |
224 |
< |
} |
222 |
> |
if (sg_update_string(&disk_ptr->mnt_point, mp.mnt_mountp) < 0) { |
223 |
> |
return NULL; |
224 |
> |
} |
225 |
|
|
226 |
|
disk_ptr->size = (long long)fs.f_frsize * (long long)fs.f_blocks; |
227 |
|
disk_ptr->avail = (long long)fs.f_frsize * (long long)fs.f_bavail; |
238 |
|
*entries=num_disks; |
239 |
|
|
240 |
|
/* If this fails, there is very little i can do about it, so |
241 |
< |
I'll ignore it :) */ |
241 |
> |
I'll ignore it :) */ |
242 |
|
#if defined(LINUX) || defined(CYGWIN) |
243 |
|
endmntent(f); |
244 |
|
#endif |
250 |
|
|
251 |
|
} |
252 |
|
|
253 |
< |
static void diskio_stat_init(diskio_stat_t *d) { |
253 |
> |
static void diskio_stat_init(sg_disk_io_stats *d) { |
254 |
|
d->disk_name = NULL; |
255 |
|
} |
256 |
|
|
257 |
< |
static void diskio_stat_destroy(diskio_stat_t *d) { |
257 |
> |
static void diskio_stat_destroy(sg_disk_io_stats *d) { |
258 |
|
free(d->disk_name); |
259 |
|
} |
260 |
|
|
261 |
< |
VECTOR_DECLARE_STATIC(diskio_stats, diskio_stat_t, 10, |
262 |
< |
diskio_stat_init, diskio_stat_destroy); |
261 |
> |
VECTOR_DECLARE_STATIC(diskio_stats, sg_disk_io_stats, 10, |
262 |
> |
diskio_stat_init, diskio_stat_destroy); |
263 |
|
|
264 |
|
#ifdef LINUX |
265 |
|
typedef struct { |
268 |
|
} partition; |
269 |
|
#endif |
270 |
|
|
271 |
< |
diskio_stat_t *get_diskio_stats(int *entries){ |
271 |
> |
sg_disk_io_stats *sg_get_disk_io_stats(int *entries){ |
272 |
|
int num_diskio; |
273 |
|
#ifndef LINUX |
274 |
< |
diskio_stat_t *diskio_stats_ptr; |
274 |
> |
sg_disk_io_stats *diskio_stats_ptr; |
275 |
|
#endif |
276 |
|
|
277 |
|
#ifdef SOLARIS |
278 |
< |
kstat_ctl_t *kc; |
279 |
< |
kstat_t *ksp; |
278 |
> |
kstat_ctl_t *kc; |
279 |
> |
kstat_t *ksp; |
280 |
|
kstat_io_t kios; |
281 |
|
#endif |
282 |
|
#ifdef LINUX |
284 |
|
char *line_ptr; |
285 |
|
int major, minor; |
286 |
|
int has_pp_stats = 1; |
287 |
< |
static partition *parts = NULL; |
303 |
< |
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; |
337 |
|
return NULL; |
338 |
|
} |
339 |
|
|
340 |
< |
disknames = malloc(size); |
340 |
> |
disknames = sg_malloc(size); |
341 |
|
if (disknames == NULL) { |
342 |
|
return NULL; |
343 |
|
} |
346 |
|
return NULL; |
347 |
|
} |
348 |
|
|
349 |
< |
dk_name = calloc(diskcount, sizeof(char *)); |
349 |
> |
dk_name = sg_malloc(diskcount * sizeof(char *)); |
350 |
|
bufpp = disknames; |
351 |
|
for (i = 0; i < diskcount && (name = strsep(&bufpp, ",")) != NULL; i++) { |
352 |
|
dk_name[i] = name; |
370 |
|
num_disks = size / sizeof(struct diskstats); |
371 |
|
#endif |
372 |
|
|
373 |
< |
stats = malloc(size); |
373 |
> |
stats = sg_malloc(size); |
374 |
|
if (stats == NULL) { |
375 |
|
return NULL; |
376 |
|
} |
380 |
|
} |
381 |
|
|
382 |
|
for (i = 0; i < num_disks; i++) { |
383 |
+ |
const char *name; |
384 |
|
u_int64_t rbytes, wbytes; |
385 |
|
|
386 |
|
#ifdef NETBSD |
407 |
|
|
408 |
|
diskio_stats_ptr->read_bytes = rbytes; |
409 |
|
diskio_stats_ptr->write_bytes = wbytes; |
425 |
– |
if (diskio_stats_ptr->disk_name != NULL) { |
426 |
– |
free(diskio_stats_ptr->disk_name); |
427 |
– |
} |
410 |
|
#ifdef NETBSD |
411 |
< |
diskio_stats_ptr->disk_name = strdup(stats[i].dk_name); |
411 |
> |
name = stats[i].dk_name; |
412 |
|
#else |
413 |
< |
diskio_stats_ptr->disk_name = strdup(dk_name[i]); |
413 |
> |
name = dk_name[i]; |
414 |
|
#endif |
415 |
+ |
if (sg_update_string(&diskio_stats_ptr->disk_name, name) < 0) { |
416 |
+ |
return NULL; |
417 |
+ |
} |
418 |
|
diskio_stats_ptr->systime = time(NULL); |
419 |
|
|
420 |
|
num_diskio++; |
429 |
|
|
430 |
|
#if defined(FREEBSD) || defined(DFBSD) |
431 |
|
if (!stats_init) { |
432 |
< |
stats.dinfo=malloc(sizeof(struct devinfo)); |
432 |
> |
stats.dinfo=sg_malloc(sizeof(struct devinfo)); |
433 |
|
if(stats.dinfo==NULL) return NULL; |
434 |
|
bzero(stats.dinfo, sizeof(struct devinfo)); |
435 |
|
stats_init = 1; |
476 |
|
diskio_stats_ptr->write_bytes=dev_ptr->bytes_written; |
477 |
|
#endif |
478 |
|
if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name); |
479 |
< |
asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number); |
479 |
> |
if (asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number) == -1) { |
480 |
> |
return NULL; |
481 |
> |
} |
482 |
|
diskio_stats_ptr->systime=time(NULL); |
483 |
|
|
484 |
|
num_diskio++; |
488 |
|
#endif |
489 |
|
#ifdef SOLARIS |
490 |
|
if ((kc = kstat_open()) == NULL) { |
491 |
< |
return NULL; |
492 |
< |
} |
491 |
> |
return NULL; |
492 |
> |
} |
493 |
|
|
494 |
|
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
495 |
< |
if (!strcmp(ksp->ks_class, "disk")) { |
495 |
> |
if (!strcmp(ksp->ks_class, "disk")) { |
496 |
|
|
497 |
|
if(ksp->ks_type != KSTAT_TYPE_IO) continue; |
498 |
|
/* We dont want metadevices appearins as num_diskio */ |
499 |
|
if(strcmp(ksp->ks_module, "md")==0) continue; |
500 |
< |
if((kstat_read(kc, ksp, &kios))==-1){ |
500 |
> |
if((kstat_read(kc, ksp, &kios))==-1){ |
501 |
|
} |
502 |
|
|
503 |
|
if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) { |
507 |
|
diskio_stats_ptr=diskio_stats+num_diskio; |
508 |
|
|
509 |
|
diskio_stats_ptr->read_bytes=kios.nread; |
523 |
– |
|
510 |
|
diskio_stats_ptr->write_bytes=kios.nwritten; |
511 |
< |
|
512 |
< |
if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name); |
513 |
< |
|
514 |
< |
diskio_stats_ptr->disk_name=strdup((char *) get_svr_from_bsd(ksp->ks_name)); |
511 |
> |
if (sg_update_string(&diskio_stats_ptr->disk_name, |
512 |
> |
sg_get_svr_from_bsd(ksp->ks_name)) < 0) { |
513 |
> |
return NULL; |
514 |
> |
} |
515 |
|
diskio_stats_ptr->systime=time(NULL); |
516 |
+ |
|
517 |
|
num_diskio++; |
518 |
|
} |
519 |
|
} |
539 |
|
if (f == NULL) goto out; |
540 |
|
now = time(NULL); |
541 |
|
|
542 |
< |
while ((line_ptr = f_read_line(f, "")) != NULL) { |
542 |
> |
while ((line_ptr = sg_f_read_line(f, "")) != NULL) { |
543 |
|
char name[100]; |
544 |
|
char *s; |
545 |
|
long long rsect, wsect; |
564 |
|
if (VECTOR_RESIZE(diskio_stats, n + 1) < 0) { |
565 |
|
goto out; |
566 |
|
} |
567 |
< |
if (n >= alloc_parts) { |
568 |
< |
alloc_parts += 16; |
582 |
< |
parts = realloc(parts, alloc_parts * sizeof *parts); |
583 |
< |
if (parts == NULL) { |
584 |
< |
alloc_parts = 0; |
585 |
< |
goto out; |
586 |
< |
} |
567 |
> |
if (VECTOR_RESIZE(parts, n + 1) < 0) { |
568 |
> |
goto out; |
569 |
|
} |
570 |
|
|
571 |
< |
if (diskio_stats[n].disk_name != NULL) |
572 |
< |
free(diskio_stats[n].disk_name); |
573 |
< |
diskio_stats[n].disk_name = strdup(name); |
571 |
> |
if (sg_update_string(&diskio_stats[n].disk_name, name) < 0) { |
572 |
> |
goto out; |
573 |
> |
} |
574 |
|
diskio_stats[n].read_bytes = rsect * 512; |
575 |
|
diskio_stats[n].write_bytes = wsect * 512; |
576 |
|
diskio_stats[n].systime = now; |
592 |
|
if (f == NULL) goto out; |
593 |
|
now = time(NULL); |
594 |
|
|
595 |
< |
line_ptr = f_read_line(f, "disk_io:"); |
595 |
> |
line_ptr = sg_f_read_line(f, "disk_io:"); |
596 |
|
if (line_ptr == NULL) goto out; |
597 |
|
|
598 |
|
while((line_ptr=strchr(line_ptr, ' '))!=NULL){ |
664 |
|
return diskio_stats; |
665 |
|
} |
666 |
|
|
667 |
< |
diskio_stat_t *get_diskio_stats_diff(int *entries){ |
668 |
< |
VECTOR_DECLARE_STATIC(diff, diskio_stat_t, 1, |
669 |
< |
diskio_stat_init, diskio_stat_destroy); |
670 |
< |
diskio_stat_t *src = NULL, *dest; |
667 |
> |
sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entries){ |
668 |
> |
VECTOR_DECLARE_STATIC(diff, sg_disk_io_stats, 1, |
669 |
> |
diskio_stat_init, diskio_stat_destroy); |
670 |
> |
sg_disk_io_stats *src = NULL, *dest; |
671 |
|
int i, j, diff_count, new_count; |
672 |
|
|
673 |
|
if (diskio_stats == NULL) { |
674 |
|
/* No previous stats, so we can't calculate a difference. */ |
675 |
< |
return get_diskio_stats(entries); |
675 |
> |
return sg_get_disk_io_stats(entries); |
676 |
|
} |
677 |
|
|
678 |
|
/* Resize the results array to match the previous stats. */ |
686 |
|
src = &diskio_stats[i]; |
687 |
|
dest = &diff[i]; |
688 |
|
|
689 |
< |
if (dest->disk_name != NULL) { |
690 |
< |
free(dest->disk_name); |
689 |
> |
if (sg_update_string(&dest->disk_name, src->disk_name) < 0) { |
690 |
> |
return NULL; |
691 |
|
} |
710 |
– |
dest->disk_name = strdup(src->disk_name); |
692 |
|
dest->read_bytes = src->read_bytes; |
693 |
|
dest->write_bytes = src->write_bytes; |
694 |
|
dest->systime = src->systime; |
695 |
|
} |
696 |
|
|
697 |
|
/* Get a new set of stats. */ |
698 |
< |
if (get_diskio_stats(&new_count) == NULL) { |
698 |
> |
if (sg_get_disk_io_stats(&new_count) == NULL) { |
699 |
|
return NULL; |
700 |
|
} |
701 |
|
|