Skip to content

Commit

Permalink
New parameter timestamp-format to change the timestamp output by use …
Browse files Browse the repository at this point in the history
…-D -l
  • Loading branch information
gsnw-sebast committed Jun 1, 2024
1 parent 46ee105 commit bf150b4
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions src/fping.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ int per_recv_flag, report_all_rtts_flag, name_flag, addr_flag, backoff_flag, rdn
int multif_flag, timeout_flag, fast_reachable;
int outage_flag = 0;
int timestamp_flag = 0;
int timestamp_format_flag = 0;
int random_data_flag = 0;
int cumulative_stats_flag = 0;
#if defined(DEBUG) || defined(_DEBUG)
Expand Down Expand Up @@ -400,6 +401,7 @@ void host_add_timeout_event(HOST_ENTRY *h, int index, int64_t ev_time);
struct event *host_get_timeout_event(HOST_ENTRY *h, int index);
void stats_add(HOST_ENTRY *h, int index, int success, int64_t latency);
void update_current_time();
void print_timestamp_format(int64_t current_time_ns, int timestamp_format);

/************************************************************
Expand Down Expand Up @@ -522,6 +524,7 @@ int main(int argc, char **argv)
{ "vcount", 'C', OPTPARSE_REQUIRED },
{ "rdns", 'd', OPTPARSE_NONE },
{ "timestamp", 'D', OPTPARSE_NONE },
{ "timestamp-format", '0', OPTPARSE_REQUIRED },
{ "elapsed", 'e', OPTPARSE_NONE },
{ "file", 'f', OPTPARSE_REQUIRED },
{ "generate", 'g', OPTPARSE_NONE },
Expand Down Expand Up @@ -562,14 +565,17 @@ int main(int argc, char **argv)
while ((c = optparse_long(&optparse_state, longopts, NULL)) != EOF) {
switch (c) {
case '0':
/*
* Use long-option example
* Define "struct optparse_long longopts": { "long-option-name", '0', OPTPARSE_NONE }
*
* if(strstr(optparse_state.optlongname, "long-option-name") != NULL) {
* long_option_flag = 1;
* }
*/
if(strstr(optparse_state.optlongname, "timestamp-format") != NULL) {
if(strcmp(optparse_state.optarg, "ctime") == 0) {
timestamp_format_flag = 1;
}else if(strcmp(optparse_state.optarg, "iso") == 0) {
timestamp_format_flag = 2;
}else if(strcmp(optparse_state.optarg, "reltime") == 0) {
timestamp_format_flag = 3;
}else{
usage(1);
}
}
break;
case '4':
#ifdef IPV6
Expand Down Expand Up @@ -1385,7 +1391,7 @@ void main_loop()

if (per_recv_flag) {
if (timestamp_flag) {
printf("[%.5f] ", (double)current_time_ns / 1e9);
print_timestamp_format(current_time_ns, timestamp_format_flag);
}
printf("%-*s : [%d], timed out",
max_hostname_len, h->host, event->ping_index);
Expand Down Expand Up @@ -2498,7 +2504,7 @@ int wait_for_reply(int64_t wait_time)
/* print received ping (unless --quiet) */
if (per_recv_flag) {
if (timestamp_flag) {
printf("[%.5f] ", (double)recv_time / 1e9);
print_timestamp_format(recv_time, timestamp_format_flag);
}
avg = h->total_time / h->num_recv;
printf("%-*s : [%d], %d bytes, %s ms",
Expand Down Expand Up @@ -2949,6 +2955,34 @@ void ev_remove(struct event_queue *queue, struct event *event)
event->ev_next = NULL;
}

/************************************************************
Function: print_human_readable_time from current_time_ns
*************************************************************/
void print_timestamp_format(int64_t current_time_ns, int timestamp_format)
{
char time_buffer[100];
time_t current_time_s;
struct tm *local_time;

if(timestamp_format >= 1 && timestamp_format <= 3) {
current_time_s = current_time_ns / 1000000000;
local_time = localtime(&current_time_s);
if(timestamp_format == 1) {
strftime(time_buffer, sizeof(time_buffer), "%c", local_time);
}else if(timestamp_format == 2) {
strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%dT%T%z", local_time);
}else if(timestamp_format == 3) {
strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d %H:%M:%S", local_time);
}
printf("[%s] ", time_buffer);
}else{
printf("[%.5f] ", (double)current_time_ns / 1e9);
}

}

/************************************************************
Function: usage
Expand Down Expand Up @@ -3002,6 +3036,7 @@ void usage(int is_error)
fprintf(out, " -C, --vcount=N same as -c, report results (not stats) in verbose format\n");
fprintf(out, " -d, --rdns show targets by name (force reverse-DNS lookup)\n");
fprintf(out, " -D, --timestamp print timestamp before each output line\n");
fprintf(out, " --timestamp-format=FORMAT show timestamp using the given format (-D are required): [ctime|iso|reltime]\n");
fprintf(out, " -e, --elapsed show elapsed time on return packets\n");
fprintf(out, " -n, --name show targets by name (reverse-DNS lookup for target IPs)\n");
fprintf(out, " -N, --netdata output compatible for netdata (-l -Q are required)\n");
Expand Down

0 comments on commit bf150b4

Please sign in to comment.