modules_k/statistics/statistics.c
0646093c
 /*
  * $Id$
  *
  * statistics module - script interface to internal statistics manager
  *
  * Copyright (C) 2006 Voice Sistem S.R.L.
  *
27642a08
  * This file is part of Kamailio, a free SIP server.
0646093c
  *
27642a08
  * Kamailio is free software; you can redistribute it and/or modify
0646093c
  * 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
  *
27642a08
  * Kamailio is distributed in the hope that it will be useful,
0646093c
  * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * History:
  * --------
  *  2006-03-14  initial version (bogdan)
  */
 
 
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 
 #include "../../sr_module.h"
 #include "../../dprint.h"
 #include "../../ut.h"
f4dab945
 #include "../../mod_fix.h"
2ce28b94
 #include "../../lib/kcore/statistics.h"
0646093c
 #include "../../mem/mem.h"
 #include "stats_funcs.h"
 
 MODULE_VERSION
 
 static int reg_param_stat( modparam_t type, void* val);
 static int mod_init(void);
 static int w_update_stat(struct sip_msg* msg, char* stat, char* n);
 static int w_reset_stat(struct sip_msg* msg, char* stat, char* foo);
 static int fixup_stat(void** param, int param_no);
 
f4dab945
 struct stat_or_pv {
 	stat_var   *stat;
 	pv_spec_t  *pv;
 };
 
 
0646093c
 
 static cmd_export_t cmds[]={
c4d2e802
 	{"update_stat",  (cmd_function)w_update_stat,  2, fixup_stat, 0,
4037ff51
 		REQUEST_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|LOCAL_ROUTE},
c4d2e802
 	{"reset_stat",   (cmd_function)w_reset_stat,    1, fixup_stat, 0,
4037ff51
 		REQUEST_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|LOCAL_ROUTE},
80998a7f
 	{0,0,0,0,0,0}
0646093c
 };
 
 static param_export_t mod_params[]={
 	{ "variable",  STR_PARAM|USE_FUNC_PARAM, (void*)reg_param_stat },
 	{ 0,0,0 }
 };
 
 
 struct module_exports exports= {
 	"statistics", /* module's name */
51716422
 	DEFAULT_DLFLAGS, /* dlopen flags */
0646093c
 	cmds,         /* exported functions */
 	mod_params,   /* param exports */
 	0,            /* exported statistics */
de26de44
 	0,            /* exported MI functions */
71c26efd
 	0,            /* exported pseudo-variables */
3eee3a4e
 	0,            /* extra processes */
0646093c
 	mod_init,     /* module initialization function */
 	0,            /* reply processing function */
 	0,            /* module destroy function */
 	0             /* per-child init function */
 };
 
 
 
 static int reg_param_stat( modparam_t type, void* val)
 {
 	return reg_statistic( (char*)val);
 }
 
 
 
351fc577
 static int mod_init(void)
0646093c
 {
 	if (register_all_mod_stats()!=0) {
de7fe5e9
 		LM_ERR("failed to register statistic variables\n");
0646093c
 		return E_UNSPEC;
 	}
 	return 0;
 }
 
 
 
 static int fixup_stat(void** param, int param_no)
 {
f4dab945
 	struct stat_or_pv *sopv;
0646093c
 	str s;
 	long n;
 	int err;
 
 	s.s = (char*)*param;
 	s.len = strlen(s.s);
 	if (param_no==1) {
f4dab945
 		/* var name - string or pv */
 		sopv = (struct stat_or_pv *)pkg_malloc(sizeof(struct stat_or_pv));
 		if (sopv==NULL) {
 			LM_ERR("no more pkg mem\n");
 			return E_OUT_OF_MEM;
 		}
 		memset( sopv, 0 , sizeof(struct stat_or_pv) );
 		/* is it pv? */
 		if (s.s[0]=='$') {
a8e660b6
 			if (fixup_pvar_null(param, 1)!=0) {
3cacd931
 				LM_ERR("invalid pv %s as parameter\n",s.s);
f4dab945
 				return E_CFG;
 			}
 			sopv->pv = (pv_spec_t*)(*param);
 		} else {
 			/* it is string */
 			sopv->stat = get_stat( &s );
 			if (sopv->stat==0) {
 				LM_ERR("variable <%s> not defined\n", s.s);
 				return E_CFG;
 			}
0646093c
 		}
f4dab945
 		pkg_free(s.s);
 		*param=(void*)sopv;
0646093c
 		return 0;
 	} else if (param_no==2) {
 		/* update value - integer */
 		if (s.s[0]=='-' || s.s[0]=='+') {
 			n = str2s( s.s+1, s.len-1, &err);
 			if (s.s[0]=='-')
 				n = -n;
 		} else {
 			n = str2s( s.s, s.len, &err);
 		}
 		if (err==0){
 			if (n==0) {
de7fe5e9
 				LM_ERR("update with 0 has no sense\n");
0646093c
 				return E_CFG;
 			}
 			pkg_free(*param);
 			*param=(void*)n;
 			return 0;
 		}else{
de7fe5e9
 			LM_ERR("bad update number <%s>\n",(char*)(*param));
0646093c
 			return E_CFG;
 		}
 	}
 	return 0;
 }
 
 
f4dab945
 static int w_update_stat(struct sip_msg *msg, char *stat_p, char *n)
0646093c
 {
f4dab945
 	struct stat_or_pv *sopv = (struct stat_or_pv *)stat_p;
 	pv_value_t pv_val;
 	stat_var *stat;
 
 	if (sopv->stat) {
 		update_stat( sopv->stat, (long)n);
 	} else {
 		if (pv_get_spec_value(msg, sopv->pv, &pv_val)!=0 ||
 		(pv_val.flags & PV_VAL_STR)==0 ) {
 			LM_ERR("failed to get pv string value\n");
 			return -1;
 		}
 		stat = get_stat( &(pv_val.rs) );
 		if ( stat == 0 ) {
 			LM_ERR("variable <%.*s> not defined\n",
 				pv_val.rs.len, pv_val.rs.s);
 			return -1;
 		}
 		update_stat( stat, (long)n);
 	}
 
0646093c
 	return 1;
 }
 
 
f4dab945
 static int w_reset_stat(struct sip_msg *msg, char* stat_p, char *foo)
0646093c
 {
f4dab945
 	struct stat_or_pv *sopv = (struct stat_or_pv *)stat_p;
 	pv_value_t pv_val;
 	stat_var *stat;
 
 	if (sopv->stat) {
 		reset_stat( sopv->stat );
 	} else {
 		if (pv_get_spec_value(msg, sopv->pv, &pv_val)!=0 ||
 		(pv_val.flags & PV_VAL_STR)==0 ) {
 			LM_ERR("failed to get pv string value\n");
 			return -1;
 		}
 		stat = get_stat( &(pv_val.rs) );
 		if ( stat == 0 ) {
 			LM_ERR("variable <%.*s> not defined\n",
 				pv_val.rs.len, pv_val.rs.s);
 			return -1;
 		}
 		reset_stat( stat );
 	}
 
 
0646093c
 	return 1;
 }