00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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 "../../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 REQUEST_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|LOCAL_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",
00073 DEFAULT_DLFLAGS,
00074 cmds,
00075 mod_params,
00076 0,
00077 0,
00078 0,
00079 0,
00080 mod_init,
00081 0,
00082 0,
00083 0
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
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
00124 if (s.s[0]=='$') {
00125 if (fixup_pvar(param)!=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
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
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