[asterisk-commits] oej: branch group/pine-cdr-pgsql-status-trunk r287300 - in /team/group/pine-c...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Sep 17 05:10:51 CDT 2010


Author: oej
Date: Fri Sep 17 05:10:47 2010
New Revision: 287300

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=287300
Log:
Adding the CLI command (copied mostly from cdr_mysql)

Modified:
    team/group/pine-cdr-pgsql-status-trunk/   (props changed)
    team/group/pine-cdr-pgsql-status-trunk/cdr/cdr_pgsql.c

Propchange: team/group/pine-cdr-pgsql-status-trunk/
------------------------------------------------------------------------------
    automerge = Note to myself: Always use fruit or frog names on branches.

Propchange: team/group/pine-cdr-pgsql-status-trunk/
------------------------------------------------------------------------------
    automerge-email = oej at edvina.net

Modified: team/group/pine-cdr-pgsql-status-trunk/cdr/cdr_pgsql.c
URL: http://svnview.digium.com/svn/asterisk/team/group/pine-cdr-pgsql-status-trunk/cdr/cdr_pgsql.c?view=diff&rev=287300&r1=287299&r2=287300
==============================================================================
--- team/group/pine-cdr-pgsql-status-trunk/cdr/cdr_pgsql.c (original)
+++ team/group/pine-cdr-pgsql-status-trunk/cdr/cdr_pgsql.c Fri Sep 17 05:10:47 2010
@@ -46,6 +46,7 @@
 #include "asterisk/config.h"
 #include "asterisk/channel.h"
 #include "asterisk/cdr.h"
+#include "asterisk/cli.h"
 #include "asterisk/module.h"
 
 #define DATE_FORMAT "'%Y-%m-%d %T'"
@@ -55,6 +56,13 @@
 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL;
 static int connected = 0;
 static int maxsize = 512, maxsize2 = 512;
+static time_t connect_time = 0;
+static int totalrecords = 0;
+static int records = 0;
+
+static struct ast_cli_entry cdr_pgsql_status_cli[] = {
+        AST_CLI_DEFINE(handle_cdr_pgsql_status, "Show connection status of the PostgreSQL CDR driver (cdr_pgsql)"),
+};
 
 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
 
@@ -98,6 +106,62 @@
 				}                                         \
 			} while (0)
 
+/*! \brief Handle the CLI command cdr pgsql status */
+static char *handle_cdr_pgsql_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "cdr pgsql status";
+		e->usage =
+			"Usage: cdr pgsql status\n"
+			"       Shows current connection status for cdr_pgsql\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	if (a->argc != 3)
+		return CLI_SHOWUSAGE;
+
+	if (connected) {
+		char status[256], status2[100] = "";
+		int ctime = time(NULL) - connect_time;
+
+		if (pgdbport) {
+			snprintf(status, 255, "Connected to %s@%s, port %s", pgdbname, pghostname, pgdbport);
+		} else {
+			snprintf(status, 255, "Connected to %s@%s", pgdbname, pghostname);
+		}
+
+		if (pgdbuser && *pgdbuser) {
+			snprintf(status2, 99, " with username %s", pgdbuser);
+		}
+		if (table && *table) {
+			snprintf(status2, 99, " using table %s", table);
+		}
+		if (ctime > 31536000) {
+			ast_cli(fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 31536000, (ctime % 31536000) / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60);
+		} else if (ctime > 86400) {
+			ast_cli(fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60);
+		} else if (ctime > 3600) {
+			ast_cli(fd, "%s%s for %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 3600, (ctime % 3600) / 60, ctime % 60);
+		} else if (ctime > 60) {
+			ast_cli(fd, "%s%s for %d minutes, %d seconds.\n", status, status2, ctime / 60, ctime % 60);
+		} else {
+			ast_cli(fd, "%s%s for %d seconds.\n", status, status2, ctime);
+		}
+		if (records == totalrecords) {
+			ast_cli(fd, "  Wrote %d records since last restart.\n", totalrecords);
+		} else {
+			ast_cli(fd, "  Wrote %d records since last restart and %d records since last reconnect.\n", totalrecords, records);
+		}
+		return RESULT_SUCCESS;
+	} else {
+		ast_cli(fd, "Not currently connected to a PgSQL server.\n");
+		return RESULT_FAILURE;
+	}
+}
+
 static int pgsql_log(struct ast_cdr *cdr)
 {
 	struct ast_tm tm;
@@ -110,6 +174,8 @@
 		conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
 		if (PQstatus(conn) != CONNECTION_BAD) {
 			connected = 1;
+			connect_time = time(NULL);
+                        records = 0;
 		} else {
 			pgerror = PQerrorMessage(conn);
 			ast_log(LOG_ERROR, "Unable to connect to database server %s.  Calls will not be logged!\n", pghostname);
@@ -281,6 +347,8 @@
 			if (PQstatus(conn) == CONNECTION_OK) {
 				ast_log(LOG_ERROR, "Connection reestablished.\n");
 				connected = 1;
+				connect_time = time(NULL);
+                        	records = 0;
 			} else {
 				pgerror = PQerrorMessage(conn);
 				ast_log(LOG_ERROR, "Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
@@ -310,6 +378,13 @@
 					pgerror = PQresultErrorMessage(result);
 					ast_log(LOG_ERROR, "HARD ERROR!  Attempted reconnection failed.  DROPPING CALL RECORD!\n");
 					ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
+				}  else {
+					/* Second try worked out ok */
+					totalrecords++;
+					records++;
+					ast_mutex_unlock(&pgsql_lock);
+					PQclear(result);
+					return 0;
 				}
 			}
 			ast_mutex_unlock(&pgsql_lock);
@@ -317,6 +392,9 @@
 			ast_free(sql);
 			ast_free(sql2);
 			return -1;
+		} else {
+			totalrecords++;
+			records++;
 		}
 		PQclear(result);
 		ast_free(sql);
@@ -331,6 +409,7 @@
 	struct columns *current;
 
 	ast_cdr_unregister(name);
+	ast_cli_unregister(&cdr_pgsql_status_cli);
 
 	PQfinish(conn);
 
@@ -579,6 +658,7 @@
 
 static int load_module(void)
 {
+	ast_cli_register(&cdr_pgsql_status_cli);
 	return config_module(0) ? AST_MODULE_LOAD_DECLINE : 0;
 }
 




More information about the asterisk-commits mailing list