stats.c 23 KB

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