- Spelling checked
[sip-router] / stats.c
1 /* 
2  * $Id$
3  *
4  * Stats reporting code. It reports through SIG_USR1 and if loaded
5  * through the SNMP module
6  *
7  * Copyright (C) 2001-2003 FhG Fokus
8  *
9  * This file is part of ser, a free SIP server.
10  *
11  * ser is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version
15  *
16  * For a license to use the ser software under conditions
17  * other than those described here, or to purchase support for this
18  * software, please contact iptel.org by e-mail at the following addresses:
19  *    info@iptel.org
20  *
21  * ser is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License 
27  * along with this program; if not, write to the Free Software 
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29  *
30  * History:
31  * -------
32  * 2003-03-19: added support for route type in find_export (janakj)
33  */
34
35
36 #ifdef STATS
37 #include "stats.h"
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <time.h>
42 #include <sys/types.h>
43 #include <unistd.h>
44 #include "dprint.h"
45 #include "mem/shm_mem.h"
46 #include "sr_module.h"
47
48 /* SNMP includes */
49 #include "modules/snmp/snmp_handler.h"
50 #include "modules/snmp/sipCommonStatsMethod.h"
51 #include "modules/snmp/sipCommonStatusCode.h"
52
53 struct stats_s *stats;          /* per process stats structure */ 
54 char *stat_file = NULL;         /* set by the parser */
55
56 /* private variables */
57 static struct stats_s *global_stats=NULL;
58 static int stats_segments=-1;   /*used also to determine if we've been init'ed*/
59
60 /* adds up global statistics and puts them into passed struct.
61  * -1 returned on failure */
62 static int collect_stats(struct stats_s *s);
63
64 /***********************8 SNMP Stuff **************************/
65 /* a small structure we use to pass around the functions needed by
66  * all the registration functions */
67 struct stats_funcs {
68         int (*reg_func)(const char *, struct sip_snmp_handler*);
69         struct sip_snmp_handler* (*new_func)(size_t);
70         void (*free_func)(struct sip_snmp_handler*);
71 };
72
73 /* SNMP Handler registration functions */
74 static int sipSummaryStatsTable_register(const struct stats_funcs *f);
75 static int sipMethodStatsTable_register(const struct stats_funcs *f);
76 static int sipStatusCodesTable_register(const struct stats_funcs *f);
77
78 /* the handlers */
79 static int collect_InReqs(struct sip_snmp_obj *, enum handler_op);
80 static int collect_OutReqs(struct sip_snmp_obj *, enum handler_op);
81 static int collect_InResp(struct sip_snmp_obj *, enum handler_op);
82 static int collect_OutResp(struct sip_snmp_obj *, enum handler_op);
83 static int sipStatsMethod_handler(struct sip_snmp_obj *o, enum handler_op op);
84 static int sipStatusCodes_handler(struct sip_snmp_obj *o, enum handler_op op);
85
86 int init_stats(int nr_of_processes)
87 {
88         LOG(L_DBG,"init_stats(): initializing stats for %d processes\n", 
89                 nr_of_processes);
90
91
92         global_stats = shm_malloc(nr_of_processes*sizeof(struct stats_s));
93         if(!global_stats) {
94                 LOG(L_ERR, "Out of memory\n");
95                 return -1;
96         }
97         stats_segments = nr_of_processes;
98
99         if(stats_register() == -1)
100                 LOG(L_WARN, "init_stats(): Couldn't register stats"
101                                         " with snmp module\n");
102
103
104         return 0;
105 }
106
107 /* sets the stats pointer for the passed process */
108 void setstats(int child_index)
109 {
110         if(stats_segments == -1 || !global_stats) {
111                 LOG(L_ERR, "Stats not initialized. Cannot set them\n");
112                 stats = NULL;
113                 return;
114         }
115         if(child_index < 0 || child_index >= stats_segments) {
116                 stats = NULL;
117                 LOG(L_ERR, "Invalid index %d while setting statistics. Only have "
118                         "space for %d processes\n", child_index, stats_segments);
119                 return;
120         }
121         stats = global_stats+child_index;
122         stats->process_index = child_index;
123         /* can't use pids[] because we may be called before the corresponding
124          * slot in pids[] is initialized (chk main_loop()) */
125         stats->pid = getpid();
126         stats->start_time = time(NULL);
127 }
128
129 /* printheader is used to print pid, date and index */
130 int dump_statistic(FILE *fp, struct stats_s *istats, int printheader)
131 {
132         struct tm res;
133         char t[256];
134         if(stats_segments == -1 || !global_stats) {
135                 LOG(L_ERR, "Stats \"engine\" not initialized\n");
136                 return -1;
137         }
138
139         if(printheader) {
140                 localtime_r(&istats->start_time, &res);
141                 strftime(t, 255, "%c", &res);
142                 
143                 fprintf(fp, "stats for process %d (pid %d) started at %s\n", 
144                                 istats->process_index, istats->pid, t);
145         }
146
147         fprintf(fp, "received requests:\ninv: %ld\tack: %ld\tcnc: %ld\t"
148                 "bye: %ld\tother: %ld\n",
149                 istats->received_requests_inv,
150                 istats->received_requests_ack,
151                 istats->received_requests_cnc,
152                 istats->received_requests_bye,
153                 istats->received_requests_other);
154         fprintf(fp, "sent requests:\n"
155                 "inv: %ld\tack: %ld\tcnc: %ld\tbye: %ld\tother: %ld\n",
156                 istats->sent_requests_inv,
157                 istats->sent_requests_ack,
158                 istats->sent_requests_cnc,
159                 istats->sent_requests_bye,
160                 istats->sent_requests_other);
161         fprintf(fp, "received responses:\n"
162                 "1: %ld\t2: %ld\t3: %ld\t4: %ld\t5: %ld\t6: %ld\tother: %ld\t"
163                 "drops: %ld\n",
164                 istats->received_responses_1,
165                 istats->received_responses_2,
166                 istats->received_responses_3,
167                 istats->received_responses_4,
168                 istats->received_responses_5,
169                 istats->received_responses_6,
170                 istats->received_responses_other,
171                 istats->received_drops);
172         fprintf(fp, "sent responses:\n"
173                 "1: %ld\t2: %ld\t3: %ld\t4: %ld\t5: %ld\t6: %ld\n",
174                 istats->sent_responses_1,
175                 istats->sent_responses_2,
176                 istats->sent_responses_3,
177                 istats->sent_responses_4,
178                 istats->sent_responses_5,
179                 istats->sent_responses_6);
180         fprintf(fp, "processed requests: %ld\t\tprocessed responses: %ld\n"
181                 "acc req time: %ld\t\t\tacc res time: %ld\nfailed on send: %ld\n\n",
182                 istats->processed_requests,
183                 istats->processed_responses,
184                 istats->acc_req_time,
185                 istats->acc_res_time,
186                 istats->failed_on_send);
187         return 0;
188 }
189
190 int dump_all_statistic()
191 {
192         register int i;
193         register struct stats_s *c;
194         static struct stats_s *g = NULL;
195         struct tm res;
196         char t[256];
197         time_t ts;
198         FILE *stat_fp = NULL;
199
200         if(stats_segments == -1 || !global_stats) {
201                 LOG(L_ERR, "%s: Can't dump statistics, not initialized!\n", __func__);
202                 return -1;
203         }
204
205         if(!stat_file) {
206                 LOG(L_ERR, "%s: Can't dump statistics, invalid stats file\n", __func__);
207                 return -1;
208         }
209
210         stat_fp = fopen(stat_file, "a");
211         if(!stat_fp) {
212                 LOG(L_ERR, "%s: Couldn't open stats file %s: %s\n", __func__, stat_file,
213                                 strerror(errno));
214                 return -1;
215         }
216
217         /* time stamp them since we're appending to the file */
218         ts = time(NULL);
219         localtime_r(&ts, &res);
220         strftime(t, 255, "%c", &res);
221         fprintf(stat_fp, "#### stats @ %s #####\n", t); 
222
223         c = global_stats;
224         for(i=0; i<stats_segments; i++) {
225                 if(dump_statistic(stat_fp, c, 1) == -1) {
226                         LOG(L_ERR, "Error dumping statistics for process %d\n", i);
227                         goto end;
228                 }
229                 c++;
230         }
231
232         fprintf(stat_fp, "## Global Stats ##\n");
233         if(!g)
234                 g = calloc(1, sizeof(struct stats_s));
235         if(!g) {
236                 LOG(L_ERR, "Couldn't dump global stats: %s\n", strerror(errno));
237                 goto end;
238         }
239         
240         if(collect_stats(g) == -1) {
241                 LOG(L_ERR, "%s: Couldn't dump global stats\n", __func__);
242                 goto end;
243         }
244         if(dump_statistic(stat_fp, g, 0) == -1) {
245                 LOG(L_ERR, "Couldn't dump global stats\n");
246                 goto end;
247         }
248 end:
249         fprintf(stat_fp, "\n");
250         fclose(stat_fp);
251
252         return 0;
253 }
254
255 static int collect_stats(struct stats_s *s)
256 {
257         register int i;
258         register struct stats_s *c;
259         if(!s) {
260                 LOG(L_ERR, "collect_stats(): Invalid stats pointer passed\n");
261                 return -1;
262         }
263         if(!global_stats || stats_segments == -1) {
264                 LOG(L_ERR, "Can't collect statistics, not initialized!!\n");
265                 return -1;
266         }
267
268         c = global_stats;
269         memset(s, '\0', sizeof(struct stats_s));
270         for(i=0; i<stats_segments; i++) {
271                 s->received_requests_inv += c->received_requests_inv;
272                 s->received_requests_ack += c->received_requests_ack;
273                 s->received_requests_cnc += c->received_requests_cnc;
274                 s->received_requests_bye += c->received_requests_bye;
275                 s->received_requests_other += c->received_requests_other;
276                 s->received_responses_1 += c->received_responses_1;
277                 s->received_responses_2 += c->received_responses_2;
278                 s->received_responses_3 += c->received_responses_3;
279                 s->received_responses_4 += c->received_responses_4;
280                 s->received_responses_5 += c->received_responses_5;
281                 s->received_responses_6 += c->received_responses_6;
282                 s->received_responses_other += c->received_responses_other;
283                 s->received_drops += c->received_drops;
284                 s->sent_requests_inv += c->sent_requests_inv;
285                 s->sent_requests_ack += c->sent_requests_ack;
286                 s->sent_requests_cnc += c->sent_requests_cnc;
287                 s->sent_requests_bye += c->sent_requests_bye;
288                 s->sent_requests_other += c->sent_requests_other;
289                 s->sent_responses_1 += c->sent_responses_1;
290                 s->sent_responses_2 += c->sent_responses_2;
291                 s->sent_responses_3 += c->sent_responses_3;
292                 s->sent_responses_4 += c->sent_responses_4;
293                 s->sent_responses_5 += c->sent_responses_5;
294                 s->sent_responses_6 += c->sent_responses_6;
295                 s->processed_requests += c->processed_requests;
296                 s->processed_responses += c->processed_responses;
297                 s->acc_req_time += c->acc_req_time;
298                 s->acc_res_time += c->acc_res_time;
299                 s->failed_on_send += c->failed_on_send;
300
301                 c++; /* next, please... */
302         }
303
304         return 0;
305 }
306
307 /*************************** SNMP Stuff ***********************/
308
309 /* ##### Registration Functions ####### */
310
311 /* Registers the handlers for:
312  * - sipSummaryStatsTable
313  * - sipMethodStatsTable
314  * - sipStatusCodesTable
315  * - sipCommonStatusCodeTable
316  *
317  * Returns 0 if snmp module not present, -1 on error, 1 on successful
318  * registration
319  */
320
321 #define reg(t) \
322         if(t##_register(&f) == -1) {    \
323                 LOG(L_ERR, "%s: Failed registering SNMP handlers\n", func);     \
324                 return -1;      \
325         }
326
327 int stats_register()
328 {
329         const char *func = __FUNCTION__;
330         struct stats_funcs f;
331
332         f.reg_func = (void*) find_export("snmp_register_handler", 2, 0);
333         f.new_func = (void*) find_export("snmp_new_handler", 1, 0);
334         f.free_func = (void*) find_export("snmp_free_handler", 1, 0);
335         if(!f.reg_func || !f.new_func || !f.free_func) {
336                 LOG(L_INFO, "%s: Couldn't find SNMP module\n", func);
337                 LOG(L_INFO, "%s: Not reporting stats through SNMP\n", func);
338                 return 0;
339         }
340
341         reg(sipSummaryStatsTable);
342         reg(sipMethodStatsTable);
343         reg(sipStatusCodesTable);
344
345         return 0;
346 }
347
348 /* Receives the function used to register SNMP handlers. Returns 0
349  * on success, -1 on error */
350 /* Registers:
351  * - sipSummaryInRequests
352  * - sipSummaryOutRequests
353  * - sipSummaryInResponses
354  * - sipSummaryOutResponses
355  * => sipSummaryTotalTransactions is handled by the tm module */
356 static int sipSummaryStatsTable_register(const struct stats_funcs *f)
357 {
358         register struct sip_snmp_handler *h;
359         register struct sip_snmp_obj *o;
360         const char *func = __FUNCTION__;
361
362         h = f->new_func(sizeof(unsigned long));
363         if(!h) {
364                 LOG(L_ERR, "%s: Error creating handler\n", func);
365                 return -1;
366         }
367         o = h->sip_obj;
368
369         /* this is the same for all of our objects */
370         o->type = SER_COUNTER;
371         *o->value.integer = 0;  /* default value. The real one is computed on
372                                                            request */
373         o->val_len = sizeof(unsigned long);
374
375         /* sipSummaryInRequests */
376         h->on_get = collect_InReqs;
377         h->on_set = h->on_end = NULL;
378         if(f->reg_func("sipSummaryInRequests", h) == -1) {
379                 LOG(L_ERR, "%s: Error registering sipSummaryInRequests\n", func);
380                 f->free_func(h);
381                 return -1;
382         }
383
384         /* sipSummaryOutRequests */
385         h->on_get = collect_OutReqs;
386         if(f->reg_func("sipSummaryOutRequests", h) == -1) {
387                 LOG(L_ERR, "%s: Error registering sipSummaryOutRequests\n", func);
388                 f->free_func(h);
389                 return -1;
390         }
391
392         /* sipSummaryInResponses */
393         h->on_get = collect_InResp;
394         if(f->reg_func("sipSummaryInResponses", h) == -1) {
395                 LOG(L_ERR, "%s: Error registering sipSummaryInResponses\n", func);
396                 f->free_func(h);
397                 return -1;
398         }
399         
400         /* sipSummaryOutResponses */
401         h->on_get = collect_OutResp;
402         if(f->reg_func("sipSummaryOutResponses", h) == -1) {
403                 LOG(L_ERR, "%s: Error registering sipSummaryOutResponses\n", func);
404                 f->free_func(h);
405                 return -1;
406         }
407
408         f->free_func(h);
409
410         return 0;
411 }
412
413 static int sipMethodStatsTable_register(const struct stats_funcs *f)
414 {
415         const char* objs[] = {
416                 "sipStatsInviteIns",
417                 "sipStatsInviteOuts",
418                 "sipStatsAckIns",
419                 "sipStatsAckOuts",
420                 "sipStatsByeIns",
421                 "sipStatsByeOuts",
422                 "sipStatsCancelIns",
423                 "sipStatsCancelOuts"
424 #if 0   /* we don't know about these */
425                 "sipStatsOptionsIns",
426                 "sipStatsOptionsOuts",
427                 "sipStatsRegisterIns",
428                 "sipStatsRegisterOuts",
429                 "sipStatsInfoIns",
430                 "sipStatsInfoOuts"
431 #endif
432         };
433         int i, num = 8;
434         const char *func = __FUNCTION__;
435         register struct sip_snmp_handler *h;
436         register struct sip_snmp_obj *o;
437
438         h = f->new_func(sizeof(unsigned long));
439         if(!h) {
440                 LOG(L_ERR, "%s: Error creating handler\n", func);
441                 return -1;
442         }
443         o = h->sip_obj;
444         o->type = SER_COUNTER;
445         *o->value.integer = 0;
446         o->val_len = sizeof(unsigned long);
447
448         h->on_get = sipStatsMethod_handler;
449         h->on_set = h->on_end = NULL;
450
451         for(i=0; i<num; i++) {
452                 if(f->reg_func(objs[i], h) == -1) {
453                         LOG(L_ERR, "%s: Error registering %s\n", func, objs[i]);
454                         f->free_func(h);
455                         return -1;
456                 }
457         }
458
459         f->free_func(h);
460
461         return 0;
462 }
463
464 static int sipStatusCodesTable_register(const struct stats_funcs *f)
465 {
466         const char *objs[] = {
467                 "sipStatsInfoClassIns",
468                 "sipStatsInfoClassOuts",
469                 "sipStatsSuccessClassIns",
470                 "sipStatsSuccessClassOuts",
471                 "sipStatsRedirClassIns",
472                 "sipStatsRedirClassOuts",
473                 "sipStatsReqFailClassIns",
474                 "sipStatsReqFailClassOuts",
475                 "sipStatsServerFailClassIns",
476                 "sipStatsServerFailClassOuts",
477                 "sipStatsGlobalFailClassIns",
478                 "sipStatsGlobalFailClassOuts",
479                 "sipStatsOtherClassesIns",
480                 "sipStatsOtherClassesOuts"
481         };
482         int i, num = 14;
483         const char *func = __FUNCTION__;
484         register struct sip_snmp_handler *h;
485         register struct sip_snmp_obj *o;
486
487         h = f->new_func(sizeof(unsigned long));
488         if(!h) {
489                 LOG(L_ERR, "%s: Error creating handler\n", func);
490                 return -1;
491         }
492         o = h->sip_obj;
493         o->type = SER_COUNTER;
494         *o->value.integer = 0;
495         o->val_len = sizeof(unsigned long);
496
497         h->on_get = sipStatusCodes_handler;
498         h->on_set = h->on_end = NULL;
499
500         for(i=0; i<num; i++) {
501                 if(f->reg_func(objs[i], h) == -1) {
502                         LOG(L_ERR, "%s: Error registering %s\n", func, objs[i]);
503                         f->free_func(h);
504                         return -1;
505                 }
506         }
507
508         f->free_func(h);
509
510         return 0;}
511
512 /* ########################## SNMP Handlers ######################### */
513
514 /*** Handlers for sipSummaryStatsTable */
515 static int collect_InReqs(struct sip_snmp_obj *o, enum handler_op op)
516 {
517         register int i;
518         register struct stats_s *c;
519         register unsigned long t1, t2, t3, t4, t5;
520         const char *func = __FUNCTION__;
521
522         if(!global_stats || stats_segments == -1) {
523                 LOG(L_ERR, "%s: Can't collect stats, they have not been initialized."
524                         "Did you call init_stats()?\n", func);
525                 return -1;
526         }
527
528         if(op != SER_GET) {
529                 LOG(L_ERR, "%s: Invalid handler operation passed\n", func);
530                 return -1;
531         }
532
533         if(!o->value.integer) {
534                 o->value.integer = calloc(1, sizeof(unsigned long));
535                 if(!o->value.integer) {
536                         LOG(L_ERR, "%s: %s\n", func, strerror(errno));
537                         return -1;
538                 }
539         }
540
541         c = global_stats;
542         t1 = t2 = t3 = t4 = t5 = 0;
543         for(i=0; i<stats_segments; i++, c++) {
544                 t1 += c->received_requests_inv;
545                 t2 += c->received_requests_ack;
546                 t3 += c->received_requests_cnc;
547                 t4 += c->received_requests_bye;
548                 t5 += c->received_requests_other;
549         }
550
551         *o->value.integer = t1 + t2 + t3 + t4 + t5; 
552         o->val_len = sizeof(unsigned long);
553         o->type = SER_COUNTER;
554         
555         return 0;
556 }
557
558 static int collect_OutReqs(struct sip_snmp_obj *o, enum handler_op op)
559 {
560         register int i;
561         register struct stats_s *c;
562         register unsigned long t1, t2, t3, t4, t5;
563         const char *func = __FUNCTION__;
564
565         if(!global_stats || stats_segments == -1) {
566                 LOG(L_ERR, "%s: Can't collect stats, they have not been initialized."
567                         "Did you call init_stats()?\n", func);
568                 return -1;
569         }
570
571         if(op != SER_GET) {
572                 LOG(L_ERR, "%s: Invalid handler operation passed\n", func);
573                 return -1;
574         }
575
576         if(!o->value.integer) {
577                 o->value.integer = calloc(1, sizeof(unsigned long));
578                 if(!o->value.integer) {
579                         LOG(L_ERR, "%s: %s\n", func, strerror(errno));
580                         return -1;
581                 }
582         }
583
584         c = global_stats;
585         t1 = t2 = t3 = t4 = t5 = 0;
586         for(i=0; i<stats_segments; i++, c++) {
587                 t1 += c->sent_requests_inv;
588                 t2 += c->sent_requests_ack;
589                 t3 += c->sent_requests_cnc;
590                 t4 += c->sent_requests_bye;
591                 t5 += c->sent_requests_other;
592         }
593
594         *o->value.integer = t1 + t2 + t3 + t4 + t5; 
595         o->val_len = sizeof(unsigned long);
596         o->type = SER_COUNTER;
597         
598         return 0;
599 }
600
601 static int collect_InResp(struct sip_snmp_obj *o, enum handler_op op)
602 {
603         register int i;
604         register struct stats_s *c;
605         register unsigned long t1, t2, t3, t4, t5, t6, t7;
606         const char *func = __FUNCTION__;
607
608         if(!global_stats || stats_segments == -1) {
609                 LOG(L_ERR, "%s: Can't collect stats, they have not been initialized."
610                         "Did you call init_stats()?\n", func);
611                 return -1;
612         }
613
614         if(op != SER_GET) {
615                 LOG(L_ERR, "%s: Invalid handler operation passed\n", func);
616                 return -1;
617         }
618
619         if(!o->value.integer) {
620                 o->value.integer = calloc(1, sizeof(unsigned long));
621                 if(!o->value.integer) {
622                         LOG(L_ERR, "%s: %s\n", func, strerror(errno));
623                         return -1;
624                 }
625         }
626
627         c = global_stats;
628         t1 = t2 = t3 = t4 = t5 = t6 = t7 = 0;
629         for(i=0; i<stats_segments; i++, c++) {
630                 t1 += c->received_responses_1;
631                 t2 += c->received_responses_2;
632                 t3 += c->received_responses_3;
633                 t4 += c->received_responses_4;
634                 t5 += c->received_responses_5;
635                 t6 += c->received_responses_6;
636                 t7 += c->received_responses_other;
637         }
638
639         *o->value.integer = t1 + t2 + t3 + t4 + t5 + t6 + t7; 
640         o->val_len = sizeof(unsigned long);
641         o->type = SER_COUNTER;
642         
643         return 0;
644 }
645
646 static int collect_OutResp(struct sip_snmp_obj *o, enum handler_op op)
647 {
648         register int i;
649         register struct stats_s *c;
650         register unsigned long t1, t2, t3, t4, t5, t6, t7;
651         const char *func = __FUNCTION__;
652
653         if(!global_stats || stats_segments == -1) {
654                 LOG(L_ERR, "%s: Can't collect stats, they have not been initialized\n",
655                         func);
656                 return -1;
657         }
658
659         if(op != SER_GET) {
660                 LOG(L_ERR, "%s: Invalid handler operation passed\n", func);
661                 return -1;
662         }
663
664         if(!o->value.integer) {
665                 o->value.integer = calloc(1, sizeof(unsigned long));
666                 if(!o->value.integer) {
667                         LOG(L_ERR, "%s: %s\n", func, strerror(errno));
668                         return -1;
669                 }
670         }
671
672         c = global_stats;
673         t1 = t2 = t3 = t4 = t5 = t6 = t7 = 0;
674         for(i=0; i<stats_segments; i++, c++) {
675                 t1 += c->sent_responses_1;
676                 t2 += c->sent_responses_2;
677                 t3 += c->sent_responses_3;
678                 t4 += c->sent_responses_4;
679                 t5 += c->sent_responses_5;
680                 t6 += c->sent_responses_6;
681                 /* XXX: Not in stats struct 
682                 t7 += c->sent_responses_other;
683                 */
684         }
685
686         *o->value.integer = t1 + t2 + t3 + t4 + t5 + t6 + t7; 
687         o->val_len = sizeof(unsigned long);
688         o->type = SER_COUNTER;
689         
690         return 0;
691 }
692
693 /***** Handlers for sipMethodStatsTable ******/
694 /* Collects the specified stat and puts the result in total. s defines
695  * the starting point in the stats array, normally global_stats */
696 #define collect_this_stat(stat, total, s) \
697         for(i=0; i<stats_segments; i++) \
698                 total += s++->stat;
699
700 static int sipStatsMethod_handler(struct sip_snmp_obj *o, enum handler_op op)
701 {
702         register struct stats_s *c;
703         register unsigned long total;
704         register int i;
705         const char *func = __FUNCTION__;
706
707         if(!o) {
708                 LOG(L_ERR, "%s: Invalid sip SNMP object passed\n", func);
709                 return -1;
710         }
711
712         if(!global_stats || stats_segments == -1) {
713                 LOG(L_ERR, "%s: Can't collect stats, they have not been initialized\n",
714                         func);
715                 return -1;
716         }
717
718         if(op != SER_GET) {
719                 LOG(L_ERR, "%s: Invalid handler operation passed\n", func);
720                 return -1;
721         }
722
723         if(!o->value.integer) {
724                 o->value.integer = calloc(1, sizeof(unsigned long));
725                 if(!o->value.integer) {
726                         LOG(L_ERR, "%s: %s\n", func, strerror(errno));
727                         return -1;
728                 }
729         }
730
731         c = global_stats;
732         total = 0;
733         switch(o->col) {
734                 /* these definitions are taken from sipMethodStatsHandler */
735                 case COLUMN_SIPSTATSINVITEINS: 
736                         collect_this_stat(received_requests_inv, total, c);
737                         break;
738                 case COLUMN_SIPSTATSINVITEOUTS: 
739                         collect_this_stat(sent_requests_inv, total, c);
740                         break;
741                 case COLUMN_SIPSTATSACKINS: 
742                         collect_this_stat(received_requests_ack, total, c);
743                         break;
744                 case COLUMN_SIPSTATSACKOUTS: 
745                         collect_this_stat(sent_requests_ack, total, c);
746                         break;
747                 case COLUMN_SIPSTATSBYEINS: 
748                         collect_this_stat(received_requests_bye, total, c);
749                         break;
750                 case COLUMN_SIPSTATSBYEOUTS: 
751                         collect_this_stat(sent_requests_bye, total, c);
752                         break;
753                 case COLUMN_SIPSTATSCANCELINS: 
754                         collect_this_stat(received_requests_cnc, total, c);
755                         break;
756                 case COLUMN_SIPSTATSCANCELOUTS: 
757                         collect_this_stat(sent_requests_cnc, total, c);
758                         break;
759                 /* ser doesn't have notion for these. We don't
760                  * register them with snmp. Here just as remainder */
761 #if 0
762                 case COLUMN_SIPSTATSOPTIONSINS:
763                 case COLUMN_SIPSTATSOPTIONSOUTS:
764                 case COLUMN_SIPSTATSREGISTERINS:
765                 case COLUMN_SIPSTATSREGISTEROUTS:
766                 case COLUMN_SIPSTATSINFOINS:
767                 case COLUMN_SIPSTATSINFOOUTS:
768                         break;
769 #endif
770         }
771
772         *o->value.integer = total;
773         o->val_len = sizeof(unsigned long);
774         o->type = SER_COUNTER;
775
776         return 0;
777 }
778
779 static int sipStatusCodes_handler(struct sip_snmp_obj *o, enum handler_op op)
780 {
781         register struct stats_s *c;
782         register unsigned long total;
783         register int i;
784         const char *func = __FUNCTION__;
785
786         if(!o) {
787                 LOG(L_ERR, "%s: Invalid sip SNMP object passed\n", func);
788                 return -1;
789         }
790
791         if(!global_stats || stats_segments == -1) {
792                 LOG(L_ERR, "%s: Can't collect stats, they have not been initialized\n",
793                         func);
794                 return -1;
795         }
796
797         if(op != SER_GET) {
798                 LOG(L_ERR, "%s: Invalid handler operation passed\n", func);
799                 return -1;
800         }
801
802         if(!o->value.integer) {
803                 o->value.integer = calloc(1, sizeof(unsigned long));
804                 if(!o->value.integer) {
805                         LOG(L_ERR, "%s: %s\n", func, strerror(errno));
806                         return -1;
807                 }
808         }
809
810         c = global_stats;
811         total = 0;
812         switch(o->col) {
813                 case COLUMN_SIPSTATSINFOCLASSINS:
814                         collect_this_stat(received_responses_1, total, c);
815                         break;
816                 case COLUMN_SIPSTATSINFOCLASSOUTS:
817                         collect_this_stat(sent_responses_1, total, c);
818                         break;
819                 case COLUMN_SIPSTATSSUCCESSCLASSINS:
820                         collect_this_stat(received_responses_2, total, c);
821                         break;
822                 case COLUMN_SIPSTATSSUCCESSCLASSOUTS:
823                         collect_this_stat(sent_responses_2, total, c);
824                         break;
825                 case COLUMN_SIPSTATSREDIRCLASSINS:
826                         collect_this_stat(received_responses_3, total, c);
827                         break;
828                 case COLUMN_SIPSTATSREDIRCLASSOUTS:
829                         collect_this_stat(sent_responses_3, total, c);
830                         break;
831                 case COLUMN_SIPSTATSREQFAILCLASSINS:
832                         collect_this_stat(received_responses_4, total, c);
833                         break;
834                 case COLUMN_SIPSTATSREQFAILCLASSOUTS:
835                         collect_this_stat(sent_responses_4, total, c);
836                         break;
837                 case COLUMN_SIPSTATSSERVERFAILCLASSINS:
838                         collect_this_stat(received_responses_5, total, c);
839                         break;
840                 case COLUMN_SIPSTATSSERVERFAILCLASSOUTS:
841                         collect_this_stat(sent_responses_5, total, c);
842                         break;
843                 case COLUMN_SIPSTATSGLOBALFAILCLASSINS:
844                         collect_this_stat(received_responses_6, total, c);
845                         break;
846                 case COLUMN_SIPSTATSGLOBALFAILCLASSOUTS:
847                         collect_this_stat(sent_responses_6, total, c);
848                         break;
849                 case COLUMN_SIPSTATSOTHERCLASSESINS:
850                         collect_this_stat(received_responses_other, total, c);
851                         break;
852                 case COLUMN_SIPSTATSOTHERCLASSESOUTS:
853                         /* FIXME: For some reason this is not defined in
854                          * struct stats_s... */
855                         /* collect_this_stat(sent_responses_other, total, c); */
856                         total = 0;
857                         break;
858         }
859
860         *o->value.integer = total;
861         o->val_len = sizeof(unsigned long);
862         o->type = SER_COUNTER;
863
864         return 0;
865 }
866
867 #endif