mirror of
https://github.com/netdata/netdata.git
synced 2025-03-16 22:13:38 +00:00
store status file in /var/lib/netdata, not in /var/cache/netdata (#19831)
* store status file in /var/lib/netdata, not in /var/cache/netdata * add db mode and db tiers to the status file * fix compilation * remove old status files * clear errno after deletions * save kubernetes status * do not cleanup old status files on every save
This commit is contained in:
parent
20c916088e
commit
90f94eba47
3 changed files with 63 additions and 4 deletions
src
|
@ -9,7 +9,7 @@
|
|||
#include <openssl/pem.h>
|
||||
#include <openssl/err.h>
|
||||
|
||||
#define STATUS_FILE_VERSION 13
|
||||
#define STATUS_FILE_VERSION 14
|
||||
|
||||
#define STATUS_FILENAME "status-netdata.json"
|
||||
|
||||
|
@ -118,6 +118,12 @@ static void daemon_status_file_to_json(BUFFER *wb, DAEMON_STATUS_FILE *ds) {
|
|||
|
||||
buffer_json_member_add_string_or_empty(wb, "ND_install_type", ds->install_type); // custom
|
||||
|
||||
if(ds->v >= 14) {
|
||||
buffer_json_member_add_string(wb, "ND_db_mode", rrd_memory_mode_name(ds->db_mode)); // custom
|
||||
buffer_json_member_add_uint64(wb, "ND_db_tiers", ds->db_tiers); // custom
|
||||
buffer_json_member_add_boolean(wb, "ND_kubernetes", ds->kubernetes); // custom
|
||||
}
|
||||
|
||||
buffer_json_member_add_object(wb, "ND_timings"); // custom
|
||||
{
|
||||
buffer_json_member_add_time_t(wb, "init", ds->timings.init);
|
||||
|
@ -226,6 +232,7 @@ static bool daemon_status_file_from_json(json_object *jobj, void *data, BUFFER *
|
|||
bool required_v4 = version >= 4 ? strict : false;
|
||||
bool required_v5 = version >= 5 ? strict : false;
|
||||
bool required_v10 = version >= 10 ? strict : false;
|
||||
bool required_v14 = version >= 14 ? strict : false;
|
||||
|
||||
// Parse timestamp
|
||||
JSONC_PARSE_TXT2CHAR_OR_ERROR_AND_RETURN(jobj, path, "@timestamp", datetime, error, required_v1);
|
||||
|
@ -252,6 +259,17 @@ static bool daemon_status_file_from_json(json_object *jobj, void *data, BUFFER *
|
|||
|
||||
if(version >= 4)
|
||||
JSONC_PARSE_UINT64_OR_ERROR_AND_RETURN(jobj, path, "ND_restarts", ds->restarts, error, required_v4);
|
||||
|
||||
if(version >= 14) {
|
||||
JSONC_PARSE_TXT2ENUM_OR_ERROR_AND_RETURN(jobj, path, "ND_db_mode", rrd_memory_mode_id, ds->db_mode, error, required_v14);
|
||||
JSONC_PARSE_UINT64_OR_ERROR_AND_RETURN(jobj, path, "ND_db_tiers", ds->db_tiers, error, required_v14);
|
||||
JSONC_PARSE_BOOL_OR_ERROR_AND_RETURN(jobj, path, "ND_kubernetes", ds->kubernetes, error, required_v14);
|
||||
}
|
||||
else {
|
||||
ds->db_mode = default_rrd_memory_mode;
|
||||
ds->db_tiers = nd_profile.storage_tiers;
|
||||
ds->kubernetes = false;
|
||||
}
|
||||
});
|
||||
|
||||
// Parse host object
|
||||
|
@ -383,6 +401,8 @@ static void daemon_status_file_refresh(DAEMON_STATUS status) {
|
|||
session_status.uptime = now_realtime_sec() - netdata_start_time;
|
||||
session_status.timestamp_ut = now_ut;
|
||||
session_status.invocation = nd_log_get_invocation_id();
|
||||
session_status.db_mode = default_rrd_memory_mode;
|
||||
session_status.db_tiers = nd_profile.storage_tiers;
|
||||
|
||||
session_status.claim_id = claim_id_get_uuid();
|
||||
|
||||
|
@ -470,11 +490,17 @@ static void daemon_status_file_refresh(DAEMON_STATUS status) {
|
|||
|
||||
// List of fallback directories to try
|
||||
static const char *status_file_fallbacks[] = {
|
||||
CACHE_DIR,
|
||||
"/tmp",
|
||||
"/run",
|
||||
"/var/run",
|
||||
".",
|
||||
};
|
||||
|
||||
static void set_dynamic_fallbacks(void) {
|
||||
status_file_fallbacks[0] = netdata_configured_cache_dir;
|
||||
}
|
||||
|
||||
static bool check_status_file(const char *directory, char *filename, size_t filename_size, time_t *mtime) {
|
||||
if(!directory || !*directory)
|
||||
return false;
|
||||
|
@ -529,12 +555,13 @@ void daemon_status_file_load(DAEMON_STATUS_FILE *ds) {
|
|||
time_t newest_mtime = 0, current_mtime;
|
||||
|
||||
// Check the primary directory first
|
||||
if(check_status_file(netdata_configured_cache_dir, current_filename, sizeof(current_filename), ¤t_mtime)) {
|
||||
if(check_status_file(netdata_configured_varlib_dir, current_filename, sizeof(current_filename), ¤t_mtime)) {
|
||||
strncpyz(newest_filename, current_filename, sizeof(newest_filename) - 1);
|
||||
newest_mtime = current_mtime;
|
||||
}
|
||||
|
||||
// Check each fallback location
|
||||
set_dynamic_fallbacks();
|
||||
for(size_t i = 0; i < _countof(status_file_fallbacks); i++) {
|
||||
if(check_status_file(status_file_fallbacks[i], current_filename, sizeof(current_filename), ¤t_mtime) &&
|
||||
(!*newest_filename || current_mtime > newest_mtime)) {
|
||||
|
@ -655,6 +682,23 @@ static void static_save_buffer_init(void) {
|
|||
buffer_flush(static_save_buffer);
|
||||
}
|
||||
|
||||
static void remove_old_status_files(const char *protected_dir) {
|
||||
FUNCTION_RUN_ONCE();
|
||||
|
||||
char filename[FILENAME_MAX];
|
||||
|
||||
set_dynamic_fallbacks();
|
||||
for(size_t i = 0; i < _countof(status_file_fallbacks); i++) {
|
||||
if(strcmp(status_file_fallbacks[i], protected_dir) == 0)
|
||||
continue;
|
||||
|
||||
snprintfz(filename, sizeof(filename), "%s/%s", status_file_fallbacks[i], STATUS_FILENAME);
|
||||
unlink(filename);
|
||||
}
|
||||
|
||||
errno_clear();
|
||||
}
|
||||
|
||||
static void daemon_status_file_save(BUFFER *wb, DAEMON_STATUS_FILE *ds, bool log) {
|
||||
buffer_flush(wb);
|
||||
buffer_json_initialize(wb, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_DEFAULT);
|
||||
|
@ -666,18 +710,22 @@ static void daemon_status_file_save(BUFFER *wb, DAEMON_STATUS_FILE *ds, bool log
|
|||
|
||||
// Try primary directory first
|
||||
bool saved = false;
|
||||
if (save_status_file(netdata_configured_cache_dir, content, content_size))
|
||||
if (save_status_file(netdata_configured_varlib_dir, content, content_size)) {
|
||||
remove_old_status_files(netdata_configured_varlib_dir);
|
||||
saved = true;
|
||||
}
|
||||
else {
|
||||
if(log)
|
||||
nd_log(NDLS_DAEMON, NDLP_DEBUG, "Failed to save status file in primary directory %s",
|
||||
netdata_configured_cache_dir);
|
||||
netdata_configured_varlib_dir);
|
||||
|
||||
// Try each fallback directory until successful
|
||||
set_dynamic_fallbacks();
|
||||
for(size_t i = 0; i < _countof(status_file_fallbacks); i++) {
|
||||
if (save_status_file(status_file_fallbacks[i], content, content_size)) {
|
||||
if(log)
|
||||
nd_log(NDLS_DAEMON, NDLP_DEBUG, "Saved status file in fallback %s", status_file_fallbacks[i]);
|
||||
|
||||
saved = true;
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include "libnetdata/libnetdata.h"
|
||||
#include "daemon/config/netdata-conf-profile.h"
|
||||
#include "database/rrd-database-mode.h"
|
||||
|
||||
typedef enum {
|
||||
DAEMON_STATUS_NONE,
|
||||
|
@ -33,6 +34,9 @@ typedef struct daemon_status_file {
|
|||
EXIT_REASON exit_reason; // the exit reason (maybe empty)
|
||||
ND_PROFILE profile; // the profile of the agent
|
||||
DAEMON_OS_TYPE os_type;
|
||||
RRD_DB_MODE db_mode;
|
||||
uint8_t db_tiers;
|
||||
bool kubernetes;
|
||||
|
||||
time_t boottime; // system boottime
|
||||
time_t uptime; // netdata uptime
|
||||
|
|
|
@ -652,5 +652,12 @@ void get_daemon_status_fields_from_system_info(DAEMON_STATUS_FILE *ds) {
|
|||
if(ri->host_os_id_like)
|
||||
strncpyz(ds->os_id_like, ri->host_os_id_like, sizeof(ds->os_id_like) - 1);
|
||||
|
||||
if(ri->is_k8s_node) {
|
||||
if (strcmp(ri->is_k8s_node, "true") == 0)
|
||||
ds->kubernetes = true;
|
||||
else
|
||||
ds->kubernetes = false;
|
||||
}
|
||||
|
||||
ds->read_system_info = true;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue