Browse code

snmpstats: add parameter to specify SNMP version

Marat Gareev authored on 07/09/2021 17:36:36 • Henning Westerholt committed on 09/09/2021 19:42:56
Showing 1 changed files
... ...
@@ -145,6 +145,8 @@ static param_export_t mod_params[] = {
145 145
 			(void *)set_snmpget_path},
146 146
 	{"snmpCommunity", PARAM_STRING | USE_FUNC_PARAM,
147 147
 			(void *)set_snmp_community},
148
+	{"snmpVersion", PARAM_STRING | USE_FUNC_PARAM,
149
+			(void *)set_snmp_version},
148 150
 	{"export_registrar", INT_PARAM, &snmp_export_registrar},
149 151
 	{0, 0, 0}
150 152
 };
... ...
@@ -178,10 +180,11 @@ volatile pid_t sysUpTime_pid;
178 180
  * for a full description. */
179 181
 static int spawn_sysUpTime_child();
180 182
 
181
-/*! Storage for the "snmpgetPath" and "snmpCommunity" kamailio.cfg parameters.
183
+/*! Storage for the "snmpgetPath", "snmpCommunity" and "snmpVersion" kamailio.cfg parameters.
182 184
  * The parameters are used to define what happens with the sysUpTime child.  */
183 185
 char *snmpget_path = NULL;
184 186
 char *snmp_community = NULL;
187
+char *snmp_version = NULL;
185 188
 
186 189
 /*!
187 190
  * This module replaces the default SIGCHLD handler with our own, as explained
... ...
@@ -462,6 +465,7 @@ static int spawn_sysUpTime_child(void)
462 465
 	char *full_path_to_snmpget = NULL;
463 466
 
464 467
 	char *snmp_community_string = "public";
468
+	char *snmp_version_string = "3";
465 469
 
466 470
 	/* Set up a new SIGCHLD handler.  The handler will be responsible for
467 471
 	 * ignoring SIGCHLDs generated by our sysUpTime child process.  Every
... ...
@@ -509,8 +513,16 @@ static int spawn_sysUpTime_child(void)
509 513
 				snmp_community_string);
510 514
 	}
511 515
 
512
-	char *args[] = {"-Ov", "-c", snmp_community_string, "localhost",
513
-			SYSUPTIME_OID, (char *)0};
516
+	if(snmp_version != NULL) {
517
+		snmp_version_string = snmp_version;
518
+	} else {
519
+		LM_INFO("A snmpVersion parameter was not provided."
520
+				"  Defaulting to %s\n",
521
+				snmp_version_string);
522
+	}
523
+
524
+	char *args[] = {"snmpget", "-v", snmp_version_string, "-Ov", "-c",
525
+			snmp_community_string, "localhost", SYSUPTIME_OID, (char *)0};
514 526
 
515 527
 	/* Make sure we have a path to snmpget, so we can retrieve the
516 528
 	 * sysUpTime. */
... ...
@@ -586,3 +598,15 @@ int set_snmp_community(modparam_t type, void *val)
586 598
 
587 599
 	return 0;
588 600
 }
601
+
602
+/* Handles setting of the snmp version. */
603
+int set_snmp_version(modparam_t type, void *val)
604
+{
605
+	if(!stringHandlerSanityCheck(type, val, "snmpVersion")) {
606
+		return -1;
607
+	}
608
+
609
+	snmp_version = (char *)val;
610
+
611
+	return 0;
612
+}
Browse code

snmpstats: updated to the new mod interface

Daniel-Constantin Mierla authored on 28/09/2018 08:40:45 • Victor Seva committed on 28/09/2018 11:03:26
Showing 1 changed files
... ...
@@ -126,15 +126,9 @@ static int mod_child_init(int rank);
126 126
  * log a useful message and kill the AgentX Sub-Agent child process */
127 127
 static void mod_destroy(void);
128 128
 
129
-static proc_export_t mod_procs[] = {
130
-	{"SNMP AgentX", 0, 0, agentx_child, 1},
131
-	{0, 0, 0, 0, 0}
132
-};
133
-
134
-
135 129
 /*!
136 130
  * This structure defines the SNMPStats parameters that can be configured
137
- * through the kamailio.cfg configuration file.  
131
+ * through the kamailio.cfg configuration file.
138 132
  */
139 133
 static param_export_t mod_params[] = {
140 134
 	{"sipEntityType", PARAM_STRING | USE_FUNC_PARAM,
... ...
@@ -157,18 +151,16 @@ static param_export_t mod_params[] = {
157 151
 
158 152
 
159 153
 struct module_exports exports = {
160
-	SNMPSTATS_MODULE_NAME, /* module's name */
161
-	DEFAULT_DLFLAGS,	   /* dlopen flags */
162
-	0,					   /* exported functions */
163
-	mod_params,			   /* param exports */
164
-	0,					   /* exported statistics */
165
-	0,					   /* MI Functions */
166
-	0,					   /* pseudo-variables */
167
-	mod_procs,			   /* extra processes */
168
-	mod_init,			   /* module initialization function */
169
-	0,					   /* reply processing function */
170
-	mod_destroy,		   /* Destroy function */
171
-	mod_child_init		   /* per-child init function */
154
+	SNMPSTATS_MODULE_NAME,	/* module name */
155
+	DEFAULT_DLFLAGS,		/* dlopen flags */
156
+	0,						/* exported functions */
157
+	mod_params,				/* exported parameters */
158
+	0,						/* exported rpc functions */
159
+	0,						/* exported pseudo-variables */
160
+	0,						/* reply processing function */
161
+	mod_init,				/* module init function */
162
+	mod_child_init,			/* per-child init function */
163
+	mod_destroy				/* module destroy function */
172 164
 };
173 165
 
174 166
 /*!
... ...
@@ -200,13 +192,13 @@ static struct sigaction old_sigchld_handler;
200 192
 
201 193
 /*! The following message codes are from Wikipedia at:
202 194
  *
203
- *     http://en.wikipedia.org/wiki/SIP_Responses 
195
+ *     http://en.wikipedia.org/wiki/SIP_Responses
204 196
  *
205 197
  * Updated by oej to use the IETF reference page
206 198
  *     http://www.iana.org/assignments/sip-parameters/sip-parameters.xml#sip-parameters-7
207 199
  *
208 200
  * If there are more message codes added at a later time, they should be added
209
- * here, and to out_message_code_names below.  
201
+ * here, and to out_message_code_names below.
210 202
  *
211 203
  * The array is used to register the statistics keeping track of the number of
212 204
  * messages received with the response code X.
... ...
@@ -235,7 +227,7 @@ char *in_message_code_names[] = {
235 227
 
236 228
 /*! The following message codes are from Wikipedia at:
237 229
  *
238
- *     http://en.wikipedia.org/wiki/SIP_Responses 
230
+ *     http://en.wikipedia.org/wiki/SIP_Responses
239 231
  *
240 232
  * Updated by oej to use the IETF reference page
241 233
  *     http://www.iana.org/assignments/sip-parameters/sip-parameters.xml#sip-parameters-7
Browse code

snmpstats: check the return code when registering statistics

Daniel-Constantin Mierla authored on 05/01/2018 08:06:07
Showing 1 changed files
... ...
@@ -302,10 +302,14 @@ static int register_message_code_statistics(void)
302 302
 			sizeof(stat_var *) * number_of_message_codes);
303 303
 
304 304
 	for(i = 0; i < number_of_message_codes; i++) {
305
-		register_stat(SNMPSTATS_MODULE_NAME, in_message_code_names[i],
306
-				&in_message_code_stats[i], 0);
307
-		register_stat(SNMPSTATS_MODULE_NAME, out_message_code_names[i],
308
-				&out_message_code_stats[i], 0);
305
+		if(register_stat(SNMPSTATS_MODULE_NAME, in_message_code_names[i],
306
+				&in_message_code_stats[i], 0)!=0) {
307
+			LM_ERR("failed to register in_message_code_names[%d]\n", i);
308
+		}
309
+		if(register_stat(SNMPSTATS_MODULE_NAME, out_message_code_names[i],
310
+				&out_message_code_stats[i], 0)!=0) {
311
+			LM_ERR("failed to register out_message_code_names[%d]\n", i);
312
+		}
309 313
 	}
310 314
 
311 315
 	return 0;
Browse code

snmpstats: init sigaction var and close fd in case of errors

Daniel-Constantin Mierla authored on 25/12/2017 20:51:05
Showing 1 changed files
... ...
@@ -539,6 +539,7 @@ static int spawn_sysUpTime_child(void)
539 539
 		LM_ERR("Ran out of memory while trying to retrieve sysUpTime.  ");
540 540
 		LM_ERR("                  kamailioSIPServiceStartTime is "
541 541
 			   "defaulting to zero\n");
542
+		close(snmpget_fd);
542 543
 		return -1;
543 544
 	} else {
544 545
 		/* Make a new string containing the full path to the binary. */
... ...
@@ -559,6 +560,7 @@ static int spawn_sysUpTime_child(void)
559 560
 
560 561
 	/* We should never be able to get here, because execve() is never
561 562
 	 * supposed to return. */
563
+	close(snmpget_fd);
562 564
 	free(full_path_to_snmpget);
563 565
 	exit(-1);
564 566
 }
... ...
@@ -587,4 +589,4 @@ int set_snmp_community(modparam_t type, void *val)
587 589
 	snmp_community = (char *)val;
588 590
 
589 591
 	return 0;
590
-}
591 592
\ No newline at end of file
593
+}
Browse code

snmpstats: clang format the code

Daniel-Constantin Mierla authored on 19/12/2017 11:04:29
Showing 1 changed files
... ...
@@ -114,12 +114,12 @@ static int snmp_export_registrar = 0;
114 114
  * This call must always return a value as soon as possible.  If it were not to
115 115
  * return, then Kamailio would not be able to initialize any of the other
116 116
  * modules. */
117
-static int  mod_init(void);
117
+static int mod_init(void);
118 118
 
119 119
 /*! This function is called when Kamailio has finished creating all instances of
120 120
  * itself.  It is at this point that we want to create our AgentX sub-agent
121 121
  * process, and register a handler for any state changes of our child. */
122
-static int  mod_child_init(int rank);
122
+static int mod_child_init(int rank);
123 123
 
124 124
 
125 125
 /*! This function is called when Kamailio is shutting down.  When this happens, we
... ...
@@ -127,8 +127,8 @@ static int  mod_child_init(int rank);
127 127
 static void mod_destroy(void);
128 128
 
129 129
 static proc_export_t mod_procs[] = {
130
-	{"SNMP AgentX",  0,  0, agentx_child, 1 },
131
-	{0,0,0,0,0}
130
+	{"SNMP AgentX", 0, 0, agentx_child, 1},
131
+	{0, 0, 0, 0, 0}
132 132
 };
133 133
 
134 134
 
... ...
@@ -136,42 +136,39 @@ static proc_export_t mod_procs[] = {
136 136
  * This structure defines the SNMPStats parameters that can be configured
137 137
  * through the kamailio.cfg configuration file.  
138 138
  */
139
-static param_export_t mod_params[] =
140
-{
141
-	{ "sipEntityType",          PARAM_STRING|USE_FUNC_PARAM,
142
-			(void *)handleSipEntityType       },
143
-	{ "MsgQueueMinorThreshold", INT_PARAM|USE_FUNC_PARAM,
144
-			(void *)set_queue_minor_threshold },
145
-	{ "MsgQueueMajorThreshold", INT_PARAM|USE_FUNC_PARAM,
146
-			(void *)set_queue_major_threshold },
147
-	{ "dlg_minor_threshold",    INT_PARAM|USE_FUNC_PARAM,
148
-			(void *)set_dlg_minor_threshold   },
149
-	{ "dlg_major_threshold",    INT_PARAM|USE_FUNC_PARAM,
150
-			(void *)set_dlg_major_threshold   },
151
-	{ "snmpgetPath",            PARAM_STRING|USE_FUNC_PARAM,
152
-			(void *)set_snmpget_path          },
153
-	{ "snmpCommunity",          PARAM_STRING|USE_FUNC_PARAM,
154
-			(void *)set_snmp_community        },
155
-	{ "export_registrar",       INT_PARAM,
156
-			&snmp_export_registrar            },
157
-	{ 0,0,0 }
139
+static param_export_t mod_params[] = {
140
+	{"sipEntityType", PARAM_STRING | USE_FUNC_PARAM,
141
+			(void *)handleSipEntityType},
142
+	{"MsgQueueMinorThreshold", INT_PARAM | USE_FUNC_PARAM,
143
+			(void *)set_queue_minor_threshold},
144
+	{"MsgQueueMajorThreshold", INT_PARAM | USE_FUNC_PARAM,
145
+			(void *)set_queue_major_threshold},
146
+	{"dlg_minor_threshold", INT_PARAM | USE_FUNC_PARAM,
147
+			(void *)set_dlg_minor_threshold},
148
+	{"dlg_major_threshold", INT_PARAM | USE_FUNC_PARAM,
149
+			(void *)set_dlg_major_threshold},
150
+	{"snmpgetPath", PARAM_STRING | USE_FUNC_PARAM,
151
+			(void *)set_snmpget_path},
152
+	{"snmpCommunity", PARAM_STRING | USE_FUNC_PARAM,
153
+			(void *)set_snmp_community},
154
+	{"export_registrar", INT_PARAM, &snmp_export_registrar},
155
+	{0, 0, 0}
158 156
 };
159 157
 
160 158
 
161
-struct module_exports exports =
162
-{
163
-	SNMPSTATS_MODULE_NAME,   /* module's name */
164
-	DEFAULT_DLFLAGS,         /* dlopen flags */
165
-	0,                       /* exported functions */
166
-	mod_params,              /* param exports */
167
-	0,                       /* exported statistics */
168
-	0,                       /* MI Functions */
169
-	0,                       /* pseudo-variables */
170
-	mod_procs,               /* extra processes */
171
-	mod_init,                /* module initialization function */
172
-	0,                       /* reply processing function */
173
-	mod_destroy,   /* Destroy function */
174
-	mod_child_init /* per-child init function */
159
+struct module_exports exports = {
160
+	SNMPSTATS_MODULE_NAME, /* module's name */
161
+	DEFAULT_DLFLAGS,	   /* dlopen flags */
162
+	0,					   /* exported functions */
163
+	mod_params,			   /* param exports */
164
+	0,					   /* exported statistics */
165
+	0,					   /* MI Functions */
166
+	0,					   /* pseudo-variables */
167
+	mod_procs,			   /* extra processes */
168
+	mod_init,			   /* module initialization function */
169
+	0,					   /* reply processing function */
170
+	mod_destroy,		   /* Destroy function */
171
+	mod_child_init		   /* per-child init function */
175 172
 };
176 173
 
177 174
 /*!
... ...
@@ -191,7 +188,7 @@ static int spawn_sysUpTime_child();
191 188
 
192 189
 /*! Storage for the "snmpgetPath" and "snmpCommunity" kamailio.cfg parameters.
193 190
  * The parameters are used to define what happens with the sysUpTime child.  */
194
-char *snmpget_path   = NULL;
191
+char *snmpget_path = NULL;
195 192
 char *snmp_community = NULL;
196 193
 
197 194
 /*!
... ...
@@ -215,22 +212,21 @@ static struct sigaction old_sigchld_handler;
215 212
  * messages received with the response code X.
216 213
  *
217 214
  */
218
-char *in_message_code_names[] = 
219
-{
215
+char *in_message_code_names[] = {
220 216
 	"100_in", "180_in", "181_in", "182_in", "183_in", "199_in",
221
-	
217
+
222 218
 	"200_in", "202_in", "204_in",
223
-	
219
+
224 220
 	"300_in", "301_in", "302_in", "305_in", "380_in",
225
-	
226
-	"400_in", "401_in", "402_in", "403_in", "404_in", "405_in", "406_in", 
227
-	"407_in", "408_in", "410_in", "412_in", "413_in", "414_in", "415_in", 
221
+
222
+	"400_in", "401_in", "402_in", "403_in", "404_in", "405_in", "406_in",
223
+	"407_in", "408_in", "410_in", "412_in", "413_in", "414_in", "415_in",
228 224
 	"416_in", "417_in", "420_in", "421_in", "422_in", "423_in", "424_in",
229 225
 	"428_in", "429_in", "430_in", "433_in", "436_in", "437_in", "438_in",
230 226
 	"439_in", "440_in", "469_in", "470_in", "480_in", "481_in", "482_in",
231 227
 	"483_in", "484_in", "485_in", "486_in", "487_in", "488_in", "489_in",
232
-	"491_in", "492_in", "493_in", "494_in", 
233
-	
228
+	"491_in", "492_in", "493_in", "494_in",
229
+
234 230
 	"500_in", "501_in", "502_in", "503_in", "504_in", "505_in", "513_in",
235 231
 	"580_in",
236 232
 
... ...
@@ -250,24 +246,24 @@ char *in_message_code_names[] =
250 246
  * The array is used to register the statistics keeping track of the number of
251 247
  * messages send out with the response code X.
252 248
  */
253
-char *out_message_code_names[] = 
254
-{
249
+char *out_message_code_names[] = {
255 250
 	"100_out", "180_out", "181_out", "182_out", "183_out", "199_out",
256
-	
251
+
257 252
 	"200_out", "202_out", "204_out",
258
-	
253
+
259 254
 	"300_out", "301_out", "302_out", "305_out", "380_out",
260
-	
261
-	"400_out", "401_out", "402_out", "403_out", "404_out", "405_out", "406_out", 
262
-	"407_out", "408_out", "410_out", "412_out", "413_out", "414_out", "415_out",
263
-	"416_out", "417_out", "420_out", "421_out", "422_out", "423_out", "424_out",
264
-	"428_out", "429_out", "430_out", "433_out", "436_out", "437_out", "438_out", 
265
-	"439_out", "440_out", "469_out", "470_out", "480_out", "481_out", "482_out",
266
-	"483_out", "484_out", "485_out", "486_out", "487_out", "488_out", "489_out",
267
-	"491_out", "492_out", "493_out", "494_out", 
268
-	
269
-	"500_out", "501_out", "502_out", "503_out", "504_out", "505_out", "513_out",
270
-	"580_out",
255
+
256
+	"400_out", "401_out", "402_out", "403_out", "404_out", "405_out",
257
+	"406_out", "407_out", "408_out", "410_out", "412_out", "413_out",
258
+	"414_out", "415_out", "416_out", "417_out", "420_out", "421_out",
259
+	"422_out", "423_out", "424_out", "428_out", "429_out", "430_out",
260
+	"433_out", "436_out", "437_out", "438_out", "439_out", "440_out",
261
+	"469_out", "470_out", "480_out", "481_out", "482_out", "483_out",
262
+	"484_out", "485_out", "486_out", "487_out", "488_out", "489_out",
263
+	"491_out", "492_out", "493_out", "494_out",
264
+
265
+	"500_out", "501_out", "502_out", "503_out", "504_out", "505_out",
266
+	"513_out", "580_out",
271 267
 
272 268
 	"600_out", "603_out", "604_out", "606_out"
273 269
 };
... ...
@@ -275,41 +271,40 @@ char *out_message_code_names[] =
275 271
 /*! message_code_stat_array[0] will be the data source for message_code_array[0]
276 272
  * message_code_stat_array[3] will be the data source for message_code_array[3]
277 273
  * and so on. */
278
-stat_var **in_message_code_stats  = NULL;
274
+stat_var **in_message_code_stats = NULL;
279 275
 stat_var **out_message_code_stats = NULL;
280 276
 
281 277
 /*! Adds the message code statistics to the statistics framework */
282
-static int register_message_code_statistics(void) 
278
+static int register_message_code_statistics(void)
283 279
 {
284 280
 	int i;
285 281
 
286
-	int number_of_message_codes = 
287
-		sizeof(in_message_code_names) / sizeof(char *);
282
+	int number_of_message_codes =
283
+			sizeof(in_message_code_names) / sizeof(char *);
288 284
 
289
-	in_message_code_stats = 
290
-		shm_malloc(sizeof(stat_var*) * number_of_message_codes);
285
+	in_message_code_stats =
286
+			shm_malloc(sizeof(stat_var *) * number_of_message_codes);
291 287
 
292
-	out_message_code_stats = 
293
-		shm_malloc(sizeof(stat_var*) * number_of_message_codes);
288
+	out_message_code_stats =
289
+			shm_malloc(sizeof(stat_var *) * number_of_message_codes);
294 290
 
295 291
 	/* We can only proceed if we had enough memory to allocate the
296 292
 	 * statistics.  Note that we don't free the memory, but we don't care
297 293
 	 * because the system is going to shut down */
298
-	if (in_message_code_stats == NULL || 
299
-			out_message_code_stats == NULL)
300
-	{
294
+	if(in_message_code_stats == NULL || out_message_code_stats == NULL) {
301 295
 		return -1;
302 296
 	}
303 297
 
304 298
 	/* Make sure everything is zeroed out */
305
-	memset(in_message_code_stats,  0, sizeof(stat_var*) * number_of_message_codes);
306
-	memset(out_message_code_stats, 0, sizeof(stat_var*) * number_of_message_codes);
299
+	memset(in_message_code_stats, 0,
300
+			sizeof(stat_var *) * number_of_message_codes);
301
+	memset(out_message_code_stats, 0,
302
+			sizeof(stat_var *) * number_of_message_codes);
307 303
 
308
-	for (i = 0; i < number_of_message_codes; i++) 
309
-	{
310
-		register_stat(SNMPSTATS_MODULE_NAME, in_message_code_names[i], 
304
+	for(i = 0; i < number_of_message_codes; i++) {
305
+		register_stat(SNMPSTATS_MODULE_NAME, in_message_code_names[i],
311 306
 				&in_message_code_stats[i], 0);
312
-		register_stat(SNMPSTATS_MODULE_NAME, out_message_code_names[i], 
307
+		register_stat(SNMPSTATS_MODULE_NAME, out_message_code_names[i],
313 308
 				&out_message_code_stats[i], 0);
314 309
 	}
315 310
 
... ...
@@ -320,32 +315,29 @@ static int register_message_code_statistics(void)
320 315
  * This call must always return a value as soon as possible.  If it were not to
321 316
  * return, then Kamailio would not be able to initialize any of the other
322 317
  * modules. */
323
-static int mod_init(void) 
318
+static int mod_init(void)
324 319
 {
325
-	if (register_message_code_statistics() < 0) 
326
-	{
320
+	if(register_message_code_statistics() < 0) {
327 321
 		return -1;
328 322
 	}
329 323
 
330 324
 	/* Initialize shared memory used to buffer communication between the
331 325
 	 * usrloc module and the snmpstats module.  */
332 326
 	initInterprocessBuffers();
333
-	
327
+
334 328
 	/* We need to register for callbacks with usrloc module, for whenever a
335 329
 	 * contact is added or removed from the system.  We need to do it now
336 330
 	 * before Kamailio's functions get a chance to load up old user data from
337 331
 	 * the database.  That load will happen if a lookup() function is come
338 332
 	 * across in kamailio.cfg. */
339 333
 
340
-	if (snmp_export_registrar!=0)
341
-	{
342
-		if(!registerForUSRLOCCallbacks())
343
-		{
334
+	if(snmp_export_registrar != 0) {
335
+		if(!registerForUSRLOCCallbacks()) {
344 336
 			/* Originally there were descriptive error messages here to help
345 337
 			 * the operator debug problems.  Turns out this may instead
346 338
 			 * alarm them about problems they don't need to worry about.  So
347 339
 			 * the messages are commented out for now */
348
-		
340
+
349 341
 			/*
350 342
 			LM_ERR("snmpstats module was unable to register callbacks"
351 343
 					" with the usrloc module\n");
... ...
@@ -374,23 +366,23 @@ static int mod_init(void)
374 366
 /*! This function is called when Kamailio has finished creating all instances of
375 367
  * itself.  It is at this point that we want to create our AgentX sub-agent
376 368
  * process, and register a handler for any state changes of our child. */
377
-static int mod_child_init(int rank) 
369
+static int mod_child_init(int rank)
378 370
 {
379 371
 	int pid;
380 372
 
381 373
 	/* We only want to setup a single process, under the main attendant. */
382
-	if (rank != PROC_MAIN) {
374
+	if(rank != PROC_MAIN) {
383 375
 		return 0;
384 376
 	}
385 377
 
386 378
 	/* Spawn SNMP AgentX process */
387
-	pid=fork_process(PROC_NOCHLDINIT, "SNMP AgentX", 1);
388
-	if (pid<0)
379
+	pid = fork_process(PROC_NOCHLDINIT, "SNMP AgentX", 1);
380
+	if(pid < 0)
389 381
 		return -1; /* error */
390
-	if(pid==0){
382
+	if(pid == 0) {
391 383
 		/* child */
392 384
 		/* initialize the config framework */
393
-		if (cfg_child_init())
385
+		if(cfg_child_init())
394 386
 			return -1;
395 387
 
396 388
 		agentx_child(1);
... ...
@@ -405,10 +397,10 @@ static int mod_child_init(int rank)
405 397
 
406 398
 /*! This function is called when Kamailio is shutting down. When this happens, we
407 399
  * log a useful message and kill the AgentX Sub-Agent child process */
408
-static void mod_destroy(void) 
400
+static void mod_destroy(void)
409 401
 {
410 402
 	LM_INFO("The SNMPStats module got the kill signal\n");
411
-	
403
+
412 404
 	freeInterprocessBuffer();
413 405
 
414 406
 	LM_INFO("Shutting down the AgentX Sub-Agent!\n");
... ...
@@ -429,18 +421,16 @@ static void sigchld_handler(int signal)
429 421
 	 * sysUpTime child process, and ignore it.  If the SIGCHLD is
430 422
 	 * from another process, we need to call Kamailio's usual
431 423
 	 * handlers */
432
-	pid_of_signalled_process = 
424
+	pid_of_signalled_process =
433 425
 			waitpid(-1, &pid_of_signalled_process_status, WNOHANG);
434 426
 
435
-	if (pid_of_signalled_process == sysUpTime_pid)
436
-	{
427
+	if(pid_of_signalled_process == sysUpTime_pid) {
437 428
 		/* It was the sysUpTime process which died, which was expected.
438 429
 		 * At this point we will never see any SIGCHLDs from any other
439 430
 		 * SNMPStats process.  This means that we can restore Kamailio's
440 431
 		 * original handlers. */
441 432
 		sigaction(SIGCHLD, &old_sigchld_handler, NULL);
442
-	} else 
443
-	{
433
+	} else {
444 434
 
445 435
 		/* We need this 'else-block' in case another Kamailio process dies
446 436
 		 * unexpectantly before the sysUpTime process dies.  If this
... ...
@@ -448,13 +438,11 @@ static void sigchld_handler(int signal)
448 438
 		 * the block above re-assigns Kamailio's original SIGCHLD
449 439
 		 * handler.  If it does happen, then we make sure to call the
450 440
 		 * default signal handlers. */
451
-		if (old_sigchld_handler.sa_handler != SIG_IGN &&
452
-				old_sigchld_handler.sa_handler != SIG_DFL)
453
-		{
441
+		if(old_sigchld_handler.sa_handler != SIG_IGN
442
+				&& old_sigchld_handler.sa_handler != SIG_DFL) {
454 443
 			(*(old_sigchld_handler.sa_handler))(signal);
455 444
 		}
456 445
 	}
457
-
458 446
 }
459 447
 
460 448
 /*!
... ...
@@ -469,13 +457,13 @@ static void sigchld_handler(int signal)
469 457
  *       a normal system uptime. Support for this has been provided to try to
470 458
  *       match the IETF Draft SIP MIBs as closely as possible. 
471 459
  */
472
-static int spawn_sysUpTime_child(void) 
460
+static int spawn_sysUpTime_child(void)
473 461
 {
474 462
 	struct sigaction new_sigchld_handler;
475 463
 
476 464
 	char *local_path_to_snmpget = "/usr/local/bin/";
477
-	char *snmpget_binary_name   = "/snmpget";
478
-	char *full_path_to_snmpget  = NULL;
465
+	char *snmpget_binary_name = "/snmpget";
466
+	char *full_path_to_snmpget = NULL;
479 467
 
480 468
 	char *snmp_community_string = "public";
481 469
 
... ...
@@ -483,16 +471,16 @@ static int spawn_sysUpTime_child(void)
483 471
 	 * ignoring SIGCHLDs generated by our sysUpTime child process.  Every
484 472
 	 * other SIGCHLD will be redirected to the old SIGCHLD handler. */
485 473
 	sigfillset(&new_sigchld_handler.sa_mask);
486
-	new_sigchld_handler.sa_flags   = SA_RESTART;
474
+	new_sigchld_handler.sa_flags = SA_RESTART;
487 475
 	new_sigchld_handler.sa_handler = sigchld_handler;
488 476
 	sigaction(SIGCHLD, &new_sigchld_handler, &old_sigchld_handler);
489 477
 
490 478
 	pid_t result_pid = fork();
491 479
 
492
-	if (result_pid < 0) {
480
+	if(result_pid < 0) {
493 481
 		LM_ERR("failed to not spawn an agent to check sysUpTime\n");
494 482
 		return -1;
495
-	} else if (result_pid != 0) {
483
+	} else if(result_pid != 0) {
496 484
 
497 485
 		/* Keep around the PID of the sysUpTime process so that the
498 486
 		 * customized SIGCHLD handler knows to ignore the SIGCHLD we
... ...
@@ -500,82 +488,75 @@ static int spawn_sysUpTime_child(void)
500 488
 		sysUpTime_pid = result_pid;
501 489
 
502 490
 		return 0;
503
-
504 491
 	}
505 492
 
506 493
 	/* If we are here, then we are the child process.  Lets set up the file
507 494
 	 * descriptors so we can capture the output of snmpget. */
508
-	int snmpget_fd = 
509
-		open(SNMPGET_TEMP_FILE, O_CREAT|O_TRUNC|O_RDWR,
510
-				S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
495
+	int snmpget_fd = open(SNMPGET_TEMP_FILE, O_CREAT | O_TRUNC | O_RDWR,
496
+			S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
511 497
 
512 498
 
513
-	if (snmpget_fd == -1) {
499
+	if(snmpget_fd == -1) {
514 500
 		LM_ERR("failed to open a temporary file "
515
-				"for snmpget to write to\n");
501
+			   "for snmpget to write to\n");
516 502
 		return -1;
517 503
 	}
518 504
 
519 505
 	/* Redirect Standard Output to our temporary file. */
520
-	dup2(snmpget_fd, 1); 
506
+	dup2(snmpget_fd, 1);
521 507
 
522
-	if (snmp_community != NULL) {
508
+	if(snmp_community != NULL) {
523 509
 		snmp_community_string = snmp_community;
524 510
 	} else {
525 511
 		LM_INFO("An snmpCommunity parameter was not provided."
526
-				"  Defaulting to %s\n",	snmp_community_string);
512
+				"  Defaulting to %s\n",
513
+				snmp_community_string);
527 514
 	}
528 515
 
529
-	char *args[] = {"-Ov", "-c",  snmp_community_string, "localhost", 
530
-		SYSUPTIME_OID, (char *) 0};
516
+	char *args[] = {"-Ov", "-c", snmp_community_string, "localhost",
517
+			SYSUPTIME_OID, (char *)0};
531 518
 
532 519
 	/* Make sure we have a path to snmpget, so we can retrieve the
533 520
 	 * sysUpTime. */
534
-	if (snmpget_path == NULL) 
535
-	{
521
+	if(snmpget_path == NULL) {
536 522
 		LM_INFO("An snmpgetPath parameter was not specified."
537
-				"  Defaulting to %s\n", local_path_to_snmpget);
538
-	}
539
-	else 
540
-	{
523
+				"  Defaulting to %s\n",
524
+				local_path_to_snmpget);
525
+	} else {
541 526
 		local_path_to_snmpget = snmpget_path;
542 527
 	}
543 528
 
544 529
 	int local_path_to_snmpget_length = strlen(local_path_to_snmpget);
545
-	int snmpget_binary_name_length   = strlen(snmpget_binary_name);
546
-				
530
+	int snmpget_binary_name_length = strlen(snmpget_binary_name);
531
+
547 532
 	/* Allocate enough memory to hold the path, the binary name, and the
548 533
 	 * null character.  We don't use pkg_memory here. */
549
-	full_path_to_snmpget = 
550
-		malloc(sizeof(char) * 
551
-				(local_path_to_snmpget_length + 
552
-				 snmpget_binary_name_length   + 1));
534
+	full_path_to_snmpget = malloc(
535
+			sizeof(char)
536
+			* (local_path_to_snmpget_length + snmpget_binary_name_length + 1));
553 537
 
554
-	if (full_path_to_snmpget == NULL) 
555
-	{
538
+	if(full_path_to_snmpget == NULL) {
556 539
 		LM_ERR("Ran out of memory while trying to retrieve sysUpTime.  ");
557
-		LM_ERR( "                  kamailioSIPServiceStartTime is "
558
-				"defaulting to zero\n");
540
+		LM_ERR("                  kamailioSIPServiceStartTime is "
541
+			   "defaulting to zero\n");
559 542
 		return -1;
560
-	}
561
-	else
562
-	{
543
+	} else {
563 544
 		/* Make a new string containing the full path to the binary. */
564 545
 		strcpy(full_path_to_snmpget, local_path_to_snmpget);
565
-		strcpy(&full_path_to_snmpget[local_path_to_snmpget_length], 
546
+		strcpy(&full_path_to_snmpget[local_path_to_snmpget_length],
566 547
 				snmpget_binary_name);
567 548
 	}
568 549
 
569 550
 	/* snmpget -Ov -c public localhost .1.3.6.1.2.1.1.3.0  */
570
-	if (execve(full_path_to_snmpget, args, NULL) == -1) {
571
-		LM_ERR( "snmpget failed to run.  Did you supply the snmpstats module"
572
-				" with a proper snmpgetPath parameter? The "
573
-				"kamailioSIPServiceStartTime is defaulting to zero\n");
551
+	if(execve(full_path_to_snmpget, args, NULL) == -1) {
552
+		LM_ERR("snmpget failed to run.  Did you supply the snmpstats module"
553
+			   " with a proper snmpgetPath parameter? The "
554
+			   "kamailioSIPServiceStartTime is defaulting to zero\n");
574 555
 		close(snmpget_fd);
575 556
 		free(full_path_to_snmpget);
576 557
 		exit(-1);
577 558
 	}
578
-	
559
+
579 560
 	/* We should never be able to get here, because execve() is never
580 561
 	 * supposed to return. */
581 562
 	free(full_path_to_snmpget);
... ...
@@ -585,9 +566,9 @@ static int spawn_sysUpTime_child(void)
585 566
 
586 567
 /*! This function is called whenever the kamailio.cfg file specifies the
587 568
  * snmpgetPath parameter.  The function will set the snmpget_path parameter. */
588
-int set_snmpget_path( modparam_t type, void *val) 
569
+int set_snmpget_path(modparam_t type, void *val)
589 570
 {
590
-	if (!stringHandlerSanityCheck(type, val, "snmpgetPath" )) {
571
+	if(!stringHandlerSanityCheck(type, val, "snmpgetPath")) {
591 572
 		return -1;
592 573
 	}
593 574
 
... ...
@@ -597,13 +578,13 @@ int set_snmpget_path( modparam_t type, void *val)
597 578
 }
598 579
 
599 580
 /* Handles setting of the snmp community string. */
600
-int set_snmp_community( modparam_t type, void *val)
581
+int set_snmp_community(modparam_t type, void *val)
601 582
 {
602
-	if (!stringHandlerSanityCheck(type, val, "snmpCommunity")) {
583
+	if(!stringHandlerSanityCheck(type, val, "snmpCommunity")) {
603 584
 		return -1;
604 585
 	}
605 586
 
606 587
 	snmp_community = (char *)val;
607 588
 
608 589
 	return 0;
609
-}
590
+}
610 591
\ No newline at end of file
Browse code

core, lib, modules: updated include paths for header files

Daniel-Constantin Mierla authored on 07/12/2016 11:07:22
Showing 1 changed files
... ...
@@ -76,22 +76,22 @@
76 76
 #include <sys/wait.h>
77 77
 #include "snmpstats.h"
78 78
 #include "snmpstats_globals.h"
79
-#include "../../timer.h"
80
-#include "../../cfg/cfg_select.h"
81
-#include "../../cfg/cfg_ctx.h"
79
+#include "../../core/timer.h"
80
+#include "../../core/cfg/cfg_select.h"
81
+#include "../../core/cfg/cfg_ctx.h"
82 82
 
83 83
 #include <net-snmp/net-snmp-config.h>
84 84
 #include <net-snmp/net-snmp-includes.h>
85 85
 #include <net-snmp/agent/net-snmp-agent-includes.h>
86 86
 
87 87
 #include "snmp_statistics.h"
88
-#include "../../sr_module.h"
89
-#include "../../dprint.h"
90
-#include "../../error.h"
91
-#include "../../ut.h"
92
-#include "../../script_cb.h"
93
-#include "../../mem/mem.h"
94
-#include "../../mem/shm_mem.h"
88
+#include "../../core/sr_module.h"
89
+#include "../../core/dprint.h"
90
+#include "../../core/error.h"
91
+#include "../../core/ut.h"
92
+#include "../../core/script_cb.h"
93
+#include "../../core/mem/mem.h"
94
+#include "../../core/mem/shm_mem.h"
95 95
 
96 96
 #include "snmpSIPRegUserTable.h"
97 97
 #include "snmpSIPContactTable.h"
Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,609 @@
1
+/*
2
+ * SNMPStats Module
3
+ * Copyright (C) 2006 SOMA Networks, INC.
4
+ * Written by: Jeffrey Magder (jmagder@somanetworks.com)
5
+ * Copyright (C) 2013 Edvina AB, Sollentuna, Sweden
6
+ * Updated and extended by: Olle E. Johansson (oej@edvina.net)
7
+ *
8
+ * This file is part of Kamailio, a free SIP server.
9
+ *
10
+ * Kamailio is free software; you can redistribute it and/or modify it
11
+ * under the terms of the GNU General Public License as published by
12
+ * the Free Software Foundation; either version 2 of the License, or
13
+ * (at your option) any later version
14
+ *
15
+ * Kamailio is distributed in the hope that it will be useful, but
16
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
+ * General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU General Public License
21
+ * along with this program; if not, write to the Free Software
22
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
23
+ * USA
24
+ *
25
+ * There are some important points to understanding the SNMPStat modules
26
+ * architecture.
27
+ *
28
+ * 1) The SNMPStats module will fork off a new process in mod_child_init when
29
+ *    the rank is equal to PROC_MAIN_PROCESS.  The sub-process will be
30
+ *    responsible for registering with a master agent (the source of snmp
31
+ *    requests), and handling all received requests.
32
+ *
33
+ * 2) The Module will register a periodic alarm checking function with a sip
34
+ *    timer using register_timer().  This function checks for alarm conditions,
35
+ *    and will send out traps to the master agent when it detects their
36
+ *    presence.
37
+ *
38
+ * 3) The SNMPStats module is required to run an external application upon
39
+ *    startup, to collect sysUpTime data from the master agent.  This involves
40
+ *    spawning a short-lived process.  For this reason, the module temporarily
41
+ *    installs a new SIGCHLD handler to deal specifically with this process.  It
42
+ *    does not change the normal SIGCHLD behaviour for any process except for
43
+ *    this short lived sysUpTime process.
44
+ *
45
+ * 4) mod_init() will initialize some interprocess communication buffers, as
46
+ *    well as callback mechanisms for the usrloc module.  To understand what the
47
+ *    interprocess buffer and callbacks are and are for, please see the comments
48
+ *    at the beginning of snmpSIPRegUserTable.c
49
+ */
50
+
51
+/*!
52
+ * \file
53
+ * \brief SNMP statistic module
54
+ * \ingroup snmpstats
55
+ * - Module: \ref snmpstats
56
+ * \author Jeffrey Magder (jmagder@somanetworks.com)
57
+ * \author Olle E. Johansson (oej@edvina.net)
58
+ */
59
+
60
+/*!
61
+ * \defgroup snmpstats SNMPSTATS :: The Kamailio snmpstats Module
62
+ *
63
+ * The SNMPStats module provides an SNMP management interface to Kamailio.
64
+ * Specifically, it provides general SNMP queryable scalar statistics, table
65
+ * representations of more complicated data such as user and contact information,
66
+ * and alarm monitoring capabilities.
67
+ */
68
+
69
+#include <stdio.h>
70
+#include <unistd.h>
71
+#include <sys/types.h>
72
+#include <sys/stat.h>
73
+#include <fcntl.h>
74
+
75
+#include <signal.h>
76
+#include <sys/wait.h>
77
+#include "snmpstats.h"
78
+#include "snmpstats_globals.h"
79
+#include "../../timer.h"
80
+#include "../../cfg/cfg_select.h"
81
+#include "../../cfg/cfg_ctx.h"
82
+
83
+#include <net-snmp/net-snmp-config.h>
84
+#include <net-snmp/net-snmp-includes.h>
85
+#include <net-snmp/agent/net-snmp-agent-includes.h>
86
+
87
+#include "snmp_statistics.h"
88
+#include "../../sr_module.h"
89
+#include "../../dprint.h"
90
+#include "../../error.h"
91
+#include "../../ut.h"
92
+#include "../../script_cb.h"
93
+#include "../../mem/mem.h"
94
+#include "../../mem/shm_mem.h"
95
+
96
+#include "snmpSIPRegUserTable.h"
97
+#include "snmpSIPContactTable.h"
98
+
99
+#include "interprocess_buffer.h"
100
+
101
+#include "hashTable.h"
102
+#include "alarm_checks.h"
103
+#include "utilities.h"
104
+#include "sub_agent.h"
105
+
106
+/* Required in every Kamailio Module. */
107
+MODULE_VERSION
108
+
109
+/* module parameter to register for usrloc callbacks or not,
110
+ * in order to export registrar records (0 - don't export, 1 - export) */
111
+static int snmp_export_registrar = 0;
112
+
113
+/*! This is the first function to be called by Kamailio, to initialize the module.
114
+ * This call must always return a value as soon as possible.  If it were not to
115
+ * return, then Kamailio would not be able to initialize any of the other
116
+ * modules. */
117
+static int  mod_init(void);
118
+
119
+/*! This function is called when Kamailio has finished creating all instances of
120
+ * itself.  It is at this point that we want to create our AgentX sub-agent
121
+ * process, and register a handler for any state changes of our child. */
122
+static int  mod_child_init(int rank);
123
+
124
+
125
+/*! This function is called when Kamailio is shutting down.  When this happens, we
126
+ * log a useful message and kill the AgentX Sub-Agent child process */
127
+static void mod_destroy(void);
128
+
129
+static proc_export_t mod_procs[] = {
130
+	{"SNMP AgentX",  0,  0, agentx_child, 1 },
131
+	{0,0,0,0,0}
132
+};
133
+
134
+
135
+/*!
136
+ * This structure defines the SNMPStats parameters that can be configured
137
+ * through the kamailio.cfg configuration file.  
138
+ */
139
+static param_export_t mod_params[] =
140
+{
141
+	{ "sipEntityType",          PARAM_STRING|USE_FUNC_PARAM,
142
+			(void *)handleSipEntityType       },
143
+	{ "MsgQueueMinorThreshold", INT_PARAM|USE_FUNC_PARAM,
144
+			(void *)set_queue_minor_threshold },
145
+	{ "MsgQueueMajorThreshold", INT_PARAM|USE_FUNC_PARAM,
146
+			(void *)set_queue_major_threshold },
147
+	{ "dlg_minor_threshold",    INT_PARAM|USE_FUNC_PARAM,
148
+			(void *)set_dlg_minor_threshold   },
149
+	{ "dlg_major_threshold",    INT_PARAM|USE_FUNC_PARAM,
150
+			(void *)set_dlg_major_threshold   },
151
+	{ "snmpgetPath",            PARAM_STRING|USE_FUNC_PARAM,
152
+			(void *)set_snmpget_path          },
153
+	{ "snmpCommunity",          PARAM_STRING|USE_FUNC_PARAM,