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
|
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <net-snmp/agent/auto_nlist.h>
#include <net-snmp/agent/hardware/memory.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/fcntl.h>
#include <sys/sysctl.h>
#include <sys/vmmeter.h>
#include <kvm.h>
#if HAVE_SYS_VMPARAM_H
#include <sys/vmparam.h>
#else
#include <vm/vm_param.h>
#endif
#define SUM_SYMBOL "cnt"
#define BUFSPACE_SYMBOL "bufspace"
quad_t swapTotal;
quad_t swapUsed;
quad_t swapFree;
int swapmode(long);
/*
* 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 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 cache_count;
unsigned int cache_max;
unsigned int bufspace;
unsigned int maxbufspace;
unsigned int inact_count;
size_t mem_size = sizeof(phys_mem);
size_t cache_size = sizeof(cache_count);
size_t buf_size = sizeof(bufspace);
size_t inact_size = sizeof(inact_count);
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("vm.stats.vm.v_cache_count", &cache_count, &cache_size, NULL, 0);
sysctlbyname("vm.stats.vm.v_cache_max", &cache_max, &cache_size, NULL, 0);
sysctlbyname("vm.stats.vm.v_inactive_count", &inact_count, &inact_size, NULL, 0);
sysctlbyname("vfs.bufspace", &bufspace, &buf_size, NULL, 0);
sysctlbyname("vfs.maxbufspace", &maxbufspace, &buf_size, NULL, 0);
#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 = 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 = 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 = pagesize;
mem->size = cache_max + inact_count;
mem->free = cache_max - 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;
}
/*
* Retained from UCD implementation
*/
#ifndef freebsd4
/*
* Executes swapinfo and parses last line
* This is just way too ugly ;)
*/
int
swapmode(long pagesize)
{
struct extensible ext;
int fd;
FILE *file;
strcpy(ext.command, "/usr/sbin/swapinfo -k");
if ((fd = get_exec_output(&ext)) != -1) {
file = fdopen(fd, "r");
while (fgets(ext.output, sizeof(ext.output), file) != NULL);
fclose(file);
wait_on_exec(&ext);
sscanf(ext.output, "%*s%*d%qd%qd", &swapUsed, &swapFree);
swapTotal = swapUsed + swapFree;
}
return 1;
}
#else
/*
* swapmode is based on a program called swapinfo written
* by Kevin Lahey <kml@rokkaku.atl.ga.us>.
*/
#include <sys/conf.h>
kvm_t *kd = NULL;
int
swapmode(long pagesize)
{
int i, n;
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;
}
#endif
|