]> git.itanic.dy.fi Git - rrdd/commitdiff
rrdtool: Read last update timestamp from database on startup
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Wed, 30 Sep 2020 17:44:37 +0000 (20:44 +0300)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Wed, 30 Sep 2020 17:44:37 +0000 (20:44 +0300)
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 <timo.t.kokkonen@iki.fi>
rrdtool.c

index 658513be69b309a1e4e017ccacb42cfa7dc396bf..d286963c66ea6c04616ad9b5151a9bd2c530eede 100644 (file)
--- 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);