Debian Codegraph @

agent/mibgroup/host/hrh_storage.c

raw
1 /*
2 * Host Resources MIB - storage group implementation - hrh_storage.c
3 *
4 */
5
6 #include <net-snmp/net-snmp-config.h>
7 #include <net-snmp/net-snmp-includes.h>
8 #include <net-snmp/agent/net-snmp-agent-includes.h>
9 #include <net-snmp/agent/hardware/memory.h>
10 #include <net-snmp/agent/hardware/fsys.h>
11 #include "host_res.h"
12 #include "hrh_filesys.h"
13 #include "hrh_storage.h"
14 #include "hr_disk.h"
15 #include "hr_filesys.h"
16 #include <net-snmp/utilities.h>
17
18
19 #include <sys/types.h>
20 #ifdef HAVE_SYS_PARAM_H
21 #include <sys/param.h>
22 #endif
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #ifdef TIME_WITH_SYS_TIME
27 # ifdef WIN32
28 # include <windows.h>
29 # include <errno.h>
30 # else
31 # include <sys/time.h>
32 # endif
33 # include <time.h>
34 #else
35 # ifdef HAVE_SYS_TIME_H
36 # include <sys/time.h>
37 # else
38 # include <time.h>
39 # endif
40 #endif
41
42 #ifdef HAVE_FCNTL_H
43 #include <fcntl.h>
44 #endif
45
46 #ifdef HAVE_STRING_H
47 #include <string.h>
48 #else
49 #include <strings.h>
50 #endif
51
52 #include <net-snmp/output_api.h>
53
54 #include <net-snmp/agent/agent_read_config.h>
55 #include <net-snmp/library/read_config.h>
56
57 #define HRSTORE_MONOTONICALLY_INCREASING
58
59 /*********************
60 *
61 * Kernel & interface information,
62 * and internal forward declarations
63 *
64 *********************/
65
66
67 static void parse_storage_config(const char *, char *);
68
69 /*********************
70 *
71 * Initialisation & common implementation functions
72 *
73 *********************/
74 int Get_Next_HR_Store(const struct netsnmp_fsys_info_s **entry);
75 void Init_HR_Store(void);
76
77 Netsnmp_Node_Handler handle_memsize;
78
79
80 #define HRSTORE_MEMSIZE 1
81 #define HRSTORE_INDEX 2
82 #define HRSTORE_TYPE 3
83 #define HRSTORE_DESCR 4
84 #define HRSTORE_UNITS 5
85 #define HRSTORE_SIZE 6
86 #define HRSTORE_USED 7
87 #define HRSTORE_FAILS 8
88
89 struct variable2 hrstore_variables[] = {
90 {HRSTORE_INDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
91 var_hrstore, 1, {1}},
92 {HRSTORE_TYPE, ASN_OBJECT_ID, NETSNMP_OLDAPI_RONLY,
93 var_hrstore, 1, {2}},
94 {HRSTORE_DESCR, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
95 var_hrstore, 1, {3}},
96 {HRSTORE_UNITS, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
97 var_hrstore, 1, {4}},
98 {HRSTORE_SIZE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
99 var_hrstore, 1, {5}},
100 {HRSTORE_USED, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
101 var_hrstore, 1, {6}},
102 {HRSTORE_FAILS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
103 var_hrstore, 1, {7}}
104 };
105 oid hrstore_variables_oid[] = { 1, 3, 6, 1, 2, 1, 25, 2 };
106 oid hrMemorySize_oid[] = { 1, 3, 6, 1, 2, 1, 25, 2, 2 };
107 oid hrStorageTable_oid[] = { 1, 3, 6, 1, 2, 1, 25, 2, 3, 1 };
108
109
110 void
111 init_hrh_storage(void)
112 {
113 char *appname;
114
115 netsnmp_register_scalar(
116 netsnmp_create_handler_registration("host/hrMemorySize", handle_memsize,
117 hrMemorySize_oid, OID_LENGTH(hrMemorySize_oid),
118 HANDLER_CAN_RONLY));
119 REGISTER_MIB("host/hr_storage", hrstore_variables, variable2,
120 hrStorageTable_oid);
121
122 appname = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
123 NETSNMP_DS_LIB_APPTYPE);
124 netsnmp_ds_register_config(ASN_BOOLEAN, appname, "skipNFSInHostResources",
125 NETSNMP_DS_APPLICATION_ID,
126 NETSNMP_DS_AGENT_SKIPNFSINHOSTRESOURCES);
127
128 netsnmp_ds_register_config(ASN_BOOLEAN, appname, "realStorageUnits",
129 NETSNMP_DS_APPLICATION_ID,
130 NETSNMP_DS_AGENT_REALSTORAGEUNITS);
131
132 snmpd_register_config_handler("storageUseNFS", parse_storage_config, NULL,
133 "1 | 2\t\t(1 = enable, 2 = disable)");
134 }
135
136 static int storageUseNFS = 1; /* Default to reporting NFS mounts as NetworkDisk */
137
138 static void
139 parse_storage_config(const char *token, char *cptr)
140 {
141 char *val;
142 int ival;
143 char *st;
144
145 val = strtok_r(cptr, " \t", &st);
146 if (!val) {
147 config_perror("Missing FLAG parameter in storageUseNFS");
148 return;
149 }
150 ival = atoi(val);
151 if (ival < 1 || ival > 2) {
152 config_perror("storageUseNFS must be 1 or 2");
153 return;
154 }
155 storageUseNFS = (ival == 1) ? 1 : 0;
156 }
157
158 /*
159 * header_hrstoreEntry(...
160 * Arguments:
161 * vp IN - pointer to variable entry that points here
162 * name IN/OUT - IN/name requested, OUT/name found
163 * length IN/OUT - length of IN/OUT oid's
164 * exact IN - TRUE if an exact match was requested
165 * var_len OUT - length of variable or 0 if function returned
166 * write_method
167 *
168 */
169 static void *
170 header_hrstoreEntry(struct variable *vp, oid *name, size_t *length, int exact,
171 size_t *var_len, WriteMethod **write_method,
172 const netsnmp_fsys_info **entry)
173 {
174 #define HRSTORE_ENTRY_NAME_LENGTH 11
175 oid newname[MAX_OID_LEN];
176 int storage_idx, LowIndex = -1;
177 int result;
178 int idx = -1;
179 netsnmp_memory_info *mem = NULL;
180
181 DEBUGMSGTL(("host/hr_storage", "var_hrstoreEntry: request "));
182 DEBUGMSGOID(("host/hr_storage", name, *length));
183 DEBUGMSG(("host/hr_storage", " exact=%d\n", exact));
184
185 memcpy((char *) newname, (char *) vp->name,
186 (int) vp->namelen * sizeof(oid));
187 result = snmp_oid_compare(name, *length, vp->name, vp->namelen);
188
189 DEBUGMSGTL(("host/hr_storage", "var_hrstoreEntry: compare "));
190 DEBUGMSGOID(("host/hr_storage", vp->name, vp->namelen));
191 DEBUGMSG(("host/hr_storage", " => %d\n", result));
192
193
194 if (result < 0 ||
195 *length <= HRSTORE_ENTRY_NAME_LENGTH ) {
196 /*
197 * Requested OID too early or too short to refer
198 * to a valid row (for the current column object).
199 * GET requests should fail, GETNEXT requests
200 * should use the first row.
201 */
202 if ( exact )
203 return NULL;
204 netsnmp_memory_load();
205 mem = netsnmp_memory_get_first( 0 );
206 }
207 else {
208 /*
209 * Otherwise, retrieve the requested
210 * (or following) row as appropriate.
211 */
212 if ( exact && *length > HRSTORE_ENTRY_NAME_LENGTH+1 )
213 return NULL; /* Too long for a valid instance */
214 idx = name[ HRSTORE_ENTRY_NAME_LENGTH ];
215 if ( idx < NETSNMP_MEM_TYPE_MAX ) {
216 netsnmp_memory_load();
217 mem = ( exact ? netsnmp_memory_get_byIdx( idx, 0 ) :
218 netsnmp_memory_get_next_byIdx( idx, 0 ));
219 }
220 }
221
222 /*
223 * If this matched a memory-based entry, then
224 * update the OID parameter(s) for GETNEXT requests.
225 */
226 if ( mem ) {
227 if ( !exact ) {
228 newname[ HRSTORE_ENTRY_NAME_LENGTH ] = mem->idx;
229 memcpy((char *) name, (char *) newname,
230 ((int) vp->namelen + 1) * sizeof(oid));
231 *length = vp->namelen + 1;
232 }
233 }
234 /*
235 * If this didn't match a memory-based entry,
236 * then consider the disk-based storage.
237 */
238 else {
239 *entry = NULL;
240 Init_HR_Store();
241 for (;;) {
242 storage_idx = Get_Next_HR_Store(entry);
243 DEBUGMSG(("host/hr_storage", "(index %d ....", storage_idx));
244 if (storage_idx == -1)
245 break;
246 newname[HRSTORE_ENTRY_NAME_LENGTH] = storage_idx;
247 DEBUGMSGOID(("host/hr_storage", newname, *length));
248 DEBUGMSG(("host/hr_storage", "\n"));
249 result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);
250 if (exact && (result == 0)) {
251 LowIndex = storage_idx;
252 /*
253 * Save storage status information
254 */
255 break;
256 }
257 if ((!exact && (result < 0)) &&
258 (LowIndex == -1 || storage_idx < LowIndex)) {
259 LowIndex = storage_idx;
260 /*
261 * Save storage status information
262 */
263 #ifdef HRSTORE_MONOTONICALLY_INCREASING
264 break;
265 #endif
266 }
267 }
268 if ( LowIndex != -1 ) {
269 if ( !exact ) {
270 newname[ HRSTORE_ENTRY_NAME_LENGTH ] = LowIndex;
271 memcpy((char *) name, (char *) newname,
272 ((int) vp->namelen + 1) * sizeof(oid));
273 *length = vp->namelen + 1;
274 }
275 mem = (netsnmp_memory_info*)0xffffffff; /* To indicate 'success' */
276 }
277 }
278
279 *write_method = (WriteMethod*)0;
280 *var_len = sizeof(long); /* default to 'long' results */
281
282 /*
283 * ... and return the appropriate row
284 */
285 DEBUGMSGTL(("host/hr_storage", "var_hrstoreEntry: process "));
286 DEBUGMSGOID(("host/hr_storage", name, *length));
287 DEBUGMSG(("host/hr_storage", " (%p)\n", mem));
288 return (void*)mem;
289 }
290
291 oid storage_type_id[] = { 1, 3, 6, 1, 2, 1, 25, 2, 1, 1 }; /* hrStorageOther */
292 int storage_type_len =
293 sizeof(storage_type_id) / sizeof(storage_type_id[0]);
294
295 /*********************
296 *
297 * System specific implementation functions
298 *
299 *********************/
300
301 int
302 handle_memsize(netsnmp_mib_handler *handler,
303 netsnmp_handler_registration *reginfo,
304 netsnmp_agent_request_info *reqinfo,
305 netsnmp_request_info *requests)
306 {
307 netsnmp_memory_info *mem_info;
308 int val;
309
310 /*
311 * We just need to handle valid GET requests, as invalid instances
312 * are rejected automatically, and (valid) GETNEXT requests are
313 * converted into the appropriate GET request.
314 *
315 * We also only ever receive one request at a time.
316 */
317 switch (reqinfo->mode) {
318 case MODE_GET:
319 netsnmp_memory_load();
320 mem_info = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_PHYSMEM, 0 );
321 if ( !mem_info || mem_info->size == -1 || mem_info->units == -1 )
322 netsnmp_set_request_error( reqinfo, requests, SNMP_NOSUCHOBJECT );
323 else {
324 val = mem_info->size; /* memtotal */
325 val *= (mem_info->units/1024);
326 snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
327 (u_char *)&val, sizeof(val));
328 }
329 return SNMP_ERR_NOERROR;
330
331 default:
332 /*
333 * we should never get here, so this is a really bad error
334 */
335 snmp_log(LOG_ERR, "unknown mode (%d) in handle_memsize\n",
336 reqinfo->mode);
337 return SNMP_ERR_GENERR;
338 }
339
340 return SNMP_ERR_NOERROR;
341 }
342
343
344 u_char *
345 var_hrstore(struct variable *vp,
346 oid * name,
347 size_t * length,
348 int exact, size_t * var_len, WriteMethod ** write_method)
349 {
350 int store_idx = 0;
351 static char string[1024];
352 void *ptr;
353 const netsnmp_fsys_info *entry = NULL;
354 netsnmp_memory_info *mem = NULL;
355
356 really_try_next:
357 ptr = header_hrstoreEntry(vp, name, length, exact, var_len, write_method,
358 &entry);
359 if (ptr == NULL)
360 return NULL;
361
362 store_idx = name[ HRSTORE_ENTRY_NAME_LENGTH ];
363 if (entry &&
364 store_idx > NETSNMP_MEM_TYPE_MAX &&
365 netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
366 NETSNMP_DS_AGENT_SKIPNFSINHOSTRESOURCES) &&
367 Check_HR_FileSys_NFS(entry))
368 return NULL;
369 if (entry && Check_HR_FileSys_AutoFs(entry))
370 return NULL;
371 if (store_idx <= NETSNMP_MEM_TYPE_MAX ) {
372 mem = (netsnmp_memory_info*)ptr;
373 }
374
375
376
377 switch (vp->magic) {
378 case HRSTORE_INDEX:
379 long_return = store_idx;
380 return (u_char *) & long_return;
381 case HRSTORE_TYPE:
382 if (store_idx > NETSNMP_MEM_TYPE_MAX)
383 if (entry->flags & NETSNMP_FS_FLAG_REMOTE && storageUseNFS)
384 storage_type_id[storage_type_len - 1] = 10; /* Network Disk */
385 else if (entry->flags & NETSNMP_FS_FLAG_REMOVE )
386 storage_type_id[storage_type_len - 1] = 5; /* Removable Disk */
387 else
388 storage_type_id[storage_type_len - 1] = 4; /* Assume fixed */
389 else
390 switch (store_idx) {
391 case NETSNMP_MEM_TYPE_PHYSMEM:
392 case NETSNMP_MEM_TYPE_USERMEM:
393 storage_type_id[storage_type_len - 1] = 2; /* RAM */
394 break;
395 case NETSNMP_MEM_TYPE_VIRTMEM:
396 case NETSNMP_MEM_TYPE_SWAP:
397 storage_type_id[storage_type_len - 1] = 3; /* Virtual Mem */
398 break;
399 default:
400 storage_type_id[storage_type_len - 1] = 1; /* Other */
401 break;
402 }
403 *var_len = sizeof(storage_type_id);
404 return (u_char *) storage_type_id;
405 case HRSTORE_DESCR:
406 if (store_idx > NETSNMP_MEM_TYPE_MAX) {
407 strlcpy(string, entry->path, sizeof(string));
408 *var_len = strlen(string);
409 return (u_char *) string;
410 } else {
411 if ( !mem || !mem->descr )
412 goto try_next;
413 *var_len = strlen(mem->descr);
414 return (u_char *) mem->descr;
415 }
416 case HRSTORE_UNITS:
417 if (store_idx > NETSNMP_MEM_TYPE_MAX) {
418 if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
419 NETSNMP_DS_AGENT_REALSTORAGEUNITS))
420 long_return = entry->units & 0x7fffffff;
421 else
422 long_return = entry->units_32;
423 } else {
424 if ( !mem || mem->units == -1 )
425 goto try_next;
426 long_return = mem->units & 0x7fffffff;
427 }
428 return (u_char *) & long_return;
429 case HRSTORE_SIZE:
430 if (store_idx > NETSNMP_MEM_TYPE_MAX) {
431 if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
432 NETSNMP_DS_AGENT_REALSTORAGEUNITS))
433 long_return = entry->size & 0x7fffffff;
434 else
435 long_return = entry->size_32;
436 } else {
437 if ( !mem || mem->size == -1 )
438 goto try_next;
439 long_return = mem->size & 0x7fffffff;
440 }
441 return (u_char *) & long_return;
442 case HRSTORE_USED:
443 if (store_idx > NETSNMP_MEM_TYPE_MAX) {
444 if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
445 NETSNMP_DS_AGENT_REALSTORAGEUNITS))
446 long_return = entry->used & 0x7fffffff;
447 else
448 long_return = entry->used_32;
449 } else {
450 if ( !mem || mem->size == -1 || mem->free == -1 )
451 goto try_next;
452 long_return = (mem->size - mem->free) & 0x7fffffff;
453 }
454 return (u_char *) & long_return;
455 case HRSTORE_FAILS:
456 if (store_idx > NETSNMP_MEM_TYPE_MAX)
457 #ifdef NETSNMP_NO_DUMMY_VALUES
458 goto try_next;
459 #else
460 long_return = 0;
461 #endif
462 else {
463 if ( !mem || mem->other == -1 )
464 goto try_next;
465 long_return = mem->other;
466 }
467 return (u_char *) & long_return;
468 default:
469 DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrstore\n",
470 vp->magic));
471 }
472 return NULL;
473
474 try_next:
475 if (!exact)
476 goto really_try_next;
477
478 return NULL;
479 }
480
481
482 /*********************
483 *
484 * Internal implementation functions
485 *
486 *********************/
487
488 void
489 Init_HR_Store(void)
490 {
491 Init_HR_FileSys();
492 }
493
494 int
495 Get_Next_HR_Store(const struct netsnmp_fsys_info_s **entry)
496 {
497 /*
498 * File-based storage
499 */
500 for (;;) {
501 Get_Next_HR_FileSys(entry);
502 if (!*entry)
503 return -1;
504 if (!(netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
505 NETSNMP_DS_AGENT_SKIPNFSINHOSTRESOURCES) &&
506 Check_HR_FileSys_NFS(*entry)) &&
507 !Check_HR_FileSys_AutoFs(*entry)) {
508 return (*entry)->idx.oids[0] + NETSNMP_MEM_TYPE_MAX;
509 }
510 }
511 }
512