#include <time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <string.h>
+
#include "rrdtool.h"
#include "process.h"
while (harvest_zombies(0));
exit(0);
}
+
+static int database_exists(struct rrd_database *db)
+{
+ struct stat s;
+
+ /* If the filename exists, stat will return zero */
+ if (db->filename)
+ return !stat(db->filename, &s);
+
+ return 1;
+}
+
+static int create_database(struct rrd_database *db)
+{
+ char cmd[] = RRDTOOL_CMD;
+// char cmd[] = "echo";
+ char *args[512], argstr[ARGSTR_LEN];
+ int idx = 0, argcnt = 0;
+ int child, i;
+
+ if (!db->sources || !db->archives) {
+ printf("Cannot create db \"%s\", insufficient source data\n",
+ db->filename);
+ return -1;
+ }
+
+ add_arg(args, argcnt, argstr, idx, " ");
+ add_arg(args, argcnt, argstr, idx, "create");
+ add_arg(args, argcnt, argstr, idx, "%s", db->filename);
+ add_arg(args, argcnt, argstr, idx, "--step");
+ add_arg(args, argcnt, argstr, idx, "%d", db->interval);
+
+ for (i = 0; db->sources[i].type; i++) {
+ add_arg(args, argcnt, argstr, idx, "DS:%s:%s:%d:%f:%f",
+ db->sources[i].name,
+ db->sources[i].type,
+ db->sources[i].heartbeat,
+ db->sources[i].min,
+ db->sources[i].max);
+ }
+
+ for (i = 0; db->archives[i].type; i++) {
+ add_arg(args, argcnt, argstr, idx, "RRA:%s:%f:%d:%d",
+ db->archives[i].type,
+ db->archives[i].xff,
+ db->archives[i].steps,
+ db->archives[i].rows);
+ }
+
+ child = run(cmd, args);
+
+ harvest_zombies(child);
+
+ return 0;
+}
+
+int rrdtool_check_databases(struct rrd_database *dbs[])
+{
+ struct rrd_database *db;
+ int i;
+
+ for (i = 0, db = dbs[i]; db; i++, db = dbs[i]) {
+ if (database_exists(db)) {
+ printf("database %s found\n", db->filename);
+ continue;
+ }
+ printf("Database %s missing, creating\n", db->filename);
+ create_database(db);
+ }
+ printf("All done\n");
+
+ return 0;
+}
char **text; /* Null terminated list of text lines */
};
+struct rrd_data_source {
+ char *type; /* Data source type, such as GAUGE or COUNTER */
+ char *name;
+ int heartbeat; /* Heartbeat value in seconds */
+ double min; /* Maximum and minimum values accepted by data source */
+ double max;
+};
+
+struct rrd_archive {
+ char *type; /* Archive type, such as MAX, MIN or AVERAGE */
+ double xff; /* xfiles factor, 0..1 exclusive */
+ int steps; /* How many points needed to construct data point */
+ int rows; /* How many points are kept */
+};
+
struct rrd_database {
char *filename; /* rrd database location */
int interval; /* Update interval */
int (*parse)(char *data); /* Parser to aquire data */
struct rrd_image **images; /* Images to draw */
+
+ struct rrd_data_source *sources; /* These are currently only used */
+ struct rrd_archive *archives; /* generating new rrd database */
+
int last_update; /* When was the data last updated */
char *name; /* Name of the database */
};
int rrdtool_draw_image(struct rrd_image *image);
int rrdtool_draw_images(struct rrd_image **image);
int rrdtool_update_data(struct rrd_database *rrd);
+int rrdtool_check_databases(struct rrd_database *dbs[]);
+
#endif