aboutsummaryrefslogtreecommitdiff
path: root/mstat_export.c
blob: 2c51e13778109052b11f855ef0ef3fcebb8054da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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;
}