Debian Codegraph @

agent/mibgroup/ucd-snmp/vmstat.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/auto_nlist.h>
5
6 #include <net-snmp/agent/hardware/cpu.h>
7 #include "vmstat.h"
8
9 FindVarMethod var_extensible_vmstat;
10
11
12
13 void
14 init_vmstat(void)
15 {
16 const oid vmstat_oid[] = { NETSNMP_UCDAVIS_MIB, 11 };
17
18 DEBUGMSGTL(("vmstat", "Initializing\n"));
19 netsnmp_register_scalar_group(
20 netsnmp_create_handler_registration("vmstat", vmstat_handler,
21 vmstat_oid, OID_LENGTH(vmstat_oid),
22 HANDLER_CAN_RONLY),
23 MIBINDEX, CPUNUMCPUS);
24 }
25
26
27 int
28 vmstat_handler(netsnmp_mib_handler *handler,
29 netsnmp_handler_registration *reginfo,
30 netsnmp_agent_request_info *reqinfo,
31 netsnmp_request_info *requests)
32 {
33 oid obj;
34 unsigned long long value = 0;
35 char cp[300];
36 netsnmp_cpu_info *info = netsnmp_cpu_get_byIdx( -1, 0 );
37
38 switch (reqinfo->mode) {
39 case MODE_GET:
40 obj = requests->requestvb->name[ requests->requestvb->name_length-2 ];
41
42 switch (obj) {
43 case MIBINDEX: /* dummy value */
44 snmp_set_var_typed_integer(requests->requestvb, ASN_INTEGER, 1);
45 break;
46
47 case ERRORNAME: /* dummy name */
48 sprintf(cp, "systemStats");
49 snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
50 cp, strlen(cp));
51 break;
52
53 /*
54 case IOSENT:
55 long_ret = vmstat(iosent);
56 return ((u_char *) (&long_ret));
57 case IORECEIVE:
58 long_ret = vmstat(ioreceive);
59 return ((u_char *) (&long_ret));
60 case IORAWSENT:
61 long_ret = vmstat(rawiosent);
62 return ((u_char *) (&long_ret));
63 case IORAWRECEIVE:
64 long_ret = vmstat(rawioreceive);
65 return ((u_char *) (&long_ret));
66 */
67
68 /*
69 * Raw CPU statistics
70 * Taken directly from the (overall) cpu_info structure.
71 *
72 * XXX - Need some form of flag to skip objects that
73 * aren't supported on a given architecture.
74 */
75 case CPURAWUSER:
76 snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER,
77 info->user_ticks & 0xffffffff);
78 break;
79 case CPURAWNICE:
80 snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER,
81 info->nice_ticks & 0xffffffff);
82 break;
83 case CPURAWSYSTEM:
84 /*
85 * Some architecture have traditionally reported a
86 * combination of CPU statistics for this object.
87 * The CPU HAL module uses 'sys2_ticks' for this,
88 * so use this value in preference to 'sys_ticks'
89 * if it has a non-zero value.
90 */
91 snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER,
92 (info->sys2_ticks ?
93 info->sys2_ticks :
94 info->sys_ticks ) & 0xffffffff);
95 break;
96 case CPURAWIDLE:
97 snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER,
98 info->idle_ticks & 0xffffffff);
99 break;
100 case CPURAWWAIT:
101 snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER,
102 info->wait_ticks & 0xffffffff);
103 break;
104 case CPURAWKERNEL:
105 snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER,
106 info->kern_ticks & 0xffffffff);
107 break;
108 case CPURAWINTR:
109 snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER,
110 info->intrpt_ticks & 0xffffffff);
111 break;
112 case CPURAWSOFTIRQ:
113 snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER,
114 info->sirq_ticks & 0xffffffff);
115 break;
116 case CPURAWSTEAL:
117 snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER,
118 info->steal_ticks & 0xffffffff);
119 break;
120 case CPURAWGUEST:
121 snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER,
122 info->guest_ticks & 0xffffffff);
123 break;
124 case CPURAWGUESTNICE:
125 snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER,
126 info->guestnice_ticks & 0xffffffff);
127 break;
128 case CPUNUMCPUS:
129 snmp_set_var_typed_integer(requests->requestvb, ASN_INTEGER,
130 cpu_num & 0x7fffffff);
131 break;
132
133 /*
134 * 'Cooked' CPU statistics
135 * Percentage usage of the specified statistic calculated
136 * over the period (1 min) that history is being kept for.
137 *
138 * This is actually a change of behaviour for some architectures,
139 * but:
140 * a) It ensures consistency across all systems
141 * a) It matches the definition of the MIB objects
142 *
143 * Note that this value will only be reported once the agent
144 * has a full minute's history collected.
145 */
146 case CPUUSER:
147 if ( info->history && info->history[0].total_hist ) {
148 value = (info->user_ticks - info->history[0].user_hist)*100;
149 if ( info->total_ticks - info->history[0].total_hist)
150 value /= (info->total_ticks - info->history[0].total_hist);
151 else
152 value = 0; /* or skip this entry */
153 snmp_set_var_typed_integer(requests->requestvb,
154 ASN_INTEGER, value & 0x7fffffff);
155 }
156 break;
157 case CPUSYSTEM:
158 if ( info->history && info->history[0].total_hist ) {
159 /* or sys2_ticks ??? */
160 value = (info->sys_ticks - info->history[0].sys_hist)*100;
161 if ( info->total_ticks - info->history[0].total_hist)
162 value /= (info->total_ticks - info->history[0].total_hist);
163 else
164 value = 0; /* or skip this entry */
165 snmp_set_var_typed_integer(requests->requestvb,
166 ASN_INTEGER, value & 0x7fffffff);
167 }
168 break;
169 case CPUIDLE:
170 if ( info->history && info->history[0].total_hist ) {
171 value = (info->idle_ticks - info->history[0].idle_hist)*100;
172 if ( info->total_ticks - info->history[0].total_hist)
173 value /= (info->total_ticks - info->history[0].total_hist);
174 else
175 value = 0; /* or skip this entry */
176 snmp_set_var_typed_integer(requests->requestvb,
177 ASN_INTEGER, value & 0x7fffffff);
178 }
179 break;
180
181 /*
182 * Similarly for the Interrupt and Context switch statistics
183 * (raw and per-second, calculated over the last minute)
184 */
185 case SYSRAWINTERRUPTS:
186 snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER,
187 info->nInterrupts & 0xffffffff);
188 break;
189 case SYSRAWCONTEXT:
190 snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER,
191 info->nCtxSwitches & 0xffffffff);
192 break;
193 case SYSINTERRUPTS:
194 if ( info->history && info->history[0].total_hist ) {
195 value = (info->nInterrupts - info->history[0].intr_hist)/60;
196 snmp_set_var_typed_integer(requests->requestvb,
197 ASN_INTEGER, value & 0x7fffffff);
198 }
199 break;
200 case SYSCONTEXT:
201 if ( info->history && info->history[0].total_hist ) {
202 value = (info->nCtxSwitches - info->history[0].ctx_hist)/60;
203 snmp_set_var_typed_integer(requests->requestvb,
204 ASN_INTEGER, value & 0x7fffffff);
205 }
206 break;
207
208 /*
209 * Similarly for the Swap statistics...
210 */
211 case RAWSWAPIN:
212 snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER,
213 info->swapIn & 0xffffffff);
214 break;
215 case RAWSWAPOUT:
216 snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER,
217 info->swapOut & 0xffffffff);
218 break;
219 case SWAPIN:
220 if ( info->history && info->history[0].total_hist ) {
221 value = (info->swapIn - info->history[0].swpi_hist)/60;
222 /* ??? value *= PAGE_SIZE; */
223 snmp_set_var_typed_integer(requests->requestvb,
224 ASN_INTEGER, value & 0x7fffffff);
225 }
226 break;
227 case SWAPOUT:
228 if ( info->history && info->history[0].total_hist ) {
229 value = (info->swapOut - info->history[0].swpo_hist)/60;
230 /* ??? value *= PAGE_SIZE; */
231 snmp_set_var_typed_integer(requests->requestvb,
232 ASN_INTEGER, value & 0x7fffffff);
233 }
234 break;
235
236 /*
237 * ... and the I/O statistics.
238 */
239 case IORAWSENT:
240 snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER,
241 info->pageOut & 0xffffffff);
242 break;
243 case IORAWRECEIVE:
244 snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER,
245 info->pageIn & 0xffffffff);
246 break;
247 case IOSENT:
248 if ( info->history && info->history[0].total_hist ) {
249 value = (info->pageOut - info->history[0].pageo_hist)/60;
250 snmp_set_var_typed_integer(requests->requestvb,
251 ASN_INTEGER, value & 0x7fffffff);
252 }
253 break;
254 case IORECEIVE:
255 if ( info->history && info->history[0].total_hist ) {
256 value = (info->pageIn - info->history[0].pagei_hist)/60;
257 snmp_set_var_typed_integer(requests->requestvb,
258 ASN_INTEGER, value & 0x7fffffff);
259 }
260 break;
261
262 default:
263 /*
264 XXX - The systemStats group is "holely", so walking it would
265 trigger this message repeatedly. We really need a form
266 of the table column registration mechanism, that would
267 work with scalar groups.
268 snmp_log(LOG_ERR,
269 "unknown object (%d) in vmstat_handler\n", (int)obj);
270 */
271 break;
272 }
273 break;
274
275 default:
276 snmp_log(LOG_ERR,
277 "unknown mode (%d) in vmstat_handler\n",
278 reqinfo->mode);
279 return SNMP_ERR_GENERR;
280 }
281
282 return SNMP_ERR_NOERROR;
283 }