about summary refs log tree commit diff
path: root/timex.c
blob: c5e94d466e28879961a0baa12986ef1a8e8e94d8 (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
#define _GNU_SOURCE
#include <sys/timex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

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

#define NTP_PPS 0

// size of input buffer for paths and lines
#define BUF_SIZE 256

static void timex_collect(scrape_req *req, void *ctx);

const struct collector timex_collector = {
  .name = "timex",
  .collect = timex_collect,
};

static void timex_collect(scrape_req *req, void *ctx) {
  (void) ctx;

  struct timespec ts;
  struct timex buf = { 0 };

  int r = adjtimex(&buf);
  if (r < 0)
    return;

  scrape_write(req, "node_timex_sync_status", 0, r != TIME_ERROR);

  scrape_write(req, "node_timex_estimated_error_seconds", 0, buf.esterror / 1e6);
  scrape_write(req, "node_timex_frequency_adjustment_ratio", 0, 1 + buf.freq/(1000000.0 * 65536.0));
  scrape_write(req, "node_timex_loop_time_constant", 0, (double)buf.constant);
  scrape_write(req, "node_timex_maxerror_seconds", 0, buf.maxerror / 1e6);
  scrape_write(req, "node_timex_offset_seconds", 0, buf.offset / 1e6);
  scrape_write(req, "node_timex_status", 0, (double)buf.status);
  scrape_write(req, "node_timex_tai_offset_seconds", 0, (double)buf.tai);
  scrape_write(req, "node_timex_tick_seconds", 0, buf.tick / 1e6);

  scrape_write(req, "node_time_seconds", 0, buf.time.tv_sec + buf.time.tv_usec / 1e6);

  if (clock_gettime(CLOCK_TAI, &ts) == 0)
    scrape_write(req, "node_clock_tai_seconds", 0, (double)ts.tv_sec + ts.tv_nsec / 1e9);

  if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
    scrape_write(req, "node_clock_monotonic_seconds", 0, (double)ts.tv_sec + ts.tv_nsec / 1e9);

  if (clock_gettime(CLOCK_BOOTTIME, &ts) == 0)
    scrape_write(req, "node_clock_boottime_seconds", 0, (double)ts.tv_sec + ts.tv_nsec / 1e9);

#if NTP_PPS
  scrape_write(req, "node_timex_pps_calibraton_total", 0, (double)buf.calcnt);
  scrape_write(req, "node_timex_pps_error_total", 0, (double)buf.errcnt);
  scrape_write(req, "node_timex_pps_frequency_hertz", 0, buf.ppsfreq / (1000000.0 * 65536.0));
  scrape_write(req, "node_timex_pps_jitter_seconds", 0, buf.jitter / 1e6);
  scrape_write(req, "node_timex_pps_jitter_total", 0, (double)buf.jitcnt);
  scrape_write(req, "node_timex_pps_shift_seconds", 0, (double)buf.shift);
  scrape_write(req, "node_timex_pps_stability_exceeded_total", 0, (double)buf.stbcnt);
  scrape_write(req, "node_timex_pps_stability_hertz", 0, buf.stabil / (1000000.0 * 65536.0));
#endif
}