1 |
< |
/* |
2 |
< |
* i-scream central monitoring system |
1 |
> |
/* |
2 |
> |
* i-scream libstatgrab |
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 |
+ |
#include "tools.h" |
35 |
|
|
36 |
|
#ifdef SOLARIS |
37 |
|
#include <sys/mnttab.h> |
40 |
|
#define VALID_FS_TYPES {"ufs", "tmpfs"} |
41 |
|
#endif |
42 |
|
|
43 |
< |
#ifdef LINUX |
38 |
< |
#include <sys/vfs.h> |
43 |
> |
#if defined(LINUX) || defined(CYGWIN) |
44 |
|
#include <mntent.h> |
45 |
< |
#include "tools.h" |
41 |
< |
#define VALID_FS_TYPES {"ext2", "ext3", "xfs", "reiserfs", "vfat", "tmpfs"} |
45 |
> |
#include <sys/vfs.h> |
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 |
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(sg_fs_stats *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; |
60 |
< |
} |
61 |
< |
strcpy(orig_ptr, newtext); |
62 |
< |
|
63 |
< |
return orig_ptr; |
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 |
+ |
static 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; |
71 |
< |
disk_stats->fs_type=NULL; |
72 |
< |
disk_stats->mnt_point=NULL; |
73 |
< |
|
74 |
< |
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){ |
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 |
< |
static disk_stat_t *disk_stats; |
81 |
< |
static int watermark=-1; |
82 |
< |
|
83 |
< |
char *fs_types[] = VALID_FS_TYPES; |
84 |
< |
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 |
|
|
113 |
< |
disk_stat_t *disk_ptr; |
113 |
> |
sg_fs_stats *disk_ptr; |
114 |
|
|
115 |
|
#ifdef SOLARIS |
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){ |
106 |
< |
disk_stats=malloc(START_VAL * sizeof(disk_stat_t)); |
107 |
< |
if(disk_stats==NULL){ |
108 |
< |
return NULL; |
109 |
< |
} |
110 |
< |
watermark=START_VAL; |
111 |
< |
init_disk_stat(0, watermark-1, disk_stats); |
112 |
< |
} |
113 |
< |
#ifdef FREEBSD |
128 |
> |
#ifdef ALLBSD |
129 |
|
nummnt=getmntinfo(&mp , MNT_LOCAL); |
130 |
|
if (nummnt<=0){ |
131 |
+ |
sg_set_error(SG_ERROR_GETMNTINFO, NULL); |
132 |
|
return NULL; |
133 |
|
} |
134 |
|
for(;nummnt--; mp++){ |
135 |
< |
valid_type=0; |
120 |
< |
for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){ |
121 |
< |
if(strcmp(mp->f_fstypename, fs_types[x]) ==0){ |
122 |
< |
valid_type=1; |
123 |
< |
break; |
124 |
< |
} |
125 |
< |
} |
135 |
> |
valid_type = is_valid_fs_type(mp->f_fstypename); |
136 |
|
#endif |
137 |
|
|
138 |
< |
#ifdef LINUX |
138 |
> |
#if defined(LINUX) || defined(CYGWIN) |
139 |
|
if ((f=setmntent("/etc/mtab", "r" ))==NULL){ |
140 |
+ |
sg_set_error(SG_ERROR_SETMNTENT, NULL); |
141 |
|
return NULL; |
142 |
|
} |
143 |
|
|
146 |
|
continue; |
147 |
|
} |
148 |
|
|
149 |
< |
valid_type=0; |
139 |
< |
for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){ |
140 |
< |
if(strcmp(mp->mnt_type, fs_types[x]) ==0){ |
141 |
< |
valid_type=1; |
142 |
< |
break; |
143 |
< |
} |
144 |
< |
} |
149 |
> |
valid_type = is_valid_fs_type(mp->mnt_type); |
150 |
|
#endif |
151 |
|
|
152 |
|
#ifdef SOLARIS |
153 |
|
if ((f=fopen("/etc/mnttab", "r" ))==NULL){ |
154 |
+ |
sg_set_error(SG_ERROR_OPEN, "/etc/mnttab"); |
155 |
|
return NULL; |
156 |
|
} |
157 |
|
while((getmntent(f, &mp)) == 0){ |
158 |
|
if ((statvfs(mp.mnt_mountp, &fs)) !=0){ |
159 |
|
continue; |
160 |
|
} |
161 |
< |
valid_type=0; |
156 |
< |
for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){ |
157 |
< |
if(strcmp(mp.mnt_fstype, fs_types[x]) ==0){ |
158 |
< |
valid_type=1; |
159 |
< |
break; |
160 |
< |
} |
161 |
< |
} |
161 |
> |
valid_type = is_valid_fs_type(mp.mnt_fstype); |
162 |
|
#endif |
163 |
|
|
164 |
|
if(valid_type){ |
165 |
< |
if(num_disks>watermark-1){ |
166 |
< |
disk_ptr=disk_stats; |
167 |
< |
if((disk_stats=realloc(disk_stats, (watermark*2 * sizeof(disk_stat_t))))==NULL){ |
168 |
< |
disk_stats=disk_ptr; |
169 |
< |
return NULL; |
170 |
< |
} |
171 |
< |
|
172 |
< |
watermark=watermark*2; |
173 |
< |
init_disk_stat(num_disks, watermark-1, disk_stats); |
165 |
> |
if (VECTOR_RESIZE(disk_stats, num_disks + 1) < 0) { |
166 |
> |
return NULL; |
167 |
|
} |
175 |
– |
|
168 |
|
disk_ptr=disk_stats+num_disks; |
169 |
< |
#ifdef FREEBSD |
170 |
< |
if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){ |
169 |
> |
|
170 |
> |
#ifdef ALLBSD |
171 |
> |
if (sg_update_string(&disk_ptr->device_name, mp->f_mntfromname) < 0) { |
172 |
|
return NULL; |
173 |
|
} |
174 |
< |
|
182 |
< |
if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->f_fstypename))==NULL){ |
174 |
> |
if (sg_update_string(&disk_ptr->fs_type, mp->f_fstypename) < 0) { |
175 |
|
return NULL; |
176 |
|
} |
177 |
< |
|
186 |
< |
if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->f_mntonname))==NULL){ |
177 |
> |
if (sg_update_string(&disk_ptr->mnt_point, mp->f_mntonname) < 0) { |
178 |
|
return NULL; |
179 |
|
} |
180 |
|
|
187 |
|
/* Freebsd doesn't have a "available" inodes */ |
188 |
|
disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes; |
189 |
|
#endif |
190 |
< |
#ifdef LINUX |
191 |
< |
if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){ |
190 |
> |
#if defined(LINUX) || defined(CYGWIN) |
191 |
> |
if (sg_update_string(&disk_ptr->device_name, mp->mnt_fsname) < 0) { |
192 |
|
return NULL; |
193 |
|
} |
194 |
|
|
195 |
< |
if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->mnt_type))==NULL){ |
195 |
> |
if (sg_update_string(&disk_ptr->fs_type, mp->mnt_type) < 0) { |
196 |
|
return NULL; |
197 |
|
} |
198 |
|
|
199 |
< |
if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->mnt_dir))==NULL){ |
199 |
> |
if (sg_update_string(&disk_ptr->mnt_point, mp->mnt_dir) < 0) { |
200 |
|
return NULL; |
201 |
|
} |
202 |
|
disk_ptr->size = (long long)fs.f_bsize * (long long)fs.f_blocks; |
210 |
|
#endif |
211 |
|
|
212 |
|
#ifdef SOLARIS |
222 |
– |
/* Memory leak in event of realloc failing */ |
213 |
|
/* Maybe make this char[bigenough] and do strncpy's and put a null in the end? |
214 |
|
* Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob |
215 |
|
* be upwards of a k each |
216 |
|
*/ |
217 |
< |
if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp.mnt_special))==NULL){ |
217 |
> |
if (sg_update_string(&disk_ptr->device_name, mp.mnt_special) < 0) { |
218 |
|
return NULL; |
219 |
|
} |
220 |
|
|
221 |
< |
if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp.mnt_fstype))==NULL){ |
222 |
< |
return NULL; |
223 |
< |
} |
221 |
> |
if (sg_update_string(&disk_ptr->fs_type, mp.mnt_fstype) < 0) { |
222 |
> |
return NULL; |
223 |
> |
} |
224 |
|
|
225 |
< |
if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp.mnt_mountp))==NULL){ |
226 |
< |
return NULL; |
227 |
< |
} |
225 |
> |
if (sg_update_string(&disk_ptr->mnt_point, mp.mnt_mountp) < 0) { |
226 |
> |
return NULL; |
227 |
> |
} |
228 |
|
|
229 |
|
disk_ptr->size = (long long)fs.f_frsize * (long long)fs.f_blocks; |
230 |
|
disk_ptr->avail = (long long)fs.f_frsize * (long long)fs.f_bavail; |
240 |
|
|
241 |
|
*entries=num_disks; |
242 |
|
|
243 |
< |
/* If this fails, there is very little i can do about it, so i'll ignore it :) */ |
244 |
< |
#if defined(LINUX) || defined(SOLARIS) |
243 |
> |
/* If this fails, there is very little i can do about it, so |
244 |
> |
I'll ignore it :) */ |
245 |
> |
#if defined(LINUX) || defined(CYGWIN) |
246 |
> |
endmntent(f); |
247 |
> |
#endif |
248 |
> |
#if defined(SOLARIS) |
249 |
|
fclose(f); |
250 |
|
#endif |
251 |
|
|
252 |
|
return disk_stats; |
253 |
|
|
254 |
|
} |
261 |
– |
void diskio_stat_init(int start, int end, diskio_stat_t *diskio_stats){ |
255 |
|
|
256 |
< |
for(diskio_stats+=start; start<end; start++){ |
257 |
< |
diskio_stats->disk_name=NULL; |
265 |
< |
|
266 |
< |
diskio_stats++; |
267 |
< |
} |
256 |
> |
static void diskio_stat_init(sg_disk_io_stats *d) { |
257 |
> |
d->disk_name = NULL; |
258 |
|
} |
259 |
|
|
260 |
< |
diskio_stat_t *diskio_stat_malloc(int needed_entries, int *cur_entries, diskio_stat_t *diskio_stats){ |
261 |
< |
|
272 |
< |
if(diskio_stats==NULL){ |
273 |
< |
|
274 |
< |
if((diskio_stats=malloc(needed_entries * sizeof(diskio_stat_t)))==NULL){ |
275 |
< |
return NULL; |
276 |
< |
} |
277 |
< |
diskio_stat_init(0, needed_entries, diskio_stats); |
278 |
< |
*cur_entries=needed_entries; |
279 |
< |
|
280 |
< |
return diskio_stats; |
281 |
< |
} |
282 |
< |
|
283 |
< |
|
284 |
< |
if(*cur_entries<needed_entries){ |
285 |
< |
diskio_stats=realloc(diskio_stats, (sizeof(diskio_stat_t)*needed_entries)); |
286 |
< |
if(diskio_stats==NULL){ |
287 |
< |
return NULL; |
288 |
< |
} |
289 |
< |
diskio_stat_init(*cur_entries, needed_entries, diskio_stats); |
290 |
< |
*cur_entries=needed_entries; |
291 |
< |
} |
292 |
< |
|
293 |
< |
return diskio_stats; |
260 |
> |
static void diskio_stat_destroy(sg_disk_io_stats *d) { |
261 |
> |
free(d->disk_name); |
262 |
|
} |
263 |
|
|
264 |
< |
static diskio_stat_t *diskio_stats=NULL; |
265 |
< |
static int num_diskio=0; |
264 |
> |
VECTOR_DECLARE_STATIC(diskio_stats, sg_disk_io_stats, 10, |
265 |
> |
diskio_stat_init, diskio_stat_destroy); |
266 |
|
|
267 |
|
#ifdef LINUX |
268 |
|
typedef struct { |
271 |
|
} partition; |
272 |
|
#endif |
273 |
|
|
274 |
< |
diskio_stat_t *get_diskio_stats(int *entries){ |
274 |
> |
sg_disk_io_stats *sg_get_disk_io_stats(int *entries){ |
275 |
> |
int num_diskio; |
276 |
> |
#ifndef LINUX |
277 |
> |
sg_disk_io_stats *diskio_stats_ptr; |
278 |
> |
#endif |
279 |
|
|
308 |
– |
static int sizeof_diskio_stats=0; |
309 |
– |
diskio_stat_t *diskio_stats_ptr; |
310 |
– |
|
280 |
|
#ifdef SOLARIS |
281 |
< |
kstat_ctl_t *kc; |
282 |
< |
kstat_t *ksp; |
281 |
> |
kstat_ctl_t *kc; |
282 |
> |
kstat_t *ksp; |
283 |
|
kstat_io_t kios; |
284 |
|
#endif |
285 |
|
#ifdef LINUX |
286 |
|
FILE *f; |
287 |
|
char *line_ptr; |
288 |
|
int major, minor; |
320 |
– |
char dev_letter; |
289 |
|
int has_pp_stats = 1; |
290 |
< |
static partition *parts = NULL; |
323 |
< |
static int alloc_parts = 0; |
290 |
> |
VECTOR_DECLARE_STATIC(parts, partition, 16, NULL, NULL); |
291 |
|
int i, n; |
292 |
|
time_t now; |
293 |
+ |
const char *format; |
294 |
|
#endif |
295 |
< |
#ifdef FREEBSD |
295 |
> |
#if defined(FREEBSD) || defined(DFBSD) |
296 |
|
static struct statinfo stats; |
297 |
|
static int stats_init = 0; |
298 |
|
int counter; |
301 |
|
long sel_gen; |
302 |
|
struct devstat *dev_ptr; |
303 |
|
#endif |
304 |
+ |
#ifdef NETBSD |
305 |
+ |
struct disk_sysctl *stats; |
306 |
+ |
#endif |
307 |
+ |
#ifdef OPENBSD |
308 |
+ |
int diskcount; |
309 |
+ |
char *disknames, *name, *bufpp; |
310 |
+ |
char **dk_name; |
311 |
+ |
struct diskstats *stats; |
312 |
+ |
#endif |
313 |
+ |
#ifdef NETBSD |
314 |
+ |
#define MIBSIZE 3 |
315 |
+ |
#endif |
316 |
+ |
#ifdef OPENBSD |
317 |
+ |
#define MIBSIZE 2 |
318 |
+ |
#endif |
319 |
+ |
#if defined(NETBSD) || defined(OPENBSD) |
320 |
+ |
int num_disks, i; |
321 |
+ |
int mib[MIBSIZE]; |
322 |
+ |
size_t size; |
323 |
+ |
#endif |
324 |
+ |
|
325 |
|
num_diskio=0; |
326 |
|
|
327 |
< |
#ifdef FREEBSD |
327 |
> |
#ifdef OPENBSD |
328 |
> |
mib[0] = CTL_HW; |
329 |
> |
mib[1] = HW_DISKCOUNT; |
330 |
> |
|
331 |
> |
size = sizeof(diskcount); |
332 |
> |
if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) { |
333 |
> |
sg_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKCOUNT"); |
334 |
> |
return NULL; |
335 |
> |
} |
336 |
> |
|
337 |
> |
mib[0] = CTL_HW; |
338 |
> |
mib[1] = HW_DISKNAMES; |
339 |
> |
|
340 |
> |
if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) { |
341 |
> |
sg_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES"); |
342 |
> |
return NULL; |
343 |
> |
} |
344 |
> |
|
345 |
> |
disknames = sg_malloc(size); |
346 |
> |
if (disknames == NULL) { |
347 |
> |
return NULL; |
348 |
> |
} |
349 |
> |
|
350 |
> |
if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) { |
351 |
> |
sg_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES"); |
352 |
> |
return NULL; |
353 |
> |
} |
354 |
> |
|
355 |
> |
dk_name = sg_malloc(diskcount * sizeof(char *)); |
356 |
> |
bufpp = disknames; |
357 |
> |
for (i = 0; i < diskcount && (name = strsep(&bufpp, ",")) != NULL; i++) { |
358 |
> |
dk_name[i] = name; |
359 |
> |
} |
360 |
> |
#endif |
361 |
> |
|
362 |
> |
#if defined(NETBSD) || defined(OPENBSD) |
363 |
> |
mib[0] = CTL_HW; |
364 |
> |
mib[1] = HW_DISKSTATS; |
365 |
> |
#ifdef NETBSD |
366 |
> |
mib[2] = sizeof(struct disk_sysctl); |
367 |
> |
#endif |
368 |
> |
|
369 |
> |
if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) { |
370 |
> |
sg_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS"); |
371 |
> |
return NULL; |
372 |
> |
} |
373 |
> |
|
374 |
> |
#ifdef NETBSD |
375 |
> |
num_disks = size / sizeof(struct disk_sysctl); |
376 |
> |
#else |
377 |
> |
num_disks = size / sizeof(struct diskstats); |
378 |
> |
#endif |
379 |
> |
|
380 |
> |
stats = sg_malloc(size); |
381 |
> |
if (stats == NULL) { |
382 |
> |
return NULL; |
383 |
> |
} |
384 |
> |
|
385 |
> |
if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) { |
386 |
> |
sg_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS"); |
387 |
> |
return NULL; |
388 |
> |
} |
389 |
> |
|
390 |
> |
for (i = 0; i < num_disks; i++) { |
391 |
> |
const char *name; |
392 |
> |
u_int64_t rbytes, wbytes; |
393 |
> |
|
394 |
> |
#ifdef NETBSD |
395 |
> |
#ifdef HAVE_DK_RBYTES |
396 |
> |
rbytes = stats[i].dk_rbytes; |
397 |
> |
wbytes = stats[i].dk_wbytes; |
398 |
> |
#else |
399 |
> |
/* Before 1.7, NetBSD merged reads and writes. */ |
400 |
> |
rbytes = wbytes = stats[i].dk_bytes; |
401 |
> |
#endif |
402 |
> |
#else |
403 |
> |
rbytes = wbytes = stats[i].ds_bytes; |
404 |
> |
#endif |
405 |
> |
|
406 |
> |
/* Don't keep stats for disks that have never been used. */ |
407 |
> |
if (rbytes == 0 && wbytes == 0) { |
408 |
> |
continue; |
409 |
> |
} |
410 |
> |
|
411 |
> |
if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) { |
412 |
> |
return NULL; |
413 |
> |
} |
414 |
> |
diskio_stats_ptr = diskio_stats + num_diskio; |
415 |
> |
|
416 |
> |
diskio_stats_ptr->read_bytes = rbytes; |
417 |
> |
diskio_stats_ptr->write_bytes = wbytes; |
418 |
> |
#ifdef NETBSD |
419 |
> |
name = stats[i].dk_name; |
420 |
> |
#else |
421 |
> |
name = dk_name[i]; |
422 |
> |
#endif |
423 |
> |
if (sg_update_string(&diskio_stats_ptr->disk_name, name) < 0) { |
424 |
> |
return NULL; |
425 |
> |
} |
426 |
> |
diskio_stats_ptr->systime = time(NULL); |
427 |
> |
|
428 |
> |
num_diskio++; |
429 |
> |
} |
430 |
> |
|
431 |
> |
free(stats); |
432 |
> |
#ifdef OPENBSD |
433 |
> |
free(dk_name); |
434 |
> |
free(disknames); |
435 |
> |
#endif |
436 |
> |
#endif |
437 |
> |
|
438 |
> |
#if defined(FREEBSD) || defined(DFBSD) |
439 |
|
if (!stats_init) { |
440 |
< |
stats.dinfo=malloc(sizeof(struct devinfo)); |
341 |
< |
bzero(stats.dinfo, sizeof(struct devinfo)); |
440 |
> |
stats.dinfo=sg_malloc(sizeof(struct devinfo)); |
441 |
|
if(stats.dinfo==NULL) return NULL; |
442 |
+ |
bzero(stats.dinfo, sizeof(struct devinfo)); |
443 |
|
stats_init = 1; |
444 |
|
} |
445 |
|
#ifdef FREEBSD5 |
446 |
< |
if ((devstat_getdevs(NULL, &stats)) < 0) return NULL; |
447 |
< |
/* Not aware of a get all devices, so i said 999. If we ever * find a machine with more than 999 disks, then i'll change * this number :) */ |
448 |
< |
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; |
446 |
> |
if ((devstat_getdevs(NULL, &stats)) < 0) { |
447 |
> |
sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL); |
448 |
> |
return NULL; |
449 |
> |
} |
450 |
> |
/* Not aware of a get all devices, so i said 999. If we ever |
451 |
> |
* find a machine with more than 999 disks, then i'll change |
452 |
> |
* this number :) |
453 |
> |
*/ |
454 |
> |
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) { |
455 |
> |
sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL); |
456 |
> |
return NULL; |
457 |
> |
} |
458 |
|
#else |
459 |
< |
if ((getdevs(&stats)) < 0) return NULL; |
459 |
> |
if ((getdevs(&stats)) < 0) { |
460 |
> |
sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL); |
461 |
> |
return NULL; |
462 |
> |
} |
463 |
|
/* Not aware of a get all devices, so i said 999. If we ever |
464 |
|
* find a machine with more than 999 disks, then i'll change |
465 |
|
* this number :) |
466 |
|
*/ |
467 |
< |
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; |
467 |
> |
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) { |
468 |
> |
sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL); |
469 |
> |
return NULL; |
470 |
> |
} |
471 |
|
#endif |
472 |
|
|
473 |
|
for(counter=0;counter<stats.dinfo->numdevs;counter++){ |
482 |
|
#else |
483 |
|
if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue; |
484 |
|
#endif |
485 |
< |
if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){ |
485 |
> |
|
486 |
> |
if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) { |
487 |
|
return NULL; |
488 |
|
} |
489 |
|
diskio_stats_ptr=diskio_stats+num_diskio; |
496 |
|
diskio_stats_ptr->write_bytes=dev_ptr->bytes_written; |
497 |
|
#endif |
498 |
|
if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name); |
499 |
< |
asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number); |
499 |
> |
if (asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number) == -1) { |
500 |
> |
sg_set_error(SG_ERROR_ASPRINTF, NULL); |
501 |
> |
return NULL; |
502 |
> |
} |
503 |
|
diskio_stats_ptr->systime=time(NULL); |
504 |
|
|
505 |
|
num_diskio++; |
509 |
|
#endif |
510 |
|
#ifdef SOLARIS |
511 |
|
if ((kc = kstat_open()) == NULL) { |
512 |
< |
return NULL; |
513 |
< |
} |
512 |
> |
sg_set_error(SG_ERROR_KSTAT_OPEN, NULL); |
513 |
> |
return NULL; |
514 |
> |
} |
515 |
|
|
516 |
|
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
517 |
< |
if (!strcmp(ksp->ks_class, "disk")) { |
517 |
> |
if (!strcmp(ksp->ks_class, "disk")) { |
518 |
|
|
519 |
|
if(ksp->ks_type != KSTAT_TYPE_IO) continue; |
520 |
|
/* We dont want metadevices appearins as num_diskio */ |
521 |
|
if(strcmp(ksp->ks_module, "md")==0) continue; |
522 |
< |
if((kstat_read(kc, ksp, &kios))==-1){ |
522 |
> |
if((kstat_read(kc, ksp, &kios))==-1){ |
523 |
|
} |
524 |
|
|
525 |
< |
if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){ |
525 |
> |
if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) { |
526 |
|
kstat_close(kc); |
527 |
|
return NULL; |
528 |
|
} |
529 |
|
diskio_stats_ptr=diskio_stats+num_diskio; |
530 |
|
|
531 |
|
diskio_stats_ptr->read_bytes=kios.nread; |
412 |
– |
|
532 |
|
diskio_stats_ptr->write_bytes=kios.nwritten; |
533 |
< |
|
534 |
< |
if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name); |
535 |
< |
|
536 |
< |
diskio_stats_ptr->disk_name=strdup(ksp->ks_name); |
533 |
> |
if (sg_update_string(&diskio_stats_ptr->disk_name, |
534 |
> |
sg_get_svr_from_bsd(ksp->ks_name)) < 0) { |
535 |
> |
return NULL; |
536 |
> |
} |
537 |
|
diskio_stats_ptr->systime=time(NULL); |
538 |
+ |
|
539 |
|
num_diskio++; |
540 |
|
} |
541 |
|
} |
548 |
|
n = 0; |
549 |
|
|
550 |
|
/* Read /proc/partitions to find what devices exist. Recent 2.4 kernels |
551 |
< |
have statistics in here too, so we can use those directly. */ |
551 |
> |
have statistics in here too, so we can use those directly. |
552 |
> |
2.6 kernels have /proc/diskstats instead with almost (but not quite) |
553 |
> |
the same format. */ |
554 |
|
|
555 |
< |
f = fopen("/proc/partitions", "r"); |
555 |
> |
f = fopen("/proc/diskstats", "r"); |
556 |
> |
format = " %d %d %99s %*d %*d %lld %*d %*d %*d %lld"; |
557 |
> |
if (f == NULL) { |
558 |
> |
f = fopen("/proc/partitions", "r"); |
559 |
> |
format = " %d %d %*d %99s %*d %*d %lld %*d %*d %*d %lld"; |
560 |
> |
} |
561 |
|
if (f == NULL) goto out; |
562 |
|
now = time(NULL); |
563 |
|
|
564 |
< |
while ((line_ptr = f_read_line(f, "")) != NULL) { |
565 |
< |
char name[20]; |
564 |
> |
while ((line_ptr = sg_f_read_line(f, "")) != NULL) { |
565 |
> |
char name[100]; |
566 |
|
char *s; |
567 |
|
long long rsect, wsect; |
568 |
|
|
569 |
< |
int nr = sscanf(line_ptr, |
443 |
< |
" %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld", |
569 |
> |
int nr = sscanf(line_ptr, format, |
570 |
|
&major, &minor, name, &rsect, &wsect); |
571 |
|
if (nr < 3) continue; |
446 |
– |
if (nr < 5) { |
447 |
– |
has_pp_stats = 0; |
448 |
– |
rsect = 0; |
449 |
– |
wsect = 0; |
450 |
– |
} |
572 |
|
|
573 |
|
/* Skip device names ending in numbers, since they're |
574 |
|
partitions. */ |
577 |
|
--s; |
578 |
|
if (*s >= '0' && *s <= '9') continue; |
579 |
|
|
580 |
< |
diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats, |
581 |
< |
diskio_stats); |
582 |
< |
if (diskio_stats == NULL) goto out; |
583 |
< |
if (n >= alloc_parts) { |
463 |
< |
alloc_parts += 16; |
464 |
< |
parts = realloc(parts, alloc_parts * sizeof *parts); |
465 |
< |
if (parts == NULL) { |
466 |
< |
alloc_parts = 0; |
467 |
< |
goto out; |
468 |
< |
} |
580 |
> |
if (nr < 5) { |
581 |
> |
has_pp_stats = 0; |
582 |
> |
rsect = 0; |
583 |
> |
wsect = 0; |
584 |
|
} |
585 |
|
|
586 |
< |
if (diskio_stats[n].disk_name != NULL) |
587 |
< |
free(diskio_stats[n].disk_name); |
588 |
< |
diskio_stats[n].disk_name = strdup(name); |
586 |
> |
if (VECTOR_RESIZE(diskio_stats, n + 1) < 0) { |
587 |
> |
goto out; |
588 |
> |
} |
589 |
> |
if (VECTOR_RESIZE(parts, n + 1) < 0) { |
590 |
> |
goto out; |
591 |
> |
} |
592 |
> |
|
593 |
> |
if (sg_update_string(&diskio_stats[n].disk_name, name) < 0) { |
594 |
> |
goto out; |
595 |
> |
} |
596 |
|
diskio_stats[n].read_bytes = rsect * 512; |
597 |
|
diskio_stats[n].write_bytes = wsect * 512; |
598 |
|
diskio_stats[n].systime = now; |
602 |
|
n++; |
603 |
|
} |
604 |
|
|
605 |
+ |
fclose(f); |
606 |
+ |
f = NULL; |
607 |
+ |
|
608 |
|
if (!has_pp_stats) { |
609 |
< |
/* This is an older kernel without stats in /proc/partitions. |
610 |
< |
Read what we can from /proc/stat instead. */ |
609 |
> |
/* This is an older kernel where /proc/partitions doesn't |
610 |
> |
contain stats. Read what we can from /proc/stat instead, and |
611 |
> |
fill in the appropriate bits of the list allocated above. */ |
612 |
|
|
613 |
|
f = fopen("/proc/stat", "r"); |
614 |
|
if (f == NULL) goto out; |
615 |
|
now = time(NULL); |
616 |
|
|
617 |
< |
line_ptr = f_read_line(f, "disk_io:"); |
617 |
> |
line_ptr = sg_f_read_line(f, "disk_io:"); |
618 |
|
if (line_ptr == NULL) goto out; |
619 |
|
|
620 |
|
while((line_ptr=strchr(line_ptr, ' '))!=NULL){ |
675 |
|
num_diskio = n; |
676 |
|
out: |
677 |
|
if (f != NULL) fclose(f); |
678 |
+ |
#endif |
679 |
|
|
680 |
+ |
#ifdef CYGWIN |
681 |
+ |
sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin"); |
682 |
+ |
return NULL; |
683 |
|
#endif |
684 |
+ |
|
685 |
|
*entries=num_diskio; |
686 |
|
|
687 |
|
return diskio_stats; |
688 |
|
} |
689 |
|
|
690 |
< |
diskio_stat_t *get_diskio_stats_diff(int *entries){ |
691 |
< |
static diskio_stat_t *diskio_stats_diff=NULL; |
692 |
< |
static int sizeof_diskio_stats_diff=0; |
693 |
< |
diskio_stat_t *diskio_stats_diff_ptr, *diskio_stats_ptr; |
694 |
< |
int disks, x, y; |
690 |
> |
sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entries){ |
691 |
> |
VECTOR_DECLARE_STATIC(diff, sg_disk_io_stats, 1, |
692 |
> |
diskio_stat_init, diskio_stat_destroy); |
693 |
> |
sg_disk_io_stats *src = NULL, *dest; |
694 |
> |
int i, j, diff_count, new_count; |
695 |
|
|
696 |
< |
if(diskio_stats==NULL){ |
697 |
< |
diskio_stats_ptr=get_diskio_stats(&disks); |
698 |
< |
*entries=disks; |
568 |
< |
return diskio_stats_ptr; |
696 |
> |
if (diskio_stats == NULL) { |
697 |
> |
/* No previous stats, so we can't calculate a difference. */ |
698 |
> |
return sg_get_disk_io_stats(entries); |
699 |
|
} |
700 |
|
|
701 |
< |
diskio_stats_diff=diskio_stat_malloc(num_diskio, &sizeof_diskio_stats_diff, diskio_stats_diff); |
702 |
< |
if(diskio_stats_diff==NULL){ |
701 |
> |
/* Resize the results array to match the previous stats. */ |
702 |
> |
diff_count = VECTOR_SIZE(diskio_stats); |
703 |
> |
if (VECTOR_RESIZE(diff, diff_count) < 0) { |
704 |
|
return NULL; |
705 |
|
} |
706 |
|
|
707 |
< |
diskio_stats_diff_ptr=diskio_stats_diff; |
708 |
< |
diskio_stats_ptr=diskio_stats; |
707 |
> |
/* Copy the previous stats into the result. */ |
708 |
> |
for (i = 0; i < diff_count; i++) { |
709 |
> |
src = &diskio_stats[i]; |
710 |
> |
dest = &diff[i]; |
711 |
|
|
712 |
< |
for(disks=0;disks<num_diskio;disks++){ |
713 |
< |
if(diskio_stats_diff_ptr->disk_name!=NULL){ |
581 |
< |
free(diskio_stats_diff_ptr->disk_name); |
712 |
> |
if (sg_update_string(&dest->disk_name, src->disk_name) < 0) { |
713 |
> |
return NULL; |
714 |
|
} |
715 |
< |
diskio_stats_diff_ptr->disk_name=strdup(diskio_stats_ptr->disk_name); |
716 |
< |
diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes; |
717 |
< |
diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes; |
718 |
< |
diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime; |
715 |
> |
dest->read_bytes = src->read_bytes; |
716 |
> |
dest->write_bytes = src->write_bytes; |
717 |
> |
dest->systime = src->systime; |
718 |
> |
} |
719 |
|
|
720 |
< |
diskio_stats_diff_ptr++; |
721 |
< |
diskio_stats_ptr++; |
720 |
> |
/* Get a new set of stats. */ |
721 |
> |
if (sg_get_disk_io_stats(&new_count) == NULL) { |
722 |
> |
return NULL; |
723 |
|
} |
724 |
|
|
725 |
< |
diskio_stats_ptr=get_diskio_stats(&disks); |
726 |
< |
diskio_stats_diff_ptr=diskio_stats_diff; |
725 |
> |
/* For each previous stat... */ |
726 |
> |
for (i = 0; i < diff_count; i++) { |
727 |
> |
dest = &diff[i]; |
728 |
|
|
729 |
< |
for(x=0;x<sizeof_diskio_stats_diff;x++){ |
730 |
< |
|
731 |
< |
if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){ |
732 |
< |
diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes; |
733 |
< |
diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes; |
734 |
< |
diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime; |
735 |
< |
}else{ |
602 |
< |
diskio_stats_ptr=diskio_stats; |
603 |
< |
for(y=0;y<disks;y++){ |
604 |
< |
if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){ |
605 |
< |
diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes; |
606 |
< |
diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes; |
607 |
< |
diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime; |
608 |
< |
|
609 |
< |
break; |
610 |
< |
} |
611 |
< |
|
612 |
< |
diskio_stats_ptr++; |
729 |
> |
/* ... find the corresponding new stat ... */ |
730 |
> |
for (j = 0; j < new_count; j++) { |
731 |
> |
/* Try the new stat in the same position first, |
732 |
> |
since that's most likely to be it. */ |
733 |
> |
src = &diskio_stats[(i + j) % new_count]; |
734 |
> |
if (strcmp(src->disk_name, dest->disk_name) == 0) { |
735 |
> |
break; |
736 |
|
} |
737 |
|
} |
738 |
+ |
if (j == new_count) { |
739 |
+ |
/* No match found. */ |
740 |
+ |
continue; |
741 |
+ |
} |
742 |
|
|
743 |
< |
diskio_stats_ptr++; |
744 |
< |
diskio_stats_diff_ptr++; |
745 |
< |
|
743 |
> |
/* ... and subtract the previous stat from it to get the |
744 |
> |
difference. */ |
745 |
> |
dest->read_bytes = src->read_bytes - dest->read_bytes; |
746 |
> |
dest->write_bytes = src->write_bytes - dest->write_bytes; |
747 |
> |
dest->systime = src->systime - dest->systime; |
748 |
|
} |
749 |
< |
|
750 |
< |
*entries=sizeof_diskio_stats_diff; |
751 |
< |
return diskio_stats_diff; |
749 |
> |
|
750 |
> |
*entries = diff_count; |
751 |
> |
return diff; |
752 |
|
} |
753 |
+ |
|