summaryrefslogtreecommitdiff
path: root/agent/mibgroup/hardware/fsys/hw_fsys.c
blob: a6cd94db08e9a8bd1cc1d8753e35d52cae80968f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-features.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <net-snmp/agent/hardware/fsys.h>
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif

netsnmp_feature_child_of(hw_fsys_get_container, netsnmp_unused)

extern void             netsnmp_fsys_arch_load( void );
extern void             netsnmp_fsys_arch_init( void );
static int  _fsys_load( void );
static void _fsys_free( void );

static int _fsysAutoUpdate = 0;   /* 0 means on-demand caching */
static void _fsys_update_stats( unsigned int, void* );

netsnmp_cache     *_fsys_cache     = NULL;
netsnmp_container *_fsys_container = NULL;
static int         _fsys_idx       = 0;
static netsnmp_fsys_info * _fsys_create_entry( void );

void init_hw_fsys( void ) {

    if ( _fsys_container )
        return;   /* Already initialised */

    DEBUGMSGTL(("fsys", "Initialise Hardware FileSystem module\n"));

    /*
     * Define a container to hold the list of filesystems
     */
    _fsys_container = netsnmp_container_find("fsysTable:table_container");
    if ( NULL == _fsys_container ) {
        snmp_log( LOG_ERR, "failed to create container for fsysTable");
        return;
    }
    netsnmp_fsys_arch_init( );

    /*
     * If we're sampling the file system information automatically,
     *   then arrange for this to be triggered regularly.
     *
     * If we're not sampling these values regularly,
     *   create a suitable cache handler instead.
     */
    if ( _fsysAutoUpdate ) {
        DEBUGMSGTL(("fsys", "Reloading Hardware FileSystems automatically (%d)\n",
                               _fsysAutoUpdate));
        snmp_alarm_register( _fsysAutoUpdate, SA_REPEAT,
                             _fsys_update_stats, NULL );
    }
    else {
        _fsys_cache = netsnmp_cache_create( 5, netsnmp_fsys_load,
                                               netsnmp_fsys_free, NULL, 0 );
        DEBUGMSGTL(("fsys", "Reloading Hardware FileSystems on-demand (%p)\n",
                               _fsys_cache));
    }
}

void shutdown_hw_fsys( void ) {
    _fsys_free();
}

#ifndef NETSNMP_FEATURE_REMOVE_HW_FSYS_GET_CONTAINER
/*
 *  Return the main fsys container
 */
netsnmp_container *netsnmp_fsys_get_container( void ) { return _fsys_container; }
#endif /* NETSNMP_FEATURE_REMOVE_HW_FSYS_GET_CONTAINER */

/*
 *  Return the main fsys cache control structure (if defined)
 */
netsnmp_cache *netsnmp_fsys_get_cache( void ) { return _fsys_cache; }


/*
 * Wrapper routine for automatically updating fsys information
 */
void
_fsys_update_stats( unsigned int clientreg, void *data )
{
    _fsys_free();
    _fsys_load();
}

/*
 * Wrapper routine for re-loading filesystem statistics on demand
 */
int
netsnmp_fsys_load( netsnmp_cache *cache, void *data )
{
    /* XXX - check cache timeliness */
    return _fsys_load();
}

/*
 * Wrapper routine for releasing expired filesystem statistics
 */
void
netsnmp_fsys_free( netsnmp_cache *cache, void *data )
{
    _fsys_free();
}


/*
 * Architecture-independent processing of loading filesystem statistics
 */
static int
_fsys_load( void )
{
    netsnmp_fsys_arch_load();
    /* XXX - update cache timestamp */
    return 0;
}

/*
 * Architecture-independent release of filesystem statistics
 */
static void
_fsys_free( void )
{
    netsnmp_fsys_info *sp;

    for (sp = CONTAINER_FIRST( _fsys_container );
         sp;
         sp = CONTAINER_NEXT(  _fsys_container, sp )) {

         sp->flags &= ~NETSNMP_FS_FLAG_ACTIVE;
    }
}


netsnmp_fsys_info *netsnmp_fsys_get_first( void ) {
    return CONTAINER_FIRST( _fsys_container );
}
netsnmp_fsys_info *netsnmp_fsys_get_next( netsnmp_fsys_info *this_ptr ) {
    return CONTAINER_NEXT( _fsys_container, this_ptr );
}

/*
 * Retrieve a filesystem entry based on the path where it is mounted,
 *  or (optionally) insert a new one into the container
 */
netsnmp_fsys_info *
netsnmp_fsys_by_path( char *path, int create_type )
{
    netsnmp_fsys_info *sp;

    DEBUGMSGTL(("fsys:path", "Get filesystem entry (%s)\n", path));

    /*
     *  Look through the list for a matching entry
     */
        /* .. or use a secondary index container ?? */
    for (sp = CONTAINER_FIRST( _fsys_container );
         sp;
         sp = CONTAINER_NEXT(  _fsys_container, sp )) {

        if ( !strcmp( path, sp->path ))
            return sp;
    }

    /*
     * Not found...
     */
    if ( create_type == NETSNMP_FS_FIND_EXIST ) {
        DEBUGMSGTL(("fsys:path", "No such filesystem entry\n"));
        return NULL;
    }

    /*
     * ... so let's create a new one
     */
    sp = _fsys_create_entry();
    if (sp)
        strlcpy(sp->path, path, sizeof(sp->path));
    return sp;
}


/*
 * Retrieve a filesystem entry based on the hardware device,
 *   (or exported path for remote mounts).
 * (Optionally) insert a new one into the container.
 */
netsnmp_fsys_info *
netsnmp_fsys_by_device( char *device, int create_type )
{
    netsnmp_fsys_info *sp;

    DEBUGMSGTL(("fsys:device", "Get filesystem entry (%s)\n", device));

    /*
     *  Look through the list for a matching entry
     */
        /* .. or use a secondary index container ?? */
    for (sp = CONTAINER_FIRST( _fsys_container );
         sp;
         sp = CONTAINER_NEXT(  _fsys_container, sp )) {

        if ( !strcmp( device, sp->device ))
            return sp;
    }

    /*
     * Not found...
     */
    if ( create_type == NETSNMP_FS_FIND_EXIST ) {
        DEBUGMSGTL(("fsys:device", "No such filesystem entry\n"));
        return NULL;
    }

    /*
     * ... so let's create a new one
     */
    sp = _fsys_create_entry();
    if (sp)
        strlcpy(sp->device, device, sizeof(sp->device));
    return sp;
}


netsnmp_fsys_info *
_fsys_create_entry( void )
{
    netsnmp_fsys_info *sp;

    sp = SNMP_MALLOC_TYPEDEF( netsnmp_fsys_info );
    if ( sp ) {
        /*
         * Set up the index value.
         *  
         * All this trouble, just for a simple integer.
         * Surely there must be a better way?
         */
        sp->idx.len  = 1;
        sp->idx.oids = SNMP_MALLOC_TYPEDEF( oid );
        sp->idx.oids[0] = ++_fsys_idx;
    }

    DEBUGMSGTL(("fsys:new", "Create filesystem entry (index = %d)\n", _fsys_idx));
    CONTAINER_INSERT( _fsys_container, sp );
    return sp;
}


/*
 *  Convert fsys size information to 1K units
 *    (attempting to avoid 32-bit overflow!)
 */
unsigned long long
_fsys_to_K( unsigned long long size, unsigned long long units )
{
    int factor = 1;

    if ( units == 0 ) {
        return 0;    /* XXX */
    } else if ( units == 1024 ) {
        return size;
    } else if ( units == 512 ) {      /* To avoid unnecessary division */
        return size/2;
    } else if ( units < 1024 ) {
        factor = 1024 / units;   /* Assuming power of two */
        return (size / factor);
    } else {
        factor = units / 1024;   /* Assuming multiple of 1K */
        return (size * factor);
    }
}

unsigned long long
netsnmp_fsys_size_ull( netsnmp_fsys_info *f) {
    if ( !f ) {
        return 0;
    }
    return _fsys_to_K( f->size, f->units );
}

unsigned long long
netsnmp_fsys_used_ull( netsnmp_fsys_info *f) {
    if ( !f ) {
        return 0;
    }
    return _fsys_to_K( f->used, f->units );
}

unsigned long long
netsnmp_fsys_avail_ull( netsnmp_fsys_info *f) {
    if ( !f ) {
        return 0;
    }
    return _fsys_to_K( f->avail, f->units );
}


int
netsnmp_fsys_size( netsnmp_fsys_info *f) {
    unsigned long long v = netsnmp_fsys_size_ull(f);
    return (int)v;
}

int
netsnmp_fsys_used( netsnmp_fsys_info *f) {
    unsigned long long v = netsnmp_fsys_used_ull(f);
    return (int)v;
}

int
netsnmp_fsys_avail( netsnmp_fsys_info *f) {
    unsigned long long v = netsnmp_fsys_avail_ull(f);
    return (int)v;
}

#ifndef INT32_MAX
#define INT32_MAX 0x7fffffff
#endif

#ifndef PRIu64
#define PRIu64 "llu"
#endif

/* recalculate f->size_32, used_32, avail_32 and units_32 from f->size & comp.*/
void
netsnmp_fsys_calculate32(netsnmp_fsys_info *f)
{
    unsigned long long s = f->size;
    unsigned shift = 0;

    while (s > INT32_MAX) {
        s = s >> 1;
        shift++;
    }

    f->size_32 = s;
    f->units_32 = f->units << shift;
    f->avail_32 = f->avail >> shift;
    f->used_32 = f->used >> shift;

    DEBUGMSGTL(("fsys", "Results of 32-bit conversion: size %" PRIu64 " -> %lu;"
		" units %" PRIu64 " -> %lu; avail %" PRIu64 " -> %lu;"
                " used %" PRIu64 " -> %lu\n",
		(uint64_t)f->size, f->size_32, (uint64_t)f->units, f->units_32,
		(uint64_t)f->avail, f->avail_32, (uint64_t)f->used, f->used_32));
}