2 * Asterisk -- A telephony toolkit for Linux.
4 * Copyright (C) 1999-2005, Digium, Inc.
6 * Manuel Guesdon <mguesdon@oxymium.net> - Postgresql RealTime Driver Author/Adaptor
7 * Mark Spencer <markster@digium.com> - Asterisk Author
8 * Matthew Boehm <mboehm@cytelcom.com> - MySQL RealTime Driver Author
10 * res_config_pgsql.c <Postgresql plugin for RealTime configuration engine>
12 * v1.0 - (07-11-05) - Initial version based on res_config_mysql v2.0
17 * \brief Postgresql plugin for Asterisk RealTime Architecture
19 * \author Mark Spencer <markster@digium.com>
20 * \author Manuel Guesdon <mguesdon@oxymium.net> - Postgresql RealTime Driver Author/Adaptor
22 * \arg http://www.postgresql.org
27 #include <libpq-fe.h> /* PostgreSQL */
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include "asterisk/file.h"
34 #include "asterisk/logger.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/config.h"
38 #include "asterisk/module.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/options.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/cli.h"
44 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
46 #define RES_CONFIG_PGSQL_CONF "res_pgsql.conf"
48 PGconn *pgsqlConn = NULL;
50 #define MAX_DB_OPTION_SIZE 64
52 static char dbhost[MAX_DB_OPTION_SIZE] = "";
53 static char dbuser[MAX_DB_OPTION_SIZE] = "";
54 static char dbpass[MAX_DB_OPTION_SIZE] = "";
55 static char dbname[MAX_DB_OPTION_SIZE] = "";
56 static char dbsock[MAX_DB_OPTION_SIZE] = "";
57 static int dbport = 5432;
58 static time_t connect_time = 0;
60 static int parse_config(void);
61 static int pgsql_reconnect(const char *database);
62 static int realtime_pgsql_status(int fd, int argc, char **argv);
66 static char cli_realtime_pgsql_status_usage[] =
67 "Usage: realtime pgsql status\n"
68 " Shows connection information for the Postgresql RealTime driver\n";
70 static struct ast_cli_entry cli_realtime_pgsql_status = {
71 { "realtime", "pgsql", "status", NULL }, realtime_pgsql_status,
72 "Shows connection information for the Postgresql RealTime driver",
73 cli_realtime_pgsql_status_usage, NULL
76 static struct ast_variable *realtime_pgsql(const char *database, const char *table, va_list ap)
78 PGresult *result = NULL;
84 const char *newparam, *newval;
85 struct ast_variable *var = NULL, *prev = NULL;
88 ast_log(LOG_WARNING, "Postgresql RealTime: No table specified.\n");
92 /* Get the first parameter and first value in our list of passed paramater/value pairs */
93 newparam = va_arg(ap, const char *);
94 newval = va_arg(ap, const char *);
95 if (!newparam || !newval) {
97 "Postgresql RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
105 /* Create the first part of the query using the first parameter/value pairs we just extracted
106 If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
107 op = strchr(newparam, ' ') ? "" : " =";
109 snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s '%s'", table, newparam, op,
111 while ((newparam = va_arg(ap, const char *))) {
112 newval = va_arg(ap, const char *);
113 if (!strchr(newparam, ' '))
117 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s '%s'", newparam,
122 /* We now have our complete statement; Lets connect to the server and execute it. */
123 ast_mutex_lock(&pgsql_lock);
124 if (!pgsql_reconnect(database)) {
125 ast_mutex_unlock(&pgsql_lock);
129 if (!(result = PQexec(pgsqlConn, sql))) {
131 "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
132 ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
133 ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s\n",
134 PQerrorMessage(pgsqlConn));
135 ast_mutex_unlock(&pgsql_lock);
138 ExecStatusType result_status = PQresultStatus(result);
139 if (result_status != PGRES_COMMAND_OK
140 && result_status != PGRES_TUPLES_OK
141 && result_status != PGRES_NONFATAL_ERROR) {
143 "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
144 ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
145 ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s (%s)\n",
146 PQresultErrorMessage(result), PQresStatus(result_status));
147 ast_mutex_unlock(&pgsql_lock);
152 ast_log(LOG_DEBUG, "1Postgresql RealTime: Result=%p Query: %s\n", result, sql);
154 if ((num_rows = PQntuples(result)) > 0) {
157 int numFields = PQnfields(result);
158 char **fieldnames = NULL;
160 ast_log(LOG_DEBUG, "Postgresql RealTime: Found %d rows.\n", num_rows);
162 if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
163 ast_mutex_unlock(&pgsql_lock);
167 for (i = 0; i < numFields; i++)
168 fieldnames[i] = PQfname(result, i);
169 for (rowIndex = 0; rowIndex < num_rows; rowIndex++) {
170 for (i = 0; i < numFields; i++) {
171 stringp = PQgetvalue(result, rowIndex, i);
173 chunk = strsep(&stringp, ";");
174 if (chunk && !ast_strlen_zero(ast_strip(chunk))) {
176 prev->next = ast_variable_new(fieldnames[i], chunk);
181 prev = var = ast_variable_new(fieldnames[i], chunk);
190 "Postgresql RealTime: Could not find any rows in table %s.\n", table);
193 ast_mutex_unlock(&pgsql_lock);
199 static struct ast_config *realtime_multi_pgsql(const char *database, const char *table, va_list ap)
201 PGresult *result = NULL;
204 const char *initfield = NULL;
208 const char *newparam, *newval;
209 struct ast_realloca ra;
210 struct ast_variable *var = NULL;
211 struct ast_config *cfg = NULL;
212 struct ast_category *cat = NULL;
215 ast_log(LOG_WARNING, "Postgresql RealTime: No table specified.\n");
219 memset(&ra, 0, sizeof(ra));
221 if (!(cfg = ast_config_new()))
224 /* Get the first parameter and first value in our list of passed paramater/value pairs */
225 newparam = va_arg(ap, const char *);
226 newval = va_arg(ap, const char *);
227 if (!newparam || !newval) {
229 "Postgresql RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
237 initfield = ast_strdupa(newparam);
238 if (initfield && (op = strchr(initfield, ' '))) {
242 /* Create the first part of the query using the first parameter/value pairs we just extracted
243 If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
245 if (!strchr(newparam, ' '))
250 snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s '%s'", table, newparam, op,
252 while ((newparam = va_arg(ap, const char *))) {
253 newval = va_arg(ap, const char *);
254 if (!strchr(newparam, ' '))
258 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s '%s'", newparam,
263 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " ORDER BY %s", initfield);
268 /* We now have our complete statement; Lets connect to the server and execute it. */
269 ast_mutex_lock(&pgsql_lock);
270 if (!pgsql_reconnect(database)) {
271 ast_mutex_unlock(&pgsql_lock);
275 if (!(result = PQexec(pgsqlConn, sql))) {
277 "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
278 ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
279 ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s\n",
280 PQerrorMessage(pgsqlConn));
281 ast_mutex_unlock(&pgsql_lock);
284 ExecStatusType result_status = PQresultStatus(result);
285 if (result_status != PGRES_COMMAND_OK
286 && result_status != PGRES_TUPLES_OK
287 && result_status != PGRES_NONFATAL_ERROR) {
289 "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
290 ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
291 ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s (%s)\n",
292 PQresultErrorMessage(result), PQresStatus(result_status));
293 ast_mutex_unlock(&pgsql_lock);
298 ast_log(LOG_DEBUG, "2Postgresql RealTime: Result=%p Query: %s\n", result, sql);
300 if ((num_rows = PQntuples(result)) > 0) {
301 int numFields = PQnfields(result);
304 char **fieldnames = NULL;
306 ast_log(LOG_DEBUG, "Postgresql RealTime: Found %d rows.\n", num_rows);
308 if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
309 ast_mutex_unlock(&pgsql_lock);
313 for (i = 0; i < numFields; i++)
314 fieldnames[i] = PQfname(result, i);
316 for (rowIndex = 0; rowIndex < num_rows; rowIndex++) {
318 if (!(cat = ast_category_new("")))
320 for (i = 0; i < numFields; i++) {
321 stringp = PQgetvalue(result, rowIndex, i);
323 chunk = strsep(&stringp, ";");
324 if (chunk && !ast_strlen_zero(ast_strip(chunk))) {
325 if (initfield && !strcmp(initfield, fieldnames[i])) {
326 ast_category_rename(cat, chunk);
328 var = ast_variable_new(fieldnames[i], chunk);
329 ast_variable_append(cat, var);
333 ast_category_append(cfg, cat);
338 "Postgresql RealTime: Could not find any rows in table %s.\n", table);
341 ast_mutex_unlock(&pgsql_lock);
347 static int update_pgsql(const char *database, const char *table, const char *keyfield,
348 const char *lookup, va_list ap)
350 PGresult *result = NULL;
353 const char *newparam, *newval;
356 ast_log(LOG_WARNING, "Postgresql RealTime: No table specified.\n");
360 /* Get the first parameter and first value in our list of passed paramater/value pairs */
361 newparam = va_arg(ap, const char *);
362 newval = va_arg(ap, const char *);
363 if (!newparam || !newval) {
365 "Postgresql RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on.\n");
373 /* Create the first part of the query using the first parameter/value pairs we just extracted
374 If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */
376 snprintf(sql, sizeof(sql), "UPDATE %s SET %s = '%s'", table, newparam, newval);
377 while ((newparam = va_arg(ap, const char *))) {
378 newval = va_arg(ap, const char *);
379 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), ", %s = '%s'", newparam,
383 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " WHERE %s = '%s'", keyfield,
386 ast_log(LOG_DEBUG, "Postgresql RealTime: Update SQL: %s\n", sql);
388 /* We now have our complete statement; Lets connect to the server and execute it. */
389 ast_mutex_lock(&pgsql_lock);
390 if (!pgsql_reconnect(database)) {
391 ast_mutex_unlock(&pgsql_lock);
395 if (!(result = PQexec(pgsqlConn, sql))) {
397 "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
398 ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
399 ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s\n",
400 PQerrorMessage(pgsqlConn));
401 ast_mutex_unlock(&pgsql_lock);
404 ExecStatusType result_status = PQresultStatus(result);
405 if (result_status != PGRES_COMMAND_OK
406 && result_status != PGRES_TUPLES_OK
407 && result_status != PGRES_NONFATAL_ERROR) {
409 "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
410 ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
411 ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s (%s)\n",
412 PQresultErrorMessage(result), PQresStatus(result_status));
413 ast_mutex_unlock(&pgsql_lock);
418 numrows = atoi(PQcmdTuples(result));
419 ast_mutex_unlock(&pgsql_lock);
421 ast_log(LOG_DEBUG, "Postgresql RealTime: Updated %d rows on table: %s\n", numrows,
424 /* From http://dev.pgsql.com/doc/pgsql/en/pgsql-affected-rows.html
425 * An integer greater than zero indicates the number of rows affected
426 * Zero indicates that no records were updated
427 * -1 indicates that the query returned an error (although, if the query failed, it should have been caught above.)
431 return (int) numrows;
436 static struct ast_config *config_pgsql(const char *database, const char *table,
437 const char *file, struct ast_config *cfg)
439 PGresult *result = NULL;
441 struct ast_variable *new_v;
442 struct ast_category *cur_cat = NULL;
445 int last_cat_metric = 0;
449 if (!file || !strcmp(file, RES_CONFIG_PGSQL_CONF)) {
450 ast_log(LOG_WARNING, "Postgresql RealTime: Cannot configure myself.\n");
454 snprintf(sql, sizeof(sql),
455 "SELECT category, var_name, var_val, cat_metric FROM %s WHERE filename='%s' and commented=0 ORDER BY filename, cat_metric desc, var_metric asc, category, var_name, var_val, id",
458 ast_log(LOG_DEBUG, "Postgresql RealTime: Static SQL: %s\n", sql);
460 /* We now have our complete statement; Lets connect to the server and execute it. */
461 ast_mutex_lock(&pgsql_lock);
462 if (!pgsql_reconnect(database)) {
463 ast_mutex_unlock(&pgsql_lock);
467 if (!(result = PQexec(pgsqlConn, sql))) {
469 "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
470 ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
471 ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s\n",
472 PQerrorMessage(pgsqlConn));
473 ast_mutex_unlock(&pgsql_lock);
476 ExecStatusType result_status = PQresultStatus(result);
477 if (result_status != PGRES_COMMAND_OK
478 && result_status != PGRES_TUPLES_OK
479 && result_status != PGRES_NONFATAL_ERROR) {
481 "Postgresql RealTime: Failed to query database. Check debug for more info.\n");
482 ast_log(LOG_DEBUG, "Postgresql RealTime: Query: %s\n", sql);
483 ast_log(LOG_DEBUG, "Postgresql RealTime: Query Failed because: %s (%s)\n",
484 PQresultErrorMessage(result), PQresStatus(result_status));
485 ast_mutex_unlock(&pgsql_lock);
490 if ((num_rows = PQntuples(result)) > 0) {
491 int numFields = PQnfields(result);
494 char **fieldnames = NULL;
496 ast_log(LOG_DEBUG, "Postgresql RealTime: Found %ld rows.\n", num_rows);
498 if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
499 ast_mutex_unlock(&pgsql_lock);
503 for (i = 0; i < numFields; i++)
504 fieldnames[i] = PQfname(result, i);
506 for (rowIndex = 0; rowIndex < num_rows; rowIndex++) {
507 char *field_category = PQgetvalue(result, rowIndex, 0);
508 char *field_var_name = PQgetvalue(result, rowIndex, 1);
509 char *field_var_val = PQgetvalue(result, rowIndex, 2);
510 char *field_cat_metric = PQgetvalue(result, rowIndex, 3);
511 if (!strcmp(field_var_name, "#include")) {
512 if (!ast_config_internal_load(field_var_val, cfg)) {
514 ast_mutex_unlock(&pgsql_lock);
520 if (strcmp(last, field_category) || last_cat_metric != atoi(field_cat_metric)) {
521 cur_cat = ast_category_new(field_category);
524 strcpy(last, field_category);
525 last_cat_metric = atoi(field_cat_metric);
526 ast_category_append(cfg, cur_cat);
528 new_v = ast_variable_new(field_var_name, field_var_val);
529 ast_variable_append(cur_cat, new_v);
533 "Postgresql RealTime: Could not find config '%s' in database.\n", file);
537 ast_mutex_unlock(&pgsql_lock);
542 static struct ast_config_engine pgsql_engine = {
544 .load_func = config_pgsql,
545 .realtime_func = realtime_pgsql,
546 .realtime_multi_func = realtime_multi_pgsql,
547 .update_func = update_pgsql
550 int load_module(void)
554 ast_mutex_lock(&pgsql_lock);
556 if (!pgsql_reconnect(NULL)) {
558 "Postgresql RealTime: Couldn't establish connection. Check debug.\n");
559 ast_log(LOG_DEBUG, "Postgresql RealTime: Cannot Connect: %s\n",
560 PQerrorMessage(pgsqlConn));
563 ast_config_engine_register(&pgsql_engine);
564 if (option_verbose) {
565 ast_verbose("Postgresql RealTime driver loaded.\n");
567 ast_cli_register(&cli_realtime_pgsql_status);
569 ast_mutex_unlock(&pgsql_lock);
574 int unload_module(void)
576 /* Aquire control before doing anything to the module itself. */
577 ast_mutex_lock(&pgsql_lock);
583 ast_cli_unregister(&cli_realtime_pgsql_status);
584 ast_config_engine_deregister(&pgsql_engine);
585 if (option_verbose) {
586 ast_verbose("Postgresql RealTime unloaded.\n");
589 STANDARD_HANGUP_LOCALUSERS;
591 /* Unlock so something else can destroy the lock. */
592 ast_mutex_unlock(&pgsql_lock);
599 /* Aquire control before doing anything to the module itself. */
600 ast_mutex_lock(&pgsql_lock);
608 if (!pgsql_reconnect(NULL)) {
610 "Postgresql RealTime: Couldn't establish connection. Check debug.\n");
611 ast_log(LOG_DEBUG, "Postgresql RealTime: Cannot Connect: %s\n",
612 PQerrorMessage(pgsqlConn));
615 ast_verbose(VERBOSE_PREFIX_2 "Postgresql RealTime reloaded.\n");
617 /* Done reloading. Release lock so others can now use driver. */
618 ast_mutex_unlock(&pgsql_lock);
623 int parse_config(void)
625 struct ast_config *config;
628 config = ast_config_load(RES_CONFIG_PGSQL_CONF);
631 if (!(s = ast_variable_retrieve(config, "general", "dbuser"))) {
633 "Postgresql RealTime: No database user found, using 'asterisk' as default.\n");
634 strcpy(dbuser, "asterisk");
636 ast_copy_string(dbuser, s, sizeof(dbuser));
639 if (!(s = ast_variable_retrieve(config, "general", "dbpass"))) {
641 "Postgresql RealTime: No database password found, using 'asterisk' as default.\n");
642 strcpy(dbpass, "asterisk");
644 ast_copy_string(dbpass, s, sizeof(dbpass));
647 if (!(s = ast_variable_retrieve(config, "general", "dbhost"))) {
649 "Postgresql RealTime: No database host found, using localhost via socket.\n");
652 ast_copy_string(dbhost, s, sizeof(dbhost));
655 if (!(s = ast_variable_retrieve(config, "general", "dbname"))) {
657 "Postgresql RealTime: No database name found, using 'asterisk' as default.\n");
658 strcpy(dbname, "asterisk");
660 ast_copy_string(dbname, s, sizeof(dbname));
663 if (!(s = ast_variable_retrieve(config, "general", "dbport"))) {
665 "Postgresql RealTime: No database port found, using 5432 as default.\n");
671 if (dbhost && !(s = ast_variable_retrieve(config, "general", "dbsock"))) {
673 "Postgresql RealTime: No database socket found, using '/tmp/pgsql.sock' as default.\n");
674 strcpy(dbsock, "/tmp/pgsql.sock");
676 ast_copy_string(dbsock, s, sizeof(dbsock));
679 ast_config_destroy(config);
682 ast_log(LOG_DEBUG, "Postgresql RealTime Host: %s\n", dbhost);
683 ast_log(LOG_DEBUG, "Postgresql RealTime Port: %i\n", dbport);
685 ast_log(LOG_DEBUG, "Postgresql RealTime Socket: %s\n", dbsock);
687 ast_log(LOG_DEBUG, "Postgresql RealTime User: %s\n", dbuser);
688 ast_log(LOG_DEBUG, "Postgresql RealTime Password: %s\n", dbpass);
689 ast_log(LOG_DEBUG, "Postgresql RealTime DBName: %s\n", dbname);
694 const char *description(void)
696 return "Postgresql RealTime Configuration Driver";
702 /* Try and get a lock. If unsuccessful, than that means another thread is using the pgsql object. */
703 if (ast_mutex_trylock(&pgsql_lock)) {
704 ast_log(LOG_DEBUG, "Postgresql RealTime: Module usage count is 1.\n");
707 ast_mutex_unlock(&pgsql_lock);
711 const char *key(void)
713 return ASTERISK_GPL_KEY;
716 static int pgsql_reconnect(const char *database)
718 char my_database[50];
720 ast_copy_string(my_database, S_OR(database, dbname), sizeof(my_database));
722 /* mutex lock should have been locked before calling this function. */
724 if (pgsqlConn && PQstatus(pgsqlConn) != CONNECTION_OK) {
729 if ((!pgsqlConn) && (dbhost || dbsock) && dbuser && dbpass && my_database) {
730 char *connInfo = NULL;
731 unsigned int size = 100 + strlen(dbhost)
734 + strlen(my_database);
736 if (!(connInfo = ast_malloc(size)))
739 sprintf(connInfo, "host=%s port=%d dbname=%s user=%s password=%s",
740 dbhost, dbport, my_database, dbuser, dbpass);
741 ast_log(LOG_DEBUG, "%u connInfo=%s\n", size, connInfo);
742 pgsqlConn = PQconnectdb(connInfo);
743 ast_log(LOG_DEBUG, "%u connInfo=%s\n", size, connInfo);
746 ast_log(LOG_DEBUG, "pgsqlConn=%p\n", pgsqlConn);
748 ast_log(LOG_DEBUG, "Postgresql RealTime: Successfully connected to database.\n");
749 connect_time = time(NULL);
753 "Postgresql RealTime: Failed to connect database server %s on %s. Check debug for more info.\n",
755 ast_log(LOG_DEBUG, "Postgresql RealTime: Cannot Connect: %s\n",
756 PQresultErrorMessage(NULL));
760 ast_log(LOG_DEBUG, "Postgresql RealTime: Everything is fine.\n");
765 static int realtime_pgsql_status(int fd, int argc, char **argv)
767 char status[256], status2[100] = "";
768 int ctime = time(NULL) - connect_time;
770 if (pgsqlConn && PQstatus(pgsqlConn) == CONNECTION_OK) {
772 snprintf(status, 255, "Connected to %s@%s, port %d", dbname, dbhost, dbport);
774 snprintf(status, 255, "Connected to %s on socket file %s", dbname, dbsock);
776 snprintf(status, 255, "Connected to %s@%s", dbname, dbhost);
779 if (dbuser && *dbuser) {
780 snprintf(status2, 99, " with username %s", dbuser);
783 if (ctime > 31536000) {
784 ast_cli(fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n",
785 status, status2, ctime / 31536000, (ctime % 31536000) / 86400,
786 (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60);
787 } else if (ctime > 86400) {
788 ast_cli(fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n", status,
789 status2, ctime / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60,
791 } else if (ctime > 3600) {
792 ast_cli(fd, "%s%s for %d hours, %d minutes, %d seconds.\n", status, status2,
793 ctime / 3600, (ctime % 3600) / 60, ctime % 60);
794 } else if (ctime > 60) {
795 ast_cli(fd, "%s%s for %d minutes, %d seconds.\n", status, status2, ctime / 60,
798 ast_cli(fd, "%s%s for %d seconds.\n", status, status2, ctime);
801 return RESULT_SUCCESS;
803 return RESULT_FAILURE;