Debian Codegraph @

agent/mibgroup/host/hr_storage.c

raw
1 /*
2 * Host Resources MIB - storage group implementation - hr_storage.c
3 *
4 */
5
6 #include <net-snmp/net-snmp-config.h>
7
8 #if defined(freebsd5)
9 /* undefine these in order to use getfsstat */
10 #undef HAVE_STATVFS
11 #undef HAVE_STRUCT_STATVFS_F_FRSIZE
12 #endif
13
14 #include <sys/types.h>
15 #ifdef HAVE_SYS_PARAM_H
16 #include <sys/param.h>
17 #endif
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #ifdef TIME_WITH_SYS_TIME
22 # include <sys/time.h>
23 # include <time.h>
24 #else
25 # ifdef HAVE_SYS_TIME_H
26 # include <sys/time.h>
27 # else
28 # include <time.h>
29 # endif
30 #endif
31
32 #if (!defined(mingw32) && !defined(WIN32))
33 #ifdef HAVE_UTMPX_H
34 #include <utmpx.h>
35 #else
36 #include <utmp.h>
37 #endif
38 #endif /* mingw32 */
39 #ifndef dynix
40 #ifdef HAVE_SYS_VM_H
41 #include <sys/vm.h>
42 #if (!defined(KERNEL) || defined(MACH_USER_API)) && defined(HAVE_SYS_VMMETER_H) /*OS X does not #include <sys/vmmeter.h> if (defined(KERNEL) && !defined(MACH_USER_API)) */
43 #include <sys/vmmeter.h>
44 #endif
45 #else
46 #ifdef HAVE_VM_VM_H
47 #include <vm/vm.h>
48 #ifdef HAVE_MACHINE_TYPES_H
49 #include <machine/types.h>
50 #endif
51 #ifdef HAVE_SYS_VMMETER_H
52 #include <sys/vmmeter.h>
53 #endif
54 #ifdef HAVE_VM_VM_PARAM_H
55 #include <vm/vm_param.h>
56 #endif
57 #else
58 #ifdef HAVE_SYS_VMPARAM_H
59 #include <sys/vmparam.h>
60 #endif
61 #ifdef HAVE_SYS_VMMAC_H
62 #include <sys/vmmac.h>
63 #endif
64 #ifdef HAVE_SYS_VMMETER_H
65 #include <sys/vmmeter.h>
66 #endif
67 #ifdef HAVE_SYS_VMSYSTM_H
68 #include <sys/vmsystm.h>
69 #endif
70 #endif /* vm/vm.h */
71 #endif /* sys/vm.h */
72 #if defined(HAVE_UVM_UVM_PARAM_H) && defined(HAVE_UVM_UVM_EXTERN_H)
73 #include <uvm/uvm_param.h>
74 #include <uvm/uvm_extern.h>
75 #elif defined(HAVE_VM_VM_PARAM_H) && defined(HAVE_VM_VM_EXTERN_H)
76 #include <vm/vm_param.h>
77 #include <vm/vm_extern.h>
78 #endif
79 #ifdef HAVE_KVM_H
80 #include <kvm.h>
81 #endif
82 #ifdef HAVE_FCNTL_H
83 #include <fcntl.h>
84 #endif
85 #ifdef HAVE_SYS_POOL_H
86 #if defined(MBPOOL_SYMBOL) && defined(MCLPOOL_SYMBOL)
87 #define __POOL_EXPOSE
88 #include <sys/pool.h>
89 #else
90 #undef HAVE_SYS_POOL_H
91 #endif
92 #endif
93 #ifdef HAVE_SYS_MBUF_H
94 #include <sys/mbuf.h>
95 #endif
96 #ifdef HAVE_SYS_SYSCTL_H
97 #include <sys/sysctl.h>
98 #if defined(CTL_HW) && defined(HW_PAGESIZE)
99 #define USE_SYSCTL
100 #endif
101 #ifdef USE_MACH_HOST_STATISTICS
102 #include <mach/mach.h>
103 #elif defined(CTL_VM) && (defined(VM_METER) || defined(VM_UVMEXP))
104 #define USE_SYSCTL_VM
105 #endif
106 #endif /* if HAVE_SYS_SYSCTL_H */
107 #endif /* ifndef dynix */
108
109 #if (defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)) && HAVE_LIBPERFSTAT_H
110 #ifdef HAVE_SYS_PROTOSW_H
111 #include <sys/protosw.h>
112 #endif
113 #include <libperfstat.h>
114 #endif
115
116
117 #include "host_res.h"
118 #include "hr_storage.h"
119 #include "hr_filesys.h"
120 #include <net-snmp/agent/auto_nlist.h>
121
122 #ifdef HAVE_MNTENT_H
123 #include <mntent.h>
124 #endif
125 #ifdef HAVE_SYS_MNTTAB_H
126 #include <sys/mnttab.h>
127 #endif
128 #ifdef HAVE_SYS_STATVFS_H
129 #include <sys/statvfs.h>
130 #endif
131 #ifdef HAVE_SYS_VFS_H
132 #include <sys/vfs.h>
133 #endif
134 #ifdef HAVE_SYS_MOUNT_H
135 #ifdef __osf__
136 #undef m_next
137 #undef m_data
138 #endif
139 #include <sys/mount.h>
140 #endif
141 #ifdef HAVE_MACHINE_PARAM_H
142 #include <machine/param.h>
143 #endif
144 #include <sys/stat.h>
145
146 #if defined(hpux10) || defined(hpux11)
147 #include <sys/pstat.h>
148 #endif
149 #if defined(solaris2)
150 #ifdef HAVE_SYS_SWAP_H
151 #include <sys/swap.h>
152 #endif
153 #endif
154
155 #ifdef HAVE_STRING_H
156 #include <string.h>
157 #else
158 #include <strings.h>
159 #endif
160 #ifdef HAVE_NBUTIL_H
161 #include <nbutil.h>
162 #endif
163
164 #include <net-snmp/utilities.h>
165 #include <net-snmp/output_api.h>
166
167 #include <net-snmp/agent/net-snmp-agent-includes.h>
168 #include <net-snmp/agent/hardware/memory.h>
169
170 #ifdef solaris2
171 #include "kernel_sunos5.h"
172 #endif
173
174 #include <net-snmp/agent/agent_read_config.h>
175 #include <net-snmp/library/read_config.h>
176
177 #define HRSTORE_MONOTONICALLY_INCREASING
178
179 /*********************
180 *
181 * Kernel & interface information,
182 * and internal forward declarations
183 *
184 *********************/
185
186
187 #ifdef solaris2
188
189 extern struct mnttab *HRFS_entry;
190 #define HRFS_mount mnt_mountp
191 #define HRFS_statfs statvfs
192 #define HRFS_HAS_FRSIZE HAVE_STRUCT_STATVFS_F_FRSIZE
193
194 #elif defined(WIN32)
195 /* fake block size */
196 #define FAKED_BLOCK_SIZE 512
197
198 extern struct win_statfs *HRFS_entry;
199 #define HRFS_statfs win_statfs
200 #define HRFS_mount f_driveletter
201
202 #elif defined(HAVE_STATVFS) && defined(__NetBSD__)
203
204 extern struct statvfs *HRFS_entry;
205 extern int fscount;
206 #define HRFS_statfs statvfs
207 #define HRFS_mount f_mntonname
208 #define HRFS_HAS_FRSIZE HAVE_STRUCT_STATVFS_F_FRSIZE
209
210 #elif defined(HAVE_STATVFS) && defined(HAVE_STRUCT_STATVFS_MNT_DIR)
211
212 extern struct mntent *HRFS_entry;
213 extern int fscount;
214 #define HRFS_statfs statvfs
215 #define HRFS_mount mnt_dir
216 #define HRFS_HAS_FRSIZE HAVE_STRUCT_STATVFS_F_FRSIZE
217
218 #elif defined(HAVE_GETFSSTAT) && !defined(HAVE_STATFS) && defined(HAVE_STATVFS)
219
220 extern struct statfs *HRFS_entry;
221 extern int fscount;
222 #define HRFS_statfs statvfs
223 #define HRFS_mount f_mntonname
224 #define HRFS_HAS_FRSIZE STRUCT_STATVFS_HAS_F_FRSIZE
225
226 #elif defined(HAVE_GETFSSTAT)
227
228 extern struct statfs *HRFS_entry;
229 extern int fscount;
230 #define HRFS_statfs statfs
231 #define HRFS_mount f_mntonname
232 #define HRFS_HAS_FRSIZE HAVE_STRUCT_STATFS_F_FRSIZE
233
234 #else
235
236 extern struct mntent *HRFS_entry;
237 #define HRFS_mount mnt_dir
238 #define HRFS_statfs statfs
239 #define HRFS_HAS_FRSIZE HAVE_STRUCT_STATFS_F_FRSIZE
240
241 #endif
242
243 #if defined(USE_MACH_HOST_STATISTICS)
244 mach_port_t myHost;
245 #endif
246
247 static void parse_storage_config(const char *, char *);
248
249 /*********************
250 *
251 * Initialisation & common implementation functions
252 *
253 *********************/
254 int Get_Next_HR_Store(void);
255 void Init_HR_Store(void);
256 int header_hrstore(struct variable *, oid *, size_t *, int,
257 size_t *, WriteMethod **);
258 void* header_hrstoreEntry(struct variable *, oid *, size_t *,
259 int, size_t *, WriteMethod **);
260 Netsnmp_Node_Handler handle_memsize;
261
262 #define HRSTORE_MEMSIZE 1
263 #define HRSTORE_INDEX 2
264 #define HRSTORE_TYPE 3
265 #define HRSTORE_DESCR 4
266 #define HRSTORE_UNITS 5
267 #define HRSTORE_SIZE 6
268 #define HRSTORE_USED 7
269 #define HRSTORE_FAILS 8
270
271 struct variable2 hrstore_variables[] = {
272 {HRSTORE_INDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
273 var_hrstore, 1, {1}},
274 {HRSTORE_TYPE, ASN_OBJECT_ID, NETSNMP_OLDAPI_RONLY,
275 var_hrstore, 1, {2}},
276 {HRSTORE_DESCR, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
277 var_hrstore, 1, {3}},
278 {HRSTORE_UNITS, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
279 var_hrstore, 1, {4}},
280 {HRSTORE_SIZE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
281 var_hrstore, 1, {5}},
282 {HRSTORE_USED, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
283 var_hrstore, 1, {6}},
284 {HRSTORE_FAILS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
285 var_hrstore, 1, {7}}
286 };
287 oid hrMemorySize_oid[] = { 1, 3, 6, 1, 2, 1, 25, 2, 2 };
288 oid hrStorageTable_oid[] = { 1, 3, 6, 1, 2, 1, 25, 2, 3, 1 };
289
290
291 void
292 init_hr_storage(void)
293 {
294 char *appname;
295
296 netsnmp_register_scalar(
297 netsnmp_create_handler_registration("host/hrMemorySize", handle_memsize,
298 hrMemorySize_oid, OID_LENGTH(hrMemorySize_oid),
299 HANDLER_CAN_RONLY));
300 REGISTER_MIB("host/hr_storage", hrstore_variables, variable2,
301 hrStorageTable_oid);
302
303 appname = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
304 NETSNMP_DS_LIB_APPTYPE);
305 netsnmp_ds_register_config(ASN_BOOLEAN, appname, "skipNFSInHostResources",
306 NETSNMP_DS_APPLICATION_ID,
307 NETSNMP_DS_AGENT_SKIPNFSINHOSTRESOURCES);
308
309 snmpd_register_config_handler("storageUseNFS", parse_storage_config, NULL,
310 "1 | 2\t\t(1 = enable, 2 = disable)");
311 }
312
313 static int storageUseNFS = 1; /* Default to reporting NFS mounts as NetworkDisk */
314
315 static void
316 parse_storage_config(const char *token, char *cptr)
317 {
318 char *val;
319 int ival;
320 char *st;
321
322 val = strtok_r(cptr, " \t", &st);
323 if (!val) {
324 config_perror("Missing FLAG parameter in storageUseNFS");
325 return;
326 }
327 ival = atoi(val);
328 if (ival < 1 || ival > 2) {
329 config_perror("storageUseNFS must be 1 or 2");
330 return;
331 }
332 storageUseNFS = (ival == 1) ? 1 : 0;
333 }
334
335 /*
336 * header_hrstoreEntry(...
337 * Arguments:
338 * vp IN - pointer to variable entry that points here
339 * name IN/OUT - IN/name requested, OUT/name found
340 * length IN/OUT - length of IN/OUT oid's
341 * exact IN - TRUE if an exact match was requested
342 * var_len OUT - length of variable or 0 if function returned
343 * write_method
344 *
345 */
346
347 void *
348 header_hrstoreEntry(struct variable *vp,
349 oid * name,
350 size_t * length,
351 int exact,
352 size_t * var_len, WriteMethod ** write_method)
353 {
354 #define HRSTORE_ENTRY_NAME_LENGTH 11
355 oid newname[MAX_OID_LEN];
356 int storage_idx, LowIndex = -1;
357 int result;
358 int idx = -1;
359 netsnmp_memory_info *mem = NULL;
360
361 DEBUGMSGTL(("host/hr_storage", "var_hrstoreEntry: request "));
362 DEBUGMSGOID(("host/hr_storage", name, *length));
363 DEBUGMSG(("host/hr_storage", " exact=%d\n", exact));
364
365 memcpy((char *) newname, (char *) vp->name,
366 (int) vp->namelen * sizeof(oid));
367 result = snmp_oid_compare(name, *length, vp->name, vp->namelen);
368
369 DEBUGMSGTL(("host/hr_storage", "var_hrstoreEntry: compare "));
370 DEBUGMSGOID(("host/hr_storage", vp->name, vp->namelen));
371 DEBUGMSG(("host/hr_storage", " => %d\n", result));
372
373
374 if (result < 0 ||
375 *length <= HRSTORE_ENTRY_NAME_LENGTH ) {
376 /*
377 * Requested OID too early or too short to refer
378 * to a valid row (for the current column object).
379 * GET requests should fail, GETNEXT requests
380 * should use the first row.
381 */
382 if ( exact )
383 return NULL;
384 netsnmp_memory_load();
385 mem = netsnmp_memory_get_first( 0 );
386 }
387 else {
388 /*
389 * Otherwise, retrieve the requested
390 * (or following) row as appropriate.
391 */
392 if ( exact && *length > HRSTORE_ENTRY_NAME_LENGTH+1 )
393 return NULL; /* Too long for a valid instance */
394 idx = name[ HRSTORE_ENTRY_NAME_LENGTH ];
395 if ( idx < NETSNMP_MEM_TYPE_MAX ) {
396 netsnmp_memory_load();
397 mem = ( exact ? netsnmp_memory_get_byIdx( idx, 0 ) :
398 netsnmp_memory_get_next_byIdx( idx, 0 ));
399 }
400 }
401
402 /*
403 * If this matched a memory-based entry, then
404 * update the OID parameter(s) for GETNEXT requests.
405 */
406 if ( mem ) {
407 if ( !exact ) {
408 newname[ HRSTORE_ENTRY_NAME_LENGTH ] = mem->idx;
409 memcpy((char *) name, (char *) newname,
410 ((int) vp->namelen + 1) * sizeof(oid));
411 *length = vp->namelen + 1;
412 }
413 }
414 /*
415 * If this didn't match a memory-based entry,
416 * then consider the disk-based storage.
417 */
418 else {
419 Init_HR_Store();
420 for (;;) {
421 storage_idx = Get_Next_HR_Store();
422 DEBUGMSG(("host/hr_storage", "(index %d ....", storage_idx));
423 if (storage_idx == -1)
424 break;
425 newname[HRSTORE_ENTRY_NAME_LENGTH] = storage_idx;
426 DEBUGMSGOID(("host/hr_storage", newname, *length));
427 DEBUGMSG(("host/hr_storage", "\n"));
428 result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);
429 if (exact && (result == 0)) {
430 LowIndex = storage_idx;
431 /*
432 * Save storage status information
433 */
434 break;
435 }
436 if ((!exact && (result < 0)) &&
437 (LowIndex == -1 || storage_idx < LowIndex)) {
438 LowIndex = storage_idx;
439 /*
440 * Save storage status information
441 */
442 #ifdef HRSTORE_MONOTONICALLY_INCREASING
443 break;
444 #endif
445 }
446 }
447 if ( LowIndex != -1 ) {
448 if ( !exact ) {
449 newname[ HRSTORE_ENTRY_NAME_LENGTH ] = LowIndex;
450 memcpy((char *) name, (char *) newname,
451 ((int) vp->namelen + 1) * sizeof(oid));
452 *length = vp->namelen + 1;
453 }
454 mem = (netsnmp_memory_info*)0xffffffff; /* To indicate 'success' */
455 }
456 }
457
458 *write_method = (WriteMethod*)0;
459 *var_len = sizeof(long); /* default to 'long' results */
460
461 /*
462 * ... and return the appropriate row
463 */
464 DEBUGMSGTL(("host/hr_storage", "var_hrstoreEntry: process "));
465 DEBUGMSGOID(("host/hr_storage", name, *length));
466 DEBUGMSG(("host/hr_storage", " (%p)\n", mem));
467 return (void*)mem;
468 }
469
470 oid storage_type_id[] = { 1, 3, 6, 1, 2, 1, 25, 2, 1, 1 }; /* hrStorageOther */
471 int storage_type_len =
472 sizeof(storage_type_id) / sizeof(storage_type_id[0]);
473
474 /*********************
475 *
476 * System specific implementation functions
477 *
478 *********************/
479
480 int
481 handle_memsize(netsnmp_mib_handler *handler,
482 netsnmp_handler_registration *reginfo,
483 netsnmp_agent_request_info *reqinfo,
484 netsnmp_request_info *requests)
485 {
486 netsnmp_memory_info *mem_info;
487 int val;
488
489 /*
490 * We just need to handle valid GET requests, as invalid instances
491 * are rejected automatically, and (valid) GETNEXT requests are
492 * converted into the appropriate GET request.
493 *
494 * We also only ever receive one request at a time.
495 */
496 switch (reqinfo->mode) {
497 case MODE_GET:
498 netsnmp_memory_load();
499 mem_info = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_PHYSMEM, 0 );
500 if ( !mem_info || mem_info->size == -1 || mem_info->units == -1 )
501 netsnmp_set_request_error( reqinfo, requests, SNMP_NOSUCHOBJECT );
502 else {
503 val = mem_info->size; /* memtotal */
504 val *= (mem_info->units/1024);
505 snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
506 (u_char *)&val, sizeof(val));
507 }
508 return SNMP_ERR_NOERROR;
509
510 default:
511 /*
512 * we should never get here, so this is a really bad error
513 */
514 snmp_log(LOG_ERR, "unknown mode (%d) in handle_memsize\n",
515 reqinfo->mode);
516 return SNMP_ERR_GENERR;
517 }
518
519 return SNMP_ERR_NOERROR;
520 }
521
522
523 u_char *
524 var_hrstore(struct variable *vp,
525 oid * name,
526 size_t * length,
527 int exact, size_t * var_len, WriteMethod ** write_method)
528 {
529 int store_idx = 0;
530 static char string[1024];
531 struct HRFS_statfs stat_buf;
532 void *ptr;
533 netsnmp_memory_info *mem = NULL;
534
535 really_try_next:
536 ptr = header_hrstoreEntry(vp, name, length, exact, var_len,
537 write_method);
538 if (ptr == NULL)
539 return NULL;
540
541 store_idx = name[ HRSTORE_ENTRY_NAME_LENGTH ];
542 if (store_idx > NETSNMP_MEM_TYPE_MAX ) {
543 if ( netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
544 NETSNMP_DS_AGENT_SKIPNFSINHOSTRESOURCES) &&
545 Check_HR_FileSys_NFS())
546 return NULL; /* or goto try_next; */
547 if (Check_HR_FileSys_AutoFs())
548 return NULL;
549 if (HRFS_statfs(HRFS_entry->HRFS_mount, &stat_buf) < 0) {
550 snmp_log_perror(HRFS_entry->HRFS_mount);
551 goto try_next;
552 }
553 } else {
554 mem = (netsnmp_memory_info*)ptr;
555 }
556
557
558
559 switch (vp->magic) {
560 case HRSTORE_INDEX:
561 long_return = store_idx;
562 return (u_char *) & long_return;
563 case HRSTORE_TYPE:
564 if (store_idx > NETSNMP_MEM_TYPE_MAX)
565 if (storageUseNFS && Check_HR_FileSys_NFS())
566 storage_type_id[storage_type_len - 1] = 10; /* Network Disk */
567 #if defined(HAVE_HASMNTOPT) && !(defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7))
568 /*
569 * hasmntopt takes "const struct mntent*", but HRFS_entry has been
570 * defined differently for AIX, so skip this for AIX
571 */
572 else if (hasmntopt(HRFS_entry, "loop") != NULL)
573 storage_type_id[storage_type_len - 1] = 5; /* Removable Disk */
574 #endif
575 else
576 storage_type_id[storage_type_len - 1] = 4; /* Assume fixed */
577 else
578 switch (store_idx) {
579 case NETSNMP_MEM_TYPE_PHYSMEM:
580 case NETSNMP_MEM_TYPE_USERMEM:
581 storage_type_id[storage_type_len - 1] = 2; /* RAM */
582 break;
583 case NETSNMP_MEM_TYPE_VIRTMEM:
584 case NETSNMP_MEM_TYPE_SWAP:
585 storage_type_id[storage_type_len - 1] = 3; /* Virtual Mem */
586 break;
587 default:
588 storage_type_id[storage_type_len - 1] = 1; /* Other */
589 break;
590 }
591 *var_len = sizeof(storage_type_id);
592 return (u_char *) storage_type_id;
593 case HRSTORE_DESCR:
594 if (store_idx > NETSNMP_MEM_TYPE_MAX) {
595 strlcpy(string, HRFS_entry->HRFS_mount, sizeof(string));
596 *var_len = strlen(string);
597 return (u_char *) string;
598 } else {
599 if ( !mem || !mem->descr )
600 goto try_next;
601 *var_len = strlen(mem->descr);
602 return (u_char *) mem->descr;
603 }
604 case HRSTORE_UNITS:
605 if (store_idx > NETSNMP_MEM_TYPE_MAX)
606 #ifdef HRFS_HAS_FRSIZE
607 long_return = stat_buf.f_frsize;
608 #else
609 long_return = stat_buf.f_bsize;
610 #endif
611 else {
612 if ( !mem || mem->units == -1 )
613 goto try_next;
614 long_return = mem->units;
615 }
616 return (u_char *) & long_return;
617 case HRSTORE_SIZE:
618 if (store_idx > NETSNMP_MEM_TYPE_MAX)
619 long_return = stat_buf.f_blocks;
620 else {
621 if ( !mem || mem->size == -1 )
622 goto try_next;
623 long_return = mem->size;
624 }
625 return (u_char *) & long_return;
626 case HRSTORE_USED:
627 if (store_idx > NETSNMP_MEM_TYPE_MAX)
628 long_return = (stat_buf.f_blocks - stat_buf.f_bfree);
629 else {
630 if ( !mem || mem->size == -1 || mem->free == -1 )
631 goto try_next;
632 long_return = mem->size - mem->free;
633 }
634 return (u_char *) & long_return;
635 case HRSTORE_FAILS:
636 if (store_idx > NETSNMP_MEM_TYPE_MAX)
637 #ifdef NETSNMP_NO_DUMMY_VALUES
638 goto try_next;
639 #else
640 long_return = 0;
641 #endif
642 else {
643 if ( !mem || mem->other == -1 )
644 goto try_next;
645 long_return = mem->other;
646 }
647 return (u_char *) & long_return;
648 default:
649 DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrstore\n",
650 vp->magic));
651 }
652 return NULL;
653
654 try_next:
655 if (!exact)
656 goto really_try_next;
657
658 return NULL;
659 }
660
661
662 /*********************
663 *
664 * Internal implementation functions
665 *
666 *********************/
667
668 static int HRS_index;
669
670 void
671 Init_HR_Store(void)
672 {
673 HRS_index = 0;
674 Init_HR_FileSys();
675 }
676
677 int
678 Get_Next_HR_Store(void)
679 {
680 /*
681 * File-based storage
682 */
683 for (;;) {
684 HRS_index = Get_Next_HR_FileSys();
685 if (HRS_index >= 0) {
686 if (!(netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
687 NETSNMP_DS_AGENT_SKIPNFSINHOSTRESOURCES) &&
688 Check_HR_FileSys_NFS()) &&
689 !Check_HR_FileSys_AutoFs()) {
690 return HRS_index + NETSNMP_MEM_TYPE_MAX;
691 }
692 } else {
693 return -1;
694 }
695 }
696 }
697
698 #if 0
699 void
700 sol_get_swapinfo(int *totalP, int *usedP)
701 {
702 struct anoninfo ainfo;
703
704 if (swapctl(SC_AINFO, &ainfo) < 0) {
705 *totalP = *usedP = 0;
706 return;
707 }
708
709 *totalP = ainfo.ani_max;
710 *usedP = ainfo.ani_resv;
711 }
712 #endif /* solaris2 */
713
714 #ifdef WIN32
715 char *win_realpath(const char *file_name, char *resolved_name)
716 {
717 char szFile[_MAX_PATH + 1];
718 char *pszRet;
719
720 pszRet = _fullpath(szFile, resolved_name, MAX_PATH);
721
722 return pszRet;
723 }
724
725 static int win_statfs (const char *path, struct win_statfs *buf)
726 {
727 HINSTANCE h;
728 FARPROC f;
729 int retval = 0;
730 char tmp [MAX_PATH], resolved_path [MAX_PATH];
731 GetFullPathName(path, MAX_PATH, resolved_path, NULL);
732 /* TODO - Fix this! The realpath macro needs defined
733 * or rewritten into the function.
734 */
735
736 win_realpath(path, resolved_path);
737
738 if (!resolved_path)
739 retval = - 1;
740 else {
741 /* check whether GetDiskFreeSpaceExA is supported */
742 h = LoadLibraryA ("kernel32.dll");
743 if (h)
744 f = GetProcAddress (h, "GetDiskFreeSpaceExA");
745 else
746 f = NULL;
747
748 if (f) {
749 ULARGE_INTEGER bytes_free, bytes_total, bytes_free2;
750 if (!f (resolved_path, &bytes_free2, &bytes_total, &bytes_free)) {
751 errno = ENOENT;
752 retval = - 1;
753 } else {
754 buf -> f_bsize = FAKED_BLOCK_SIZE;
755 buf -> f_bfree = (bytes_free.QuadPart) / FAKED_BLOCK_SIZE;
756 buf -> f_files = buf -> f_blocks = (bytes_total.QuadPart) / FAKED_BLOCK_SIZE;
757 buf -> f_ffree = buf -> f_bavail = (bytes_free2.QuadPart) / FAKED_BLOCK_SIZE;
758 }
759 } else {
760 DWORD sectors_per_cluster, bytes_per_sector;
761 if (h) FreeLibrary (h);
762 if (!GetDiskFreeSpaceA (resolved_path, &sectors_per_cluster,
763 &bytes_per_sector, &buf -> f_bavail, &buf -> f_blocks)) {
764 errno = ENOENT;
765 retval = - 1;
766 } else {
767 buf -> f_bsize = sectors_per_cluster * bytes_per_sector;
768 buf -> f_files = buf -> f_blocks;
769 buf -> f_ffree = buf -> f_bavail;
770 buf -> f_bfree = buf -> f_bavail;
771 }
772 }
773 if (h) FreeLibrary (h);
774 }
775
776 /* get the FS volume information */
777 if (strspn (":", resolved_path) > 0) resolved_path [3] = '\0'; /* we want only the root */
778 if (GetVolumeInformation (resolved_path, NULL, 0, &buf -> f_fsid, &buf -> f_namelen,
779 NULL, tmp, MAX_PATH)) {
780 if (strcasecmp ("NTFS", tmp) == 0) {
781 buf -> f_type = NTFS_SUPER_MAGIC;
782 } else {
783 buf -> f_type = MSDOS_SUPER_MAGIC;
784 }
785 } else {
786 errno = ENOENT;
787 retval = - 1;
788 }
789 return retval;
790 }
791 #endif /* WIN32 */