about summary refs log tree commit diff
path: root/main.c
blob: 34bf944c75e7d9e5b7145f0d0aca518af491e6ee (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#define _POSIX_C_SOURCE 200809L

#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.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;
  bool daemonize;
  const char *pidfile;
};

const struct config default_config = {
  .port = "9100",
  .daemonize = true,
  .pidfile = 0,
};

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 bool daemonize(struct config *cfg);
static void handler(scrape_req *req, void *ctx_ptr);

int main(int argc, char *argv[]) {
  struct config cfg = default_config;
  struct handler_ctx ctx;

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

  scrape_server *server = scrape_listen(cfg.port);
  if (!server)
    return 1;

  if (cfg.daemonize)
    if (!daemonize(&cfg))
      return 1;

  scrape_serve(server, handler, &ctx);
  scrape_close(server);

  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;
    } else if (strcmp(argv[arg], "--foreground") == 0) {
      cfg->daemonize = false;
      goto next_arg;
    } else if (strncmp(argv[arg], "--pidfile=", 10) == 0) {
      cfg->pidfile = &argv[arg][10];
      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 bool daemonize(struct config *cfg) {
  pid_t pid;

  pid = fork();
  if (pid == -1) {
    perror("fork");
    return false;
  }

  if (pid > 0) {
    // in the parent, verify the intermediate process exits successfully
    int status;
    if (waitpid(pid, &status, 0) == -1) {
      perror("waitpid(daemon)");
      exit(1);
    }
    if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
      fprintf(stderr, "daemonized process exited unexpectedly: %x\n", status);
      exit(1);
    }
    exit(0);
  }

  // in the intermediate process, prepare for daemon life and fork again

  if (setsid() == -1) {
    perror("setsid");
    exit(1);
  }

  close(0);
  close(1);
  close(2);

  pid = fork();
  if (pid == -1)
    exit(2);
  else if (pid > 0)
    exit(0);  // lets the parent know all is well

  // attempt to record the grandchild PID, if asked to

  if (cfg->pidfile) {
    FILE *f = fopen(cfg->pidfile, "w");
    if (f) {
      fprintf(f, "%ld\n", (long) getpid());
      fclose(f);
    }
  }

  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);
  }
}