Debian Codegraph @

agent/mibgroup/ucd-snmp/memory_hpux.c

raw
1 #include <net-snmp/net-snmp-config.h>
2 #ifdef HAVE_STRING_H
3 #include <string.h>
4 #endif
5 #ifdef HAVE_STDLIB_H
6 #include <stdlib.h>
7 #endif
8
9 #include <sys/types.h>
10
11 #include <net-snmp/net-snmp-includes.h>
12 #include <net-snmp/agent/net-snmp-agent-includes.h>
13 #include <net-snmp/agent/auto_nlist.h>
14 #include "memory.h" /* the module-specific header */
15 #include "memory_hpux.h" /* the module-specific header */
16
17 #include <sys/pstat.h>
18
19 #define MAXSTRSIZE 80
20 #define DEFAULTMINIMUMSWAP 16000 /* kilobytes */
21
22 int minimumswap;
23 static char errmsg[1024];
24
25 static FindVarMethod var_extensible_mem;
26 static long getFreeSwap(void);
27 static long getTotalFree(void);
28 static long getTotalSwap(void);
29
30 struct swapinfo {
31 unsigned long total_swap; /* in kilobytes */
32 unsigned long free_swap; /* in kilobytes */
33 };
34
35 void
36 memory_parse_config(const char *token, char *cptr)
37 {
38 minimumswap = atoi(cptr);
39 }
40
41 void
42 memory_free_config(void)
43 {
44 minimumswap = DEFAULTMINIMUMSWAP;
45 }
46
47 void
48 init_memory_hpux(void)
49 {
50
51 struct variable2 extensible_mem_variables[] = {
52 {MIBINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
53 var_extensible_mem, 1, {MIBINDEX}},
54 {ERRORNAME, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
55 var_extensible_mem, 1, {ERRORNAME}},
56 {MEMTOTALSWAP, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
57 var_extensible_mem, 1, {MEMTOTALSWAP}},
58 {MEMAVAILSWAP, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
59 var_extensible_mem, 1, {MEMAVAILSWAP}},
60 {MEMTOTALREAL, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
61 var_extensible_mem, 1, {MEMTOTALREAL}},
62 {MEMAVAILREAL, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
63 var_extensible_mem, 1, {MEMAVAILREAL}},
64 {MEMTOTALSWAPTXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
65 var_extensible_mem, 1, {MEMTOTALSWAPTXT}},
66 {MEMUSEDSWAPTXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
67 var_extensible_mem, 1, {MEMUSEDSWAPTXT}},
68 {MEMTOTALREALTXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
69 var_extensible_mem, 1, {MEMTOTALREALTXT}},
70 {MEMUSEDREALTXT, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
71 var_extensible_mem, 1, {MEMUSEDREALTXT}},
72 {MEMTOTALFREE, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
73 var_extensible_mem, 1, {MEMTOTALFREE}},
74 {MEMSWAPMINIMUM, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
75 var_extensible_mem, 1, {MEMSWAPMINIMUM}},
76 {MEMSHARED, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
77 var_extensible_mem, 1, {MEMSHARED}},
78 {MEMBUFFER, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
79 var_extensible_mem, 1, {MEMBUFFER}},
80 {MEMCACHED, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
81 var_extensible_mem, 1, {MEMCACHED}},
82 {ERRORFLAG, ASN_INTEGER, NETSNMP_OLDAPI_RONLY,
83 var_extensible_mem, 1, {ERRORFLAG}},
84 {ERRORMSG, ASN_OCTET_STR, NETSNMP_OLDAPI_RONLY,
85 var_extensible_mem, 1, {ERRORMSG}}
86 };
87
88 /*
89 * Define the OID pointer to the top of the mib tree that we're
90 * registering underneath
91 */
92 oid mem_variables_oid[] = { NETSNMP_UCDAVIS_MIB, NETSNMP_MEMMIBNUM };
93
94 /*
95 * register ourselves with the agent to handle our mib tree
96 */
97 REGISTER_MIB("ucd-snmp/memory", extensible_mem_variables, variable2,
98 mem_variables_oid);
99
100 snmpd_register_config_handler("swap", memory_parse_config,
101 memory_free_config, "min-avail");
102
103 } /* end init_hpux */
104
105 static int
106 get_swapinfo(struct swapinfo *swap)
107 {
108
109 struct pst_swapinfo pss;
110 int i = 0;
111
112 while (pstat_getswap(&pss, sizeof(pss), (size_t) 1, i) != -1) {
113 if (pss.pss_idx == (unsigned) i) {
114 swap->total_swap += pss.pss_nblksenabled;
115 swap->free_swap += 4 * pss.pss_nfpgs; /* nfpgs is in 4-byte blocks - who knows why? */
116 i++;
117 } else
118 return;
119 }
120 } /* end get_swapinfo */
121
122 static u_char *
123 var_extensible_mem(struct variable *vp,
124 oid * name,
125 size_t * length,
126 int exact,
127 size_t * var_len, WriteMethod ** write_method)
128 {
129
130 struct swapinfo swap;
131 struct pst_static pst;
132 struct pst_dynamic psd;
133 static long long_ret;
134
135 /*
136 * Initialize the return value to 0
137 */
138 long_ret = 0;
139 swap.total_swap = 0;
140 swap.free_swap = 0;
141
142 if (header_generic(vp, name, length, exact, var_len, write_method))
143 return (NULL);
144
145 switch (vp->magic) {
146 case MIBINDEX:
147 long_ret = 0;
148 return ((u_char *) (&long_ret));
149 case ERRORNAME: /* dummy name */
150 sprintf(errmsg, "swap");
151 *var_len = strlen(errmsg);
152 return ((u_char *) (errmsg));
153 case MEMTOTALSWAP:
154 get_swapinfo(&swap);
155 long_ret = swap.total_swap;
156 return ((u_char *) (&long_ret));
157 case MEMAVAILSWAP:
158 get_swapinfo(&swap);
159 long_ret = swap.free_swap;
160 return ((u_char *) (&long_ret));
161 case MEMSWAPMINIMUM:
162 long_ret = minimumswap;
163 return ((u_char *) (&long_ret));
164 case MEMTOTALREAL:
165 /*
166 * Retrieve the static memory statistics
167 */
168 if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) {
169 snmp_log(LOG_ERR, "memory_hpux: pstat_getstatic failed!\n");
170 return (NULL);
171 }
172 long_ret = pst.page_size / 1024 * pst.physical_memory;
173 return ((u_char *) (&long_ret));
174 case MEMAVAILREAL:
175 /*
176 * Retrieve the static memory statistics
177 */
178 if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) {
179 snmp_log(LOG_ERR, "memory_hpux: pstat_getstatic failed!\n");
180 return (NULL);
181 }
182 /*
183 * Retrieve the dynamic memory statistics
184 */
185 if (pstat_getdynamic(&psd, sizeof(psd), (size_t) 1, 0) == -1) {
186 snmp_log(LOG_ERR, "memory_hpux: pstat_getdynamic failed!\n");
187 return (NULL);
188 }
189 long_ret = pst.page_size / 1024 * psd.psd_free;
190 return ((u_char *) (&long_ret));
191 case MEMTOTALSWAPTXT:
192 /*
193 * Retrieve the static memory statistics
194 */
195 if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) {
196 snmp_log(LOG_ERR, "memory_hpux: pstat_getstatic failed!\n");
197 return (NULL);
198 }
199 /*
200 * Retrieve the dynamic memory statistics
201 */
202 if (pstat_getdynamic(&psd, sizeof(psd), (size_t) 1, 0) == -1) {
203 snmp_log(LOG_ERR, "memory_hpux: pstat_getdynamic failed!\n");
204 return (NULL);
205 }
206 long_ret = pst.page_size / 1024 * psd.psd_vmtxt;
207 return ((u_char *) (&long_ret));
208 case MEMUSEDSWAPTXT:
209 /*
210 * Retrieve the static memory statistics
211 */
212 if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) {
213 snmp_log(LOG_ERR, "memory_hpux: pstat_getstatic failed!\n");
214 return (NULL);
215 }
216 /*
217 * Retrieve the dynamic memory statistics
218 */
219 if (pstat_getdynamic(&psd, sizeof(psd), (size_t) 1, 0) == -1) {
220 snmp_log(LOG_ERR, "memory_hpux: pstat_getdynamic failed!\n");
221 return (NULL);
222 }
223 long_ret = pst.page_size / 1024 * (psd.psd_vmtxt - psd.psd_avmtxt);
224 return ((u_char *) (&long_ret));
225 case MEMTOTALREALTXT:
226 /*
227 * Retrieve the static memory statistics
228 */
229 if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) {
230 snmp_log(LOG_ERR, "memory_hpux: pstat_getstatic failed!\n");
231 return (NULL);
232 }
233 /*
234 * Retrieve the dynamic memory statistics
235 */
236 if (pstat_getdynamic(&psd, sizeof(psd), (size_t) 1, 0) == -1) {
237 snmp_log(LOG_ERR, "memory_hpux: pstat_getdynamic failed!\n");
238 return (NULL);
239 }
240 long_ret = pst.page_size / 1024 * psd.psd_rmtxt;
241 return ((u_char *) (&long_ret));
242 case MEMUSEDREALTXT:
243 /*
244 * Retrieve the static memory statistics
245 */
246 if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) {
247 snmp_log(LOG_ERR, "memory_hpux: pstat_getstatic failed!\n");
248 return (NULL);
249 }
250 /*
251 * Retrieve the dynamic memory statistics
252 */
253 if (pstat_getdynamic(&psd, sizeof(psd), (size_t) 1, 0) == -1) {
254 snmp_log(LOG_ERR, "memory_hpux: pstat_getdynamic failed!\n");
255 return (NULL);
256 }
257 long_ret = pst.page_size / 1024 * (psd.psd_rmtxt - psd.psd_armtxt);
258 return ((u_char *) (&long_ret));
259 case MEMTOTALFREE:
260 /*
261 * Retrieve the static memory statistics
262 */
263 if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) == -1) {
264 snmp_log(LOG_ERR, "memory_hpux: pstat_getstatic failed!\n");
265 return (NULL);
266 }
267 /*
268 * Retrieve the dynamic memory statistics
269 */
270 if (pstat_getdynamic(&psd, sizeof(psd), (size_t) 1, 0) == -1) {
271 snmp_log(LOG_ERR, "memory_hpux: pstat_getdynamic failed!\n");
272 return (NULL);
273 }
274 get_swapinfo(&swap);
275 long_ret = (pst.page_size / 1024 * psd.psd_free) + swap.free_swap;
276 return ((u_char *) (&long_ret));
277 case ERRORFLAG:
278 get_swapinfo(&swap);
279 long_ret = (swap.free_swap > minimumswap) ? 0 : 1;
280 return ((u_char *) (&long_ret));
281 case ERRORMSG:
282 get_swapinfo(&swap);
283 if ((swap.free_swap > minimumswap) ? 0 : 1)
284 sprintf(errmsg, "Running out of swap space (%ld)", long_ret);
285 else
286 errmsg[0] = 0;
287 *var_len = strlen(errmsg);
288 return ((u_char *) (errmsg));
289
290 } /* end case */
291
292 return (NULL);
293 }