modules_k/statistics/statistics.c

00001 /*
00002  * $Id$
00003  *
00004  * statistics module - script interface to internal statistics manager
00005  *
00006  * Copyright (C) 2006 Voice Sistem S.R.L.
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software 
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  * History:
00025  * --------
00026  *  2006-03-14  initial version (bogdan)
00027  */
00028 
00029 
00030 #include <stdio.h>
00031 #include <string.h>
00032 #include <stdlib.h>
00033 
00034 #include "../../sr_module.h"
00035 #include "../../dprint.h"
00036 #include "../../ut.h"
00037 #include "../../mod_fix.h"
00038 #include "../../lib/kcore/statistics.h"
00039 #include "../../mem/mem.h"
00040 #include "stats_funcs.h"
00041 
00042 MODULE_VERSION
00043 
00044 static int reg_param_stat( modparam_t type, void* val);
00045 static int mod_init(void);
00046 static int w_update_stat(struct sip_msg* msg, char* stat, char* n);
00047 static int w_reset_stat(struct sip_msg* msg, char* stat, char* foo);
00048 static int fixup_stat(void** param, int param_no);
00049 
00050 struct stat_or_pv {
00051         stat_var   *stat;
00052         pv_spec_t  *pv;
00053 };
00054 
00055 
00056 
00057 static cmd_export_t cmds[]={
00058         {"update_stat",  (cmd_function)w_update_stat,  2, fixup_stat, 0,
00059                 ANY_ROUTE},
00060         {"reset_stat",   (cmd_function)w_reset_stat,    1, fixup_stat, 0,
00061                 REQUEST_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|LOCAL_ROUTE},
00062         {0,0,0,0,0,0}
00063 };
00064 
00065 static param_export_t mod_params[]={
00066         { "variable",  STR_PARAM|USE_FUNC_PARAM, (void*)reg_param_stat },
00067         { 0,0,0 }
00068 };
00069 
00070 
00071 struct module_exports exports= {
00072         "statistics", /* module's name */
00073         DEFAULT_DLFLAGS, /* dlopen flags */
00074         cmds,         /* exported functions */
00075         mod_params,   /* param exports */
00076         0,            /* exported statistics */
00077         0,            /* exported MI functions */
00078         0,            /* exported pseudo-variables */
00079         0,            /* extra processes */
00080         mod_init,     /* module initialization function */
00081         0,            /* reply processing function */
00082         0,            /* module destroy function */
00083         0             /* per-child init function */
00084 };
00085 
00086 
00087 
00088 static int reg_param_stat( modparam_t type, void* val)
00089 {
00090         return reg_statistic( (char*)val);
00091 }
00092 
00093 
00094 
00095 static int mod_init(void)
00096 {
00097         if (register_all_mod_stats()!=0) {
00098                 LM_ERR("failed to register statistic variables\n");
00099                 return E_UNSPEC;
00100         }
00101         return 0;
00102 }
00103 
00104 
00105 
00106 static int fixup_stat(void** param, int param_no)
00107 {
00108         struct stat_or_pv *sopv;
00109         str s;
00110         long n;
00111         int err;
00112 
00113         s.s = (char*)*param;
00114         s.len = strlen(s.s);
00115         if (param_no==1) {
00116                 /* var name - string or pv */
00117                 sopv = (struct stat_or_pv *)pkg_malloc(sizeof(struct stat_or_pv));
00118                 if (sopv==NULL) {
00119                         LM_ERR("no more pkg mem\n");
00120                         return E_OUT_OF_MEM;
00121                 }
00122                 memset( sopv, 0 , sizeof(struct stat_or_pv) );
00123                 /* is it pv? */
00124                 if (s.s[0]=='$') {
00125                         if (fixup_pvar_null(param, 1)!=0) {
00126                                 LM_ERR("invalid pv %s as parameter\n",s.s);
00127                                 return E_CFG;
00128                         }
00129                         sopv->pv = (pv_spec_t*)(*param);
00130                 } else {
00131                         /* it is string */
00132                         sopv->stat = get_stat( &s );
00133                         if (sopv->stat==0) {
00134                                 LM_ERR("variable <%s> not defined\n", s.s);
00135                                 return E_CFG;
00136                         }
00137                 }
00138                 pkg_free(s.s);
00139                 *param=(void*)sopv;
00140                 return 0;
00141         } else if (param_no==2) {
00142                 /* update value - integer */
00143                 if (s.s[0]=='-' || s.s[0]=='+') {
00144                         n = str2s( s.s+1, s.len-1, &err);
00145                         if (s.s[0]=='-')
00146                                 n = -n;
00147                 } else {
00148                         n = str2s( s.s, s.len, &err);
00149                 }
00150                 if (err==0){
00151                         if (n==0) {
00152                                 LM_ERR("update with 0 has no sense\n");
00153                                 return E_CFG;
00154                         }
00155                         pkg_free(*param);
00156                         *param=(void*)n;
00157                         return 0;
00158                 }else{
00159                         LM_ERR("bad update number <%s>\n",(char*)(*param));
00160                         return E_CFG;
00161                 }
00162         }
00163         return 0;
00164 }
00165 
00166 
00167 static int w_update_stat(struct sip_msg *msg, char *stat_p, char *n)
00168 {
00169         struct stat_or_pv *sopv = (struct stat_or_pv *)stat_p;
00170         pv_value_t pv_val;
00171         stat_var *stat;
00172 
00173         if (sopv->stat) {
00174                 update_stat( sopv->stat, (long)n);
00175         } else {
00176                 if (pv_get_spec_value(msg, sopv->pv, &pv_val)!=0 ||
00177                 (pv_val.flags & PV_VAL_STR)==0 ) {
00178                         LM_ERR("failed to get pv string value\n");
00179                         return -1;
00180                 }
00181                 stat = get_stat( &(pv_val.rs) );
00182                 if ( stat == 0 ) {
00183                         LM_ERR("variable <%.*s> not defined\n",
00184                                 pv_val.rs.len, pv_val.rs.s);
00185                         return -1;
00186                 }
00187                 update_stat( stat, (long)n);
00188         }
00189 
00190         return 1;
00191 }
00192 
00193 
00194 static int w_reset_stat(struct sip_msg *msg, char* stat_p, char *foo)
00195 {
00196         struct stat_or_pv *sopv = (struct stat_or_pv *)stat_p;
00197         pv_value_t pv_val;
00198         stat_var *stat;
00199 
00200         if (sopv->stat) {
00201                 reset_stat( sopv->stat );
00202         } else {
00203                 if (pv_get_spec_value(msg, sopv->pv, &pv_val)!=0 ||
00204                 (pv_val.flags & PV_VAL_STR)==0 ) {
00205                         LM_ERR("failed to get pv string value\n");
00206                         return -1;
00207                 }
00208                 stat = get_stat( &(pv_val.rs) );
00209                 if ( stat == 0 ) {
00210                         LM_ERR("variable <%.*s> not defined\n",
00211                                 pv_val.rs.len, pv_val.rs.s);
00212                         return -1;
00213                 }
00214                 reset_stat( stat );
00215         }
00216 
00217 
00218         return 1;
00219 }
00220 
00221