0
0
Fork 0
mirror of https://github.com/netdata/netdata.git synced 2025-04-29 23:20:01 +00:00
netdata_netdata/exporting/exporting_engine.c
Costa Tsaousis 8fc3b351a2
Allow netdata plugins to expose functions for querying more information about specific charts ()
* function renames and code cleanup in popen.c; no actual code changes

* netdata popen() now opens both child process stdin and stdout and returns FILE * for both

* pass both input and output to parser structures

* updated rrdset to call custom functions

* RRDSET FUNCTION leading calls for both sync and async operation

* put RRDSET functions to a separate file

* added format and timeout at function definition

* support for synchronous (internal plugins) and asynchronous (external plugins and children) functions

* /api/v1/function endpoint

* functions are now attached to the host and there is a dictionary view per chart

* functions implemented at plugins.d

* remove the defer until keyword hook from plugins.d when it is done

* stream sender implementation of functions

* sanitization of all functions so that certain characters are only allowed

* strictier sanitization

* common max size

* 1st working plugins.d example

* always init inflight dictionary

* properly destroy dictionaries to avoid parallel insertion of items

* add more debugging on disconnection reasons

* add more debugging on disconnection reasons again

* streaming receiver respects newlines

* dont use the same fp for both streaming receive and send

* dont free dbengine memory with internal checks

* make sender proceed in the buffer

* added timing info and garbage collection at plugins.d

* added info about routing nodes

* added info about routing nodes with delay

* added more info about delays

* added more info about delays again

* signal sending thread to wake up

* streaming version labeling and commented code to support capabilities

* added functions to /api/v1/data, /api/v1/charts, /api/v1/chart, /api/v1/info

* redirect top output to stdout

* address coverity findings

* fix resource leaks of popen

* log attempts to connect to individual destinations

* better messages

* properly parse destinations

* try to find a function from the most matching to the least matching

* log added streaming destinations

* rotate destinations bypassing a node in the middle that does not accept our connection

* break the loops properly

* use typedef to define callbacks

* capabilities negotiation during streaming

* functions exposed upstream based on capabilities; compression disabled per node persisting reconnects; always try to connect with all capabilities

* restore functionality to lookup functions

* better logging of capabilities

* remove old versions from capabilities when a newer version is there

* fix formatting

* optimization for plugins.d rrdlabels to avoid creating and destructing dictionaries all the time

* delayed health initialization for rrddim and rrdset

* cleanup health initialization

* fix for popen() not returning the right value

* add health worker jobs for initializing rrdset and rrddim

* added content type support for functions; apps.plugin permanent function to display all the processes

* fixes for functions parameters parsing in apps.plugin

* fix for process matching in apps.plugiin

* first working function for apps.plugin

* Dashboard ACL is disabled for functions; Function errors are all in JSON format

* apps.plugin function processes returns json table

* use json_escape_string() to escape message

* fix formatting

* apps.plugin exposes all its metrics to function processes

* fix json formatting when filtering out some rows

* reopen the internal pipe of rrdpush in case of errors

* misplaced statement

* do not use buffer->len

* support for GLOBAL functions (functions that are not linked to a chart

* added /api/v1/functions endpoint; removed format from the FUNCTIONS api;

* swagger documentation about the new api end points

* added plugins.d documentation about functions

* never re-close a file

* remove uncessesary ifdef

* fixed issues identified by codacy

* fix for null label value

* make edit-config copy-and-paste friendly

* Revert "make edit-config copy-and-paste friendly"

This reverts commit 54500c0e0a.

* reworked sender handshake to fix coverity findings

* timeout is zero, for both send_timeout() and recv_timeout()

* properly detect that parent closed the socket

* support caching of function responses; limit function response to 10MB; added protection from malformed function responses

* disabled excessive logging

* added units to apps.plugin function processes and normalized all values to be human readable

* shorter field names

* fixed issues reported

* fixed apps.plugin error response; tested that pluginsd can properly handle faulty responses

* use double linked list macros for double linked list management

* faster apps.plugin function printing by minimizing file operations

* added memory percentage

* fix compatibility issues with older compilers and FreeBSD

* rrdpush sender code cleanup; rrhost structure cleanup from sender flags and variables;

* fix letftover variable in ifdef

* apps.plugin: do not call detach from the thread; exit immediately when input is broken

* exclude AR charts from health

* flush cleaner; prefer sender output

* clarity

* do not fill the cbuffer if not connected

* fix

* dont enabled host->sender if streaming is not enabled; send host label updates to parent;

* functions are only available through ACLK

* Prepared statement reports only in dev mode

* fix AR chart detection

* fix for streaming not being enabling itself

* more cleanup of sender and receiver structures

* moved read-only flags and configuration options to rrdhost->options

* fixed merge with master

* fix for incomplete rename

* prevent service thread from working on charts that are being collected

Co-authored-by: Stelios Fragkakis <52996999+stelfrag@users.noreply.github.com>
2022-10-05 14:13:46 +03:00

217 lines
5.8 KiB
C

// SPDX-License-Identifier: GPL-3.0-or-later
#include "exporting_engine.h"
static struct engine *engine = NULL;
void analytics_exporting_connectors_ssl(BUFFER *b)
{
#ifdef ENABLE_HTTPS
if (netdata_ssl_exporting_ctx) {
for (struct instance *instance = engine->instance_root; instance; instance = instance->next) {
struct simple_connector_data *connector_specific_data = instance->connector_specific_data;
if (connector_specific_data->flags == NETDATA_SSL_HANDSHAKE_COMPLETE) {
buffer_strcat(b, "exporting");
break;
}
}
}
#endif
buffer_strcat(b, "|");
}
void analytics_exporting_connectors(BUFFER *b)
{
if (!engine)
return;
uint8_t count = 0;
for (struct instance *instance = engine->instance_root; instance; instance = instance->next) {
if (count)
buffer_strcat(b, "|");
switch (instance->config.type) {
case EXPORTING_CONNECTOR_TYPE_GRAPHITE:
buffer_strcat(b, "Graphite");
break;
case EXPORTING_CONNECTOR_TYPE_GRAPHITE_HTTP:
buffer_strcat(b, "GraphiteHTTP");
break;
case EXPORTING_CONNECTOR_TYPE_JSON:
buffer_strcat(b, "JSON");
break;
case EXPORTING_CONNECTOR_TYPE_JSON_HTTP:
buffer_strcat(b, "JSONHTTP");
break;
case EXPORTING_CONNECTOR_TYPE_OPENTSDB:
buffer_strcat(b, "OpenTSDB");
break;
case EXPORTING_CONNECTOR_TYPE_OPENTSDB_HTTP:
buffer_strcat(b, "OpenTSDBHTTP");
break;
case EXPORTING_CONNECTOR_TYPE_PROMETHEUS_REMOTE_WRITE:
#if ENABLE_PROMETHEUS_REMOTE_WRITE
buffer_strcat(b, "PrometheusRemoteWrite");
#endif
break;
case EXPORTING_CONNECTOR_TYPE_KINESIS:
#if HAVE_KINESIS
buffer_strcat(b, "Kinesis");
#endif
break;
case EXPORTING_CONNECTOR_TYPE_PUBSUB:
#if ENABLE_EXPORTING_PUBSUB
buffer_strcat(b, "Pubsub");
#endif
break;
case EXPORTING_CONNECTOR_TYPE_MONGODB:
#if HAVE_MONGOC
buffer_strcat(b, "MongoDB");
#endif
break;
default:
buffer_strcat(b, "Unknown");
}
count++;
}
}
/**
* Exporting Clean Engine
*
* Clean all variables allocated inside engine structure
*
* @param en a pointer to the structure that will be cleaned.
*/
static void exporting_clean_engine()
{
if (!engine)
return;
#if HAVE_KINESIS
if (engine->aws_sdk_initialized)
aws_sdk_shutdown();
#endif
#if ENABLE_PROMETHEUS_REMOTE_WRITE
if (engine->protocol_buffers_initialized)
protocol_buffers_shutdown();
#endif
//Cleanup web api
prometheus_clean_server_root();
for (struct instance *instance = engine->instance_root; instance;) {
struct instance *current_instance = instance;
instance = instance->next;
clean_instance(current_instance);
}
freez((void *)engine->config.hostname);
freez(engine);
}
/**
* Clean up the main exporting thread and all connector workers on Netdata exit
*
* @param ptr thread data.
*/
static void exporting_main_cleanup(void *ptr)
{
struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
info("cleaning up...");
if (!engine) {
static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
return;
}
engine->exit = 1;
int found = 0;
usec_t max = 2 * USEC_PER_SEC, step = 50000;
for (struct instance *instance = engine->instance_root; instance; instance = instance->next) {
if (!instance->exited) {
found++;
info("stopping worker for instance %s", instance->config.name);
uv_mutex_unlock(&instance->mutex);
instance->data_is_ready = 1;
uv_cond_signal(&instance->cond_var);
} else
info("found stopped worker for instance %s", instance->config.name);
}
while (found && max > 0) {
max -= step;
info("Waiting %d exporting connectors to finish...", found);
sleep_usec(step);
found = 0;
for (struct instance *instance = engine->instance_root; instance; instance = instance->next) {
if (!instance->exited)
found++;
}
}
exporting_clean_engine();
static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
}
/**
* Exporting engine main
*
* The main thread used to control the exporting engine.
*
* @param ptr a pointer to netdata_static_structure.
*
* @return It always returns NULL.
*/
void *exporting_main(void *ptr)
{
netdata_thread_cleanup_push(exporting_main_cleanup, ptr);
engine = read_exporting_config();
if (!engine) {
info("EXPORTING: no exporting connectors configured");
goto cleanup;
}
if (init_connectors(engine) != 0) {
error("EXPORTING: cannot initialize exporting connectors");
send_statistics("EXPORTING_START", "FAIL", "-");
goto cleanup;
}
RRDSET *st_main_rusage = NULL;
RRDDIM *rd_main_user = NULL;
RRDDIM *rd_main_system = NULL;
create_main_rusage_chart(&st_main_rusage, &rd_main_user, &rd_main_system);
usec_t step_ut = localhost->rrd_update_every * USEC_PER_SEC;
heartbeat_t hb;
heartbeat_init(&hb);
while (!netdata_exit) {
heartbeat_next(&hb, step_ut);
engine->now = now_realtime_sec();
if (mark_scheduled_instances(engine))
prepare_buffers(engine);
send_main_rusage(st_main_rusage, rd_main_user, rd_main_system);
#ifdef UNIT_TESTING
return NULL;
#endif
}
cleanup:
netdata_thread_cleanup_pop(1);
return NULL;
}