From 7971cc02be59a7d9115a8e436474433807588c6c Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 14 Sep 2022 15:54:37 -0400 Subject: Initial commit --- mstat_export.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 mstat_export.c (limited to 'mstat_export.c') diff --git a/mstat_export.c b/mstat_export.c new file mode 100644 index 0000000..2c51e13 --- /dev/null +++ b/mstat_export.c @@ -0,0 +1,67 @@ +#include "common.h" + +int main(int argc, char *argv[]) { + FILE *fp; + struct mstat_record_t p; + char **fields; + size_t fields_total; + + if (argc < 2) { + fprintf(stderr, "Missing path to *.mstat data file\n"); + exit(1); + } + + if (access(argv[1], F_OK)) { + perror(argv[1]); + exit(1); + } + + fp = mstat_open(argv[1]); + if (!fp) { + perror(argv[1]); + exit(1); + } + + fields = mstat_read_fields(fp); + if (!fields) { + fprintf(stderr, "Unable to obtain field names from %s\n", argv[1]); + exit(1); + } + + fields_total = mstat_get_field_count(fp); + for (size_t i = 0; i < fields_total; i++) { + printf("%s", fields[i]); + if (i < fields_total - 1) { + printf(","); + } + } + puts(""); + + if (mstat_rewind(fp) < 0) { + perror("Unable to rewind"); + exit(1); + } + + while (!mstat_iter(fp, &p)) { + char buf[1024] = {0}; + for (size_t i = 0; i < fields_total; i++) { + struct mstat_record_t *pptr = &p; + union mstat_field_t result; + result = mstat_get_field_by_name(pptr, fields[i]); + + if (!strcmp(fields[i], "timestamp")) { + snprintf(buf, sizeof(buf) - 1, "%lf", result.d64); + } else { + snprintf(buf, sizeof(buf) - 1, "%zu", result.u64); + } + if (i < fields_total - 1) { + strcat(buf, ","); + } + printf("%s", buf); + } + puts(""); + } + + fclose(fp); + return 0; +} \ No newline at end of file -- cgit