#include <errno.h>
#include <stdlib.h>
+#include <rrd.h>
+
+#include "args.h"
#include "ctx.h"
#include "daemon.h"
#include "graph.h"
int collecty_graph_render(collecty_graph* self,
const char* object, char** buffer, size_t* length) {
- char* p = NULL;
+ collecty_args* args = NULL;
+ FILE* f = NULL;
+ int w = 0;
+ int h = 0;
int r;
- r = asprintf(&p, "GRAPH");
+ // Log action
+ DEBUG(self->ctx, "Rendering %s...\n", collecty_graph_get_name(self));
+
+ // Open a file handle to the output buffer
+ f = open_memstream(buffer, length);
+ if (!f) {
+ ERROR(self->ctx, "Failed to open memory stream: %m\n");
+ r = -errno;
+ goto ERROR;
+ }
+
+ // Allocate a new argument list
+ r = collecty_args_create(&args, self->ctx);
if (r < 0)
- return r;
+ goto ERROR;
- *buffer = p;
- *length = r;
+ // XXX TODO Fill this with content
- return 0;
+ // Dump the command
+ r = collecty_args_dump(args);
+ if (r < 0)
+ goto ERROR;
+
+ // Render the graph
+ r = rrd_graph(collecty_args_argc(args), (char**)collecty_args_argv(args),
+ NULL, &w, &h, f, NULL, NULL);
+ if (r < 0) {
+ ERROR(self->ctx, "Failed to generate the graph: %s\n", rrd_get_error());
+ rrd_clear_error();
+ r = -ENOTSUP;
+ goto ERROR;
+ }
+
+ // Log action
+ DEBUG(self->ctx, "Rendered graph %s:\n", collecty_graph_get_name(self));
+ DEBUG(self->ctx, " Width : %d\n", w);
+ DEBUG(self->ctx, " Height : %d\n", h);
+
+ERROR:
+ if (args)
+ collecty_args_unref(args);
+ if (f)
+ fclose(f);
+
+ return r;
}