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