#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <sys/stat.h>
#include <rrd.h>
return 0;
}
-static int td_source_migrate(td_source* self, const char* path) {
- rrd_info_t* info = NULL;
+static int td_source_migrate(td_source* self, const char* path, rrd_info_t* info) {
int r;
- // Fetch information from the RRD file
- info = rrd_info_r(path);
-
// Does this RRD need migration?
r = td_source_needs_migration(self, path, info);
if (r < 0) {
ERROR(self->ctx, "Failed to check whether %s needs migration: %s\n", path, strerror(-r));
- goto ERROR;
-
- // Migrate
- } else if (r) {
- DEBUG(self->ctx, "%s needs migration!\n", path);
-
- // Create a new database but use the old one as source
- r = td_source_create_database(self, path, path);
- if (r < 0)
- goto ERROR;
+ return r;
}
-ERROR:
- if (info)
- rrd_info_free(info);
+ // No need to migrate
+ if (r == 0)
+ return 0;
- return r;
+ // Migrate the database
+ return td_source_create_database(self, path, path);
}
/*
*/
static int td_source_commit_samples(td_source* self,
const char* object, unsigned int num_samples, const char** samples) {
- struct stat st = {};
+ rrd_info_t* info = NULL;
char path[PATH_MAX];
int r;
if (r < 0)
return r;
- // Try to stat() the file
- r = stat(path, &st);
- if (r < 0) {
+ // Fetch information from the RRD file
+ info = rrd_info_r(path);
+ if (!info) {
switch (errno) {
case ENOENT:
r = td_source_create_database(self, path, NULL);
if (r < 0)
- return r;
+ goto ERROR;
break;
default:
- return -errno;
+ ERROR(self->ctx, "Failed to read RRD header from %s: %m\n", path);
+ r = -errno;
+ goto ERROR;
}
}
// Migrate the RRD file
- r = td_source_migrate(self, path);
+ r = td_source_migrate(self, path, info);
if (r < 0)
- return r;
+ goto ERROR;
// Write the samples
r = rrd_update_r(path, NULL, num_samples, samples);
return -EFAULT;
}
- return 0;
+ERROR:
+ if (info)
+ rrd_info_free(info);
+
+ return r;
}
int td_source_commit_metrics(td_source* self, const char* object,