Debian Codegraph @

agent/mibgroup/hardware/fsys/hw_fsys.c

raw
1 #define __USE_MINGW_ANSI_STDIO 0
2 #include <net-snmp/net-snmp-config.h>
3 #include <net-snmp/net-snmp-features.h>
4 #include <net-snmp/net-snmp-includes.h>
5 #include <net-snmp/agent/net-snmp-agent-includes.h>
6 #include <net-snmp/agent/hardware/fsys.h>
7 #include "hw_fsys.h"
8 #include "hardware/fsys/hw_fsys_private.h"
9 #ifdef HAVE_INTTYPES_H
10 #include <inttypes.h>
11 #endif
12 #if defined(HAVE_PCRE_H)
13 #include <pcre.h>
14 #elif defined(HAVE_REGEX_H)
15 #include <sys/types.h>
16 #include <regex.h>
17 #endif
18
19 netsnmp_feature_child_of(hw_fsys_get_container, netsnmp_unused);
20
21 static int _fsysAutoUpdate = 0; /* 0 means on-demand caching */
22
23 static netsnmp_cache *_fsys_cache;
24 static netsnmp_container *_fsys_container;
25 static int _fsys_idx;
26
27 static void _parse_mount_config(const char *, char *);
28 static void _free_mount_config(void);
29
30 conf_mount_list *ignoremount_list;
31
32 /*
33 * Architecture-independent processing of loading filesystem statistics
34 */
35 static int
36 _fsys_load(void)
37 {
38 netsnmp_fsys_arch_load();
39 /* XXX - update cache timestamp */
40 return 0;
41 }
42
43 /*
44 * Architecture-independent release of filesystem statistics
45 */
46 static void
47 _fsys_free(void)
48 {
49 netsnmp_fsys_info *sp;
50
51 for (sp = CONTAINER_FIRST(_fsys_container); sp;
52 sp = CONTAINER_NEXT(_fsys_container, sp)) {
53 sp->flags &= ~NETSNMP_FS_FLAG_ACTIVE;
54 }
55 }
56
57 /*
58 * Wrapper routine for automatically updating fsys information
59 */
60 void
61 _fsys_update_stats(unsigned int clientreg, void *data)
62 {
63 _fsys_free();
64 _fsys_load();
65 }
66
67 void init_hw_fsys( void ) {
68
69 if ( _fsys_container )
70 return; /* Already initialised */
71
72 DEBUGMSGTL(("fsys", "Initialise Hardware FileSystem module\n"));
73
74 /*
75 * Define a container to hold the list of filesystems
76 */
77 _fsys_container = netsnmp_container_find("fsysTable:table_container");
78 if ( NULL == _fsys_container ) {
79 snmp_log( LOG_ERR, "failed to create container for fsysTable");
80 return;
81 }
82 netsnmp_fsys_arch_init( );
83
84 /*
85 * If we're sampling the file system information automatically,
86 * then arrange for this to be triggered regularly.
87 *
88 * If we're not sampling these values regularly,
89 * create a suitable cache handler instead.
90 */
91 if ( _fsysAutoUpdate ) {
92 DEBUGMSGTL(("fsys", "Reloading Hardware FileSystems automatically (%d)\n",
93 _fsysAutoUpdate));
94 snmp_alarm_register( _fsysAutoUpdate, SA_REPEAT,
95 _fsys_update_stats, NULL );
96 }
97 else {
98 _fsys_cache = netsnmp_cache_create( 5, netsnmp_fsys_load,
99 netsnmp_fsys_free, NULL, 0 );
100 DEBUGMSGTL(("fsys", "Reloading Hardware FileSystems on-demand (%p)\n",
101 _fsys_cache));
102 }
103
104 snmpd_register_config_handler("ignoremount", _parse_mount_config,
105 _free_mount_config, "name");
106 }
107
108 void shutdown_hw_fsys( void ) {
109 _fsys_free();
110 }
111
112 #ifndef NETSNMP_FEATURE_REMOVE_HW_FSYS_GET_CONTAINER
113 /*
114 * Return the main fsys container
115 */
116 netsnmp_container *netsnmp_fsys_get_container( void ) { return _fsys_container; }
117 #endif /* NETSNMP_FEATURE_REMOVE_HW_FSYS_GET_CONTAINER */
118
119 /*
120 * Return the main fsys cache control structure (if defined)
121 */
122 netsnmp_cache *netsnmp_fsys_get_cache( void ) { return _fsys_cache; }
123
124
125 /*
126 * Wrapper routine for re-loading filesystem statistics on demand
127 */
128 int
129 netsnmp_fsys_load( netsnmp_cache *cache, void *data )
130 {
131 /* XXX - check cache timeliness */
132 return _fsys_load();
133 }
134
135 /*
136 * Wrapper routine for releasing expired filesystem statistics
137 */
138 void
139 netsnmp_fsys_free( netsnmp_cache *cache, void *data )
140 {
141 _fsys_free();
142 }
143
144
145 netsnmp_fsys_info *netsnmp_fsys_get_first(void) {
146 return CONTAINER_FIRST(_fsys_container);
147 }
148
149 netsnmp_fsys_info *netsnmp_fsys_get_next(const netsnmp_fsys_info *this_ptr) {
150 return CONTAINER_NEXT(_fsys_container, this_ptr);
151 }
152
153 netsnmp_fsys_info *
154 _fsys_create_entry(void)
155 {
156 netsnmp_fsys_info *sp;
157
158 sp = SNMP_MALLOC_TYPEDEF(netsnmp_fsys_info);
159 if (!sp)
160 return NULL;
161
162 /*
163 * Set up the index value.
164 *
165 * All this trouble, just for a simple integer.
166 * Surely there must be a better way?
167 */
168 sp->idx.len = 1;
169 sp->idx.oids = SNMP_MALLOC_TYPEDEF( oid );
170 sp->idx.oids[0] = ++_fsys_idx;
171
172 DEBUGMSGTL(("fsys:new", "Create filesystem entry (index = %d)\n", _fsys_idx));
173 CONTAINER_INSERT(_fsys_container, sp);
174 return sp;
175 }
176
177 /*
178 * Retrieve a filesystem entry based on the path where it is mounted,
179 * or (optionally) insert a new one into the container
180 */
181 netsnmp_fsys_info *
182 netsnmp_fsys_by_path( char *path, int create_type )
183 {
184 netsnmp_fsys_info *sp;
185
186 DEBUGMSGTL(("fsys:path", "Get filesystem entry (%s)\n", path));
187
188 /*
189 * Look through the list for a matching entry
190 */
191 /* .. or use a secondary index container ?? */
192 for (sp = CONTAINER_FIRST( _fsys_container );
193 sp;
194 sp = CONTAINER_NEXT( _fsys_container, sp )) {
195
196 if ( !strcmp( path, sp->path ))
197 return sp;
198 }
199
200 /*
201 * Not found...
202 */
203 if ( create_type == NETSNMP_FS_FIND_EXIST ) {
204 DEBUGMSGTL(("fsys:path", "No such filesystem entry\n"));
205 return NULL;
206 }
207
208 /*
209 * ... so let's create a new one
210 */
211 sp = _fsys_create_entry();
212 if (sp)
213 strlcpy(sp->path, path, sizeof(sp->path));
214 return sp;
215 }
216
217
218 /*
219 * Retrieve a filesystem entry based on the hardware device,
220 * (or exported path for remote mounts).
221 * (Optionally) insert a new one into the container.
222 */
223 netsnmp_fsys_info *
224 netsnmp_fsys_by_device( char *device, int create_type )
225 {
226 netsnmp_fsys_info *sp;
227
228 DEBUGMSGTL(("fsys:device", "Get filesystem entry (%s)\n", device));
229
230 /*
231 * Look through the list for a matching entry
232 */
233 /* .. or use a secondary index container ?? */
234 for (sp = CONTAINER_FIRST( _fsys_container );
235 sp;
236 sp = CONTAINER_NEXT( _fsys_container, sp )) {
237
238 if ( !strcmp( device, sp->device ))
239 return sp;
240 }
241
242 /*
243 * Not found...
244 */
245 if ( create_type == NETSNMP_FS_FIND_EXIST ) {
246 DEBUGMSGTL(("fsys:device", "No such filesystem entry\n"));
247 return NULL;
248 }
249
250 /*
251 * ... so let's create a new one
252 */
253 sp = _fsys_create_entry();
254 if (sp)
255 strlcpy(sp->device, device, sizeof(sp->device));
256 return sp;
257 }
258
259
260 /*
261 * Convert fsys size information to 1K units
262 * (attempting to avoid 32-bit overflow!)
263 */
264 unsigned long long
265 _fsys_to_K( unsigned long long size, unsigned long long units )
266 {
267 int factor = 1;
268
269 if ( units == 0 ) {
270 return 0; /* XXX */
271 } else if ( units == 1024 ) {
272 return size;
273 } else if ( units == 512 ) { /* To avoid unnecessary division */
274 return size/2;
275 } else if ( units < 1024 ) {
276 factor = 1024 / units; /* Assuming power of two */
277 return (size / factor);
278 } else {
279 factor = units / 1024; /* Assuming multiple of 1K */
280 return (size * factor);
281 }
282 }
283
284 unsigned long long
285 netsnmp_fsys_size_ull(const netsnmp_fsys_info *f)
286 {
287 if ( !f ) {
288 return 0;
289 }
290 return _fsys_to_K( f->size, f->units );
291 }
292
293 unsigned long long
294 netsnmp_fsys_used_ull(const netsnmp_fsys_info *f)
295 {
296 if ( !f ) {
297 return 0;
298 }
299 return _fsys_to_K( f->used, f->units );
300 }
301
302 unsigned long long
303 netsnmp_fsys_avail_ull(const netsnmp_fsys_info *f)
304 {
305 if ( !f ) {
306 return 0;
307 }
308 return _fsys_to_K( f->avail, f->units );
309 }
310
311
312 int
313 netsnmp_fsys_size(const netsnmp_fsys_info *f)
314 {
315 unsigned long long v = netsnmp_fsys_size_ull(f);
316 return (int)v;
317 }
318
319 int
320 netsnmp_fsys_used(const netsnmp_fsys_info *f)
321 {
322 unsigned long long v = netsnmp_fsys_used_ull(f);
323 return (int)v;
324 }
325
326 int
327 netsnmp_fsys_avail(const netsnmp_fsys_info *f)
328 {
329 unsigned long long v = netsnmp_fsys_avail_ull(f);
330 return (int)v;
331 }
332
333 #ifndef INT32_MAX
334 #define INT32_MAX 0x7fffffff
335 #endif
336
337 #ifndef PRIu64
338 #define PRIu64 "llu"
339 #endif
340
341 /* recalculate f->size_32, used_32, avail_32 and units_32 from f->size & comp.*/
342 void
343 netsnmp_fsys_calculate32(netsnmp_fsys_info *f)
344 {
345 unsigned long long s = f->size;
346 unsigned shift = 0;
347
348 while (s > INT32_MAX) {
349 s = s >> 1;
350 shift++;
351 }
352
353 f->size_32 = s;
354 f->units_32 = f->units << shift;
355 f->avail_32 = f->avail >> shift;
356 f->used_32 = f->used >> shift;
357
358 DEBUGMSGTL(("fsys", "Results of 32-bit conversion: size %" PRIu64 " -> %lu;"
359 " units %" PRIu64 " -> %lu; avail %" PRIu64 " -> %lu;"
360 " used %" PRIu64 " -> %lu\n",
361 (uint64_t)f->size, f->size_32, (uint64_t)f->units, f->units_32,
362 (uint64_t)f->avail, f->avail_32, (uint64_t)f->used, f->used_32));
363 }
364
365 static void
366 _parse_mount_config(const char *token, char *cptr)
367 {
368 conf_mount_list *m_new;
369 char *name, *st = NULL;
370 #if defined(HAVE_PCRE_H)
371 const char *pcre_error;
372 int pcre_error_offset;
373 int is_regex = 0;
374 #elif defined(HAVE_REGEX_H)
375 int r = 0;
376 int is_regex = 0;
377 #endif
378
379 name = strtok_r(cptr, " \t", &st);
380 if (strcmp(name, "-r") == 0) {
381 #if defined(HAVE_PCRE_H) || defined(HAVE_REGEX_H)
382 is_regex = 1;
383 name = strtok_r(NULL, " \t", &st);
384 #else
385 config_perror("Missing regex support");
386 return;
387 #endif
388 }
389 m_new = SNMP_MALLOC_TYPEDEF(conf_mount_list);
390 if (!m_new) {
391 config_perror("Out of memory");
392 goto err;
393 }
394 m_new->name = strdup(name);
395 if (!m_new->name) {
396 config_perror("Out of memory");
397 goto err;
398 }
399 #if defined(HAVE_PCRE_H)
400 if (is_regex) {
401 m_new->regex_ptr = pcre_compile(m_new->name, 0, &pcre_error,
402 &pcre_error_offset, NULL);
403 if (!m_new->regex_ptr) {
404 config_perror(pcre_error);
405 goto err;
406 }
407 }
408 #elif defined(HAVE_REGEX_H)
409 if (is_regex) {
410 m_new->regex_ptr = malloc(sizeof(regex_t));
411 if (!m_new->regex_ptr) {
412 config_perror("Out of memory");
413 goto err;
414 }
415 r = regcomp(m_new->regex_ptr, m_new->name, REG_NOSUB);
416 if (r) {
417 char buf[BUFSIZ];
418 size_t regerror_len = 0;
419
420 regerror_len = regerror(r, m_new->regex_ptr, buf, BUFSIZ);
421 if (regerror_len >= BUFSIZ)
422 buf[BUFSIZ - 1] = '\0';
423 else
424 buf[regerror_len] = '\0';
425 config_perror(buf);
426 goto err;
427 }
428 }
429 #endif
430 m_new->next = ignoremount_list;
431 ignoremount_list = m_new;
432 return;
433
434 err:
435 if (m_new) {
436 #if defined(HAVE_PCRE_H) || defined(HAVE_REGEX_H)
437 free(m_new->regex_ptr);
438 #endif
439 free(m_new->name);
440 }
441 free (m_new);
442 }
443
444 static void
445 _free_mount_config(void)
446 {
447 conf_mount_list *m_ptr = ignoremount_list, *m_next;
448 while (m_ptr) {
449 m_next = m_ptr->next;
450 #if defined(HAVE_PCRE_H) || defined(HAVE_REGEX_H)
451 free(m_ptr->regex_ptr);
452 #endif
453 free(m_ptr->name);
454 free(m_ptr);
455 m_ptr = m_next;
456 }
457 ignoremount_list = NULL;
458 }