mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-06 12:36:58 +00:00
cleanup the show uptime code, and minor changes
to patch 6274 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@8161 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
130
cli.c
130
cli.c
@@ -278,118 +278,70 @@ static char uptime_help[] =
|
|||||||
" Shows Asterisk uptime information.\n"
|
" Shows Asterisk uptime information.\n"
|
||||||
" The seconds word returns the uptime in seconds only.\n";
|
" The seconds word returns the uptime in seconds only.\n";
|
||||||
|
|
||||||
static char *format_uptimestr(time_t timeval)
|
static void print_uptimestr(int fd, time_t timeval, const char *prefix, int printsec)
|
||||||
{
|
{
|
||||||
int years = 0, weeks = 0, days = 0, hours = 0, mins = 0, secs = 0;
|
int x; /* the main part - years, weeks, etc. */
|
||||||
char timestr[256]="";
|
char timestr[256]="", *s = timestr;
|
||||||
int bytes = 0;
|
size_t maxbytes = sizeof(timestr);
|
||||||
int maxbytes = 0;
|
|
||||||
int offset = 0;
|
|
||||||
#define SECOND (1)
|
#define SECOND (1)
|
||||||
#define MINUTE (SECOND*60)
|
#define MINUTE (SECOND*60)
|
||||||
#define HOUR (MINUTE*60)
|
#define HOUR (MINUTE*60)
|
||||||
#define DAY (HOUR*24)
|
#define DAY (HOUR*24)
|
||||||
#define WEEK (DAY*7)
|
#define WEEK (DAY*7)
|
||||||
#define YEAR (DAY*365)
|
#define YEAR (DAY*365)
|
||||||
#define ESS(x) ((x == 1) ? "" : "s")
|
#define ESS(x) ((x == 1) ? "" : "s") /* plural suffix */
|
||||||
|
#define NEEDCOMMA(x) ((x)? ",": "") /* define if we need a comma */
|
||||||
maxbytes = sizeof(timestr);
|
if (timeval < 0) /* invalid, nothing to show */
|
||||||
if (timeval < 0)
|
return;
|
||||||
return NULL;
|
if (printsec) { /* plain seconds output */
|
||||||
|
ast_build_string(&s, &maxbytes, "%lu", (u_long)timeval);
|
||||||
|
timeval = 0; /* bypass the other cases */
|
||||||
|
}
|
||||||
if (timeval > YEAR) {
|
if (timeval > YEAR) {
|
||||||
years = (timeval / YEAR);
|
x = (timeval / YEAR);
|
||||||
timeval -= (years * YEAR);
|
timeval -= (x * YEAR);
|
||||||
if (years > 0) {
|
ast_build_string(&s, &maxbytes, "%d year%s%s ", x, ESS(x),NEEDCOMMA(timeval));
|
||||||
snprintf(timestr + offset, maxbytes, "%d year%s, ", years, ESS(years));
|
|
||||||
bytes = strlen(timestr + offset);
|
|
||||||
offset += bytes;
|
|
||||||
maxbytes -= bytes;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (timeval > WEEK) {
|
if (timeval > WEEK) {
|
||||||
weeks = (timeval / WEEK);
|
x = (timeval / WEEK);
|
||||||
timeval -= (weeks * WEEK);
|
timeval -= (x * WEEK);
|
||||||
if (weeks > 0) {
|
ast_build_string(&s, &maxbytes, "%d week%s%s ", x, ESS(x),NEEDCOMMA(timeval));
|
||||||
snprintf(timestr + offset, maxbytes, "%d week%s, ", weeks, ESS(weeks));
|
|
||||||
bytes = strlen(timestr + offset);
|
|
||||||
offset += bytes;
|
|
||||||
maxbytes -= bytes;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (timeval > DAY) {
|
if (timeval > DAY) {
|
||||||
days = (timeval / DAY);
|
x = (timeval / DAY);
|
||||||
timeval -= (days * DAY);
|
timeval -= (x * DAY);
|
||||||
if (days > 0) {
|
ast_build_string(&s, &maxbytes, "%d day%s%s ", x, ESS(x),NEEDCOMMA(timeval));
|
||||||
snprintf(timestr + offset, maxbytes, "%d day%s, ", days, ESS(days));
|
|
||||||
bytes = strlen(timestr + offset);
|
|
||||||
offset += bytes;
|
|
||||||
maxbytes -= bytes;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (timeval > HOUR) {
|
if (timeval > HOUR) {
|
||||||
hours = (timeval / HOUR);
|
x = (timeval / HOUR);
|
||||||
timeval -= (hours * HOUR);
|
timeval -= (x * HOUR);
|
||||||
if (hours > 0) {
|
ast_build_string(&s, &maxbytes, "%d hour%s%s ", x, ESS(x),NEEDCOMMA(timeval));
|
||||||
snprintf(timestr + offset, maxbytes, "%d hour%s, ", hours, ESS(hours));
|
|
||||||
bytes = strlen(timestr + offset);
|
|
||||||
offset += bytes;
|
|
||||||
maxbytes -= bytes;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (timeval > MINUTE) {
|
if (timeval > MINUTE) {
|
||||||
mins = (timeval / MINUTE);
|
x = (timeval / MINUTE);
|
||||||
timeval -= (mins * MINUTE);
|
timeval -= (x * MINUTE);
|
||||||
if (mins > 0) {
|
ast_build_string(&s, &maxbytes, "%d minute%s%s ", x, ESS(x),NEEDCOMMA(timeval));
|
||||||
snprintf(timestr + offset, maxbytes, "%d minute%s, ", mins, ESS(mins));
|
|
||||||
bytes = strlen(timestr + offset);
|
|
||||||
offset += bytes;
|
|
||||||
maxbytes -= bytes;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
secs = timeval;
|
x = timeval;
|
||||||
|
if (x > 0)
|
||||||
if (secs > 0) {
|
ast_build_string(&s, &maxbytes, "%d second%s ", x, ESS(x));
|
||||||
snprintf(timestr + offset, maxbytes, "%d second%s", secs, ESS(secs));
|
if (timestr[0] != '\0')
|
||||||
}
|
ast_cli(fd, "%s: %s\n", prefix, timestr);
|
||||||
|
|
||||||
return timestr ? strdup(timestr) : NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int handle_showuptime(int fd, int argc, char *argv[])
|
static int handle_showuptime(int fd, int argc, char *argv[])
|
||||||
{
|
{
|
||||||
time_t curtime, tmptime;
|
/* 'show uptime [seconds]' */
|
||||||
char *timestr;
|
time_t curtime = time(NULL);
|
||||||
int printsec;
|
int printsec = (argc == 3 && !strcasecmp(argv[2],"seconds"));
|
||||||
|
|
||||||
printsec = ((argc == 3) && (!strcasecmp(argv[2],"seconds")));
|
if (argc != 2 && !printsec)
|
||||||
if ((argc != 2) && (!printsec))
|
|
||||||
return RESULT_SHOWUSAGE;
|
return RESULT_SHOWUSAGE;
|
||||||
|
if (ast_startuptime)
|
||||||
time(&curtime);
|
print_uptimestr(fd, curtime - ast_startuptime, "System uptime", printsec);
|
||||||
if (ast_startuptime) {
|
if (ast_lastreloadtime)
|
||||||
tmptime = curtime - ast_startuptime;
|
print_uptimestr(fd, curtime - ast_lastreloadtime, "Last reload", printsec);
|
||||||
if (printsec) {
|
|
||||||
ast_cli(fd, "System uptime: %lu\n",(u_long)tmptime);
|
|
||||||
} else {
|
|
||||||
timestr = format_uptimestr(tmptime);
|
|
||||||
if (timestr) {
|
|
||||||
ast_cli(fd, "System uptime: %s\n", timestr);
|
|
||||||
free(timestr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (ast_lastreloadtime) {
|
|
||||||
tmptime = curtime - ast_lastreloadtime;
|
|
||||||
if (printsec) {
|
|
||||||
ast_cli(fd, "Last reload: %lu\n", (u_long) tmptime);
|
|
||||||
} else {
|
|
||||||
timestr = format_uptimestr(tmptime);
|
|
||||||
if ((timestr) && (!printsec)) {
|
|
||||||
ast_cli(fd, "Last reload: %s\n", timestr);
|
|
||||||
free(timestr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return RESULT_SUCCESS;
|
return RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user