~ubuntu-branches/ubuntu/trusty/net-snmp/trusty

« back to all changes in this revision

Viewing changes to agent/mibgroup/host/hr_storage.c

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2013-05-16 21:01:27 UTC
  • mfrom: (1.2.4) (1.1.23 sid)
  • Revision ID: package-import@ubuntu.com-20130516210127-y6meyeiobdx0131q
Tags: 5.7.2~dfsg-4ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - debian/rules: Don't stop service in runlevels 0 and 6.
  - debian/snmpd.init: LSBify the init script.
  - debian/snmp.preinst, debian/snmp.prerm: Kill any/all processes owned
    by snmp user before install/uninstall.
  - Add apport hook.
* debian/patches/ubuntu-fix-lp-587828.patch: Drop. Fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
#if defined(freebsd5)
9
9
/* undefine these in order to use getfsstat */
10
10
#undef HAVE_STATVFS
11
 
#undef STRUCT_STATVFS_HAS_F_FRSIZE
 
11
#undef HAVE_STRUCT_STATVFS_F_FRSIZE
12
12
#endif
13
13
 
14
14
#include <sys/types.h>
19
19
#include <unistd.h>
20
20
#endif
21
21
#if TIME_WITH_SYS_TIME
22
 
# ifdef WIN32
23
 
#  include <windows.h>
24
 
#  include <errno.h>
25
 
#  include <sys/timeb.h>
26
 
# else
27
 
#  include <sys/time.h>
28
 
# endif
 
22
# include <sys/time.h>
29
23
# include <time.h>
30
24
#else
31
25
# if HAVE_SYS_TIME_H
112
106
#endif                          /* if HAVE_SYS_SYSCTL_H */
113
107
#endif                          /* ifndef dynix */
114
108
 
115
 
#if (defined(aix4) || defined(aix5) || defined(aix6)) && HAVE_LIBPERFSTAT_H
 
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
116
113
#include <libperfstat.h>
117
114
#endif
118
115
 
160
157
#else
161
158
#include <strings.h>
162
159
#endif
 
160
#if HAVE_NBUTIL_H
 
161
#include <nbutil.h>
 
162
#endif
163
163
 
164
164
#include <net-snmp/utilities.h>
165
165
#include <net-snmp/output_api.h>
189
189
extern struct mnttab *HRFS_entry;
190
190
#define HRFS_mount      mnt_mountp
191
191
#define HRFS_statfs     statvfs
192
 
#define HRFS_HAS_FRSIZE STRUCT_STATVFS_HAS_F_FRSIZE
 
192
#define HRFS_HAS_FRSIZE HAVE_STRUCT_STATVFS_F_FRSIZE
193
193
 
194
194
#elif defined(WIN32)
195
195
/* fake block size */
205
205
extern int      fscount;
206
206
#define HRFS_statfs     statvfs
207
207
#define HRFS_mount      f_mntonname
208
 
#define HRFS_HAS_FRSIZE STRUCT_STATVFS_HAS_F_FRSIZE
 
208
#define HRFS_HAS_FRSIZE HAVE_STRUCT_STATVFS_F_FRSIZE
209
209
 
210
 
#elif defined(HAVE_STATVFS)  && defined(STRUCT_STATVFS_HAS_MNT_DIR)
 
210
#elif defined(HAVE_STATVFS)  && defined(HAVE_STRUCT_STATVFS_MNT_DIR)
211
211
 
212
212
extern struct mntent *HRFS_entry;
213
213
extern int      fscount;
214
214
#define HRFS_statfs     statvfs
215
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
216
224
#define HRFS_HAS_FRSIZE STRUCT_STATVFS_HAS_F_FRSIZE
217
225
 
218
226
#elif defined(HAVE_GETFSSTAT)
221
229
extern int      fscount;
222
230
#define HRFS_statfs     statfs
223
231
#define HRFS_mount      f_mntonname
224
 
#define HRFS_HAS_FRSIZE STRUCT_STATFS_HAS_F_FRSIZE
 
232
#define HRFS_HAS_FRSIZE HAVE_STRUCT_STATFS_F_FRSIZE
225
233
 
226
234
#else
227
235
 
228
236
extern struct mntent *HRFS_entry;
229
237
#define HRFS_mount      mnt_dir
230
238
#define HRFS_statfs     statfs
231
 
#define HRFS_HAS_FRSIZE STRUCT_STATFS_HAS_F_FRSIZE
 
239
#define HRFS_HAS_FRSIZE HAVE_STRUCT_STATFS_F_FRSIZE
232
240
 
233
241
#endif
234
242
        
249
257
                               size_t *, WriteMethod **);
250
258
void*           header_hrstoreEntry(struct variable *, oid *, size_t *,
251
259
                                    int, size_t *, WriteMethod **);
 
260
Netsnmp_Node_Handler handle_memsize;
252
261
 
253
262
#ifdef solaris2
254
263
void            sol_get_swapinfo(int *, int *);
263
272
#define HRSTORE_USED            7
264
273
#define HRSTORE_FAILS           8
265
274
 
266
 
struct variable4 hrstore_variables[] = {
267
 
    {HRSTORE_MEMSIZE, ASN_INTEGER, RONLY, var_hrstore, 1, {2}},
268
 
    {HRSTORE_INDEX, ASN_INTEGER, RONLY, var_hrstore, 3, {3, 1, 1}},
269
 
    {HRSTORE_TYPE, ASN_OBJECT_ID, RONLY, var_hrstore, 3, {3, 1, 2}},
270
 
    {HRSTORE_DESCR, ASN_OCTET_STR, RONLY, var_hrstore, 3, {3, 1, 3}},
271
 
    {HRSTORE_UNITS, ASN_INTEGER, RONLY, var_hrstore, 3, {3, 1, 4}},
272
 
    {HRSTORE_SIZE, ASN_INTEGER, RONLY, var_hrstore, 3, {3, 1, 5}},
273
 
    {HRSTORE_USED, ASN_INTEGER, RONLY, var_hrstore, 3, {3, 1, 6}},
274
 
    {HRSTORE_FAILS, ASN_COUNTER, RONLY, var_hrstore, 3, {3, 1, 7}}
 
275
struct variable2 hrstore_variables[] = {
 
276
    {HRSTORE_INDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
 
277
     var_hrstore, 1, {1}},
 
278
    {HRSTORE_TYPE, ASN_OBJECT_ID, NETSNMP_OLDAPI_RONLY,
 
279
     var_hrstore, 1, {2}},
 
280
    {HRSTORE_DESCR, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
 
281
     var_hrstore, 1, {3}},
 
282
    {HRSTORE_UNITS, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
 
283
     var_hrstore, 1, {4}},
 
284
    {HRSTORE_SIZE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
 
285
     var_hrstore, 1, {5}},
 
286
    {HRSTORE_USED, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
 
287
     var_hrstore, 1, {6}},
 
288
    {HRSTORE_FAILS, ASN_COUNTER, NETSNMP_OLDAPI_RONLY,
 
289
     var_hrstore, 1, {7}}
275
290
};
276
291
oid             hrstore_variables_oid[] = { 1, 3, 6, 1, 2, 1, 25, 2 };
 
292
oid             hrMemorySize_oid[]   = { 1, 3, 6, 1, 2, 1, 25, 2, 2 };
 
293
oid             hrStorageTable_oid[] = { 1, 3, 6, 1, 2, 1, 25, 2, 3, 1 };
277
294
 
278
295
 
279
296
void
281
298
{
282
299
    char *appname;
283
300
 
284
 
    REGISTER_MIB("host/hr_storage", hrstore_variables, variable4,
285
 
                 hrstore_variables_oid);
 
301
    netsnmp_register_scalar(
 
302
        netsnmp_create_handler_registration("host/hrMemorySize", handle_memsize,
 
303
                           hrMemorySize_oid, OID_LENGTH(hrMemorySize_oid),
 
304
                                             HANDLER_CAN_RONLY));
 
305
    REGISTER_MIB("host/hr_storage", hrstore_variables, variable2,
 
306
                 hrStorageTable_oid);
286
307
 
287
308
    appname = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
288
309
                                    NETSNMP_DS_LIB_APPTYPE);
317
338
}
318
339
 
319
340
/*
320
 
 * header_hrstore(...
 
341
 * header_hrstoreEntry(...
321
342
 * Arguments:
322
343
 * vp     IN      - pointer to variable entry that points here
323
344
 * name    IN/OUT  - IN/name requested, OUT/name found
328
349
 * 
329
350
 */
330
351
 
331
 
int
332
 
header_hrstore(struct variable *vp,
333
 
               oid * name,
334
 
               size_t * length,
335
 
               int exact, size_t * var_len, WriteMethod ** write_method)
336
 
{
337
 
#define HRSTORE_NAME_LENGTH     9
338
 
    oid             newname[MAX_OID_LEN];
339
 
    int             result;
340
 
 
341
 
    DEBUGMSGTL(("host/hr_storage", "var_hrstore: "));
342
 
    DEBUGMSGOID(("host/hr_storage", name, *length));
343
 
    DEBUGMSG(("host/hr_storage", " %d\n", exact));
344
 
 
345
 
    memcpy((char *) newname, (char *) vp->name, vp->namelen * sizeof(oid));
346
 
    newname[HRSTORE_NAME_LENGTH] = 0;
347
 
    result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);
348
 
    if ((exact && (result != 0)) || (!exact && (result >= 0)))
349
 
        return (MATCH_FAILED);
350
 
    memcpy((char *) name, (char *) newname,
351
 
           (vp->namelen + 1) * sizeof(oid));
352
 
    *length = vp->namelen + 1;
353
 
 
354
 
    *write_method = 0;
355
 
    *var_len = sizeof(long);    /* default to 'long' results */
356
 
    return (MATCH_SUCCEEDED);
357
 
}
358
 
 
359
352
void *
360
353
header_hrstoreEntry(struct variable *vp,
361
354
                    oid * name,
467
460
        }
468
461
    }
469
462
 
470
 
    *write_method = 0;
 
463
    *write_method = (WriteMethod*)0;
471
464
    *var_len = sizeof(long);    /* default to 'long' results */
472
465
 
473
466
    /*
475
468
     */
476
469
    DEBUGMSGTL(("host/hr_storage", "var_hrstoreEntry: process "));
477
470
    DEBUGMSGOID(("host/hr_storage", name, *length));
478
 
    DEBUGMSG(("host/hr_storage", " (%x)\n", mem));
 
471
    DEBUGMSG(("host/hr_storage", " (%p)\n", mem));
479
472
    return (void*)mem;
480
473
}
481
474
 
489
482
         *
490
483
         *********************/
491
484
 
492
 
static const char *hrs_descr[] = {
493
 
    NULL,
494
 
    "Memory Buffers",           /* HRS_TYPE_MBUF */
495
 
    "Real Memory",              /* HRS_TYPE_MEM */
496
 
    "Swap Space"                /* HRS_TYPE_SWAP */
497
 
};
498
 
 
 
485
int
 
486
handle_memsize(netsnmp_mib_handler *handler,
 
487
                netsnmp_handler_registration *reginfo,
 
488
                netsnmp_agent_request_info *reqinfo,
 
489
                netsnmp_request_info *requests)
 
490
{
 
491
    netsnmp_memory_info *mem_info;
 
492
    int val;
 
493
 
 
494
    /*
 
495
     * We just need to handle valid GET requests, as invalid instances
 
496
     *   are rejected automatically, and (valid) GETNEXT requests are
 
497
     *   converted into the appropriate GET request.
 
498
     *
 
499
     * We also only ever receive one request at a time.
 
500
     */
 
501
    switch (reqinfo->mode) {
 
502
    case MODE_GET:
 
503
        netsnmp_memory_load();
 
504
        mem_info = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_PHYSMEM, 0 );
 
505
        if ( !mem_info || mem_info->size == -1 || mem_info->units == -1 )
 
506
            netsnmp_set_request_error( reqinfo, requests, SNMP_NOSUCHOBJECT );
 
507
        else {
 
508
            val  =  mem_info->size;     /* memtotal */
 
509
            val *= (mem_info->units/1024);
 
510
            snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
 
511
                                     (u_char *)&val, sizeof(val));
 
512
        }
 
513
        return SNMP_ERR_NOERROR;
 
514
 
 
515
    default:
 
516
        /*
 
517
         * we should never get here, so this is a really bad error 
 
518
         */
 
519
        snmp_log(LOG_ERR, "unknown mode (%d) in handle_memsize\n",
 
520
                 reqinfo->mode);
 
521
        return SNMP_ERR_GENERR;
 
522
    }
 
523
 
 
524
    return SNMP_ERR_NOERROR;
 
525
}
499
526
 
500
527
 
501
528
u_char         *
510
537
    void                *ptr;
511
538
    netsnmp_memory_info *mem = NULL;
512
539
 
513
 
    if (vp->magic == HRSTORE_MEMSIZE) {
514
 
        if (header_hrstore(vp, name, length, exact, var_len, write_method)
515
 
            == MATCH_FAILED)
516
 
            return NULL;
517
 
    } else {
518
 
 
519
540
really_try_next:
520
541
        ptr = header_hrstoreEntry(vp, name, length, exact, var_len,
521
542
                                        write_method);
523
544
            return NULL;
524
545
 
525
546
        store_idx = name[ HRSTORE_ENTRY_NAME_LENGTH ];
526
 
        if (HRFS_entry &&
527
 
            store_idx > NETSNMP_MEM_TYPE_MAX &&
528
 
            netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
529
 
                                   NETSNMP_DS_AGENT_SKIPNFSINHOSTRESOURCES) &&
530
 
            Check_HR_FileSys_NFS())
531
 
            return NULL;
532
547
        if (store_idx > NETSNMP_MEM_TYPE_MAX ) {
 
548
            if ( netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
 
549
                                        NETSNMP_DS_AGENT_SKIPNFSINHOSTRESOURCES) &&
 
550
                 Check_HR_FileSys_NFS())
 
551
                return NULL;  /* or goto try_next; */
533
552
            if (HRFS_statfs(HRFS_entry->HRFS_mount, &stat_buf) < 0) {
534
553
                snmp_log_perror(HRFS_entry->HRFS_mount);
535
554
                goto try_next;
537
556
        } else {
538
557
            mem = (netsnmp_memory_info*)ptr;
539
558
        }
540
 
    }
541
559
 
542
560
 
543
561
 
544
562
    switch (vp->magic) {
545
 
    case HRSTORE_MEMSIZE:
546
 
        netsnmp_memory_load();
547
 
        mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_PHYSMEM, 0 );
548
 
        if ( !mem || mem->size == -1 || mem->units == -1 )
549
 
            return NULL;
550
 
        long_return = mem->size * (mem->units / 1024);
551
 
        return (u_char *) & long_return;
552
 
 
553
563
    case HRSTORE_INDEX:
554
564
        long_return = store_idx;
555
565
        return (u_char *) & long_return;
557
567
        if (store_idx > NETSNMP_MEM_TYPE_MAX)
558
568
            if (storageUseNFS && Check_HR_FileSys_NFS())
559
569
                storage_type_id[storage_type_len - 1] = 10;     /* Network Disk */
560
 
#if HAVE_HASMNTOPT && !(defined(aix4) || defined(aix5) || defined(aix6))
 
570
#if HAVE_HASMNTOPT && !(defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7))
561
571
            /* 
562
572
             * hasmntopt takes "const struct mntent*", but HRFS_entry has been
563
573
             * defined differently for AIX, so skip this for AIX
585
595
        return (u_char *) storage_type_id;
586
596
    case HRSTORE_DESCR:
587
597
        if (store_idx > NETSNMP_MEM_TYPE_MAX) {
588
 
            strncpy(string, HRFS_entry->HRFS_mount, sizeof(string)-1);
589
 
            string[ sizeof(string)-1 ] = 0;
 
598
            strlcpy(string, HRFS_entry->HRFS_mount, sizeof(string));
590
599
            *var_len = strlen(string);
591
600
            return (u_char *) string;
592
601
        } else {
659
668
         *
660
669
         *********************/
661
670
 
662
 
static int      FS_storage;
663
671
static int      HRS_index;
664
672
 
665
673
void