コード例 #1
0
ファイル: memory_netbsd.c プロジェクト: ColdStart/SNMPD
    /*
     * If there are multiple swap devices, record
     *   the statistics for each one individually.
     */
void
swapinfo(long pagesize)
{
    int             i, n;
    struct swapent *s;
    netsnmp_memory_info *mem;
    char buf[1024];

        /*
         * If there's only one swap device, don't bother
         */
    n = swapctl( SWAP_NSWAP, NULL, 0 );
    if ( n <= 1 )
        return;

    s = (struct swapent*)calloc(n, sizeof(struct swapent));
    swapctl( SWAP_STATS, s, n );

    for (i = 0; i < n; ++i) {
        mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SWAP+1+i, 1 );
        if (!mem)
            continue;
        if (!mem->descr) {
         /* sprintf(buf, "swap #%d", s[i].se_dev); */
            sprintf(buf, "swap %s",  s[i].se_path);
            mem->descr = strdup( buf );
        }
        mem->units = pagesize;
        mem->size  = s[i].se_nblks;
        mem->free  = s[i].se_nblks - s[i].se_inuse;
        mem->other = -1;
    }
}
コード例 #2
0
ファイル: memory_hpux.cpp プロジェクト: duniansampa/SigLog
/*
 * Retained from UCD implementation
 */
void
get_swapinfo(long *total, long *free, long *size)
{
    struct pst_swapinfo  pss;
    netsnmp_memory_info *mem;
    int  i = 0;
    char buf[1024];

    while (pstat_getswap(&pss, sizeof(pss), (size_t) 1, i) != -1) {
        if (pss.pss_idx == (unsigned) i) {
            /*
             * TODO - Skip if only one swap device
             */
            mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SWAP+1+i, 1 );
            if (!mem)
                continue;
            if (!mem->descr) {
                sprintf(buf, "swap #%d %s", i, pss.pss_mntpt);
                mem->descr = strdup( buf );
            }
            mem->units = 1024;
            mem->size  = (pss.pss_nblksavail * (DEV_BSIZE/512)) / 2;    /* Or pss_nblks      ? */
            mem->free  = 4*pss.pss_nfpgs;           /* Or pss_nblksavail ? */
            mem->other = -1;
            *total +=mem->size;
            *free  +=mem->free;
            *size   = mem->units;  /* Hopefully consistent! */
            i++;
        } else
            return;
    }
}                               /* end get_swapinfo */
コード例 #3
0
int
swapmode(long pagesize)
{
    int             i, n;
    static kvm_t   *kd = NULL;
    struct kvm_swap kswap[16];
    netsnmp_memory_info *mem;
    char buf[1024];

    if (kd == NULL)
        kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
    n = kvm_getswapinfo(kd, kswap, sizeof(kswap) / sizeof(kswap[0]), 0);

    swapUsed = swapTotal = swapFree = 0;

    if ( n > 1 ) {
        /*
         * If there are multiple swap devices, then record
         *   the statistics for each one separately...
         */
        for (i = 0; i < n; ++i) {
            mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SWAP+1+i, 1 );
            if (!mem)
                continue;
            if (!mem->descr) {
                sprintf(buf, "swap %s", kswap[i].ksw_devname);
                mem->descr = strdup( buf );
            }
            mem->units = pagesize;
            mem->size  = kswap[i].ksw_total;
            mem->free  = kswap[i].ksw_total - kswap[i].ksw_used;
            /*
             *  ... and keep a running total for the overall swap stats
             */
            swapTotal += kswap[i].ksw_total;
            swapUsed  += kswap[i].ksw_used;
        }
    } else {
        /*
         * If there's only one swap device, then don't bother
         *   with individual statistics.
         */
        swapTotal += kswap[0].ksw_total;
        swapUsed  += kswap[0].ksw_used;
    }

    swapFree = swapTotal - swapUsed;
    return n;
}
コード例 #4
0
ファイル: hrh_storage.c プロジェクト: michalklempa/net-snmp
int
handle_memsize(netsnmp_mib_handler *handler,
                netsnmp_handler_registration *reginfo,
                netsnmp_agent_request_info *reqinfo,
                netsnmp_request_info *requests)
{
    netsnmp_memory_info *mem_info;
    int val;

    /*
     * We just need to handle valid GET requests, as invalid instances
     *   are rejected automatically, and (valid) GETNEXT requests are
     *   converted into the appropriate GET request.
     *
     * We also only ever receive one request at a time.
     */
    switch (reqinfo->mode) {
    case MODE_GET:
        netsnmp_memory_load();
        mem_info = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_PHYSMEM, 0 );
        if ( !mem_info || mem_info->size == -1 || mem_info->units == -1 )
            netsnmp_set_request_error( reqinfo, requests, SNMP_NOSUCHOBJECT );
	else {
            val  =  mem_info->size;     /* memtotal */
            val *= (mem_info->units/1024);
            snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
                                     (u_char *)&val, sizeof(val));
        }
        return SNMP_ERR_NOERROR;

    default:
        /*
         * we should never get here, so this is a really bad error 
         */
        snmp_log(LOG_ERR, "unknown mode (%d) in handle_memsize\n",
                 reqinfo->mode);
        return SNMP_ERR_GENERR;
    }

    return SNMP_ERR_NOERROR;
}
コード例 #5
0
ファイル: memory_hpux.cpp プロジェクト: duniansampa/SigLog
    /*
     * Load the latest memory usage statistics
     */
int netsnmp_mem_arch_load( netsnmp_cache *cache, void *magic ) {

    struct pst_static pst;
    struct pst_dynamic psd;
    netsnmp_memory_info *mem;
    long total_swap = 0;
    long free_swap  = 0;
    long size  = 0;

    /*
     * Retrieve the memory information from the underlying O/S...
     */
    if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) {
        snmp_log(LOG_ERR, "memory_hpux: pstat_getstatic failed!\n");
        return -1;
    }
    if (pstat_getdynamic(&psd, sizeof(psd), (size_t) 1, 0) == -1) {
        snmp_log(LOG_ERR, "memory_hpux: pstat_getdynamic failed!\n");
        return -1;
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_PHYSMEM, 1 );
    if (!mem) {
        snmp_log_perror("No Memory info entry");
    } else {
        if (!mem->descr)
            mem->descr = strdup( "Physical memory" );
        mem->units = pst.page_size;
        mem->size  = pst.physical_memory;
        mem->free  = psd.psd_free;
        mem->other = -1;
    }

    get_swapinfo(&total_swap, &free_swap, &size);
    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SWAP, 1 );
    if (!mem) {
        snmp_log_perror("No Swap info entry");
    } else {
        if (!mem->descr)
            mem->descr = strdup( "Swap space (total)" );
        mem->units = size;
        mem->size  = total_swap;
        mem->free  = free_swap;
        mem->other = -1;
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_STEXT, 1 );
    if (!mem) {
        snmp_log_perror("No Swap text entry");
    } else {
        if (!mem->descr)
            mem->descr = strdup( "Swapped text pages" );
        mem->units = pst.page_size;
        mem->size  = psd.psd_vmtxt;
        mem->free  = psd.psd_avmtxt;
        mem->other = -1;
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_RTEXT, 1 );
    if (!mem) {
        snmp_log_perror("No real text entry");
    } else {
        if (!mem->descr)
            mem->descr = strdup( "Real text pages" );
        mem->units = pst.page_size;
        mem->size  = psd.psd_rmtxt;
        mem->free  = psd.psd_armtxt;
	mem->other = -1;
    }

/*
    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_MISC, 1 );
    if (!mem) {
        snmp_log_perror("No Buffer, etc info entry");
    } else {
        mem->units = 1024;
        mem->size = -1;
        mem->free = (pst.page_size/1024)*psd.psd_free + swap.free_swap;
        mem->other = -1;
    }
 */
    return 0;
}
コード例 #6
0
    /*
     * Load the latest memory usage statistics
     */
int netsnmp_mem_arch_load( netsnmp_cache *cache, void *magic ) {

    netsnmp_memory_info *mem;
    long           pagesize;
    int            nswap;

    struct vmmeter vmem;
    struct vmtotal total;
    size_t         total_size  = sizeof(total);
    int            total_mib[] = { CTL_VM, VM_METER };

    u_long         phys_mem;
    u_long         user_mem;
    unsigned int   bufspace;
    unsigned int   maxbufspace;
    size_t         mem_size  = sizeof(phys_mem);
    size_t         buf_size  = sizeof(bufspace);
    int            phys_mem_mib[] = { CTL_HW, HW_PHYSMEM };
    int            user_mem_mib[] = { CTL_HW, HW_USERMEM };

    /*
     * Retrieve the memory information from the underlying O/S...
     */
    sysctl(total_mib,    2, &total,    &total_size,    NULL, 0);
    sysctl(phys_mem_mib, 2, &phys_mem, &mem_size,      NULL, 0);
    sysctl(user_mem_mib, 2, &user_mem, &mem_size,      NULL, 0);
    sysctlbyname("vfs.bufspace",    &bufspace,    &buf_size, NULL, 0);
    sysctlbyname("vfs.maxbufspace", &maxbufspace, &buf_size, NULL, 0);
    auto_nlist(SUM_SYMBOL,      (char *) &vmem,     sizeof(vmem));
#ifndef freebsd4
    pagesize = 1024;
#else
    pagesize = getpagesize();
#endif

    /*
     * ... and save this in a standard form.
     */
    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_PHYSMEM, 1 );
    if (!mem) {
        snmp_log_perror("No Physical Memory info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Physical memory");
        mem->units = pagesize;
        mem->size  = user_mem/pagesize;
        mem->free  = total.t_free;
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_USERMEM, 1 );
    if (!mem) {
        snmp_log_perror("No (user) Memory info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Real memory");
        mem->units = pagesize;
        mem->size  = total.t_rm;
        mem->free  = total.t_arm;
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_VIRTMEM, 1 );
    if (!mem) {
        snmp_log_perror("No Virtual Memory info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Virtual memory");
        mem->units = pagesize;
        mem->size  = total.t_vm;
        mem->free  = total.t_avm;
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SHARED, 1 );
    if (!mem) {
        snmp_log_perror("No Shared Memory info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Shared virtual memory");
        mem->units = pagesize;
        mem->size  = total.t_vmshr;
        mem->free  = total.t_avmshr;
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SHARED2, 1 );
    if (!mem) {
        snmp_log_perror("No Shared2 Memory info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Shared real memory");
        mem->units = pagesize;
        mem->size  = total.t_rmshr;
        mem->free  = total.t_armshr;
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_CACHED, 1 );
    if (!mem) {
        snmp_log_perror("No Cached Memory info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Cached memory");
        mem->units = vmem.v_page_size;
        mem->size  = vmem.v_cache_max;
        mem->free  = vmem.v_cache_max - vmem.v_cache_count;
    }

    nswap = swapmode(pagesize);
    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SWAP, 1 );
    if (!mem) {
        snmp_log_perror("No Swap info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup( (nswap>1) ? "Swap space (total)"
                                            : "Swap space");
        mem->units = pagesize;
        mem->size  = swapTotal;
        mem->free  = swapFree;
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_MBUF, 1 );
    if (!mem) {
        snmp_log_perror("No Buffer, etc info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Memory buffers");
        mem->units = 1024;
        mem->size  =  maxbufspace / 1024;
        mem->free  = (maxbufspace - bufspace)/1024;
    }

    return 0;
}
コード例 #7
0
ファイル: memory_irix.cpp プロジェクト: duniansampa/SigLog
    /*
     * Load the latest memory usage statistics
     */
int netsnmp_mem_arch_load( netsnmp_cache *cache, void *magic ) {

    netsnmp_memory_info *mem;
    struct rminfo meminfo; /* struct for getting memory info, see sys/sysmp.h */
    int pagesz, rminfosz;
    off_t swaptotal, swapfree;

    /*
     * Retrieve the memory information from the underlying O/S...
     */
    DEBUGMSGTL(("hardware/memory/memory_irix", "Start retrieving values from OS\n"));
    pagesz = getpagesize();
    DEBUGMSGTL(("hardware/memory/memory_irix", "Page size: %d\n", pagesz));
    rminfosz = (int)sysmp(MP_SASZ, MPSA_RMINFO);
    DEBUGMSGTL(("hardware/memory/memory_irix", "rminfo size: %d\n", rminfosz));
    if (sysmp(MP_SAGET, MPSA_RMINFO, &meminfo, rminfosz) < 0) {
	snmp_log(LOG_ERR, "memory_irix: sysmp failed!\n");
        return -1;
    }
    swapctl(SC_GETSWAPTOT, &swaptotal);
    swapctl(SC_GETFREESWAP, &swapfree);

    /*
     * ... and save this in a standard form.
     */
    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_PHYSMEM, 1 );
    if (!mem) {
        snmp_log_perror("No Physical Memory info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Physical memory");
        mem->units = pagesz;
        mem->size  = meminfo.physmem;
        mem->free  = meminfo.availrmem;
        mem->other = -1;
        DEBUGMSGTL(("hardware/memory/memory_irix", "Physical memory: size %u, free %u\n", mem->size, mem->free));
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_VIRTMEM, 1 );
    if (!mem) {
        snmp_log_perror("No Virtual Memory info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Virtual memory");
        mem->units = pagesz;     /* swaptotal is in blocks, so adjust below */
        mem->size  = meminfo.physmem + (swaptotal*512/pagesz);
        mem->free  = meminfo.availsmem;
        mem->other = -1;
        DEBUGMSGTL(("hardware/memory/memory_irix", "Virtual memory: size %u, free %u\n", mem->size, mem->free));
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SWAP, 1 );
    if (!mem) {
        snmp_log_perror("No Swap info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Swap space");
        mem->units = 1024;
        mem->size  = swaptotal/2; /* blocks to KB */
        mem->free  = swapfree/2;  /* blocks to KB */
        mem->other = -1;
        DEBUGMSGTL(("hardware/memory/memory_irix", "Swap: size %u, free %u\n", mem->size, mem->free));
    }

    return 0;
}
コード例 #8
0
ファイル: memory_linux.c プロジェクト: millken/zhuxianB30
/*
 * Load the latest memory usage statistics
 */
int netsnmp_mem_arch_load( netsnmp_cache *cache, void *magic ) {
    int          statfd;
    static char *buff  = NULL;
    static int   bsize = 0;
    static int   first = 1;
    ssize_t      bytes_read;
    char        *b;
    unsigned long memtotal = 0,  memfree = 0, memshared = 0,
                  buffers = 0,   cached = 0,
                  swaptotal = 0, swapfree = 0;

    netsnmp_memory_info *mem;

    /*
     * Retrieve the memory information from the underlying O/S...
     */
    if ((statfd = open(MEMINFO_FILE, O_RDONLY, 0)) == -1) {
        snmp_log_perror(MEMINFO_FILE);
        return -1;
    }
    if (bsize == 0) {
        bsize = MEMINFO_INIT_SIZE;
        buff = malloc(bsize+1);
        if (NULL == buff) {
            snmp_log(LOG_ERR, "malloc failed\n");
            return -1;
        }
    }
    while ((bytes_read = read(statfd, buff, bsize)) == bsize) {
        b = realloc(buff, bsize + MEMINFO_STEP_SIZE + 1);
        if (NULL == b) {
            snmp_log(LOG_ERR, "malloc failed\n");
            return -1;
        }
        buff = b;
        bsize += MEMINFO_STEP_SIZE;
        DEBUGMSGTL(("mem", "/proc/meminfo buffer increased to %d\n", bsize));
        close(statfd);
        statfd = open(MEMINFO_FILE, O_RDONLY, 0);
        if (statfd == -1) {
            snmp_log_perror(MEMINFO_FILE);
            return -1;
        }
    }
    close(statfd);
    if (bytes_read <= 0) {
        snmp_log_perror(MEMINFO_FILE);
    } else {
        buff[bytes_read] = '\0';
    }

    /*
     * ... parse this into a more useable form...
     */
    b = strstr(buff, "MemTotal: ");
    if (b)
        sscanf(b, "MemTotal: %lu", &memtotal);
    else {
        if (first)
            snmp_log(LOG_ERR, "No MemTotal line in /proc/meminfo\n");
    }
    b = strstr(buff, "MemFree: ");
    if (b)
        sscanf(b, "MemFree: %lu", &memfree);
    else {
        if (first)
            snmp_log(LOG_ERR, "No MemFree line in /proc/meminfo\n");
    }
    b = strstr(buff, "MemShared: ");
    if (b)
        sscanf(b, "MemShared: %lu", &memshared);
    else {
        if (first)
            if (0 == netsnmp_os_prematch("Linux","2.4"))
                snmp_log(LOG_ERR, "No MemShared line in /proc/meminfo\n");
    }
    b = strstr(buff, "Buffers: ");
    if (b)
        sscanf(b, "Buffers: %lu", &buffers);
    else {
        if (first)
            snmp_log(LOG_ERR, "No Buffers line in /proc/meminfo\n");
    }
    b = strstr(buff, "Cached: ");
    if (b)
        sscanf(b, "Cached: %lu", &cached);
    else {
        if (first)
            snmp_log(LOG_ERR, "No Cached line in /proc/meminfo\n");
    }
    b = strstr(buff, "SwapTotal: ");
    if (b)
        sscanf(b, "SwapTotal: %lu", &swaptotal);
    else {
        if (first)
            snmp_log(LOG_ERR, "No SwapTotal line in /proc/meminfo\n");
    }
    b = strstr(buff, "SwapFree: ");
    if (b)
        sscanf(b, "SwapFree: %lu", &swapfree);
    else {
        if (first)
            snmp_log(LOG_ERR, "No SwapFree line in /proc/meminfo\n");
    }
    first = 0;


    /*
     * ... and save this in a standard form.
     */
    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_PHYSMEM, 1 );
    if (!mem) {
        snmp_log_perror("No Physical Memory info entry");
    } else {
        if (!mem->descr)
            mem->descr = strdup("Physical memory");
        mem->units = 1024;
        mem->size  = memtotal;
        mem->free  = memfree;
        mem->other = -1;
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_VIRTMEM, 1 );
    if (!mem) {
        snmp_log_perror("No Virtual Memory info entry");
    } else {
        if (!mem->descr)
            mem->descr = strdup("Virtual memory");
        mem->units = 1024;
        mem->size  = memtotal+swaptotal;
        mem->free  = memfree +swapfree;
        mem->other = -1;
    }

    /* Shared memory is not reported by Linux 2.6 kernel */
    if (0 != netsnmp_os_prematch("Linux","2.6")) {
        mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SHARED, 1 );
        if (!mem) {
            snmp_log_perror("No Shared Memory info entry");
        } else {
            if (!mem->descr)
                mem->descr = strdup("Shared memory");
            mem->units = 1024;
            mem->size  = memshared;
            mem->free  = -1;
            mem->other = -1;
        }
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_CACHED, 1 );
    if (!mem) {
        snmp_log_perror("No Cached Memory info entry");
    } else {
        if (!mem->descr)
            mem->descr = strdup("Cached memory");
        mem->units = 1024;
        mem->size  = cached;
        mem->free  = 0;     /* Report cached size/used as equal */
        mem->other = -1;
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SWAP, 1 );
    if (!mem) {
        snmp_log_perror("No Swap info entry");
    } else {
        if (!mem->descr)
            mem->descr = strdup("Swap space");
        mem->units = 1024;
        mem->size  = swaptotal;
        mem->free  = swapfree;
        mem->other = -1;
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_MBUF, 1 );
    if (!mem) {
        snmp_log_perror("No Buffer, etc info entry");
    } else {
        if (!mem->descr)
            mem->descr = strdup("Memory buffers");
        mem->units = 1024;
        mem->size  = memtotal;  /* Traditionally we've always regarded
                                   all memory as potentially available
                                   for memory buffers. */
        mem->free  = memtotal - buffers;
        mem->other = -1;
    }

    return 0;
}
コード例 #9
0
ファイル: memory_aix.cpp プロジェクト: duniansampa/SigLog
    /*
     * Load the latest memory usage statistics
     */
int netsnmp_mem_arch_load( netsnmp_cache *cache, void *magic ) {

    netsnmp_memory_info    *mem;
    perfstat_memory_total_t pstat_mem;
    long                    pagesize;

    /*
     * Retrieve the memory information from the underlying O/S...
     */
    if (perfstat_memory_total((perfstat_id_t *)NULL, &pstat_mem,
                        sizeof(perfstat_memory_total_t), 1) < 1) {
        snmp_log(LOG_ERR, "memory_aix: perfstat_memory_total failed!\n");
        return -1;
    }
    pagesize = getpagesize();

    /*
     * ... and save this in a standard form.
     */
    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_PHYSMEM, 1 );
    if (!mem) {
        snmp_log_perror("No Physical Memory info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Physical memory");
        mem->units = pagesize;   /* or 4096 */
        mem->size = pstat_mem.real_total;
        mem->free = pstat_mem.real_free;
    }

    /* ??? Duplicates Physical Memory statistics? */
    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_USERMEM, 1 );
    if (!mem) {
        snmp_log_perror("No (user) Memory info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Real memory");
        mem->units = pagesize;   /* or 4096 */
        mem->size = pstat_mem.real_total;  /* ? less system memory ? */
        mem->free = pstat_mem.real_free;
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_VIRTMEM, 1 );
    if (!mem) {
        snmp_log_perror("No Virtual Memory info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Virtual memory");
        mem->units = pagesize;   /* or 4096 */
        mem->size = pstat_mem.virt_total;
        mem->free = pstat_mem.real_free + pstat_mem.pgsp_free;
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SWAP, 1 );
    if (!mem) {
        snmp_log_perror("No Swap info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Swap space");
        mem->units = pagesize;   /* or 4096 */
        mem->size = pstat_mem.pgsp_total;
        mem->free = pstat_mem.pgsp_free;
        mem->other = pstat_mem.pgsp_rsvd;
    }

    return 0;
}
コード例 #10
0
ファイル: memory.c プロジェクト: ClausKlein/net-snmp
int
handle_memory(netsnmp_mib_handler *handler,
                netsnmp_handler_registration *reginfo,
                netsnmp_agent_request_info *reqinfo,
                netsnmp_request_info *requests)
{
    netsnmp_memory_info *mem_info;
    int val;
    char buf[1024];

    /*
     * We just need to handle valid GET requests, as invalid instances
     *   are rejected automatically, and (valid) GETNEXT requests are
     *   converted into the appropriate GET request.
     *
     * We also only ever receive one request at a time.
     */
    switch (reqinfo->mode) {
    case MODE_GET:
        netsnmp_memory_load();
        switch (requests->requestvb->name[ reginfo->rootoid_len - 2 ]) {
        case MEMORY_INDEX:
            val = 0;
            break;
        case MEMORY_ERRNAME:
            sprintf(buf, "swap");
            snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                                     (u_char *)buf, strlen(buf));
            return SNMP_ERR_NOERROR;
        case MEMORY_SWAP_TOTAL:
            mem_info = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SWAP, 0 );
            if (!mem_info)
               goto NOSUCH;
            val  =  mem_info->size;     /* swaptotal */
            val *= (mem_info->units/1024);
            break;
        case MEMORY_SWAP_AVAIL:
            mem_info = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SWAP, 0 );
            if (!mem_info)
               goto NOSUCH;
            val  =  mem_info->free;     /* swapfree */
            val *= (mem_info->units/1024);
            break;
        case MEMORY_REAL_TOTAL:
            mem_info = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_PHYSMEM, 0 );
            if (!mem_info)
               goto NOSUCH;
            val  =  mem_info->size;     /* memtotal */
            val *= (mem_info->units/1024);
            break;
        case MEMORY_REAL_AVAIL:
            mem_info = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_PHYSMEM, 0 );
            if (!mem_info)
               goto NOSUCH;
            val  =  mem_info->free;     /* memfree */
            val *= (mem_info->units/1024);
            break;
        case MEMORY_STXT_TOTAL:
            mem_info = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_STEXT, 0 );
            if (!mem_info)
               goto NOSUCH;
            val  =  mem_info->size;
            val *= (mem_info->units/1024);
            break;
        case MEMORY_STXT_AVAIL:    /* Deprecated */
        case MEMORY_STXT_USED:
            /*
             *   The original MIB description of memAvailSwapTXT
             * was inconsistent with that implied by the name.
             *   Retain the actual behaviour for the (sole)
             * implementation of this object, but deprecate it in
             * favour of a more consistently named replacement object.
             */
            mem_info = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_STEXT, 0 );
            if (!mem_info)
               goto NOSUCH;
            val  = (mem_info->size - mem_info->free);
            val *= (mem_info->units/1024);
            break;
        case MEMORY_RTXT_TOTAL:
            mem_info = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_RTEXT, 0 );
            if (!mem_info)
               goto NOSUCH;
            val  =  mem_info->size;
            val *= (mem_info->units/1024);
            break;
        case MEMORY_RTXT_AVAIL:    /* Deprecated */
        case MEMORY_RTXT_USED:
            /*
             *   The original MIB description of memAvailRealTXT
             * was inconsistent with that implied by the name.
             *   Retain the actual behaviour for the (sole)
             * implementation of this object, but deprecate it in
             * favour of a more consistently named replacement object.
             */
            mem_info = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_RTEXT, 0 );
            if (!mem_info)
               goto NOSUCH;
            val  = (mem_info->size - mem_info->free);
            val *= (mem_info->units/1024);
            break;
        case MEMORY_FREE:
            mem_info = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_VIRTMEM, 0 );
            if (!mem_info)
               goto NOSUCH;
            val  =  mem_info->free;     /* memfree + swapfree */
            val *= (mem_info->units/1024);
            break;
        case MEMORY_SWAP_MIN:
            val = minimum_swap;
            break;
        case MEMORY_SHARED:
            mem_info = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SHARED, 0 );
            if (!mem_info)
               goto NOSUCH;
            val  =  mem_info->size;     /* memshared */
            val *= (mem_info->units/1024);
            break;
        case MEMORY_BUFFER:
            mem_info = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_MBUF, 0 );
            if (!mem_info || mem_info->size == -1)
               goto NOSUCH;
            val  = (mem_info->size - mem_info->free);      /* buffers */
            val *= (mem_info->units/1024);
            break;
        case MEMORY_CACHED:
            mem_info = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_CACHED, 0 );
            if (!mem_info || mem_info->size== -1)
               goto NOSUCH;
            val  = (mem_info->size - mem_info->free);      /* cached */
            val *= (mem_info->units/1024);
            break;
        case MEMORY_SWAP_ERROR:
            mem_info = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SWAP, 0 );
            if (!mem_info)
               goto NOSUCH;
            val = ((mem_info->units / 1024) * mem_info->free > minimum_swap) ? 0 : 1;
            break;
        case MEMORY_SWAP_ERRMSG:
            mem_info = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SWAP, 0 );
            if (!mem_info)
               goto NOSUCH;
            if ((mem_info->units / 1024) * mem_info->free > minimum_swap)
                buf[0] = 0;
            else
                sprintf(buf, "Running out of swap space (%ld)", mem_info->free);
            snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                                     (u_char *)buf, strlen(buf));
            return SNMP_ERR_NOERROR;
        default:
            snmp_log(LOG_ERR,
                     "unknown object (%" NETSNMP_PRIo "u) in handle_memory\n",
                     requests->requestvb->name[ reginfo->rootoid_len - 2 ]);
NOSUCH:
            netsnmp_set_request_error( reqinfo, requests, SNMP_NOSUCHOBJECT );
            return SNMP_ERR_NOERROR;
        }
        /*
         * All non-integer objects (and errors) have already been
         * processed.  So return the integer value.
         */
        snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
                                 (u_char *)&val, sizeof(val));
        break;

    default:
        /*
         * we should never get here, so this is a really bad error 
         */
        snmp_log(LOG_ERR, "unknown mode (%d) in handle_memory\n",
                 reqinfo->mode);
        return SNMP_ERR_GENERR;
    }

    return SNMP_ERR_NOERROR;
}
コード例 #11
0
ファイル: hrh_storage.c プロジェクト: michalklempa/net-snmp
void *
header_hrstoreEntry(struct variable *vp,
                    oid * name,
                    size_t * length,
                    int exact,
                    size_t * var_len, WriteMethod ** write_method)
{
#define HRSTORE_ENTRY_NAME_LENGTH	11
    oid             newname[MAX_OID_LEN];
    int             storage_idx, LowIndex = -1;
    int             result;
    int                  idx = -1;
    netsnmp_memory_info *mem  = NULL;

    DEBUGMSGTL(("host/hr_storage", "var_hrstoreEntry: request "));
    DEBUGMSGOID(("host/hr_storage", name, *length));
    DEBUGMSG(("host/hr_storage", " exact=%d\n", exact));

    memcpy((char *) newname, (char *) vp->name,
           (int) vp->namelen * sizeof(oid));
    result = snmp_oid_compare(name, *length, vp->name, vp->namelen);

    DEBUGMSGTL(("host/hr_storage", "var_hrstoreEntry: compare "));
    DEBUGMSGOID(("host/hr_storage", vp->name, vp->namelen));
    DEBUGMSG(("host/hr_storage", " => %d\n", result));


    if (result < 0 ||
        *length <= HRSTORE_ENTRY_NAME_LENGTH ) {
       /*
        * Requested OID too early or too short to refer
        *   to a valid row (for the current column object).
        * GET requests should fail, GETNEXT requests
        *   should use the first row.
        */
        if ( exact )
            return NULL;
        netsnmp_memory_load();
        mem = netsnmp_memory_get_first( 0 );
    }
    else {
        /*
         * Otherwise, retrieve the requested
         *  (or following) row as appropriate.
         */
        if ( exact && *length > HRSTORE_ENTRY_NAME_LENGTH+1 )
            return NULL;   /* Too long for a valid instance */
        idx = name[ HRSTORE_ENTRY_NAME_LENGTH ];
        if ( idx < NETSNMP_MEM_TYPE_MAX ) {
            netsnmp_memory_load();
            mem = ( exact ? netsnmp_memory_get_byIdx( idx, 0 ) :
                       netsnmp_memory_get_next_byIdx( idx, 0 ));
        }
    }

    /*
     * If this matched a memory-based entry, then
     *    update the OID parameter(s) for GETNEXT requests.
     */
    if ( mem ) {
        if ( !exact ) {
            newname[ HRSTORE_ENTRY_NAME_LENGTH ] = mem->idx;
            memcpy((char *) name, (char *) newname,
                   ((int) vp->namelen + 1) * sizeof(oid));
            *length = vp->namelen + 1;
        }
    }
    /*
     * If this didn't match a memory-based entry,
     *   then consider the disk-based storage.
     */
    else {
        Init_HR_Store();
        for (;;) {
            storage_idx = Get_Next_HR_Store();
            DEBUGMSG(("host/hr_storage", "(index %d ....", storage_idx));
            if (storage_idx == -1)
                break;
            newname[HRSTORE_ENTRY_NAME_LENGTH] = storage_idx;
            DEBUGMSGOID(("host/hr_storage", newname, *length));
            DEBUGMSG(("host/hr_storage", "\n"));
            result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);
            if (exact && (result == 0)) {
                LowIndex = storage_idx;
                /*
                 * Save storage status information 
                 */
                break;
            }
            if ((!exact && (result < 0)) &&
                (LowIndex == -1 || storage_idx < LowIndex)) {
                LowIndex = storage_idx;
                /*
                 * Save storage status information 
                 */
#ifdef HRSTORE_MONOTONICALLY_INCREASING
                break;
#endif
            }
        }
        if ( LowIndex != -1 ) {
            if ( !exact ) {
                newname[ HRSTORE_ENTRY_NAME_LENGTH ] = LowIndex;
                memcpy((char *) name, (char *) newname,
                       ((int) vp->namelen + 1) * sizeof(oid));
                *length = vp->namelen + 1;
            }
            mem = (netsnmp_memory_info*)0xffffffff;   /* To indicate 'success' */
        }
    }

    *write_method = (WriteMethod*)0;
    *var_len = sizeof(long);    /* default to 'long' results */

    /*
     *  ... and return the appropriate row
     */
    DEBUGMSGTL(("host/hr_storage", "var_hrstoreEntry: process "));
    DEBUGMSGOID(("host/hr_storage", name, *length));
    DEBUGMSG(("host/hr_storage", " (%p)\n", mem));
    return (void*)mem;
}
コード例 #12
0
ファイル: memory_netbsd.c プロジェクト: ColdStart/SNMPD
    /*
     * Load the latest memory usage statistics
     */
int netsnmp_mem_arch_load( netsnmp_cache *cache, void *magic ) {

    netsnmp_memory_info *mem;
    long           pagesize;

    struct uvmexp  uvmexp;
    size_t         uvmexp_size  = sizeof(uvmexp);

    struct vmtotal total;
    size_t         total_size  = sizeof(total);

    quad_t          phys_mem;
    quad_t          user_mem;
    size_t          mem_size  = sizeof(phys_mem);
#if defined(openbsd)
    int             phys_mem_mib[] = { CTL_HW, HW_PHYSMEM64 };
    int             user_mem_mib[] = { CTL_HW, HW_USERMEM64 };
    int             uvmexp_mib[] = { CTL_VM, VM_UVMEXP };
    int             total_mib[] = { CTL_VM, VM_METER };
#else
    unsigned int    bufspace;
    unsigned int    maxbufspace;
    size_t          buf_size  = sizeof(bufspace);
#endif

    /*
     * Retrieve the memory information from the underlying O/S...
     */
#if defined(openbsd)
    sysctl(uvmexp_mib,   2, &uvmexp,   &uvmexp_size,   NULL, 0);
    sysctl(total_mib,    2, &total,    &total_size,    NULL, 0);
    sysctl(phys_mem_mib, 2, &phys_mem, &mem_size,      NULL, 0);
    sysctl(user_mem_mib, 2, &user_mem, &mem_size,      NULL, 0);
#else
    if (sysctlbyname("vm.uvmexp", &uvmexp, &uvmexp_size, NULL, 0) == -1) {
        snmp_log(LOG_ERR, "sysctl vm.uvmexp failed (errno %d)\n", errno);
        return -1;
    }
    if (sysctlbyname("vm.vmmeter", &total,  &total_size, NULL, 0) == -1) {
        snmp_log(LOG_ERR, "sysctl vm.vmmeter failed (errno %d)\n", errno);
        return -1;
    }
    if (sysctlbyname("hw.physmem64", &phys_mem, &mem_size, NULL, 0) == -1) {
        snmp_log(LOG_ERR, "sysctl hw.physmem64 failed (errno %d)\n", errno);
        return -1;
    }
    if (sysctlbyname("hw.usermem64", &user_mem, &mem_size, NULL, 0) == -1) {
        snmp_log(LOG_ERR, "sysctl hw.usermem64 failed (errno %d)\n", errno);
        return -1;
    }
    if (sysctlbyname("vm.bufmem", &bufspace, &buf_size, NULL, 0) == -1) {
        snmp_log(LOG_ERR, "sysctl vm.bufmem failed (errno %d)\n", errno);
        return -1;
    }
    if (sysctlbyname("vm.bufmem_hiwater", &maxbufspace, &buf_size, NULL, 0) == -1) {
        snmp_log(LOG_ERR, "sysctl vm.bufmem_hiwater failed (errno %d)\n", errno);
        return -1;
    }

#endif
    pagesize = sysconf(_SC_PAGESIZE);

    /*
     * ... and save this in a standard form.
     */
    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_PHYSMEM, 1 );
    if (!mem) {
        snmp_log_perror("No Physical Memory info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Physical memory");
        mem->units = pagesize;
        mem->size  = phys_mem/pagesize;
        mem->free  = total.t_free;
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_USERMEM, 1 );
    if (!mem) {
        snmp_log_perror("No (user) Memory info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Real memory");
        mem->units = pagesize;
        mem->size  = user_mem/pagesize;
        mem->free  = uvmexp.free;
    }

#if 1
    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_VIRTMEM, 1 );
    if (!mem) {
        snmp_log_perror("No Virtual Memory info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Virtual memory");
        mem->units = pagesize;
        mem->size  = total.t_vm;
        mem->free  = total.t_avm;
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SHARED, 1 );
    if (!mem) {
        snmp_log_perror("No Shared Memory info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Shared virtual memory");
        mem->units = pagesize;
        mem->size  = total.t_vmshr;
        mem->free  = total.t_avmshr;
    }

    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SHARED2, 1 );
    if (!mem) {
        snmp_log_perror("No Shared2 Memory info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Shared real memory");
        mem->units = pagesize;
        mem->size  = total.t_rmshr;
        mem->free  = total.t_armshr;
    }
#endif

#ifdef SWAP_NSWAP
    swapinfo(pagesize);
#endif
    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SWAP, 1 );
    if (!mem) {
        snmp_log_perror("No Swap info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Swap space");
        mem->units = pagesize;
        mem->size  = uvmexp.swpages;
        mem->free  = uvmexp.swpages - uvmexp.swpginuse;
        mem->other = -1;
    }

#if defined(__NetBSD__)
    mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_MBUF, 1 );
    if (!mem) {
        snmp_log_perror("No Buffer, etc info entry");
    } else {
        if (!mem->descr)
             mem->descr = strdup("Memory buffers");
        mem->units = 1024;
        mem->size  =  maxbufspace            /1024;
        mem->size  = (maxbufspace - bufspace)/1024;
    }
#endif

    return 0;
}