Debian Codegraph @

agent/mibgroup/host/hrh_filesys.c

raw
1 /*
2 * Host Resources MIB - File System device group implementation (HAL rewrite) - hrh_filesys.c
3 *
4 */
5 /* Portions of this file are subject to the following copyright(s). See
6 * the Net-SNMP's COPYING file for more details and other copyrights
7 * that may apply:
8 */
9 /*
10 * Portions of this file are copyrighted by:
11 * Copyright (C) 2007 Apple, Inc. All rights reserved.
12 * Use is subject to license terms specified in the COPYING file
13 * distributed with the Net-SNMP package.
14 */
15
16 #include <net-snmp/net-snmp-config.h>
17 #include <net-snmp/net-snmp-features.h>
18 #include <net-snmp/net-snmp-includes.h>
19 #include <net-snmp/agent/net-snmp-agent-includes.h>
20 #include <net-snmp/agent/hardware/memory.h>
21 #include <net-snmp/agent/hardware/fsys.h>
22 #include "host_res.h"
23 #include "hrh_filesys.h"
24 #include "hrh_storage.h"
25 #include "hr_disk.h"
26 #include "hr_filesys.h"
27 #include <net-snmp/utilities.h>
28
29 #ifdef HAVE_MNTENT_H
30 #include <mntent.h>
31 #endif
32 #ifdef HAVE_SYS_MNTENT_H
33 #include <sys/mntent.h>
34 #endif
35 #ifdef HAVE_SYS_MNTTAB_H
36 #include <sys/mnttab.h>
37 #endif
38 #ifdef HAVE_SYS_STATVFS_H
39 #include <sys/statvfs.h>
40 #endif
41 #ifdef HAVE_SYS_VFS_H
42 #include <sys/vfs.h>
43 #endif
44 #ifdef HAVE_SYS_PARAM_H
45 #include <sys/param.h>
46 #endif
47 #ifdef HAVE_SYS_MOUNT_H
48 #include <sys/mount.h>
49 #endif
50
51 #include <ctype.h>
52 #ifdef HAVE_STRING_H
53 #include <string.h>
54 #endif
55 #ifdef HAVE_STDLIB_H
56 #include <stdlib.h>
57 #endif
58
59 #if defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
60 #include <sys/mntctl.h>
61 #include <sys/vmount.h>
62 #include <sys/statfs.h>
63 #endif
64
65 netsnmp_feature_require(date_n_time);
66 netsnmp_feature_require(ctime_to_timet);
67
68 #define HRFS_MONOTONICALLY_INCREASING
69
70 /*********************
71 *
72 * Kernel & interface information,
73 * and internal forward declarations
74 *
75 *********************/
76
77 #define FULL_DUMP 0
78 #define PART_DUMP 1
79
80 static u_char *when_dumped(const char *filesys, int level, size_t *length);
81
82 /*********************
83 *
84 * Initialisation & common implementation functions
85 *
86 *********************/
87
88 #define HRFSYS_INDEX 1
89 #define HRFSYS_MOUNT 2
90 #define HRFSYS_RMOUNT 3
91 #define HRFSYS_TYPE 4
92 #define HRFSYS_ACCESS 5
93 #define HRFSYS_BOOT 6
94 #define HRFSYS_STOREIDX 7
95 #define HRFSYS_FULLDUMP 8
96 #define HRFSYS_PARTDUMP 9
97
98 struct variable4 hrfsys_variables[] = {
99 {HRFSYS_INDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
100 var_hrhfilesys, 2, {1, 1}},
101 {HRFSYS_MOUNT, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
102 var_hrhfilesys, 2, {1, 2}},
103 {HRFSYS_RMOUNT, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
104 var_hrhfilesys, 2, {1, 3}},
105 {HRFSYS_TYPE, ASN_OBJECT_ID, NETSNMP_OLDAPI_RONLY,
106 var_hrhfilesys, 2, {1, 4}},
107 {HRFSYS_ACCESS, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
108 var_hrhfilesys, 2, {1, 5}},
109 {HRFSYS_BOOT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
110 var_hrhfilesys, 2, {1, 6}},
111 {HRFSYS_STOREIDX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
112 var_hrhfilesys, 2, {1, 7}},
113 {HRFSYS_FULLDUMP, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
114 var_hrhfilesys, 2, {1, 8}},
115 {HRFSYS_PARTDUMP, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
116 var_hrhfilesys, 2, {1, 9}},
117 };
118 oid hrfsys_variables_oid[] = { 1, 3, 6, 1, 2, 1, 25, 3, 8 };
119
120 void
121 init_hrh_filesys(void)
122 {
123 REGISTER_MIB("host/hr_filesys", hrfsys_variables, variable4,
124 hrfsys_variables_oid);
125 }
126
127 /*
128 * header_hrhfilesys(...
129 * Arguments:
130 * vp IN - pointer to variable entry that points here
131 * name IN/OUT - IN/name requested, OUT/name found
132 * length IN/OUT - length of IN/OUT oid's
133 * exact IN - TRUE if an exact match was requested
134 * var_len OUT - length of variable or 0 if function returned
135 * write_method
136 *
137 */
138
139 static int
140 header_hrhfilesys(struct variable *vp, oid *name, size_t *length,
141 int exact, size_t *var_len, WriteMethod **write_method,
142 const netsnmp_fsys_info **entry)
143 {
144 #define HRFSYS_ENTRY_NAME_LENGTH 11
145 oid newname[MAX_OID_LEN];
146 int fsys_idx, LowIndex = -1;
147 int result;
148
149 DEBUGMSGTL(("host/hr_filesys", "var_hrhfilesys: "));
150 DEBUGMSGOID(("host/hr_filesys", name, *length));
151 DEBUGMSG(("host/hr_filesys", " %d\n", exact));
152
153 memcpy((char *) newname, (char *) vp->name, vp->namelen * sizeof(oid));
154 /*
155 * Find "next" file system entry
156 */
157
158 *entry = NULL;
159 Init_HR_FileSys();
160 for (;;) {
161 fsys_idx = Get_Next_HR_FileSys(entry);
162 if (fsys_idx == -1)
163 break;
164 newname[HRFSYS_ENTRY_NAME_LENGTH] = fsys_idx;
165 result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);
166 if (exact && (result == 0)) {
167 LowIndex = fsys_idx;
168 break;
169 }
170 if ((!exact && (result < 0)) &&
171 (LowIndex == -1 || fsys_idx < LowIndex)) {
172 LowIndex = fsys_idx;
173 #ifdef HRFS_MONOTONICALLY_INCREASING
174 break;
175 #endif
176 }
177 }
178
179 if (LowIndex == -1) {
180 DEBUGMSGTL(("host/hr_filesys", "... index out of range\n"));
181 return (MATCH_FAILED);
182 }
183
184 memcpy((char *) name, (char *) newname,
185 (vp->namelen + 1) * sizeof(oid));
186 *length = vp->namelen + 1;
187 *write_method = NULL;
188 *var_len = sizeof(long); /* default to 'long' results */
189
190 DEBUGMSGTL(("host/hr_filesys", "... get filesys stats "));
191 DEBUGMSGOID(("host/hr_filesys", name, *length));
192 DEBUGMSG(("host/hr_filesys", "\n"));
193
194 return LowIndex;
195 }
196
197
198 oid fsys_type_id[] = { 1, 3, 6, 1, 2, 1, 25, 3, 9, 1 }; /* hrFSOther */
199 int fsys_type_len =
200 sizeof(fsys_type_id) / sizeof(fsys_type_id[0]);
201
202
203
204 /*********************
205 *
206 * System specific implementation functions
207 *
208 *********************/
209
210
211 u_char *
212 var_hrhfilesys(struct variable *vp,
213 oid * name,
214 size_t * length,
215 int exact, size_t * var_len, WriteMethod ** write_method)
216 {
217 int fsys_idx;
218 static char *string;
219 static char empty_str[1];
220 const netsnmp_fsys_info *entry = NULL;
221
222 fsys_idx = header_hrhfilesys(vp, name, length, exact, var_len, write_method,
223 &entry);
224 if (fsys_idx == MATCH_FAILED)
225 return NULL;
226
227 switch (vp->magic) {
228 case HRFSYS_INDEX:
229 long_return = fsys_idx;
230 return (u_char *) & long_return;
231 case HRFSYS_MOUNT:
232 free(string);
233 string = NULL;
234 *var_len = 0;
235 if (asprintf(&string, "%s", entry->path) >= 0)
236 *var_len = strlen(string);
237 return (u_char *)(string ? string : empty_str);
238 case HRFSYS_RMOUNT:
239 free(string);
240 if (entry->flags & NETSNMP_FS_FLAG_REMOTE) {
241 if (asprintf(&string, "%s", entry->device) < 0)
242 string = NULL;
243 } else {
244 string = strdup("");
245 }
246 *var_len = string ? strlen(string) : 0;
247 return (u_char *)(string ? string : empty_str);
248
249 case HRFSYS_TYPE:
250 fsys_type_id[fsys_type_len - 1] =
251 (entry->type > _NETSNMP_FS_TYPE_LOCAL ?
252 NETSNMP_FS_TYPE_OTHER : entry->type);
253 *var_len = sizeof(fsys_type_id);
254 return (u_char *) fsys_type_id;
255
256 case HRFSYS_ACCESS:
257 long_return = entry->flags & NETSNMP_FS_FLAG_RONLY ? 2 : 1;
258 return (u_char *) & long_return;
259 case HRFSYS_BOOT:
260 long_return = entry->flags & NETSNMP_FS_FLAG_BOOTABLE ? 1 : 2;
261 return (u_char *) & long_return;
262 case HRFSYS_STOREIDX:
263 long_return = fsys_idx + NETSNMP_MEM_TYPE_MAX;
264 return (u_char *) & long_return;
265 case HRFSYS_FULLDUMP:
266 return when_dumped(entry->path, FULL_DUMP, var_len);
267 case HRFSYS_PARTDUMP:
268 return when_dumped(entry->path, PART_DUMP, var_len);
269 default:
270 DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrhfilesys\n",
271 vp->magic));
272 }
273 return NULL;
274 }
275
276
277 /*********************
278 *
279 * Internal implementation functions
280 *
281 *********************/
282
283 void
284 Init_HR_FileSys(void)
285 {
286 netsnmp_cache *c = netsnmp_fsys_get_cache();
287 netsnmp_cache_check_and_reload(c);
288 }
289
290 int
291 Get_Next_HR_FileSys(const netsnmp_fsys_info **entry)
292 {
293 *entry = *entry ? netsnmp_fsys_get_next(*entry) : netsnmp_fsys_get_first();
294 /* Skip "inactive" entries */
295 while (*entry && !((*entry)->flags & NETSNMP_FS_FLAG_ACTIVE))
296 *entry = netsnmp_fsys_get_next(*entry);
297
298 return *entry ? (*entry)->idx.oids[0] : -1;
299 }
300
301
302
303 static u_char *
304 when_dumped(const char *filesys, int level, size_t *length)
305 {
306 time_t dumpdate = 0, tmp;
307 FILE *dump_fp;
308 char line[1024];
309 const char *cp1;
310 char *cp2, *cp3;
311
312 /*
313 * Look for the relevent entries in /etc/dumpdates
314 *
315 * This is complicated by the fact that disks are
316 * mounted using block devices, but dumps are
317 * done via the raw character devices.
318 * Thus the device names in /etc/dumpdates and
319 * /etc/mnttab don't match.
320 * These comparisons are therefore made using the
321 * final portion of the device name only.
322 */
323
324 if (*filesys == '\0') /* No filesystem name? */
325 return date_n_time(NULL, length);
326 cp1 = strrchr(filesys, '/'); /* Find the last element of the current FS */
327
328 if (cp1 == NULL)
329 cp1 = filesys;
330
331 if ((dump_fp = fopen("/etc/dumpdates", "r")) == NULL)
332 return date_n_time(NULL, length);
333
334 while (fgets(line, sizeof(line), dump_fp) != NULL) {
335 cp2 = strchr(line, ' '); /* Start by looking at the device name only */
336 if (cp2 != NULL) {
337 *cp2 = '\0';
338 cp3 = strrchr(line, '/'); /* and find the last element */
339 if (cp3 == NULL)
340 cp3 = line;
341
342 if (strcmp(cp1, cp3) != 0) /* Wrong FS */
343 continue;
344
345 ++cp2;
346 while (isspace(0xFF & *cp2))
347 ++cp2; /* Now find the dump level */
348
349 if (level == FULL_DUMP) {
350 if (*(cp2++) != '0')
351 continue; /* Not interested in partial dumps */
352 while (isspace(0xFF & *cp2))
353 ++cp2;
354
355 dumpdate = ctime_to_timet(cp2);
356 fclose(dump_fp);
357 return date_n_time(&dumpdate, length);
358 } else { /* Partial Dump */
359 if (*(cp2++) == '0')
360 continue; /* Not interested in full dumps */
361 while (isspace(0xFF & *cp2))
362 ++cp2;
363
364 tmp = ctime_to_timet(cp2);
365 if (tmp > dumpdate)
366 dumpdate = tmp; /* Remember the 'latest' partial dump */
367 }
368 }
369 }
370
371 fclose(dump_fp);
372
373 return date_n_time(&dumpdate, length);
374 }
375
376
377 #define RAW_DEVICE_PREFIX "/dev/rdsk"
378 #define COOKED_DEVICE_PREFIX "/dev/dsk"
379
380 char *
381 cook_device(char *dev)
382 {
383 static char cooked_dev[SNMP_MAXPATH+1];
384
385 if (!strncmp(dev, RAW_DEVICE_PREFIX, strlen(RAW_DEVICE_PREFIX))) {
386 strlcpy(cooked_dev, COOKED_DEVICE_PREFIX, sizeof(cooked_dev));
387 strlcat(cooked_dev, dev + strlen(RAW_DEVICE_PREFIX),
388 sizeof(cooked_dev));
389 } else {
390 strlcpy(cooked_dev, dev, sizeof(cooked_dev));
391 }
392
393 return cooked_dev;
394 }
395
396
397 int
398 Get_FSIndex(char *dev)
399 {
400 netsnmp_fsys_info *fsys;
401
402 fsys = netsnmp_fsys_by_device( dev, NETSNMP_FS_FIND_EXIST );
403 return (fsys ? fsys->idx.oids[0] : -1 );
404 }
405
406 long
407 Get_FSSize(char *dev)
408 {
409 netsnmp_fsys_info *fsys;
410
411 fsys = netsnmp_fsys_by_device( dev, NETSNMP_FS_FIND_EXIST );
412 if ( fsys )
413 return netsnmp_fsys_size( fsys );
414 else
415 return -1;
416 }
417
418 int
419 Check_HR_FileSys_NFS(const netsnmp_fsys_info *entry)
420 {
421 return entry->flags & NETSNMP_FS_FLAG_REMOTE ? 1 : 0;
422 }
423
424 int
425 Check_HR_FileSys_AutoFs(const netsnmp_fsys_info *entry)
426 {
427 return entry->type == NETSNMP_FS_TYPE_AUTOFS;
428 }