From 930f125fe8f173527b8ff387b8adea5c6a0f1d88 Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Wed, 30 Sep 2020 20:44:37 +0300 Subject: [PATCH] rrdtool: Read last update timestamp from database on startup Instead of just re-updating all possible databases instantly on the startup, read the last update timestamp from the database and do the update with normal schedule. This is useful if we just happen to restart the daemon and the last update happened just a moment ago. Signed-off-by: Timo Kokkonen --- rrdtool.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/rrdtool.c b/rrdtool.c index 658513b..d286963 100644 --- a/rrdtool.c +++ b/rrdtool.c @@ -388,6 +388,35 @@ static int database_exists(struct rrd_database *db) return 0; } +static int get_last_update(struct rrd_database *db) +{ + char cmd[] = RRDTOOL_CMD; + char *args[10], argstr[ARGSTR_LEN]; + char buf[16]; + int idx = 0, argcnt = 0; + int ofd, efd, child; + int ret; + + add_arg(args, argcnt, argstr, idx, RRDTOOL_CMD); + add_arg(args, argcnt, argstr, idx, "last"); + add_arg(args, argcnt, argstr, idx, db->filename); + + child = run_piped(cmd, args, NULL, &ofd, &efd); + ret = read(ofd, buf, sizeof(buf) - 1); + if (ret < 0) { + pr_err("Error reading: %m\n"); + buf[0] = 0; + } else { + buf[ret] = 0; + } + + db->last_update = atoi(buf); + + clear_zombie(child); + + return 0; +} + static int create_database(struct rrd_database *db) { char cmd[] = RRDTOOL_CMD; @@ -443,6 +472,7 @@ int rrdtool_create_missing_databases(struct rrd_database *dbs[]) for (i = 0, db = dbs[i]; db; i++, db = dbs[i]) { if (database_exists(db)) { pr_info("database %s found\n", db->filename); + get_last_update(db); continue; } pr_info("Database %s missing, creating\n", db->filename); -- 2.45.0