Debian Codegraph @

agent/mibgroup/hardware/memory/memory_linux.c

raw
1 #include <net-snmp/net-snmp-config.h>
2 #include <net-snmp/net-snmp-includes.h>
3 #include <net-snmp/agent/net-snmp-agent-includes.h>
4 #include <net-snmp/agent/hardware/memory.h>
5
6 #include <unistd.h>
7 #include <fcntl.h>
8
9 /*
10 * Try to use an initial size that will cover default cases. We aren't talking
11 * about huge files, so why fiddle about with reallocs?
12 */
13 #define MEMINFO_INIT_SIZE 4096
14 #define MEMINFO_STEP_SIZE 4096
15 #define MEMINFO_FILE "/proc/meminfo"
16
17 /*
18 * Load the latest memory usage statistics
19 */
20 int netsnmp_mem_arch_load( netsnmp_cache *cache, void *magic ) {
21 int statfd;
22 static char *buff = NULL;
23 static int bsize = 0;
24 static int first = 1;
25 ssize_t bytes_read;
26 char *b;
27 int have_memavail = 0;
28 unsigned long memtotal = 0, memavail = 0, memfree = 0, memshared = 0,
29 buffers = 0, cached = 0, sreclaimable = 0,
30 swaptotal = 0, swapfree = 0;
31
32 netsnmp_memory_info *mem;
33
34 /*
35 * Retrieve the memory information from the underlying O/S...
36 */
37 if ((statfd = open(MEMINFO_FILE, O_RDONLY, 0)) == -1) {
38 snmp_log_perror(MEMINFO_FILE);
39 return -1;
40 }
41 if (bsize == 0) {
42 bsize = MEMINFO_INIT_SIZE;
43 buff = (char*)malloc(bsize+1);
44 if (NULL == buff) {
45 snmp_log(LOG_ERR, "malloc failed\n");
46 close(statfd);
47 return -1;
48 }
49 }
50 while ((bytes_read = read(statfd, buff, bsize)) == bsize) {
51 b = (char*)realloc(buff, bsize + MEMINFO_STEP_SIZE + 1);
52 if (NULL == b) {
53 snmp_log(LOG_ERR, "malloc failed\n");
54 close(statfd);
55 return -1;
56 }
57 buff = b;
58 bsize += MEMINFO_STEP_SIZE;
59 DEBUGMSGTL(("mem", "/proc/meminfo buffer increased to %d\n", bsize));
60 close(statfd);
61 statfd = open(MEMINFO_FILE, O_RDONLY, 0);
62 if (statfd == -1) {
63 snmp_log_perror(MEMINFO_FILE);
64 return -1;
65 }
66 }
67 close(statfd);
68 if (bytes_read <= 0) {
69 snmp_log_perror(MEMINFO_FILE);
70 buff[0] = '\0';
71 } else {
72 buff[bytes_read] = '\0';
73 }
74
75 /*
76 * ... parse this into a more useable form...
77 */
78 b = strstr(buff, "MemTotal: ");
79 if (b)
80 sscanf(b, "MemTotal: %lu", &memtotal);
81 else {
82 if (first)
83 snmp_log(LOG_ERR, "No MemTotal line in /proc/meminfo\n");
84 }
85 b = strstr(buff, "MemAvailable: ");
86 if (b) {
87 have_memavail = 1;
88 sscanf(b, "MemAvailable: %lu", &memavail);
89 }
90 b = strstr(buff, "MemFree: ");
91 if (b)
92 sscanf(b, "MemFree: %lu", &memfree);
93 else {
94 if (first)
95 snmp_log(LOG_ERR, "No MemFree line in /proc/meminfo\n");
96 }
97 if (0 == netsnmp_os_prematch("Linux","2.4")) {
98 b = strstr(buff, "MemShared: ");
99 if (b)
100 sscanf(b, "MemShared: %lu", &memshared);
101 else if (first)
102 snmp_log(LOG_ERR, "No MemShared line in /proc/meminfo\n");
103 }
104 else {
105 b = strstr(buff, "Shmem: ");
106 if (b)
107 sscanf(b, "Shmem: %lu", &memshared);
108 else if (first)
109 snmp_log(LOG_ERR, "No Shmem line in /proc/meminfo\n");
110 }
111 b = strstr(buff, "Buffers: ");
112 if (b)
113 sscanf(b, "Buffers: %lu", &buffers);
114 else {
115 if (first)
116 snmp_log(LOG_ERR, "No Buffers line in /proc/meminfo\n");
117 }
118 b = strstr(buff, "Cached: ");
119 if (b)
120 sscanf(b, "Cached: %lu", &cached);
121 else {
122 if (first)
123 snmp_log(LOG_ERR, "No Cached line in /proc/meminfo\n");
124 }
125 b = strstr(buff, "SwapTotal: ");
126 if (b)
127 sscanf(b, "SwapTotal: %lu", &swaptotal);
128 else {
129 if (first)
130 snmp_log(LOG_ERR, "No SwapTotal line in /proc/meminfo\n");
131 }
132 b = strstr(buff, "SwapFree: ");
133 if (b)
134 sscanf(b, "SwapFree: %lu", &swapfree);
135 else {
136 if (first)
137 snmp_log(LOG_ERR, "No SwapFree line in /proc/meminfo\n");
138 }
139 b = strstr(buff, "SReclaimable: ");
140 if (b)
141 sscanf(b, "SReclaimable: %lu", &sreclaimable);
142 first = 0;
143
144
145 /*
146 * ... and save this in a standard form.
147 */
148 mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_PHYSMEM, 1 );
149 if (!mem) {
150 snmp_log_perror("No Physical Memory info entry");
151 } else {
152 if (!mem->descr)
153 mem->descr = strdup("Physical memory");
154 mem->units = 1024;
155 mem->size = memtotal;
156 mem->free = memfree;
157 mem->other = -1;
158 }
159
160 if (have_memavail) {
161 mem = netsnmp_memory_get_byIdx(NETSNMP_MEM_TYPE_AVAILMEM, 1);
162 if (mem) {
163 if (!mem->descr)
164 mem->descr = strdup("Available memory");
165 mem->units = 1024;
166 mem->size = memavail;
167 mem->free = memavail;
168 mem->other = -1;
169 }
170 }
171
172 mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_VIRTMEM, 1 );
173 if (!mem) {
174 snmp_log_perror("No Virtual Memory info entry");
175 } else {
176 if (!mem->descr)
177 mem->descr = strdup("Virtual memory");
178 mem->units = 1024;
179 mem->size = memtotal+swaptotal;
180 mem->free = memfree +swapfree;
181 mem->other = -1;
182 }
183
184 mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SHARED, 1 );
185 if (!mem) {
186 snmp_log_perror("No Shared Memory info entry");
187 } else {
188 if (!mem->descr)
189 mem->descr = strdup("Shared memory");
190 mem->units = 1024;
191 mem->size = memshared;
192 mem->free = 0; /* All in use */
193 mem->other = -1;
194 }
195
196 mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_CACHED, 1 );
197 if (!mem) {
198 snmp_log_perror("No Cached Memory info entry");
199 } else {
200 if (!mem->descr)
201 mem->descr = strdup("Cached memory");
202 mem->units = 1024;
203 mem->size = cached + sreclaimable;
204 mem->free = 0; /* Report cached size/used as equal */
205 mem->other = -1;
206 }
207
208 mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SWAP, 1 );
209 if (!mem) {
210 snmp_log_perror("No Swap info entry");
211 } else {
212 if (!mem->descr)
213 mem->descr = strdup("Swap space");
214 mem->units = 1024;
215 mem->size = swaptotal;
216 mem->free = swapfree;
217 mem->other = -1;
218 }
219
220 mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_MBUF, 1 );
221 if (!mem) {
222 snmp_log_perror("No Buffer, etc info entry");
223 } else {
224 if (!mem->descr)
225 mem->descr = strdup("Memory buffers");
226 mem->units = 1024;
227 mem->size = memtotal; /* Traditionally we've always regarded
228 all memory as potentially available
229 for memory buffers. */
230 mem->free = memtotal - buffers;
231 mem->other = -1;
232 }
233
234 return 0;
235 }