File: stats.c

package info (click to toggle)
kamailio 4.2.0-2+deb8u3
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 56,276 kB
  • sloc: ansic: 552,836; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (873 lines) | stat: -rw-r--r-- 23,315 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
/* 
 * $Id$
 *
 * Stats reporting code. It reports through SIG_USR1 and if loaded
 * through the SNMP module
 *
 * Copyright (C) 2001-2003 FhG Fokus
 *
 * This file is part of ser, a free SIP server.
 *
 * ser is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * For a license to use the ser software under conditions
 * other than those described here, or to purchase support for this
 * software, please contact iptel.org by e-mail at the following addresses:
 *    info@iptel.org
 *
 * ser is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * History:
 * -------
 * 2003-03-19: added support for route type in find_export (janakj)
 */

/*!
 * \file
 * \brief SIP-router core :: Stats reporting code
 * \ingroup core
 * Module: \ref core
 */


#ifdef STATS
#include "stats.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include "dprint.h"
#include "mem/shm_mem.h"
#include "sr_module.h"

/* SNMP includes */
#include "modules/snmp/snmp_handler.h"
#include "modules/snmp/sipCommonStatsMethod.h"
#include "modules/snmp/sipCommonStatusCode.h"

struct stats_s *stats;		/* per process stats structure */ 
char *stat_file = NULL;		/* set by the parser */

/* private variables */
static struct stats_s *global_stats=NULL;
static int stats_segments=-1;	/*used also to determine if we've been init'ed*/

/* adds up global statistics and puts them into passed struct.
 * -1 returned on failure */
static int collect_stats(struct stats_s *s);

/***********************8 SNMP Stuff **************************/
/* a small structure we use to pass around the functions needed by
 * all the registration functions */
struct stats_funcs {
	int (*reg_func)(const char *, struct sip_snmp_handler*);
	struct sip_snmp_handler* (*new_func)(size_t);
	void (*free_func)(struct sip_snmp_handler*);
};

/* SNMP Handler registration functions */
static int sipSummaryStatsTable_register(const struct stats_funcs *f);
static int sipMethodStatsTable_register(const struct stats_funcs *f);
static int sipStatusCodesTable_register(const struct stats_funcs *f);

/* the handlers */
static int collect_InReqs(struct sip_snmp_obj *, enum handler_op);
static int collect_OutReqs(struct sip_snmp_obj *, enum handler_op);
static int collect_InResp(struct sip_snmp_obj *, enum handler_op);
static int collect_OutResp(struct sip_snmp_obj *, enum handler_op);
static int sipStatsMethod_handler(struct sip_snmp_obj *o, enum handler_op op);
static int sipStatusCodes_handler(struct sip_snmp_obj *o, enum handler_op op);

int init_stats(int nr_of_processes)
{
	LM_DBG("initializing stats for %d processes\n", 
		nr_of_processes);


	global_stats = shm_malloc(nr_of_processes*sizeof(struct stats_s));
	if(!global_stats) {
		LM_ERR("Out of memory\n");
		return -1;
	}
	stats_segments = nr_of_processes;

	if(stats_register() == -1)
		LM_WARN("Couldn't register stats with snmp module\n");


	return 0;
}

/* sets the stats pointer for the passed process */
void setstats(int child_index)
{
	if(stats_segments == -1 || !global_stats) {
		LM_ERR("Stats not initialized. Cannot set them\n");
		stats = NULL;
		return;
	}
	if(child_index < 0 || child_index >= stats_segments) {
		stats = NULL;
		LM_ERR("Invalid index %d while setting statistics. Only have "
			"space for %d processes\n", child_index, stats_segments);
		return;
	}
	stats = global_stats+child_index;
	stats->process_index = child_index;
	/* can't use pids[] because we may be called before the corresponding
	 * slot in pids[] is initialized (chk main_loop()) */
	stats->pid = getpid();
	stats->start_time = time(NULL);
}

/* printheader is used to print pid, date and index */
int dump_statistic(FILE *fp, struct stats_s *istats, int printheader)
{
	struct tm res;
	char t[256];
	if(stats_segments == -1 || !global_stats) {
		LM_ERR("Stats \"engine\" not initialized\n");
		return -1;
	}

	if(printheader) {
		localtime_r(&istats->start_time, &res);
		strftime(t, 255, "%c", &res);
		
		fprintf(fp, "stats for process %d (pid %d) started at %s\n", 
				istats->process_index, istats->pid, t);
	}

	fprintf(fp, "received requests:\ninv: %ld\tack: %ld\tcnc: %ld\t"
		"bye: %ld\tother: %ld\n",
		istats->received_requests_inv,
		istats->received_requests_ack,
		istats->received_requests_cnc,
		istats->received_requests_bye,
		istats->received_requests_other);
	fprintf(fp, "sent requests:\n"
		"inv: %ld\tack: %ld\tcnc: %ld\tbye: %ld\tother: %ld\n",
		istats->sent_requests_inv,
		istats->sent_requests_ack,
		istats->sent_requests_cnc,
		istats->sent_requests_bye,
		istats->sent_requests_other);
	fprintf(fp, "received responses:\n"
		"1: %ld\t2: %ld\t3: %ld\t4: %ld\t5: %ld\t6: %ld\tother: %ld\t"
		"drops: %ld\n",
		istats->received_responses_1,
		istats->received_responses_2,
		istats->received_responses_3,
		istats->received_responses_4,
		istats->received_responses_5,
		istats->received_responses_6,
		istats->received_responses_other,
		istats->received_drops);
	fprintf(fp, "sent responses:\n"
		"1: %ld\t2: %ld\t3: %ld\t4: %ld\t5: %ld\t6: %ld\n",
		istats->sent_responses_1,
		istats->sent_responses_2,
		istats->sent_responses_3,
		istats->sent_responses_4,
		istats->sent_responses_5,
		istats->sent_responses_6);
	fprintf(fp, "processed requests: %ld\t\tprocessed responses: %ld\n"
		"acc req time: %ld\t\t\tacc res time: %ld\nfailed on send: %ld\n\n",
		istats->processed_requests,
		istats->processed_responses,
		istats->acc_req_time,
		istats->acc_res_time,
		istats->failed_on_send);
	return 0;
}

int dump_all_statistic()
{
	register int i;
	register struct stats_s *c;
	static struct stats_s *g = NULL;
	struct tm res;
	char t[256];
	time_t ts;
	FILE *stat_fp = NULL;

	if(stats_segments == -1 || !global_stats) {
		LM_ERR("%s: Can't dump statistics, not initialized!\n", __func__);
		return -1;
	}

	if(!stat_file) {
		LM_ERR("%s: Can't dump statistics, invalid stats file\n", __func__);
		return -1;
	}

	stat_fp = fopen(stat_file, "a");
	if(!stat_fp) {
		LM_ERR("%s: Couldn't open stats file %s: %s\n", __func__, stat_file,
				strerror(errno));
		return -1;
	}

	/* time stamp them since we're appending to the file */
	ts = time(NULL);
	localtime_r(&ts, &res);
	strftime(t, 255, "%c", &res);
	fprintf(stat_fp, "#### stats @ %s #####\n", t); 

	c = global_stats;
	for(i=0; i<stats_segments; i++) {
		if(dump_statistic(stat_fp, c, 1) == -1) {
			LM_ERR("Error dumping statistics for process %d\n", i);
			goto end;
		}
		c++;
	}

	fprintf(stat_fp, "## Global Stats ##\n");
	if(!g)
		g = calloc(1, sizeof(struct stats_s));
	if(!g) {
		LM_ERR("Couldn't dump global stats: %s\n", strerror(errno));
		goto end;
	}
	
	if(collect_stats(g) == -1) {
		LM_ERR("%s: Couldn't dump global stats\n", __func__);
		goto end;
	}
	if(dump_statistic(stat_fp, g, 0) == -1) {
		LM_ERR("Couldn't dump global stats\n");
		goto end;
	}
end:
	fprintf(stat_fp, "\n");
	fclose(stat_fp);

	return 0;
}

static int collect_stats(struct stats_s *s)
{
	register int i;
	register struct stats_s *c;
	if(!s) {
		LM_ERR("Invalid stats pointer passed\n");
		return -1;
	}
	if(!global_stats || stats_segments == -1) {
		LM_ERR("Can't collect statistics, not initialized!!\n");
		return -1;
	}

	c = global_stats;
	memset(s, '\0', sizeof(struct stats_s));
	for(i=0; i<stats_segments; i++) {
		s->received_requests_inv += c->received_requests_inv;
		s->received_requests_ack += c->received_requests_ack;
		s->received_requests_cnc += c->received_requests_cnc;
		s->received_requests_bye += c->received_requests_bye;
		s->received_requests_other += c->received_requests_other;
		s->received_responses_1 += c->received_responses_1;
		s->received_responses_2 += c->received_responses_2;
		s->received_responses_3 += c->received_responses_3;
		s->received_responses_4 += c->received_responses_4;
		s->received_responses_5 += c->received_responses_5;
		s->received_responses_6 += c->received_responses_6;
		s->received_responses_other += c->received_responses_other;
		s->received_drops += c->received_drops;
		s->sent_requests_inv += c->sent_requests_inv;
		s->sent_requests_ack += c->sent_requests_ack;
		s->sent_requests_cnc += c->sent_requests_cnc;
		s->sent_requests_bye += c->sent_requests_bye;
		s->sent_requests_other += c->sent_requests_other;
		s->sent_responses_1 += c->sent_responses_1;
		s->sent_responses_2 += c->sent_responses_2;
		s->sent_responses_3 += c->sent_responses_3;
		s->sent_responses_4 += c->sent_responses_4;
		s->sent_responses_5 += c->sent_responses_5;
		s->sent_responses_6 += c->sent_responses_6;
		s->processed_requests += c->processed_requests;
		s->processed_responses += c->processed_responses;
		s->acc_req_time += c->acc_req_time;
		s->acc_res_time += c->acc_res_time;
		s->failed_on_send += c->failed_on_send;

		c++; /* next, please... */
	}

	return 0;
}

/*************************** SNMP Stuff ***********************/

/* ##### Registration Functions ####### */

/* Registers the handlers for:
 * - sipSummaryStatsTable
 * - sipMethodStatsTable
 * - sipStatusCodesTable
 * - sipCommonStatusCodeTable
 *
 * Returns 0 if snmp module not present, -1 on error, 1 on successful
 * registration
 */

#define reg(t) \
	if(t##_register(&f) == -1) {	\
		LM_ERR("%s: Failed registering SNMP handlers\n", func);	\
		return -1;	\
	}

int stats_register()
{
	const char *func = __FUNCTION__;
	struct stats_funcs f;

	f.reg_func = (void*) find_export("snmp_register_handler", 2, 0);
	f.new_func = (void*) find_export("snmp_new_handler", 1, 0);
	f.free_func = (void*) find_export("snmp_free_handler", 1, 0);
	if(!f.reg_func || !f.new_func || !f.free_func) {
		LM_INFO("%s: Couldn't find SNMP module\n", func);
		LM_INFO("%s: Not reporting stats through SNMP\n", func);
		return 0;
	}

	reg(sipSummaryStatsTable);
	reg(sipMethodStatsTable);
	reg(sipStatusCodesTable);

	return 0;
}

/* Receives the function used to register SNMP handlers. Returns 0
 * on success, -1 on error */
/* Registers:
 * - sipSummaryInRequests
 * - sipSummaryOutRequests
 * - sipSummaryInResponses
 * - sipSummaryOutResponses
 * => sipSummaryTotalTransactions is handled by the tm module */
static int sipSummaryStatsTable_register(const struct stats_funcs *f)
{
	register struct sip_snmp_handler *h;
	register struct sip_snmp_obj *o;
	const char *func = __FUNCTION__;

	h = f->new_func(sizeof(unsigned long));
	if(!h) {
		LM_ERR("%s: Error creating handler\n", func);
		return -1;
	}
	o = h->sip_obj;

	/* this is the same for all of our objects */
	o->type = SER_COUNTER;
	*o->value.integer = 0;	/* default value. The real one is computed on
							   request */
	o->val_len = sizeof(unsigned long);

	/* sipSummaryInRequests */
	h->on_get = collect_InReqs;
	h->on_set = h->on_end = NULL;
	if(f->reg_func("sipSummaryInRequests", h) == -1) {
		LM_ERR("%s: Error registering sipSummaryInRequests\n", func);
		f->free_func(h);
		return -1;
	}

	/* sipSummaryOutRequests */
	h->on_get = collect_OutReqs;
	if(f->reg_func("sipSummaryOutRequests", h) == -1) {
		LM_ERR("%s: Error registering sipSummaryOutRequests\n", func);
		f->free_func(h);
		return -1;
	}

	/* sipSummaryInResponses */
	h->on_get = collect_InResp;
	if(f->reg_func("sipSummaryInResponses", h) == -1) {
		LM_ERR("%s: Error registering sipSummaryInResponses\n", func);
		f->free_func(h);
		return -1;
	}
	
	/* sipSummaryOutResponses */
	h->on_get = collect_OutResp;
	if(f->reg_func("sipSummaryOutResponses", h) == -1) {
		LM_ERR("%s: Error registering sipSummaryOutResponses\n", func);
		f->free_func(h);
		return -1;
	}

	f->free_func(h);

	return 0;
}

static int sipMethodStatsTable_register(const struct stats_funcs *f)
{
	const char* objs[] = {
		"sipStatsInviteIns",
		"sipStatsInviteOuts",
		"sipStatsAckIns",
		"sipStatsAckOuts",
		"sipStatsByeIns",
		"sipStatsByeOuts",
		"sipStatsCancelIns",
		"sipStatsCancelOuts"
#if 0	/* we don't know about these */
		"sipStatsOptionsIns",
		"sipStatsOptionsOuts",
		"sipStatsRegisterIns",
		"sipStatsRegisterOuts",
		"sipStatsInfoIns",
		"sipStatsInfoOuts"
#endif
	};
	int i, num = 8;
	const char *func = __FUNCTION__;
	register struct sip_snmp_handler *h;
	register struct sip_snmp_obj *o;

	h = f->new_func(sizeof(unsigned long));
	if(!h) {
		LM_ERR("%s: Error creating handler\n", func);
		return -1;
	}
	o = h->sip_obj;
	o->type = SER_COUNTER;
	*o->value.integer = 0;
	o->val_len = sizeof(unsigned long);

	h->on_get = sipStatsMethod_handler;
	h->on_set = h->on_end = NULL;

	for(i=0; i<num; i++) {
		if(f->reg_func(objs[i], h) == -1) {
			LM_ERR("%s: Error registering %s\n", func, objs[i]);
			f->free_func(h);
			return -1;
		}
	}

	f->free_func(h);

	return 0;
}

static int sipStatusCodesTable_register(const struct stats_funcs *f)
{
	const char *objs[] = {
		"sipStatsInfoClassIns",
		"sipStatsInfoClassOuts",
		"sipStatsSuccessClassIns",
		"sipStatsSuccessClassOuts",
		"sipStatsRedirClassIns",
		"sipStatsRedirClassOuts",
		"sipStatsReqFailClassIns",
		"sipStatsReqFailClassOuts",
		"sipStatsServerFailClassIns",
		"sipStatsServerFailClassOuts",
		"sipStatsGlobalFailClassIns",
		"sipStatsGlobalFailClassOuts",
		"sipStatsOtherClassesIns",
		"sipStatsOtherClassesOuts"
	};
	int i, num = 14;
	const char *func = __FUNCTION__;
	register struct sip_snmp_handler *h;
	register struct sip_snmp_obj *o;

	h = f->new_func(sizeof(unsigned long));
	if(!h) {
		LM_ERR("%s: Error creating handler\n", func);
		return -1;
	}
	o = h->sip_obj;
	o->type = SER_COUNTER;
	*o->value.integer = 0;
	o->val_len = sizeof(unsigned long);

	h->on_get = sipStatusCodes_handler;
	h->on_set = h->on_end = NULL;

	for(i=0; i<num; i++) {
		if(f->reg_func(objs[i], h) == -1) {
			LM_ERR("%s: Error registering %s\n", func, objs[i]);
			f->free_func(h);
			return -1;
		}
	}

	f->free_func(h);

	return 0;}

/* ########################## SNMP Handlers ######################### */

/*** Handlers for sipSummaryStatsTable */
static int collect_InReqs(struct sip_snmp_obj *o, enum handler_op op)
{
	register int i;
	register struct stats_s *c;
	register unsigned long t1, t2, t3, t4, t5;
	const char *func = __FUNCTION__;

	if(!global_stats || stats_segments == -1) {
		LM_ERR("%s: Can't collect stats, they have not been initialized."
			"Did you call init_stats()?\n", func);
		return -1;
	}

	if(op != SER_GET) {
		LM_ERR("%s: Invalid handler operation passed\n", func);
		return -1;
	}

	if(!o->value.integer) {
		o->value.integer = calloc(1, sizeof(unsigned long));
		if(!o->value.integer) {
			LM_ERR("%s: %s\n", func, strerror(errno));
			return -1;
		}
	}

	c = global_stats;
	t1 = t2 = t3 = t4 = t5 = 0;
	for(i=0; i<stats_segments; i++, c++) {
		t1 += c->received_requests_inv;
		t2 += c->received_requests_ack;
		t3 += c->received_requests_cnc;
		t4 += c->received_requests_bye;
		t5 += c->received_requests_other;
	}

	*o->value.integer = t1 + t2 + t3 + t4 + t5; 
	o->val_len = sizeof(unsigned long);
	o->type = SER_COUNTER;
	
	return 0;
}

static int collect_OutReqs(struct sip_snmp_obj *o, enum handler_op op)
{
	register int i;
	register struct stats_s *c;
	register unsigned long t1, t2, t3, t4, t5;
	const char *func = __FUNCTION__;

	if(!global_stats || stats_segments == -1) {
		LM_ERR("%s: Can't collect stats, they have not been initialized."
			"Did you call init_stats()?\n", func);
		return -1;
	}

	if(op != SER_GET) {
		LM_ERR("%s: Invalid handler operation passed\n", func);
		return -1;
	}

	if(!o->value.integer) {
		o->value.integer = calloc(1, sizeof(unsigned long));
		if(!o->value.integer) {
			LM_ERR("%s: %s\n", func, strerror(errno));
			return -1;
		}
	}

	c = global_stats;
	t1 = t2 = t3 = t4 = t5 = 0;
	for(i=0; i<stats_segments; i++, c++) {
		t1 += c->sent_requests_inv;
		t2 += c->sent_requests_ack;
		t3 += c->sent_requests_cnc;
		t4 += c->sent_requests_bye;
		t5 += c->sent_requests_other;
	}

	*o->value.integer = t1 + t2 + t3 + t4 + t5; 
	o->val_len = sizeof(unsigned long);
	o->type = SER_COUNTER;
	
	return 0;
}

static int collect_InResp(struct sip_snmp_obj *o, enum handler_op op)
{
	register int i;
	register struct stats_s *c;
	register unsigned long t1, t2, t3, t4, t5, t6, t7;
	const char *func = __FUNCTION__;

	if(!global_stats || stats_segments == -1) {
		LM_ERR("%s: Can't collect stats, they have not been initialized."
			"Did you call init_stats()?\n", func);
		return -1;
	}

	if(op != SER_GET) {
		LM_ERR("%s: Invalid handler operation passed\n", func);
		return -1;
	}

	if(!o->value.integer) {
		o->value.integer = calloc(1, sizeof(unsigned long));
		if(!o->value.integer) {
			LM_ERR("%s: %s\n", func, strerror(errno));
			return -1;
		}
	}

	c = global_stats;
	t1 = t2 = t3 = t4 = t5 = t6 = t7 = 0;
	for(i=0; i<stats_segments; i++, c++) {
		t1 += c->received_responses_1;
		t2 += c->received_responses_2;
		t3 += c->received_responses_3;
		t4 += c->received_responses_4;
		t5 += c->received_responses_5;
		t6 += c->received_responses_6;
		t7 += c->received_responses_other;
	}

	*o->value.integer = t1 + t2 + t3 + t4 + t5 + t6 + t7; 
	o->val_len = sizeof(unsigned long);
	o->type = SER_COUNTER;
	
	return 0;
}

static int collect_OutResp(struct sip_snmp_obj *o, enum handler_op op)
{
	register int i;
	register struct stats_s *c;
	register unsigned long t1, t2, t3, t4, t5, t6, t7;
	const char *func = __FUNCTION__;

	if(!global_stats || stats_segments == -1) {
		LM_ERR("%s: Can't collect stats, they have not been initialized\n",
			func);
		return -1;
	}

	if(op != SER_GET) {
		LM_ERR("%s: Invalid handler operation passed\n", func);
		return -1;
	}

	if(!o->value.integer) {
		o->value.integer = calloc(1, sizeof(unsigned long));
		if(!o->value.integer) {
			LM_ERR("%s: %s\n", func, strerror(errno));
			return -1;
		}
	}

	c = global_stats;
	t1 = t2 = t3 = t4 = t5 = t6 = t7 = 0;
	for(i=0; i<stats_segments; i++, c++) {
		t1 += c->sent_responses_1;
		t2 += c->sent_responses_2;
		t3 += c->sent_responses_3;
		t4 += c->sent_responses_4;
		t5 += c->sent_responses_5;
		t6 += c->sent_responses_6;
		/* XXX: Not in stats struct 
		t7 += c->sent_responses_other;
		*/
	}

	*o->value.integer = t1 + t2 + t3 + t4 + t5 + t6 + t7; 
	o->val_len = sizeof(unsigned long);
	o->type = SER_COUNTER;
	
	return 0;
}

/***** Handlers for sipMethodStatsTable ******/
/* Collects the specified stat and puts the result in total. s defines
 * the starting point in the stats array, normally global_stats */
#define collect_this_stat(stat, total, s) \
	for(i=0; i<stats_segments; i++)	\
		total += s++->stat;

static int sipStatsMethod_handler(struct sip_snmp_obj *o, enum handler_op op)
{
	register struct stats_s *c;
	register unsigned long total;
	register int i;
	const char *func = __FUNCTION__;

	if(!o) {
		LM_ERR("%s: Invalid sip SNMP object passed\n", func);
		return -1;
	}

	if(!global_stats || stats_segments == -1) {
		LM_ERR("%s: Can't collect stats, they have not been initialized\n",
			func);
		return -1;
	}

	if(op != SER_GET) {
		LM_ERR("%s: Invalid handler operation passed\n", func);
		return -1;
	}

	if(!o->value.integer) {
		o->value.integer = calloc(1, sizeof(unsigned long));
		if(!o->value.integer) {
			LM_ERR("%s: %s\n", func, strerror(errno));
			return -1;
		}
	}

	c = global_stats;
	total = 0;
	switch(o->col) {
		/* these definitions are taken from sipMethodStatsHandler */
		case COLUMN_SIPSTATSINVITEINS: 
			collect_this_stat(received_requests_inv, total, c);
			break;
		case COLUMN_SIPSTATSINVITEOUTS: 
			collect_this_stat(sent_requests_inv, total, c);
			break;
		case COLUMN_SIPSTATSACKINS: 
			collect_this_stat(received_requests_ack, total, c);
			break;
		case COLUMN_SIPSTATSACKOUTS: 
			collect_this_stat(sent_requests_ack, total, c);
			break;
		case COLUMN_SIPSTATSBYEINS: 
			collect_this_stat(received_requests_bye, total, c);
			break;
		case COLUMN_SIPSTATSBYEOUTS: 
			collect_this_stat(sent_requests_bye, total, c);
			break;
		case COLUMN_SIPSTATSCANCELINS: 
			collect_this_stat(received_requests_cnc, total, c);
			break;
		case COLUMN_SIPSTATSCANCELOUTS: 
			collect_this_stat(sent_requests_cnc, total, c);
			break;
		/* ser doesn't have notion for these. We don't
		 * register them with snmp. Here just as remainder */
#if 0
		case COLUMN_SIPSTATSOPTIONSINS:
		case COLUMN_SIPSTATSOPTIONSOUTS:
		case COLUMN_SIPSTATSREGISTERINS:
		case COLUMN_SIPSTATSREGISTEROUTS:
		case COLUMN_SIPSTATSINFOINS:
		case COLUMN_SIPSTATSINFOOUTS:
			break;
#endif
	}

	*o->value.integer = total;
	o->val_len = sizeof(unsigned long);
	o->type = SER_COUNTER;

	return 0;
}

static int sipStatusCodes_handler(struct sip_snmp_obj *o, enum handler_op op)
{
	register struct stats_s *c;
	register unsigned long total;
	register int i;
	const char *func = __FUNCTION__;

	if(!o) {
		LM_ERR("%s: Invalid sip SNMP object passed\n", func);
		return -1;
	}

	if(!global_stats || stats_segments == -1) {
		LM_ERR("%s: Can't collect stats, they have not been initialized\n",
			func);
		return -1;
	}

	if(op != SER_GET) {
		LM_ERR("%s: Invalid handler operation passed\n", func);
		return -1;
	}

	if(!o->value.integer) {
		o->value.integer = calloc(1, sizeof(unsigned long));
		if(!o->value.integer) {
			LM_ERR("%s: %s\n", func, strerror(errno));
			return -1;
		}
	}

	c = global_stats;
	total = 0;
	switch(o->col) {
		case COLUMN_SIPSTATSINFOCLASSINS:
			collect_this_stat(received_responses_1, total, c);
			break;
		case COLUMN_SIPSTATSINFOCLASSOUTS:
			collect_this_stat(sent_responses_1, total, c);
			break;
		case COLUMN_SIPSTATSSUCCESSCLASSINS:
			collect_this_stat(received_responses_2, total, c);
			break;
		case COLUMN_SIPSTATSSUCCESSCLASSOUTS:
			collect_this_stat(sent_responses_2, total, c);
			break;
		case COLUMN_SIPSTATSREDIRCLASSINS:
			collect_this_stat(received_responses_3, total, c);
			break;
		case COLUMN_SIPSTATSREDIRCLASSOUTS:
			collect_this_stat(sent_responses_3, total, c);
			break;
		case COLUMN_SIPSTATSREQFAILCLASSINS:
			collect_this_stat(received_responses_4, total, c);
			break;
		case COLUMN_SIPSTATSREQFAILCLASSOUTS:
			collect_this_stat(sent_responses_4, total, c);
			break;
		case COLUMN_SIPSTATSSERVERFAILCLASSINS:
			collect_this_stat(received_responses_5, total, c);
			break;
		case COLUMN_SIPSTATSSERVERFAILCLASSOUTS:
			collect_this_stat(sent_responses_5, total, c);
			break;
		case COLUMN_SIPSTATSGLOBALFAILCLASSINS:
			collect_this_stat(received_responses_6, total, c);
			break;
		case COLUMN_SIPSTATSGLOBALFAILCLASSOUTS:
			collect_this_stat(sent_responses_6, total, c);
			break;
		case COLUMN_SIPSTATSOTHERCLASSESINS:
			collect_this_stat(received_responses_other, total, c);
			break;
		case COLUMN_SIPSTATSOTHERCLASSESOUTS:
			/* FIXME: For some reason this is not defined in
			 * struct stats_s... */
			/* collect_this_stat(sent_responses_other, total, c); */
			total = 0;
			break;
	}

	*o->value.integer = total;
	o->val_len = sizeof(unsigned long);
	o->type = SER_COUNTER;

	return 0;
}

#endif