1 /*
2  * SNMPStats Module
3  * Copyright (C) 2006 SOMA Networks, INC.
4  * Written by: Jeffrey Magder (jmagder@somanetworks.com)
5  *
6  * This file is part of Kamailio, a free SIP server.
7  *
8  * Kamailio is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * Kamailio is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21  * USA
22  *
23  * This file implements all scalares defines in the KAMAILIO-SIP-SERVER-MIB.  For
24  * a full description of the scalars, please see KAMAILIO-SIP-SERVER-MIB.
25  *
26  */
27 
28 #include <string.h>
29 
30 #include "snmp_statistics.h"
31 #include "../../core/sr_module.h"
32 #include "../../core/config.h"
33 #include "../usrloc/usrloc.h"
34 
35 #include <net-snmp/net-snmp-config.h>
36 #include <net-snmp/net-snmp-includes.h>
37 #include <net-snmp/agent/net-snmp-agent-includes.h>
38 #include "snmpSIPServerObjects.h"
39 
40 #include "snmpstats_globals.h"
41 #include "utilities.h"
42 
43 /* Used for kamailioSIPRegUserLookupCounter.  (See the MIB file for details) */
44 unsigned int global_UserLookupCounter;
45 
46 
47 /* Initializes the kamailioSIPServerObjects module.  This involves:
48  *
49  *  - Registering all OID's
50  *  - Setting up handlers for all OID's
51  *
52  * This function is mostly auto-generated. */
init_kamailioSIPServerObjects(void)53 void init_kamailioSIPServerObjects(void)
54 {
55 	static oid kamailioSIPProxyStatefulness_oid[] = {
56 			KAMAILIO_OID, 3, 1, 2, 1, 3, 1};
57 
58 	static oid kamailioSIPProxyRecordRoute_oid[] = {
59 			KAMAILIO_OID, 3, 1, 2, 1, 3, 3};
60 
61 	static oid kamailioSIPProxyAuthMethod_oid[] = {
62 			KAMAILIO_OID, 3, 1, 2, 1, 3, 4};
63 
64 	static oid kamailioSIPNumProxyRequireFailures_oid[] = {
65 			KAMAILIO_OID, 3, 1, 2, 1, 4, 1};
66 
67 	static oid kamailioSIPRegMaxContactExpiryDuration_oid[] = {
68 			KAMAILIO_OID, 3, 1, 2, 1, 5, 2};
69 
70 	static oid kamailioSIPRegMaxUsers_oid[] = {KAMAILIO_OID, 3, 1, 2, 1, 5, 3};
71 
72 	static oid kamailioSIPRegCurrentUsers_oid[] = {
73 			KAMAILIO_OID, 3, 1, 2, 1, 5, 4};
74 
75 	static oid kamailioSIPRegDfltRegActiveInterval_oid[] = {
76 			KAMAILIO_OID, 3, 1, 2, 1, 5, 5};
77 
78 	static oid kamailioSIPRegUserLookupCounter_oid[] = {
79 			KAMAILIO_OID, 3, 1, 2, 1, 5, 8};
80 
81 	static oid kamailioSIPRegAcceptedRegistrations_oid[] = {
82 			KAMAILIO_OID, 3, 1, 2, 1, 6, 1};
83 
84 	static oid kamailioSIPRegRejectedRegistrations_oid[] = {
85 			KAMAILIO_OID, 3, 1, 2, 1, 6, 2};
86 
87 	DEBUGMSGTL(("kamailioSIPServerObjects", "Initializing\n"));
88 
89 	netsnmp_register_scalar(netsnmp_create_handler_registration(
90 			"kamailioSIPProxyStatefulness", handle_kamailioSIPProxyStatefulness,
91 			kamailioSIPProxyStatefulness_oid,
92 			OID_LENGTH(kamailioSIPProxyStatefulness_oid), HANDLER_CAN_RONLY));
93 
94 	netsnmp_register_scalar(netsnmp_create_handler_registration(
95 			"kamailioSIPProxyRecordRoute", handle_kamailioSIPProxyRecordRoute,
96 			kamailioSIPProxyRecordRoute_oid,
97 			OID_LENGTH(kamailioSIPProxyRecordRoute_oid), HANDLER_CAN_RONLY));
98 
99 	netsnmp_register_scalar(netsnmp_create_handler_registration(
100 			"kamailioSIPProxyAuthMethod", handle_kamailioSIPProxyAuthMethod,
101 			kamailioSIPProxyAuthMethod_oid,
102 			OID_LENGTH(kamailioSIPProxyAuthMethod_oid), HANDLER_CAN_RONLY));
103 
104 	netsnmp_register_scalar(netsnmp_create_handler_registration(
105 			"kamailioSIPNumProxyRequireFailures",
106 			handle_kamailioSIPNumProxyRequireFailures,
107 			kamailioSIPNumProxyRequireFailures_oid,
108 			OID_LENGTH(kamailioSIPNumProxyRequireFailures_oid),
109 			HANDLER_CAN_RONLY));
110 
111 	netsnmp_register_scalar(netsnmp_create_handler_registration(
112 			"kamailioSIPRegMaxContactExpiryDuration",
113 			handle_kamailioSIPRegMaxContactExpiryDuration,
114 			kamailioSIPRegMaxContactExpiryDuration_oid,
115 			OID_LENGTH(kamailioSIPRegMaxContactExpiryDuration_oid),
116 			HANDLER_CAN_RONLY));
117 
118 	netsnmp_register_scalar(
119 			netsnmp_create_handler_registration("kamailioSIPRegMaxUsers",
120 					handle_kamailioSIPRegMaxUsers, kamailioSIPRegMaxUsers_oid,
121 					OID_LENGTH(kamailioSIPRegMaxUsers_oid), HANDLER_CAN_RONLY));
122 
123 	netsnmp_register_scalar(netsnmp_create_handler_registration(
124 			"kamailioSIPRegCurrentUsers", handle_kamailioSIPRegCurrentUsers,
125 			kamailioSIPRegCurrentUsers_oid,
126 			OID_LENGTH(kamailioSIPRegCurrentUsers_oid), HANDLER_CAN_RONLY));
127 
128 	netsnmp_register_scalar(netsnmp_create_handler_registration(
129 			"kamailioSIPRegDfltRegActiveInterval",
130 			handle_kamailioSIPRegDfltRegActiveInterval,
131 			kamailioSIPRegDfltRegActiveInterval_oid,
132 			OID_LENGTH(kamailioSIPRegDfltRegActiveInterval_oid),
133 			HANDLER_CAN_RONLY));
134 
135 	netsnmp_register_scalar(netsnmp_create_handler_registration(
136 			"kamailioSIPRegUserLookupCounter",
137 			handle_kamailioSIPRegUserLookupCounter,
138 			kamailioSIPRegUserLookupCounter_oid,
139 			OID_LENGTH(kamailioSIPRegUserLookupCounter_oid),
140 			HANDLER_CAN_RONLY));
141 
142 	netsnmp_register_scalar(netsnmp_create_handler_registration(
143 			"kamailioSIPRegAcceptedRegistrations",
144 			handle_kamailioSIPRegAcceptedRegistrations,
145 			kamailioSIPRegAcceptedRegistrations_oid,
146 			OID_LENGTH(kamailioSIPRegAcceptedRegistrations_oid),
147 			HANDLER_CAN_RONLY));
148 
149 	netsnmp_register_scalar(netsnmp_create_handler_registration(
150 			"kamailioSIPRegRejectedRegistrations",
151 			handle_kamailioSIPRegRejectedRegistrations,
152 			kamailioSIPRegRejectedRegistrations_oid,
153 			OID_LENGTH(kamailioSIPRegRejectedRegistrations_oid),
154 			HANDLER_CAN_RONLY));
155 }
156 
157 /* The following are handlers for the scalars in the KAMAILIO-SIP-SERVER-MIB. */
158 
handle_kamailioSIPProxyStatefulness(netsnmp_mib_handler * handler,netsnmp_handler_registration * reginfo,netsnmp_agent_request_info * reqinfo,netsnmp_request_info * requests)159 int handle_kamailioSIPProxyStatefulness(netsnmp_mib_handler *handler,
160 		netsnmp_handler_registration *reginfo,
161 		netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests)
162 {
163 	int statefullness;
164 
165 	if(module_loaded("dialog") || module_loaded("ims_dialog")) {
166 		statefullness = PROXY_STATEFULNESS_CALL_STATEFUL;
167 	} else if(module_loaded("tm")) {
168 		statefullness = PROXY_STATEFULNESS_TRANSACTION_STATEFUL;
169 	} else {
170 		statefullness = PROXY_STATEFULNESS_STATELESS;
171 	}
172 
173 	if(reqinfo->mode == MODE_GET) {
174 		snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
175 				(u_char *)&statefullness, sizeof(int));
176 		return SNMP_ERR_NOERROR;
177 	}
178 
179 	return SNMP_ERR_GENERR;
180 }
181 
182 
handle_kamailioSIPProxyRecordRoute(netsnmp_mib_handler * handler,netsnmp_handler_registration * reginfo,netsnmp_agent_request_info * reqinfo,netsnmp_request_info * requests)183 int handle_kamailioSIPProxyRecordRoute(netsnmp_mib_handler *handler,
184 		netsnmp_handler_registration *reginfo,
185 		netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests)
186 {
187 	/* If the rr module is loaded, then we support record route.  otherwise,
188 	 * we want to return 2, which is false for SNMP TruthValue. */
189 	int supportRecordRoute = TC_FALSE;
190 
191 	if(module_loaded("rr")) {
192 		supportRecordRoute = TC_TRUE;
193 	}
194 
195 	if(reqinfo->mode == MODE_GET) {
196 		snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
197 				(u_char *)&supportRecordRoute, sizeof(int));
198 		return SNMP_ERR_NOERROR;
199 	}
200 
201 	return SNMP_ERR_GENERR;
202 }
203 
handle_kamailioSIPProxyAuthMethod(netsnmp_mib_handler * handler,netsnmp_handler_registration * reginfo,netsnmp_agent_request_info * reqinfo,netsnmp_request_info * requests)204 int handle_kamailioSIPProxyAuthMethod(netsnmp_mib_handler *handler,
205 		netsnmp_handler_registration *reginfo,
206 		netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests)
207 {
208 
209 	/* The result needs to be returned as an SNMP bit field. */
210 	unsigned int auth_bitfield = SIP_AUTH_METHOD_NONE;
211 
212 	if(module_loaded("tls")) {
213 		auth_bitfield |= SIP_AUTH_METHOD_TLS;
214 		auth_bitfield &= ~SIP_AUTH_METHOD_NONE;
215 	}
216 
217 	/* We can have both tls and auth loaded simultaneously.  Therefore we
218 	 * use an if instead of a else/else-if. */
219 	if(module_loaded("auth")) {
220 		auth_bitfield |= SIP_AUTH_METHOD_DIGEST;
221 		auth_bitfield &= ~SIP_AUTH_METHOD_NONE;
222 	}
223 
224 	if(reqinfo->mode == MODE_GET) {
225 		snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
226 				(u_char *)&auth_bitfield, 1);
227 		return SNMP_ERR_NOERROR;
228 	}
229 
230 	return SNMP_ERR_GENERR;
231 }
232 
handle_kamailioSIPNumProxyRequireFailures(netsnmp_mib_handler * handler,netsnmp_handler_registration * reginfo,netsnmp_agent_request_info * reqinfo,netsnmp_request_info * requests)233 int handle_kamailioSIPNumProxyRequireFailures(netsnmp_mib_handler *handler,
234 		netsnmp_handler_registration *reginfo,
235 		netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests)
236 {
237 	int result = get_statistic("bad_msg_hdr");
238 
239 	if(reqinfo->mode == MODE_GET) {
240 		snmp_set_var_typed_value(requests->requestvb, ASN_COUNTER,
241 				(u_char *)&result, sizeof(int));
242 		return SNMP_ERR_NOERROR;
243 	}
244 
245 	return SNMP_ERR_GENERR;
246 }
247 
handle_kamailioSIPRegMaxContactExpiryDuration(netsnmp_mib_handler * handler,netsnmp_handler_registration * reginfo,netsnmp_agent_request_info * reqinfo,netsnmp_request_info * requests)248 int handle_kamailioSIPRegMaxContactExpiryDuration(netsnmp_mib_handler *handler,
249 		netsnmp_handler_registration *reginfo,
250 		netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests)
251 {
252 	int result = get_statistic("max_expires");
253 
254 	if(reqinfo->mode == MODE_GET) {
255 		snmp_set_var_typed_value(requests->requestvb, ASN_UNSIGNED,
256 				(u_char *)&result, sizeof(int));
257 		return SNMP_ERR_NOERROR;
258 	}
259 
260 	return SNMP_ERR_GENERR;
261 }
262 
handle_kamailioSIPRegMaxUsers(netsnmp_mib_handler * handler,netsnmp_handler_registration * reginfo,netsnmp_agent_request_info * reqinfo,netsnmp_request_info * requests)263 int handle_kamailioSIPRegMaxUsers(netsnmp_mib_handler *handler,
264 		netsnmp_handler_registration *reginfo,
265 		netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests)
266 {
267 
268 	/* Kamailio doesn't currently have a parameterized maximum number of
269 	 * users.  So we return the maximum value an unsigned32 SNMP datatype
270 	 * can hold.  */
271 	unsigned int result = 0xffffffff;
272 
273 	if(reqinfo->mode == MODE_GET) {
274 		snmp_set_var_typed_value(requests->requestvb, ASN_UNSIGNED,
275 				(u_char *)&result, sizeof(unsigned int));
276 		return SNMP_ERR_NOERROR;
277 	}
278 
279 	return SNMP_ERR_GENERR;
280 }
281 
handle_kamailioSIPRegCurrentUsers(netsnmp_mib_handler * handler,netsnmp_handler_registration * reginfo,netsnmp_agent_request_info * reqinfo,netsnmp_request_info * requests)282 int handle_kamailioSIPRegCurrentUsers(netsnmp_mib_handler *handler,
283 		netsnmp_handler_registration *reginfo,
284 		netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests)
285 {
286 	int max_users = 0;
287 
288 	max_users = get_statistic("registered_users");
289 
290 	if(reqinfo->mode == MODE_GET) {
291 		snmp_set_var_typed_value(requests->requestvb, ASN_GAUGE,
292 				(u_char *)&max_users, sizeof(int));
293 		return SNMP_ERR_NOERROR;
294 	}
295 
296 	return SNMP_ERR_GENERR;
297 }
298 
handle_kamailioSIPRegDfltRegActiveInterval(netsnmp_mib_handler * handler,netsnmp_handler_registration * reginfo,netsnmp_agent_request_info * reqinfo,netsnmp_request_info * requests)299 int handle_kamailioSIPRegDfltRegActiveInterval(netsnmp_mib_handler *handler,
300 		netsnmp_handler_registration *reginfo,
301 		netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests)
302 {
303 	int result = get_statistic("default_expire");
304 
305 	if(reqinfo->mode == MODE_GET) {
306 		snmp_set_var_typed_value(requests->requestvb, ASN_UNSIGNED,
307 				(u_char *)&result, sizeof(int));
308 		return SNMP_ERR_NOERROR;
309 	}
310 
311 	return SNMP_ERR_GENERR;
312 }
313 
314 
handle_kamailioSIPRegUserLookupCounter(netsnmp_mib_handler * handler,netsnmp_handler_registration * reginfo,netsnmp_agent_request_info * reqinfo,netsnmp_request_info * requests)315 int handle_kamailioSIPRegUserLookupCounter(netsnmp_mib_handler *handler,
316 		netsnmp_handler_registration *reginfo,
317 		netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests)
318 {
319 	int result = ++global_UserLookupCounter;
320 
321 	/* If we have had so many requests that we've hit our maximum index,
322 	 * then we reset our counter back to 1.  For this not to cause problems,
323 	 * it will be required that old rows belonging to the table
324 	 * kamailioSIPRegUserLookupTable are eventually deleted. */
325 	if(global_UserLookupCounter > MAX_USER_LOOKUP_COUNTER) {
326 		global_UserLookupCounter = 1;
327 	}
328 
329 	if(reqinfo->mode == MODE_GET) {
330 		snmp_set_var_typed_value(requests->requestvb, ASN_COUNTER,
331 				(u_char *)&result, sizeof(int));
332 		return SNMP_ERR_NOERROR;
333 	}
334 
335 	return SNMP_ERR_GENERR;
336 }
337 
handle_kamailioSIPRegAcceptedRegistrations(netsnmp_mib_handler * handler,netsnmp_handler_registration * reginfo,netsnmp_agent_request_info * reqinfo,netsnmp_request_info * requests)338 int handle_kamailioSIPRegAcceptedRegistrations(netsnmp_mib_handler *handler,
339 		netsnmp_handler_registration *reginfo,
340 		netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests)
341 {
342 	int result = get_statistic("accepted_regs");
343 
344 	if(reqinfo->mode == MODE_GET) {
345 		snmp_set_var_typed_value(requests->requestvb, ASN_COUNTER,
346 				(u_char *)&result, sizeof(int));
347 		return SNMP_ERR_NOERROR;
348 	}
349 
350 	return SNMP_ERR_GENERR;
351 }
352 
handle_kamailioSIPRegRejectedRegistrations(netsnmp_mib_handler * handler,netsnmp_handler_registration * reginfo,netsnmp_agent_request_info * reqinfo,netsnmp_request_info * requests)353 int handle_kamailioSIPRegRejectedRegistrations(netsnmp_mib_handler *handler,
354 		netsnmp_handler_registration *reginfo,
355 		netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests)
356 {
357 	int result = get_statistic("rejected_regs");
358 
359 	if(reqinfo->mode == MODE_GET) {
360 		snmp_set_var_typed_value(requests->requestvb, ASN_COUNTER,
361 				(u_char *)&result, sizeof(int));
362 		return SNMP_ERR_NOERROR;
363 	}
364 
365 	return SNMP_ERR_GENERR;
366 }