From 9d369a19b0fa72095ddeccf7dff6e2418043baed Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Wed, 30 Sep 2020 20:15:15 +0300 Subject: [PATCH] rrdtool: Use the same timestamp with rrdtool and with logs Acquiring the data and updating rrdtool database takes some time. Use the same timestamp for both so that the times are consistent. Signed-off-by: Timo Kokkonen --- rrdtool.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/rrdtool.c b/rrdtool.c index e347400..658513b 100644 --- a/rrdtool.c +++ b/rrdtool.c @@ -196,9 +196,8 @@ static int sanitize_rrd_update_data(char *data) return entries; } -static int write_to_logfile(struct rrd_database *rrd, const char *data) +static int write_to_logfile(struct rrd_database *rrd, const char *data, time_t now) { - time_t t = time(NULL); int fd, ret; int spacing, i; char filename[1024]; @@ -212,7 +211,7 @@ static int write_to_logfile(struct rrd_database *rrd, const char *data) if (rrd->logfile_timestamp_fmt) time_stamp_fmt = rrd->logfile_timestamp_fmt; - strftime(filename, sizeof(filename), rrd->logfile, localtime(&t)); + strftime(filename, sizeof(filename), rrd->logfile, localtime(&now)); fd = open(filename, O_RDWR | O_APPEND | O_CREAT | O_CLOEXEC, 0644); if (fd < 0) { @@ -220,7 +219,7 @@ static int write_to_logfile(struct rrd_database *rrd, const char *data) return -1; } - strftime(logstr, sizeof(logstr), time_stamp_fmt, localtime(&t)); + strftime(logstr, sizeof(logstr), time_stamp_fmt, localtime(&now)); str_ptr = logstr + strlen(logstr); @@ -279,9 +278,10 @@ static int do_rrdtool_update_data(struct rrd_database *rrd) 0 }; int l; + time_t now = time(NULL); bzero(data, sizeof(data)); - l = sprintf(data, "%zd:", time(NULL)); + l = sprintf(data, "%zd:", now); if (rrd->parser && rrd->parser->parse) { rrd->parser->parse(data + l, rrd->parser_data, @@ -291,7 +291,7 @@ static int do_rrdtool_update_data(struct rrd_database *rrd) pr_info("Data: %s\n", data); sanitize_rrd_update_data(data + l); - write_to_logfile(rrd, data); + write_to_logfile(rrd, data, now); run(cmd, cmdline); } -- 2.44.0