]> git.ipfire.org Git - collecty.git/commitdiff
module: Create scaffolding to create the database if it does not exist
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 18:04:38 +0000 (18:04 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Sep 2025 18:04:38 +0000 (18:04 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/module.c

index 8b9cd6ae5dabcbdbffdcdbefb05256981c7ae91b..0af412a3cf482140645017c10e6cb985fe7099f7 100644 (file)
@@ -23,6 +23,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/stat.h>
 
 #include <rrd.h>
 
@@ -268,11 +269,20 @@ int collecty_module_submit(collecty_module* self,
        return collecty_daemon_submit(self->daemon, self, object, value);
 }
 
+static int collecty_module_create_database(collecty_module* self, const char* path) {
+       DEBUG(self->ctx, "Creating database for %s at %s\n", collecty_module_name(self), path);
+
+       // XXX TODO
+
+       return 0;
+}
+
 /*
        Called to write all collected samples to disk
 */
 int collecty_module_commit(collecty_module* self,
                const char* object, unsigned int num_samples, const char** samples) {
+       struct stat st = {};
        char path[PATH_MAX];
        int r;
 
@@ -281,6 +291,21 @@ int collecty_module_commit(collecty_module* self,
        if (r < 0)
                return r;
 
+       // Try to stat() the file
+       r = stat(path, &st);
+       if (r < 0) {
+               switch (errno) {
+                       case ENOENT:
+                               r = collecty_module_create_database(self, path);
+                               if (r < 0)
+                                       return r;
+                               break;
+
+                       default:
+                               return -errno;
+               }
+       }
+
        // Write the samples
        r = rrd_update_r(path, NULL, num_samples, samples);
        if (r < 0) {