about summary refs log tree commit diff
path: root/main.c
blob: aba42692a62b6c9966339693f584a4e0efa10fce (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#define _POSIX_C_SOURCE 200809L

#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>

#include "collector.h"
#include "scrape.h"
#include "util.h"

#ifndef XCOLLECTORS
#define XCOLLECTORS \
  X(cpu) X(diskstats) X(filesystem) X(hwmon) \
  X(meminfo) X(network) X(textfile) X(uname)
#endif

#define X(c) extern const struct collector c##_collector;
XCOLLECTORS
#undef X

#define X(c) &c##_collector,
static const struct collector *collectors[] = {
  XCOLLECTORS
};
#undef X
#define NCOLLECTORS (sizeof collectors / sizeof *collectors)

enum tristate { flag_off = -1, flag_undef = 0, flag_on = 1 };

struct config {
  const char *port;
};

struct handler_ctx {
  struct {
    enum tristate enabled;
    struct slist *args;
    struct slist **next_arg;
    void *ctx;
  } collectors[NCOLLECTORS];
};

static bool parse_args(int argc, char *argv[], struct config *cfg, struct handler_ctx *ctx);
static bool initialize(struct handler_ctx *ctx, int max_args);
static void handler(scrape_req *req, void *ctx_ptr);

int main(int argc, char *argv[]) {
  struct config cfg = {
    .port = "9100",
  };
  struct handler_ctx ctx;

  if (!parse_args(argc, argv, &cfg, &ctx))
    return 1;
  if (!initialize(&ctx, argc > 0 ? argc - 1 : 0))
    return 1;
  if (!scrape_serve(cfg.port, handler, &ctx))
    return 1;

  return 0;
};

static bool parse_args(int argc, char *argv[], struct config *cfg, struct handler_ctx *ctx) {
  for (size_t i = 0; i < NCOLLECTORS; i++) {
    ctx->collectors[i].args = 0;
    ctx->collectors[i].next_arg = &ctx->collectors[i].args;
    ctx->collectors[i].enabled = flag_undef;
  }

  enum tristate enabled_default = flag_on;

  for (int arg = 1; arg < argc; arg++) {
    // check for collector arguments

    for (size_t i = 0; i < NCOLLECTORS; i++) {
      size_t name_len = strlen(collectors[i]->name);
      if (strncmp(argv[arg], "--", 2) == 0
          && strncmp(argv[arg] + 2, collectors[i]->name, name_len) == 0
          && argv[arg][2 + name_len] == '-') {
        char *carg = argv[arg] + 2 + name_len + 1;
        if (strcmp(carg, "on") == 0) {
          ctx->collectors[i].enabled = flag_on;
          enabled_default = flag_off;
        } else if (strcmp(carg, "off") == 0) {
          ctx->collectors[i].enabled = flag_off;
        } else if (collectors[i]->init && collectors[i]->has_args) {
          ctx->collectors[i].next_arg = slist_append(ctx->collectors[i].next_arg, carg);
        } else {
          fprintf(stderr, "unknown argument: %s (collector %s takes no arguments)\n", argv[arg], collectors[i]->name);
          return false;
        }
        goto next_arg;
      }
    }

    // parse any non-collector arguments

    // TODO --help
    if (strncmp(argv[arg], "--port=", 7) == 0) {
      cfg->port = &argv[arg][7];
      goto next_arg;
    }

    fprintf(stderr, "unknown argument: %s\n", argv[arg]);
    return false;
 next_arg: ;
  }

  for (size_t i = 0; i < NCOLLECTORS; i++)
    if (ctx->collectors[i].enabled == flag_undef)
      ctx->collectors[i].enabled = enabled_default;

  return true;
}

static bool initialize(struct handler_ctx *ctx, int max_args) {
  int argc;
  char *argv[max_args + 1];

  for (size_t i = 0; i < NCOLLECTORS; i++) {
    if (ctx->collectors[i].enabled == flag_on && collectors[i]->init) {
      argc = 0;
      for (struct slist *arg = ctx->collectors[i].args; arg && argc < max_args; arg = arg->next)
        argv[argc++] = arg->data;
      argv[argc] = 0;

      ctx->collectors[i].ctx = collectors[i]->init(argc, argv);

      if (!ctx->collectors[i].ctx) {
        fprintf(stderr, "failed to initialize collector %s\n", collectors[i]->name);
        return false;
      }
    }
  }

  return true;
}

static void handler(scrape_req *req, void *ctx_ptr) {
  struct handler_ctx *ctx = ctx_ptr;

  for (size_t c = 0; c < NCOLLECTORS; c++) {
    if (ctx->collectors[c].enabled == flag_on)
      collectors[c]->collect(req, ctx->collectors[c].ctx);
  }
}