Skip to content

Commit

Permalink
Add OpenMetrics API
Browse files Browse the repository at this point in the history
  • Loading branch information
zuckschwerdt committed Mar 4, 2024
1 parent b2a9bf3 commit aa89887
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 11 deletions.
19 changes: 14 additions & 5 deletions include/rtl_433.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,20 @@ typedef struct r_cfg {
char const *sr_filename;
int sr_execopen;
int watchdog; ///< SDR acquire stall watchdog
/* stats*/
time_t frames_since; ///< stats start time
unsigned frames_count; ///< stats counter for interval
unsigned frames_fsk; ///< stats counter for interval
unsigned frames_events; ///< stats counter for interval
/* global stats */
time_t running_since; ///< program start time statistic
unsigned total_frames_count; ///< total frames recieved statistic
unsigned total_frames_squelch; ///< total frames with noise only statistic
unsigned total_frames_ook; ///< total frames with ook demod statistic
unsigned total_frames_fsk; ///< total frames with fsk demod statistic
unsigned total_frames_events; ///< total frames with decoder events statistic
/* sdr stats */
time_t sdr_since; ///< time of last SDR connect statistic
/* per report interval stats */
time_t frames_since; ///< time at start of report interval statistic
unsigned frames_ook; ///< counter of ook demods for report interval statistic
unsigned frames_fsk; ///< counter of fsk demods for report interval statistic
unsigned frames_events; ///< counter of decoder events for report interval statistic
struct mg_mgr *mgr;
} r_cfg_t;

Expand Down
134 changes: 134 additions & 0 deletions src/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,137 @@ static void handle_redirect(struct mg_connection *nc, struct http_message *hm)
"\r\n\r\n");
}

static void handle_openmetrics(struct mg_connection *nc, struct http_message *hm)
{
if (mg_vcmp(&hm->method, "GET") != 0) {
mg_http_send_error(nc, 405, NULL); // 405 Method Not Allowed
return;
}

struct http_server_context *ctx = nc->user_data;
r_cfg_t *cfg = ctx->cfg;

time_t now;
time(&now);
int decode_events = 0;
int decode_ok = 0;
int decode_messages = 0;
int decode_fail_other = 0;
int decode_abort_length = 0;
int decode_abort_early = 0;
int decode_fail_mic = 0;
int decode_fail_sanity = 0;

// FIXME: these are not runtime totals but reset each report interval
list_t *r_devs = &cfg->demod->r_devs;
for (void **iter = r_devs->elems; iter && *iter; ++iter) {
r_device *r_dev = *iter;
decode_events += r_dev->decode_events;
decode_ok += r_dev->decode_ok;
decode_messages += r_dev->decode_messages;
decode_fail_other += r_dev->decode_fails[-DECODE_FAIL_OTHER];
decode_abort_length += r_dev->decode_fails[-DECODE_ABORT_LENGTH];
decode_abort_early += r_dev->decode_fails[-DECODE_ABORT_EARLY];
decode_fail_mic += r_dev->decode_fails[-DECODE_FAIL_MIC];
decode_fail_sanity += r_dev->decode_fails[-DECODE_FAIL_SANITY];
}

char buf[3000];
int len = snprintf(buf, sizeof(buf),
"# TYPE uptime_seconds counter\n"
"# UNIT uptime_seconds seconds\n"
"# HELP uptime_seconds Program uptime.\n"
"uptime_seconds_total %.1f\n"
"uptime_seconds_created %.1f\n"
"# TYPE decoder_enabled gauge\n"
"# HELP decoder_enabled Number of enabled decoders.\n"
"decoder_enabled %u\n"
"# TYPE input_uptime_seconds counter\n"
"# UNIT input_uptime_seconds seconds\n"
"# HELP input_uptime_seconds SDR Receiver uptime.\n"
"input_uptime_seconds_total %.1f\n"
"input_uptime_seconds_created %.1f\n"
"# TYPE input_count_frames counter\n"
"# UNIT input_count_frames frames\n"
"# HELP input_count_frames Number of SDR frames received.\n"
"input_count_frames_total %u\n"
"# TYPE input_squelch_frames counter\n"
"# UNIT input_squelch_frames frames\n"
"# HELP input_squelch_frames Number of SDR frames skipped by squelch.\n"
"input_squelch_frames_total %u\n"
"# TYPE input_ook_frames counter\n"
"# UNIT input_ook_frames frames\n"
"# HELP input_ook_frames Number of SDR frames with OOK demodulation.\n"
"input_ook_frames_total %u\n"
"# TYPE input_fsk_frames counter\n"
"# UNIT input_fsk_frames frames\n"
"# HELP input_fsk_frames Number of SDR frames with FSK demodulation.\n"
"input_fsk_frames_total %u\n"
"# TYPE input_event_frames counter\n"
"# UNIT input_event_frames frames\n"
"# HELP input_event_frames Number of SDR frames with decode events.\n"
"input_event_frames_total %u\n"
"# TYPE decode_events counter\n"
"# UNIT decode_events events\n"
"# HELP decode_events Number of all decoder events.\n"
"decode_events_total %d\n"
"# TYPE decode_ok_events counter\n"
"# UNIT decode_ok_events events\n"
"# HELP decode_ok_events Number of decoder OK events.\n"
"decode_ok_events_total %d\n"
"# TYPE decode_messages_events counter\n"
"# UNIT decode_messages_events events\n"
"# HELP decode_messages_events Number of decoder message events.\n"
"decode_messages_events_total %d\n"
"# TYPE decode_abort_early_events counter\n"
"# UNIT decode_abort_early_events events\n"
"# HELP decode_abort_early_events Number of abort early decoder events.\n"
"decode_abort_early_events_total %d\n"
"# TYPE decode_abort_length_events counter\n"
"# UNIT decode_abort_length_events events\n"
"# HELP decode_abort_length_events Number of abort length decoder events.\n"
"decode_abort_length_events_total %d\n"
"# TYPE decode_fail_sanity_events counter\n"
"# UNIT decode_fail_sanity_events events\n"
"# HELP decode_fail_sanity_events Number of fail sanity decoder events.\n"
"decode_fail_sanity_events_total %d\n"
"# TYPE decode_fail_mic_events counter\n"
"# UNIT decode_fail_mic_events events\n"
"# HELP decode_fail_mic_events Number of fail MIC decoder events.\n"
"decode_fail_mic_events_total %d\n"
"# TYPE decode_fail_other_events counter\n"
"# UNIT decode_fail_other_events events\n"
"# HELP decode_fail_other_events Number of fail other decoder events.\n"
"decode_fail_other_events_total %d\n"
"# EOF\n",
(float)(now - cfg->running_since), // uptime_seconds_total,
(float)cfg->running_since, // uptime_seconds_created,
(unsigned)cfg->demod->r_devs.len, // decoder_enabled,
(float)(now - cfg->sdr_since), // input_uptime_seconds_total,
(float)cfg->sdr_since, // input_uptime_seconds_created,
cfg->total_frames_count, // input_count_frames_total,
cfg->total_frames_squelch, // input_squelch_frames_total,
cfg->total_frames_ook, // input_ook_frames_total,
cfg->total_frames_fsk, // input_fsk_frames_total,
cfg->total_frames_events, // input_event_frames_total,
decode_events, // decode_events_total,
decode_ok, // decode_ok_events_total,
decode_messages, // decode_messages_events_total,
decode_abort_early, // decode_abort_early_events_total,
decode_abort_length, // decode_abort_length_events_total,
decode_fail_sanity, // decode_fail_sanity_events_total,
decode_fail_mic, // decode_fail_mic_events_total
decode_fail_other); // decode_fail_other_events_total,

mg_printf(nc,
"HTTP/1.1 200 OK\r\n"
"Content-Length: %u\r\n"
"\r\n",
len);
mg_send(nc, buf, (size_t)len);
nc->flags |= MG_F_SEND_AND_CLOSE;
}

// reply to ws command
static void rpc_response_ws(rpc_t *rpc, int ret_code, char const *message, int arg)
{
Expand Down Expand Up @@ -1039,6 +1170,9 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data)
else if (mg_vcmp(&hm->uri, "/stream") == 0) {
handle_json_stream(nc, hm);
}
else if (mg_vcmp(&hm->uri, "/metrics") == 0) {
handle_openmetrics(nc, hm);
}
else if (mg_vcmp(&hm->uri, "/api") == 0) {
//handle_api_query(nc, hm);
}
Expand Down
5 changes: 3 additions & 2 deletions src/r_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ void r_init_cfg(r_cfg_t *cfg)
// initialize tables
baseband_init();

time(&cfg->running_since);
time(&cfg->frames_since);

list_ensure_size(&cfg->demod->r_devs, 100);
Expand Down Expand Up @@ -930,7 +931,7 @@ data_t *create_report_data(r_cfg_t *cfg, int level)
}

data = data_make(
"count", "", DATA_INT, cfg->frames_count,
"count", "", DATA_INT, cfg->frames_ook,
"fsk", "", DATA_INT, cfg->frames_fsk,
"events", "", DATA_INT, cfg->frames_events,
NULL);
Expand All @@ -954,7 +955,7 @@ void flush_report_data(r_cfg_t *cfg)
list_t *r_devs = &cfg->demod->r_devs;

time(&cfg->frames_since);
cfg->frames_count = 0;
cfg->frames_ook = 0;
cfg->frames_fsk = 0;
cfg->frames_events = 0;

Expand Down
16 changes: 12 additions & 4 deletions src/rtl_433.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,9 @@ static void sdr_callback(unsigned char *iq_buf, uint32_t len, void *ctx)
int noise_only = avg_db < demod->noise_level + 3.0f; // or demod->min_level_auto?
// always process frames if loader, dumper, or analyzers are in use, otherwise skip silent frames
int process_frame = demod->squelch_offset <= 0 || !noise_only || demod->load_info.format || demod->analyze_pulses || demod->dumper.len || demod->samp_grab;
cfg->total_frames_count += 1;
if (noise_only) {
cfg->total_frames_squelch += 1;
demod->noise_level = (demod->noise_level * 7 + avg_db) / 8; // fast fall over 8 frames
// If auto_level and noise level well below min_level and significant change in noise level
if (demod->auto_level > 0 && demod->noise_level < demod->min_level - 3.0f
Expand All @@ -474,8 +476,9 @@ static void sdr_callback(unsigned char *iq_buf, uint32_t len, void *ctx)
noise_only ? "noise" : "signal", avg_db, demod->noise_level);
}

if (process_frame)
baseband_low_pass_filter(demod->buf.temp, demod->am_buf, n_samples, &demod->lowpass_filter_state);
if (process_frame) {
baseband_low_pass_filter(demod->buf.temp, demod->am_buf, n_samples, &demod->lowpass_filter_state);
}

// FM demodulation
// Select the correct fsk pulse detector
Expand Down Expand Up @@ -534,7 +537,9 @@ static void sdr_callback(unsigned char *iq_buf, uint32_t len, void *ctx)
if (demod->analyze_pulses) fprintf(stderr, "Detected OOK package\t%s\n", time_pos_str(cfg, demod->pulse_data.start_ago, time_str));

p_events += run_ook_demods(&demod->r_devs, &demod->pulse_data);
cfg->frames_count++;
cfg->total_frames_ook += 1;
cfg->total_frames_events += p_events > 0;
cfg->frames_ook +=1;
cfg->frames_events += p_events > 0;

for (void **iter = demod->dumper.elems; iter && *iter; ++iter) {
Expand All @@ -559,7 +564,9 @@ static void sdr_callback(unsigned char *iq_buf, uint32_t len, void *ctx)
if (demod->analyze_pulses) fprintf(stderr, "Detected FSK package\t%s\n", time_pos_str(cfg, demod->fsk_pulse_data.start_ago, time_str));

p_events += run_fsk_demods(&demod->r_devs, &demod->fsk_pulse_data);
cfg->frames_fsk++;
cfg->total_frames_fsk +=1;
cfg->total_frames_events += p_events > 0;
cfg->frames_fsk += 1;
cfg->frames_events += p_events > 0;

for (void **iter = demod->dumper.elems; iter && *iter; ++iter) {
Expand Down Expand Up @@ -1504,6 +1511,7 @@ static void timer_handler(struct mg_connection *nc, int ev, void *ev_data)
if (cfg->dev_state == DEVICE_STATE_STARTING
|| cfg->dev_state == DEVICE_STATE_GRACE) {
cfg->dev_state = DEVICE_STATE_STARTED;
time(&cfg->sdr_since);
}
cfg->watchdog = 0;
break;
Expand Down

0 comments on commit aa89887

Please sign in to comment.