[sr-dev] git:master: acc: new parameter time_mode

Daniel-Constantin Mierla miconda at gmail.com
Tue Sep 3 14:32:25 CEST 2013


Module: sip-router
Branch: master
Commit: d7884500e89d32bffd34e915b473458645c6dedf
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=d7884500e89d32bffd34e915b473458645c6dedf

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Tue Sep  3 13:28:54 2013 +0200

acc: new parameter time_mode

- store additional time value in time_attr attribute/db column
- if time_mode==1, then time_attr stores the microseconds
- if time_mode==2, then time_attr store the seconds.miliseconds
  (proposed by FS#163)
- if time_mode==0 (default), then it is like now, only timestamp stored
- time_attr value can be set via parameter with same name - it
  represents syslog value or db table column
- for db accounting, when time_mode==1, the type of column has to be
  int, and for time_mode==2, the type of column has to be double
- features implemented only for syslog and db accounting

---

 modules/acc/acc.c     |   48 ++++++++++++++++++++++++++++++++++++++++--------
 modules/acc/acc_api.h |    4 +++-
 modules/acc/acc_mod.c |   13 ++++++++++++-
 modules/acc/acc_mod.h |    4 ++++
 4 files changed, 59 insertions(+), 10 deletions(-)

diff --git a/modules/acc/acc.c b/modules/acc/acc.c
index 0a563d6..7f08f17 100644
--- a/modules/acc/acc.c
+++ b/modules/acc/acc.c
@@ -95,9 +95,9 @@ extern int acc_db_insert_mode;
 
 /* arrays used to collect the values before being
  * pushed to the storage backend (whatever used) */
-static str val_arr[ACC_CORE_LEN+MAX_ACC_EXTRA+MAX_ACC_LEG];
-static int int_arr[ACC_CORE_LEN+MAX_ACC_EXTRA+MAX_ACC_LEG];
-static char type_arr[ACC_CORE_LEN+MAX_ACC_EXTRA+MAX_ACC_LEG];
+static str val_arr[ACC_CORE_LEN+MAX_ACC_EXTRA+MAX_ACC_LEG+2];
+static int int_arr[ACC_CORE_LEN+MAX_ACC_EXTRA+MAX_ACC_LEG+2];
+static char type_arr[ACC_CORE_LEN+MAX_ACC_EXTRA+MAX_ACC_LEG+2];
 
 /********************************************
  *        acc CORE function
@@ -173,7 +173,9 @@ int core2strar(struct sip_msg *req, str *c_vals, int *i_vals, char *t_vals)
 	c_vals[5] = acc_env.reason;
 	t_vals[5] = TYPE_STR;
 
-	acc_env.ts = time(NULL);
+	gettimeofday(&acc_env.tv, NULL);
+	acc_env.ts = acc_env.tv.tv_sec;
+
 	return ACC_CORE_LEN;
 }
 
@@ -275,8 +277,23 @@ int acc_log_request( struct sip_msg *rq)
 	*(p++) = '\n';
 	*(p++) = 0;
 
-	LM_GEN2(log_facility, log_level, "%.*stimestamp=%lu%s",
-		acc_env.text.len, acc_env.text.s,(unsigned long) acc_env.ts, log_msg);
+	if(acc_time_mode==1) {
+		LM_GEN2(log_facility, log_level, "%.*stimestamp=%lu;%s=%u%s",
+			acc_env.text.len, acc_env.text.s,(unsigned long) acc_env.ts,
+			acc_time_attr.s, (unsigned int)acc_env.tv.tv_usec,
+			log_msg);
+	} else if(acc_time_mode==2) {
+		LM_GEN2(log_facility, log_level, "%.*stimestamp=%lu;%s=%.3f%s",
+			acc_env.text.len, acc_env.text.s,(unsigned long) acc_env.ts,
+			acc_time_attr.s,
+			(((double)(acc_env.tv.tv_sec * 1000)
+							+ (acc_env.tv.tv_usec / 1000)) / 1000),
+			log_msg);
+	} else {
+		LM_GEN2(log_facility, log_level, "%.*stimestamp=%lu%s",
+			acc_env.text.len, acc_env.text.s,(unsigned long) acc_env.ts,
+			log_msg);
+	}
 
 	return 1;
 }
@@ -289,8 +306,8 @@ int acc_log_request( struct sip_msg *rq)
 #ifdef SQL_ACC
 
 /* caution: keys need to be aligned to core format */
-static db_key_t db_keys[ACC_CORE_LEN+1+MAX_ACC_EXTRA+MAX_ACC_LEG];
-static db_val_t db_vals[ACC_CORE_LEN+1+MAX_ACC_EXTRA+MAX_ACC_LEG];
+static db_key_t db_keys[ACC_CORE_LEN+2+MAX_ACC_EXTRA+MAX_ACC_LEG];
+static db_val_t db_vals[ACC_CORE_LEN+2+MAX_ACC_EXTRA+MAX_ACC_LEG];
 
 
 static void acc_db_init_keys(void)
@@ -311,6 +328,9 @@ static void acc_db_init_keys(void)
 	db_keys[n++] = &acc_sipreason_col;
 	db_keys[n++] = &acc_time_col;
 	time_idx = n-1;
+	if(acc_time_mode==1 || acc_time_mode==2) {
+		db_keys[n++] = &acc_time_attr;;
+	}
 
 	/* init the extra db keys */
 	for(extra=db_extra; extra ; extra=extra->next)
@@ -326,6 +346,11 @@ static void acc_db_init_keys(void)
 		VAL_NULL(db_vals+i)=0;
 	}
 	VAL_TYPE(db_vals+time_idx)=DB1_DATETIME;
+	if(acc_time_mode==1) {
+		VAL_TYPE(db_vals+time_idx+1)=DB1_INT;
+	} else if(acc_time_mode==2) {
+		VAL_TYPE(db_vals+time_idx+1)=DB1_DOUBLE;
+	}
 }
 
 
@@ -384,6 +409,13 @@ int acc_db_request( struct sip_msg *rq)
 		VAL_STR(db_vals+i) = val_arr[i];
 	/* time value */
 	VAL_TIME(db_vals+(m++)) = acc_env.ts;
+	/* extra time value */
+	if(acc_time_mode==1) {
+		VAL_INT(db_vals+(m++)) = (int)acc_env.tv.tv_usec;
+	} else if(acc_time_mode==2) {
+		VAL_DOUBLE(db_vals+(m++)) = ((acc_env.tv.tv_sec * 1000)
+							+ (acc_env.tv.tv_usec / 1000)) / 1000;
+	}
 
 	/* extra columns */
 	m += extra2strar( db_extra, rq, val_arr+m, int_arr+m, type_arr+m);
diff --git a/modules/acc/acc_api.h b/modules/acc/acc_api.h
index bc52eee..aa51380 100644
--- a/modules/acc/acc_api.h
+++ b/modules/acc/acc_api.h
@@ -36,6 +36,7 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <sys/time.h>
 
 #include "../../str.h"
 #include "../../dprint.h"
@@ -57,6 +58,7 @@ typedef struct acc_enviroment {
 	struct hdr_field *to;
 	str text;
 	time_t ts;
+	struct timeval tv;
 } acc_enviroment_t;
 
 /* acc extra parameter */
@@ -104,7 +106,7 @@ typedef struct acc_engine {
 
 #define MAX_ACC_EXTRA 64
 #define MAX_ACC_LEG   16
-#define ACC_CORE_LEN 6
+#define ACC_CORE_LEN  6
 
 
 enum {TYPE_NULL = 0, TYPE_INT, TYPE_STR};
diff --git a/modules/acc/acc_mod.c b/modules/acc/acc_mod.c
index 9f40e14..e127603 100644
--- a/modules/acc/acc_mod.c
+++ b/modules/acc/acc_mod.c
@@ -110,6 +110,14 @@ static char* leg_info_str = 0;	/*!< multi call-leg support */
 struct acc_extra *leg_info = 0;
 int acc_prepare_flag = -1; /*!< should the request be prepared for later acc */
 
+/* ----- time mode variables ------- */
+/*! \name AccTimeModeVariables  Time Mode Variables */
+/*@{*/
+
+int acc_time_mode  = 0;
+str acc_time_attr  = str_init("time_attr");
+
+/*@}*/
 
 /* ----- SYSLOG acc variables ----------- */
 /*! \name AccSyslogVariables  Syslog Variables */
@@ -138,7 +146,7 @@ str cdr_start_str = str_init("st");
 str cdr_end_str = str_init("et");
 str cdr_duration_str = str_init("d");
 
-/*@{*/
+/*@}*/
 
 /* ----- RADIUS acc variables ----------- */
 /*! \name AccRadiusVariables  Radius Variables */     
@@ -291,6 +299,9 @@ static param_export_t params[] = {
 	{"acc_time_column",      STR_PARAM, &acc_time_col.s       },
 	{"db_insert_mode",       INT_PARAM, &acc_db_insert_mode   },
 #endif
+	/* time-mode-specific */
+	{"time_mode",            INT_PARAM, &acc_time_mode        },
+	{"time_attr",            PARAM_STR, &acc_time_attr        },
 	{0,0,0}
 };
 
diff --git a/modules/acc/acc_mod.h b/modules/acc/acc_mod.h
index 5e7792a..07a7dc1 100644
--- a/modules/acc/acc_mod.h
+++ b/modules/acc/acc_mod.h
@@ -94,5 +94,9 @@ extern str acc_sipreason_col;
 extern str acc_time_col;
 #endif /* SQL_ACC */
 
+/* time mode */
+extern int acc_time_mode;
+extern str acc_time_attr;
+
 
 #endif




More information about the sr-dev mailing list