blob: 9b0bc507a36c7f9dd14bf655a894c2b2250c877e (
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
|
#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/hardware/memory.h>
#include <unistd.h>
# ifdef HAVE_SYS_SWAP_H
# include <sys/swap.h>
# endif
# ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
# endif
/*
* Retained from UCD implementation
*/
long
getTotalSwap(void)
{
long total_swp_sectors = -1;
size_t max_elements = MAXSWAPDEVS;
swapsize_t swap_dblks[MAXSWAPDEVS];
swapstat_t swap_status;
int swap_sizes;
if ((swap_sizes =
getswapstat(max_elements, swap_dblks, &swap_status) >= 0))
total_swp_sectors =
swap_dblks[0].sws_size * swap_dblks[0].sws_total;
return (total_swp_sectors);
}
long
getFreeSwap(void)
{
long free_swp_sectors = -1;
size_t max_elements = MAXSWAPDEVS;
swapsize_t swap_dblks[MAXSWAPDEVS];
swapstat_t swap_status;
int i, swap_sizes;
if ((swap_sizes =
getswapstat(max_elements, swap_dblks, &swap_status) >= 0)) {
for (i = 0; i < swap_sizes; i++)
free_swp_sectors +=
swap_dblks[0].sws_size * swap_dblks[i].sws_avail;
}
return (free_swp_sectors);
}
/*
* return is in kilobytes
*/
long
getTotalFree(void)
{
long free_swap = S2KB(getFreeSwap());
long free_mem = P2KB(sysconf(_SC_FREEMEM));
if (free_swap < 0)
return (free_swap);
if (free_mem < 0)
return (free_mem);
free_mem += free_swap;
return (free_mem);
}
/*
* Load the latest memory usage statistics
*/
int netsnmp_mem_arch_load( netsnmp_cache *cache, void *magic ) {
netsnmp_memory_info *mem;
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 = P2KB(1)*1024;
mem->size = sysconf(_SC_PHYSMEM);
mem->free = sysconf(_SC_FREEMEM);
}
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 = S2KB(1)*1024;
mem->size = getTotalSwap();
mem->free = getFreeSwap();
}
/*
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 = getTotalFree();
mem->other = -1;
}
*/
return 0;
}
|