From 97aa5002b68383e402bf8226755c27f9506eb857 Mon Sep 17 00:00:00 2001 From: manuelceroni Date: Tue, 11 Feb 2025 14:56:56 +0100 Subject: [PATCH] Fixed Redis reads and writes time series --- .../minute/system/redis_timeseries.lua | 38 ++++++++++++++++--- scripts/lua/modules/redis_api.lua | 4 -- .../modules/timeseries/schemas/ts_minute.lua | 4 +- .../lua/modules/timeseries/ts_utils_core.lua | 2 +- scripts/lua/modules/timeseries_info.lua | 2 +- 5 files changed, 37 insertions(+), 13 deletions(-) diff --git a/scripts/callbacks/minute/system/redis_timeseries.lua b/scripts/callbacks/minute/system/redis_timeseries.lua index f5dc7a23309b..fd6aa6cbfa2e 100644 --- a/scripts/callbacks/minute/system/redis_timeseries.lua +++ b/scripts/callbacks/minute/system/redis_timeseries.lua @@ -17,6 +17,28 @@ local hits_stats = ntop.getCacheStats() local old_hits_stats = ntop.getCache(hits_key) -- ############################################## +local read_op = { + num_get = true, + num_hget = true, + num_hgetall = true, + num_hkeys = true, + num_llen = true, + num_lpop_rpop = true, + num_strlen = true, + num_ttl = true, + num_resolver_get_address = true +} + +local write_op = { + num_set = true, + num_hset = true, + num_lpush_rpush = true, + num_trim = true, + num_del = true, + num_hdel = true, + num_expire = true, + num_resolver_set_address = true +} if redis_api.redisTimeseriesEnabled() then require "ts_minute" @@ -26,7 +48,10 @@ if redis_api.redisTimeseriesEnabled() then local when = os.time() local stats = redis_api.getStats() - + + local reads = 0 + local writes = 0 + if(not isEmptyString(old_hits_stats)) then old_hits_stats = json.decode(old_hits_stats) or {} else @@ -40,18 +65,21 @@ if redis_api.redisTimeseriesEnabled() then if stats["dbsize"] then ts_utils.append("redis:keys", {ifid = ifid, num_keys = stats["dbsize"]}, when) end - if stats["reads"] and stats["writes"] then - ts_utils.append("redis:reads_writes", {ifid = ifid, num_reads = stats["reads"], num_writes = stats["writes"]}, when) - end for key, val in pairs(hits_stats) do if(old_hits_stats[key] ~= nil) then local delta = math.max(val - old_hits_stats[key], 0) - + if read_op[key] then + reads=reads+delta + elseif write_op[key] then + writes=writes+delta + end -- Dump the delta value as a gauge ts_utils.append("redis:hits", {ifid = ifid, command = key, num_calls = delta}, when) end end + + ts_utils.append("redis:reads_writes_v2", {ifid = ifid, num_reads = reads, num_writes = writes}, when) ntop.setCache(hits_key, json.encode(hits_stats)) end \ No newline at end of file diff --git a/scripts/lua/modules/redis_api.lua b/scripts/lua/modules/redis_api.lua index 33b8fe81e561..2adaa1cae221 100644 --- a/scripts/lua/modules/redis_api.lua +++ b/scripts/lua/modules/redis_api.lua @@ -85,10 +85,6 @@ function redis_api.getStats() memory = redis_status["used_memory_rss"], -- The number of keys in the database dbsize = redis_status["dbsize"], - -- The number of reads in the database - reads = redis_status["total_reads_processed"], - -- The number of writes in the database - writes = redis_status["total_writes_processed"], -- Health health = getHealth(redis_status) } diff --git a/scripts/lua/modules/timeseries/schemas/ts_minute.lua b/scripts/lua/modules/timeseries/schemas/ts_minute.lua index df7830dcafea..86adfa46b9bb 100644 --- a/scripts/lua/modules/timeseries/schemas/ts_minute.lua +++ b/scripts/lua/modules/timeseries/schemas/ts_minute.lua @@ -711,8 +711,8 @@ schema:addMetric("num_keys") -- ################################################ -schema = ts_utils.newSchema("redis:reads_writes", { - metrics_type = ts_utils.metrics.counter, +schema = ts_utils.newSchema("redis:reads_writes_v2", { + metrics_type = ts_utils.metrics.gauge, is_system_schema = true, step = 60 }) diff --git a/scripts/lua/modules/timeseries/ts_utils_core.lua b/scripts/lua/modules/timeseries/ts_utils_core.lua index 82b166eff7d3..0c770e71ea06 100644 --- a/scripts/lua/modules/timeseries/ts_utils_core.lua +++ b/scripts/lua/modules/timeseries/ts_utils_core.lua @@ -856,7 +856,7 @@ function ts_utils.getPossiblyChangedSchemas() "host:tcp_rx_stats", "host:udp_sent_unicast", "host:dns_qry_rcvd_rsp_sent", "host:dns_qry_sent_rsp_rcvd", "host:tcp_tx_stats", "iface:hosts_anomalies", -- Added missing ifid tag "influxdb:storage_size", "influxdb:exported_points", "influxdb:exports", "influxdb:rtt", "system:cpu_load", - "process:resident_memory", "redis:keys", "redis:memory", "redis:reads_writes", "periodic_script:timeseries_writes", + "process:resident_memory", "redis:keys", "redis:memory", "redis:reads_writes_v2", "periodic_script:timeseries_writes", "mac:arp_rqst_sent_rcvd_rpls", -- Active Monitoring "am_host:http_stats_min", "am_host:https_stats_min", "am_host:val_min", "am_host:http_stats_5mins", "am_host:https_stats_5mins", "am_host:val_5mins", "am_host:http_stats_hour", "am_host:https_stats_hour", diff --git a/scripts/lua/modules/timeseries_info.lua b/scripts/lua/modules/timeseries_info.lua index bf70631a49b7..0e36ba507314 100644 --- a/scripts/lua/modules/timeseries_info.lua +++ b/scripts/lua/modules/timeseries_info.lua @@ -1893,7 +1893,7 @@ local community_timeseries = { { } } }, { - schema = "redis:reads_writes", + schema = "redis:reads_writes_v2", id = timeseries_id.redis, label = i18n("system_stats.redis.redis_reads_writes"), priority = 0,