File: redis_api.lua

package info (click to toggle)
ntopng 5.2.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 121,832 kB
  • sloc: javascript: 143,431; cpp: 71,175; ansic: 11,108; sh: 4,687; makefile: 911; python: 587; sql: 512; pascal: 234; perl: 118; ruby: 52; exp: 4
file content (90 lines) | stat: -rw-r--r-- 2,243 bytes parent folder | download
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
--
-- (C) 2019-21 - ntop.org
--

local redis_api = {}

-- ##############################################

local function getRedisStatus()
    local redis = ntop.getCacheStatus()
    local redis_info = redis["info"]
    local res = {}
 
    for _, k in pairs(redis_info:split("\r\n")) do
       local k = k:split(":")
 
       if k then
      local v_k = k[1]
      local v = tonumber(k[2]) or k[2]
 
      res[v_k] = v
       end
    end
 
 
    if redis["dbsize"] then
       res["dbsize"] = redis["dbsize"]
    end
 
    return res
end
 
 -- ##############################################
 
local function getHealth(redis_status)
    local health = "green"
 
    if ntop.isWindows() then
        -- See Windows note in script.getStats()
        return health
    end
 
    if redis_status["aof_enabled"] and redis_status["aof_enabled"] ~= 0 then
        -- If here the use of Redis Append Only File (AOF) is enabled
        -- so we should check for its errors
        if redis_status["aof_last_bgrewrite_status"] ~= "ok" or redis_status["aof_last_write_status"] ~= "ok" then
            health = "red"
        end
    end
 
    if redis_status["rdb_last_bgsave_status"] ~= "ok" then
        health = "red"
    end
 
    return health
end
 
-- ##############################################

-- NOTE: on Windows, some stats are missing from script.getRedisStatus():
--    - aof_last_bgrewrite_status
--    - aof_last_write_status
--    - rdb_last_bgsave_status
--    - dbsize
function redis_api.getStats()
    local redis_status = getRedisStatus()

    local res = {
        -- used_memory_rss: Number of bytes that Redis allocated
        -- as seen by the operating system (a.k.a resident set size).
        -- This is the number reported by tools such as top(1) and ps(1)
        memory = redis_status["used_memory_rss"],
        -- The number of keys in the database
        dbsize = redis_status["dbsize"],
        -- Health
        health = getHealth(redis_status)
    }

    return res
end

-- ###############################################

function redis_api.redisTimeseriesEnabled()
    return(ntop.getPref("ntopng.prefs.system_probes_timeseries") ~= "0")
end

-- ###############################################

return redis_api