mirror of
https://github.com/ntop/ntopng.git
synced 2026-05-03 01:10:10 +00:00
Added assets dashboard
This commit is contained in:
parent
97ca40a349
commit
5f876167e5
25 changed files with 2311 additions and 1191 deletions
|
|
@ -16,36 +16,91 @@ local table_name = "assets"
|
|||
|
||||
-- ##############################################
|
||||
|
||||
local function build_where(ifid, filters)
|
||||
local where = ""
|
||||
-- Exception for the status filter, it's last_seen = 0 or last_seen != 0
|
||||
local status_filter = filters["status"]
|
||||
local os_filter = filters["os_type"]
|
||||
local server_filter = filters["server_type"]
|
||||
filters["server_type"] = nil
|
||||
filters["os_type"] = nil
|
||||
filters["status"] = nil
|
||||
|
||||
for key, value in pairs(filters) do
|
||||
where = where .. "AND"
|
||||
if tonumber(value) then
|
||||
value = tonumber(value)
|
||||
else
|
||||
value = string.format("'%s'", value)
|
||||
end
|
||||
|
||||
where = string.format("%s %s=%s ", where, key, value)
|
||||
end
|
||||
|
||||
if status_filter then
|
||||
where = string.format("%s AND %s%s%s", where, "last_seen",
|
||||
ternary(status_filter == "0", "=", "!="), "0")
|
||||
end
|
||||
|
||||
if os_filter then
|
||||
if isEmptyString(os_filter) or tostring(os_filter) == "0" then
|
||||
where = string.format("%s AND %s", where,
|
||||
"NOT simpleJSONHas(json_info, 'os_type')")
|
||||
else
|
||||
where = string.format("%s AND %s'%d'", where,
|
||||
"JSONExtractString(json_info, 'os_type') == ",
|
||||
tostring(os_filter))
|
||||
end
|
||||
end
|
||||
|
||||
if server_filter then
|
||||
local server_type = ''
|
||||
if tonumber(server_filter) == 0 then
|
||||
server_type = "dns_server"
|
||||
elseif tonumber(server_filter) == 1 then
|
||||
server_type = "dhcp_server"
|
||||
elseif tonumber(server_filter) == 2 then
|
||||
server_type = "smtp_server"
|
||||
elseif tonumber(server_filter) == 3 then
|
||||
server_type = "ntp_server"
|
||||
elseif tonumber(server_filter) == 4 then
|
||||
server_type = "imap_server"
|
||||
elseif tonumber(server_filter) == 5 then
|
||||
server_type = "pop_server"
|
||||
end
|
||||
where = string.format("%s AND %s", where,
|
||||
"simpleJSONExtractString(json_info, '" ..
|
||||
server_type .. "') == 'true'")
|
||||
end
|
||||
filters["status"] = status_filter
|
||||
filters["os_type"] = os_filter
|
||||
filters["server_type"] = server_filter
|
||||
return where
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
local function partiallyFormatInfo(res)
|
||||
local res_formatted = {}
|
||||
for _, res_unformatted in pairs(res or {}) do
|
||||
local tmp = res_unformatted
|
||||
local json_info = json.decode(res_unformatted.json_info or "") or {}
|
||||
tmp["is_dns_server"] = ((json_info["dns_server"] or "false") == "true")
|
||||
tmp["is_dhcp_server"] = ((json_info["dhcp_server"] or "false") == "true")
|
||||
tmp["is_smtp_server"] = ((json_info["smtp_server"] or "false") == "true")
|
||||
tmp["is_dhcp_server"] =
|
||||
((json_info["dhcp_server"] or "false") == "true")
|
||||
tmp["is_smtp_server"] =
|
||||
((json_info["smtp_server"] or "false") == "true")
|
||||
tmp["is_ntp_server"] = ((json_info["ntp_server"] or "false") == "true")
|
||||
tmp["is_imap_server"] = ((json_info["imap_server"] or "false") == "true")
|
||||
tmp["is_imap_server"] =
|
||||
((json_info["imap_server"] or "false") == "true")
|
||||
tmp["is_pop_server"] = ((json_info["pop_server"] or "false") == "true")
|
||||
|
||||
if tmp["is_dns_server"] then
|
||||
json_info["dns_server"] = nil
|
||||
end
|
||||
if tmp["is_dhcp_server"] then
|
||||
json_info["dhcp_server"] = nil
|
||||
end
|
||||
if tmp["is_smtp_server"] then
|
||||
json_info["smtp_server"] = nil
|
||||
end
|
||||
if tmp["is_ntp_server"] then
|
||||
json_info["ntp_server"] = nil
|
||||
end
|
||||
if tmp["is_imap_server"] then
|
||||
json_info["imap_server"] = nil
|
||||
end
|
||||
if tmp["is_pop_server"] then
|
||||
json_info["pop_server"] = nil
|
||||
end
|
||||
if tmp["is_dns_server"] then json_info["dns_server"] = nil end
|
||||
if tmp["is_dhcp_server"] then json_info["dhcp_server"] = nil end
|
||||
if tmp["is_smtp_server"] then json_info["smtp_server"] = nil end
|
||||
if tmp["is_ntp_server"] then json_info["ntp_server"] = nil end
|
||||
if tmp["is_imap_server"] then json_info["imap_server"] = nil end
|
||||
if tmp["is_pop_server"] then json_info["pop_server"] = nil end
|
||||
|
||||
if json_info["os_type"] then
|
||||
tmp["os_type"] = json_info["os_type"]
|
||||
|
|
@ -96,14 +151,17 @@ end
|
|||
-- ##############################################
|
||||
|
||||
local function getAssetInfo(ifid, key, type)
|
||||
if isEmptyString(key) then
|
||||
return nil
|
||||
end
|
||||
if isEmptyString(key) then return nil end
|
||||
local query = string.format(
|
||||
"SELECT type, key, ifid, ip, mac, vlan, network, name, device_type, manufacturer, %s , %s, gateway_mac, json_info %s FROM %s WHERE key='%s' AND ifid=%d AND type='%s'",
|
||||
ternary(hasClickHouseSupport(), "toUnixTimestamp(last_seen) as last_seen", "last_seen"),
|
||||
ternary(hasClickHouseSupport(), "toUnixTimestamp(first_seen) as first_seen", "first_seen"),
|
||||
ternary(hasClickHouseSupport(), ", version", ""), table_name, key, ifid, type)
|
||||
"SELECT type, key, ifid, ip, mac, vlan, network, name, device_type, manufacturer, %s , %s, gateway_mac, json_info %s FROM %s WHERE key='%s' AND ifid=%d AND type='%s'",
|
||||
ternary(hasClickHouseSupport(),
|
||||
"toUnixTimestamp(last_seen) as last_seen",
|
||||
"last_seen"),
|
||||
ternary(hasClickHouseSupport(),
|
||||
"toUnixTimestamp(first_seen) as first_seen",
|
||||
"first_seen"),
|
||||
ternary(hasClickHouseSupport(), ", version", ""),
|
||||
table_name, key, ifid, type)
|
||||
local res = interface.alert_store_query(query)
|
||||
res = partiallyFormatInfo(res)
|
||||
return res
|
||||
|
|
@ -115,7 +173,7 @@ end
|
|||
local function cleanValues(table_to_clean)
|
||||
for key, value in pairs(table_to_clean or {}) do
|
||||
if type(value) == 'string' then
|
||||
table_to_clean[key] = string.gsub(value, "'", "")
|
||||
table_to_clean[key] = string.gsub(value, "'", "")
|
||||
end
|
||||
end
|
||||
return table_to_clean
|
||||
|
|
@ -132,7 +190,8 @@ local function updateData(entry, ifid, type)
|
|||
entry.first_seen = data.first_seen -- Keep the old first_seen
|
||||
-- Merge the json_info field, note, that in case of duplicates, the data from
|
||||
-- entry table are used.
|
||||
local unified_json = table.merge(data.json_info or {}, entry.json_info or {})
|
||||
local unified_json = table.merge(data.json_info or {},
|
||||
entry.json_info or {})
|
||||
entry = cleanValues(entry)
|
||||
entry.json_info = json.encode(cleanValues(unified_json))
|
||||
end
|
||||
|
|
@ -155,43 +214,20 @@ end
|
|||
|
||||
-- ##############################################
|
||||
|
||||
local function getAssetData(ifid, order, sort, start, length, filters, asset_type, check_last_seen)
|
||||
if not ifid then
|
||||
ifid = interface.getId()
|
||||
end
|
||||
|
||||
if sort == "ip" and hasClickHouseSupport() then
|
||||
sort = "toIPv6(ip)"
|
||||
end
|
||||
local where = ""
|
||||
|
||||
-- Exception for the status filter, it's last_seen = 0 or last_seen != 0
|
||||
local status_filter = filters["status"]
|
||||
filters["status"] = nil
|
||||
|
||||
for key, value in pairs(filters or {}) do
|
||||
where = where .. "AND"
|
||||
if tonumber(value) then
|
||||
value = tonumber(value)
|
||||
else
|
||||
value = string.format("'%s'", value)
|
||||
end
|
||||
|
||||
where = string.format("%s %s=%s ", where, key, value)
|
||||
end
|
||||
|
||||
if status_filter then
|
||||
where = string.format("%s AND %s%s%s", where, "last_seen", ternary(status_filter == "0", "=", "!="), "0")
|
||||
end
|
||||
filters["status"] = status_filter
|
||||
local function getAssetData(ifid, order, sort, start, length, filters,
|
||||
asset_type, check_last_seen)
|
||||
if not ifid then ifid = interface.getId() end
|
||||
|
||||
if sort == "ip" and hasClickHouseSupport() then sort = "toIPv6(ip)" end
|
||||
local where = build_where(ifid, filters)
|
||||
local sort_query = ""
|
||||
local limit_query = ""
|
||||
|
||||
if sort and order then
|
||||
if sort == "last_seen" then
|
||||
-- Set last seen = 0 at start or end
|
||||
sort_query = string.format("ORDER BY (%s = 0) %s, %s %s", sort, order, sort, order)
|
||||
sort_query = string.format("ORDER BY (%s = 0) %s, %s %s", sort,
|
||||
order, sort, order)
|
||||
else
|
||||
sort_query = string.format("ORDER BY %s %s", sort, order)
|
||||
end
|
||||
|
|
@ -205,17 +241,21 @@ local function getAssetData(ifid, order, sort, start, length, filters, asset_typ
|
|||
|
||||
if hasClickHouseSupport() then
|
||||
query = string.format(
|
||||
"SELECT a.type, a.key, a.ifid, a.ip, a.mac, a.vlan, a.network, a.name, a.device_type, a.manufacturer, %s, %s, a.gateway_mac, a.json_info, a.version" ..
|
||||
" FROM %s a INNER JOIN (SELECT type, key, MAX(version) AS max_version FROM %s WHERE type='%s' AND ifid=%d %s GROUP BY type, key) AS latest" ..
|
||||
" ON a.type = latest.type AND a.key = latest.key AND a.version = latest.max_version %s %s",
|
||||
ternary(hasClickHouseSupport(), "toUnixTimestamp(a.last_seen) as last_seen", "a.last_seen"),
|
||||
ternary(hasClickHouseSupport(), "toUnixTimestamp(a.first_seen) as first_seen", "a.first_seen"), table_name,
|
||||
table_name, asset_type, -- Only hosts here
|
||||
tonumber(ifid), where, sort_query, limit_query)
|
||||
"SELECT a.type, a.key, a.ifid, a.ip, a.mac, a.vlan, a.network, a.name, a.device_type, a.manufacturer, %s, %s, a.gateway_mac, a.json_info, a.version" ..
|
||||
" FROM %s a INNER JOIN (SELECT type, key, MAX(version) AS max_version FROM %s WHERE type='%s' AND ifid=%d %s GROUP BY type, key) AS latest" ..
|
||||
" ON a.type = latest.type AND a.key = latest.key AND a.version = latest.max_version %s %s",
|
||||
ternary(hasClickHouseSupport(),
|
||||
"toUnixTimestamp(a.last_seen) as last_seen",
|
||||
"a.last_seen"),
|
||||
ternary(hasClickHouseSupport(),
|
||||
"toUnixTimestamp(a.first_seen) as first_seen",
|
||||
"a.first_seen"), table_name, table_name, asset_type, -- Only hosts here
|
||||
tonumber(ifid), where, sort_query, limit_query)
|
||||
else
|
||||
query = string.format(
|
||||
"SELECT type, key, ifid, ip, mac, vlan, network, name, device_type, manufacturer, last_seen, first_seen, gateway_mac, json_info" ..
|
||||
" FROM %s WHERE type='%s' AND ifid=%d %s %s %s", table_name, asset_type, -- Only hosts here
|
||||
"SELECT type, key, ifid, ip, mac, vlan, network, name, device_type, manufacturer, last_seen, first_seen, gateway_mac, json_info" ..
|
||||
" FROM %s WHERE type='%s' AND ifid=%d %s %s %s",
|
||||
table_name, asset_type, -- Only hosts here
|
||||
tonumber(ifid), where, sort_query, limit_query)
|
||||
end
|
||||
return interface.alert_store_query(query)
|
||||
|
|
@ -224,40 +264,20 @@ end
|
|||
-- ##############################################
|
||||
|
||||
local function getNumAssets(ifid, filters, asset_type, check_last_seen)
|
||||
if not ifid then
|
||||
ifid = interface.getId()
|
||||
end
|
||||
local where = ""
|
||||
-- Exception for the status filter, it's last_seen = 0 or last_seen != 0
|
||||
local status_filter = filters["status"]
|
||||
filters["status"] = nil
|
||||
|
||||
for key, value in pairs(filters) do
|
||||
where = where .. "AND"
|
||||
if tonumber(value) then
|
||||
value = tonumber(value)
|
||||
else
|
||||
value = string.format("'%s'", value)
|
||||
end
|
||||
|
||||
where = string.format("%s %s=%s ", where, key, value)
|
||||
end
|
||||
|
||||
if status_filter then
|
||||
where = string.format("%s AND %s%s%s", where, "last_seen", ternary(status_filter == "0", "=", "!="), "0")
|
||||
end
|
||||
filters["status"] = status_filter
|
||||
if not ifid then ifid = interface.getId() end
|
||||
local where = build_where(ifid, filters)
|
||||
|
||||
local query = nil
|
||||
if hasClickHouseSupport() then
|
||||
query = string.format(
|
||||
"SELECT count(*) as count FROM %s a INNER JOIN (SELECT type, key, MAX(version) AS max_version FROM %s WHERE type='%s' AND ifid=%d %s GROUP BY type, key) AS latest" ..
|
||||
" ON a.type = latest.type AND a.key = latest.key AND a.version = latest.max_version", table_name,
|
||||
table_name, asset_type, -- Only hosts here
|
||||
"SELECT count(*) as count FROM %s a INNER JOIN (SELECT type, key, MAX(version) AS max_version FROM %s WHERE type='%s' AND ifid=%d %s GROUP BY type, key) AS latest" ..
|
||||
" ON a.type = latest.type AND a.key = latest.key AND a.version = latest.max_version",
|
||||
table_name, table_name, asset_type, -- Only hosts here
|
||||
tonumber(ifid), where)
|
||||
else
|
||||
query = string.format("SELECT COUNT(*) as count " .. "FROM %s WHERE type='%s' %s AND ifid=%d", table_name,
|
||||
asset_type, where, ifid)
|
||||
query = string.format("SELECT COUNT(*) as count " ..
|
||||
"FROM %s WHERE type='%s' %s AND ifid=%d",
|
||||
table_name, asset_type, where, ifid)
|
||||
end
|
||||
|
||||
return interface.alert_store_query(query)
|
||||
|
|
@ -273,7 +293,9 @@ end
|
|||
|
||||
-- @brief insert assetkey
|
||||
function asset_utils.getLastVersion(ifid)
|
||||
local query = string.format("SELECT version FROM %s WHERE ifid=%d ORDER BY version DESC LIMIT 1", table_name, ifid)
|
||||
local query = string.format(
|
||||
"SELECT version FROM %s WHERE ifid=%d ORDER BY version DESC LIMIT 1",
|
||||
table_name, ifid)
|
||||
local last_version = interface.alert_store_query(query) or {}
|
||||
if type(last_version) == "string" then
|
||||
last_version = nil
|
||||
|
|
@ -292,7 +314,8 @@ function asset_utils.insertHost(entry, version, ifid)
|
|||
local query = nil
|
||||
entry = updateData(entry, ifid, "host")
|
||||
if not isIPv4(entry["ip"]) and not isIPv6(entry["ip"]) then
|
||||
traceError(TRACE_ERROR, TRACE_CONSOLE, "Detected Asset without IP Address:\n")
|
||||
traceError(TRACE_ERROR, TRACE_CONSOLE,
|
||||
"Detected Asset without IP Address:\n")
|
||||
tprint(entry)
|
||||
return
|
||||
end
|
||||
|
|
@ -301,21 +324,35 @@ function asset_utils.insertHost(entry, version, ifid)
|
|||
query = string.format("INSERT INTO %s " ..
|
||||
"(type, key, ifid, ip, mac, vlan, network, name, device_type, manufacturer, first_seen, last_seen, version, json_info) " ..
|
||||
"VALUES ('%s','%s', %u, '%s', '%s', %u, %u, %s, %u, %s, %u, %u, %u, '%s')",
|
||||
table_name, entry["type"], entry["key"], ifid, entry["ip"] or "", entry["mac"] or "", entry["vlan"] or 0,
|
||||
entry["network"] or 0, ternary(not isEmptyString(entry["name"]), string.format("'%s'", entry["name"]),
|
||||
"NULL"), entry["device_type"], ternary(not isEmptyString(entry["manufacturer"]),
|
||||
string.format("'%s'", entry["manufacturer"]), "NULL"), entry["first_seen"], entry["last_seen"] or 0,
|
||||
version, entry["json_info"] or "")
|
||||
table_name, entry["type"], entry["key"], ifid,
|
||||
entry["ip"] or "", entry["mac"] or "",
|
||||
entry["vlan"] or 0, entry["network"] or 0,
|
||||
ternary(not isEmptyString(entry["name"]),
|
||||
string.format("'%s'", entry["name"]),
|
||||
"NULL"), entry["device_type"],
|
||||
ternary(not isEmptyString(entry["manufacturer"]),
|
||||
string.format("'%s'",
|
||||
entry["manufacturer"]),
|
||||
"NULL"), entry["first_seen"],
|
||||
entry["last_seen"] or 0, version,
|
||||
entry["json_info"] or "")
|
||||
else
|
||||
query = string.format("INSERT INTO %s " ..
|
||||
"(type, key, ifid, ip, mac, vlan, network, name, device_type, manufacturer, first_seen, last_seen, json_info) " ..
|
||||
"VALUES ('%s','%s', %u, '%s','%s', %u, %u, %s, %u, %s, %u, %u, '%s') " ..
|
||||
"ON CONFLICT(key) DO UPDATE SET last_seen = %u, first_seen = %u;", table_name,
|
||||
entry["type"], entry["key"], ifid, entry["ip"], entry["mac"] or "", entry["vlan"] or 0,
|
||||
entry["network"] or 0, ternary(not isEmptyString(entry["name"]), string.format("'%s'", entry["name"]),
|
||||
"NULL"), entry["device_type"], ternary(not isEmptyString(entry["manufacturer"]),
|
||||
string.format("'%s'", entry["manufacturer"]), "NULL"), entry["first_seen"], entry["last_seen"] or 0,
|
||||
entry["json_info"] or "", entry["last_seen"] or 0, entry["first_seen"] or 0)
|
||||
"ON CONFLICT(key) DO UPDATE SET last_seen = %u, first_seen = %u;",
|
||||
table_name, entry["type"], entry["key"], ifid,
|
||||
entry["ip"], entry["mac"] or "",
|
||||
entry["vlan"] or 0, entry["network"] or 0,
|
||||
ternary(not isEmptyString(entry["name"]),
|
||||
string.format("'%s'", entry["name"]),
|
||||
"NULL"), entry["device_type"],
|
||||
ternary(not isEmptyString(entry["manufacturer"]),
|
||||
string.format("'%s'",
|
||||
entry["manufacturer"]),
|
||||
"NULL"), entry["first_seen"],
|
||||
entry["last_seen"] or 0, entry["json_info"] or "",
|
||||
entry["last_seen"] or 0, entry["first_seen"] or 0)
|
||||
end
|
||||
|
||||
return interface.alert_store_query(query)
|
||||
|
|
@ -327,18 +364,27 @@ function asset_utils.insertMac(entry, version, ifid)
|
|||
if hasClickHouseSupport() then
|
||||
query = string.format("INSERT INTO %s " ..
|
||||
"(type, key, ifid, mac, manufacturer, vlan, device_type, first_seen, last_seen, version, json_info) " ..
|
||||
"SELECT '%s','%s', %u, '%s','%s', %u, %u, %u, %u, %u, '%s'", table_name,
|
||||
entry["type"], entry["key"], tonumber(ifid), entry["mac"], entry["manufacturer"], 0, -- VLAN
|
||||
tonumber(entry["device_type"]), tonumber(entry["first_seen"]), tonumber(entry["last_seen"] or 0),
|
||||
tonumber(version), entry["json_info"] or "")
|
||||
"SELECT '%s','%s', %u, '%s','%s', %u, %u, %u, %u, %u, '%s'",
|
||||
table_name, entry["type"], entry["key"],
|
||||
tonumber(ifid), entry["mac"],
|
||||
entry["manufacturer"], 0, -- VLAN
|
||||
tonumber(entry["device_type"]), tonumber(entry["first_seen"]),
|
||||
tonumber(entry["last_seen"] or 0),
|
||||
tonumber(version), entry["json_info"] or "")
|
||||
else
|
||||
query = string.format("INSERT INTO %s " ..
|
||||
"(type, key, ifid, mac, manufacturer, device_type, first_seen, last_seen, json_info) " ..
|
||||
"VALUES ('%s','%s', %u, '%s','%s', %u, %u, %u, '%s') " ..
|
||||
"ON CONFLICT(key) DO UPDATE SET last_seen = %u, first_seen = %u;", table_name,
|
||||
entry["type"], entry["key"], tonumber(ifid), entry["mac"], entry["manufacturer"],
|
||||
tonumber(entry["device_type"] or 0), tonumber(entry["first_seen"] or 0), tonumber(entry["last_seen"] or 0),
|
||||
entry["json_info"] or "", tonumber(entry["last_seen"] or 0), tonumber(entry["first_seen"]))
|
||||
"ON CONFLICT(key) DO UPDATE SET last_seen = %u, first_seen = %u;",
|
||||
table_name, entry["type"], entry["key"],
|
||||
tonumber(ifid), entry["mac"],
|
||||
entry["manufacturer"],
|
||||
tonumber(entry["device_type"] or 0),
|
||||
tonumber(entry["first_seen"] or 0),
|
||||
tonumber(entry["last_seen"] or 0),
|
||||
entry["json_info"] or "",
|
||||
tonumber(entry["last_seen"] or 0),
|
||||
tonumber(entry["first_seen"]))
|
||||
end
|
||||
|
||||
return interface.alert_store_query(query)
|
||||
|
|
@ -347,14 +393,18 @@ end
|
|||
-- ##############################################
|
||||
|
||||
function asset_utils.getDevicesAssets(ifid, order, sort, start, length, filters)
|
||||
return getAssetData(ifid, order, sort, start, length, filters, "mac" --[[ Asset Type ]] , false)
|
||||
return
|
||||
getAssetData(ifid, order, sort, start, length, filters, "mac" --[[ Asset Type ]] ,
|
||||
false)
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
-- Return the lists of inactive hosts from the DB
|
||||
function asset_utils.getHostsAssets(ifid, order, sort, start, length, filters)
|
||||
return getAssetData(ifid, order, sort, start, length, filters, "host" --[[ Asset Type ]] , true)
|
||||
return
|
||||
getAssetData(ifid, order, sort, start, length, filters, "host" --[[ Asset Type ]] ,
|
||||
true)
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
|
@ -375,21 +425,28 @@ end
|
|||
|
||||
-- Return the lists of inactive hosts from the DB
|
||||
function asset_utils.getFilters(ifid)
|
||||
if not ifid then
|
||||
ifid = interface.getId()
|
||||
end
|
||||
if not ifid then ifid = interface.getId() end
|
||||
|
||||
local query = string.format("SELECT 'manufacturer' AS filter, manufacturer AS value, COUNT(*) AS count " ..
|
||||
"FROM %s where type='host' AND ifid=%d GROUP BY manufacturer UNION ALL " ..
|
||||
"SELECT 'device_type' AS filter, %s AS value, COUNT(*) AS count " ..
|
||||
"FROM %s where type='host' AND ifid=%d GROUP BY device_type UNION ALL " ..
|
||||
"SELECT 'vlan' AS filter, %s AS value, COUNT(*) AS count " ..
|
||||
"FROM %s where type='host' AND ifid=%d GROUP BY vlan UNION ALL " ..
|
||||
"SELECT 'network' AS filter, %s AS value, COUNT(*) AS count " ..
|
||||
"FROM %s where type='host' AND ifid=%d GROUP BY network", table_name, ifid,
|
||||
ternary(hasClickHouseSupport(), "CAST(device_type, 'String')", "CAST(device_type AS CHAR)"), table_name, ifid,
|
||||
ternary(hasClickHouseSupport(), "CAST(vlan, 'String')", "CAST(vlan AS CHAR)"), table_name, ifid, ternary(
|
||||
hasClickHouseSupport(), "CAST(network, 'String')", "CAST(network AS CHAR)"), table_name, ifid)
|
||||
local query = string.format(
|
||||
"SELECT 'manufacturer' AS filter, manufacturer AS value, COUNT(*) AS count " ..
|
||||
"FROM %s where type='host' AND ifid=%d GROUP BY manufacturer UNION ALL " ..
|
||||
"SELECT 'device_type' AS filter, %s AS value, COUNT(*) AS count " ..
|
||||
"FROM %s where type='host' AND ifid=%d GROUP BY device_type UNION ALL " ..
|
||||
"SELECT 'vlan' AS filter, %s AS value, COUNT(*) AS count " ..
|
||||
"FROM %s where type='host' AND ifid=%d GROUP BY vlan UNION ALL " ..
|
||||
"SELECT 'os_type' AS filter, JSONExtractString(json_info, 'os_type') AS value, COUNT(*) AS count " ..
|
||||
"FROM %s where type='host' AND ifid=%d GROUP BY value UNION ALL " ..
|
||||
"SELECT 'network' AS filter, %s AS value, COUNT(*) AS count " ..
|
||||
"FROM %s where type='host' AND ifid=%d GROUP BY network",
|
||||
table_name, ifid,
|
||||
ternary(hasClickHouseSupport(),
|
||||
"CAST(device_type, 'String')",
|
||||
"CAST(device_type AS CHAR)"), table_name, ifid,
|
||||
ternary(hasClickHouseSupport(), "CAST(vlan, 'String')",
|
||||
"CAST(vlan AS CHAR)"), table_name, ifid,
|
||||
table_name, ifid,
|
||||
ternary(hasClickHouseSupport(), "CAST(network, 'String')",
|
||||
"CAST(network AS CHAR)"), table_name, ifid)
|
||||
local res = interface.alert_store_query(query)
|
||||
return res
|
||||
end
|
||||
|
|
@ -402,9 +459,7 @@ end
|
|||
|
||||
-- ##############################################
|
||||
|
||||
function asset_utils.getMacInfo(ifid, key)
|
||||
return getAssetInfo(ifid, key, "mac")
|
||||
end
|
||||
function asset_utils.getMacInfo(ifid, key) return getAssetInfo(ifid, key, "mac") end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
|
|
@ -429,11 +484,13 @@ function asset_utils.editMac(device, trigger_alert, mac_status, ifid)
|
|||
trigger_alert = trigger_alert
|
||||
})
|
||||
if hasClickHouseSupport() then
|
||||
asset_utils.insertMac(fields, tonumber(fields.version) + 1, tonumber(ifid))
|
||||
asset_utils.insertMac(fields, tonumber(fields.version) + 1,
|
||||
tonumber(ifid))
|
||||
else
|
||||
local update_query = string.format(
|
||||
"UPDATE %s SET `json_info`='%s' WHERE type='mac' AND ifid=%d AND key='%s'", table_name,
|
||||
fields.json_info, fields.ifid, fields.key)
|
||||
"UPDATE %s SET `json_info`='%s' WHERE type='mac' AND ifid=%d AND key='%s'",
|
||||
table_name, fields.json_info,
|
||||
fields.ifid, fields.key)
|
||||
interface.alert_store_query(update_query)
|
||||
end
|
||||
end
|
||||
|
|
@ -445,9 +502,12 @@ end
|
|||
function asset_utils.deleteAll(ifid, type)
|
||||
local query = ""
|
||||
if hasClickHouseSupport() then
|
||||
query = string.format("ALTER TABLE %s DELETE WHERE type='%s' and ifid=%d", table_name, type, tonumber(ifid))
|
||||
query = string.format(
|
||||
"ALTER TABLE %s DELETE WHERE type='%s' and ifid=%d",
|
||||
table_name, type, tonumber(ifid))
|
||||
else
|
||||
query = string.format("DELETE FROM %s WHERE type='%s' and ifid=%d", table_name, type, tonumber(ifid))
|
||||
query = string.format("DELETE FROM %s WHERE type='%s' and ifid=%d",
|
||||
table_name, type, tonumber(ifid))
|
||||
end
|
||||
interface.alert_store_query(query)
|
||||
end
|
||||
|
|
@ -459,9 +519,12 @@ function asset_utils.deleteMac(device, ifid)
|
|||
local query = ""
|
||||
|
||||
if hasClickHouseSupport() then
|
||||
query = string.format("ALTER TABLE %s DELETE WHERE key='%s' and type='mac'", table_name, key)
|
||||
query = string.format(
|
||||
"ALTER TABLE %s DELETE WHERE key='%s' and type='mac'",
|
||||
table_name, key)
|
||||
else
|
||||
query = string.format("DELETE FROM %s WHERE key='%s' and type='mac'", table_name, key)
|
||||
query = string.format("DELETE FROM %s WHERE key='%s' and type='mac'",
|
||||
table_name, key)
|
||||
end
|
||||
|
||||
interface.alert_store_query(query)
|
||||
|
|
@ -473,9 +536,13 @@ function asset_utils.deleteHost(ifid, serial_key)
|
|||
local query = ""
|
||||
|
||||
if hasClickHouseSupport() then
|
||||
query = string.format("ALTER TABLE %s DELETE WHERE key='%s' AND type='host' AND ifid=%s", table_name, serial_key, ifid)
|
||||
query = string.format(
|
||||
"ALTER TABLE %s DELETE WHERE key='%s' AND type='host' AND ifid=%s",
|
||||
table_name, serial_key, ifid)
|
||||
else
|
||||
query = string.format("DELETE FROM %s WHERE key='%s' and type='host' AND ifid=%s", table_name, serial_key, ifid)
|
||||
query = string.format(
|
||||
"DELETE FROM %s WHERE key='%s' and type='host' AND ifid=%s",
|
||||
table_name, serial_key, ifid)
|
||||
end
|
||||
|
||||
interface.alert_store_query(query)
|
||||
|
|
@ -487,12 +554,154 @@ function asset_utils.deleteAllEntriesSince(ifid, type, last_seen)
|
|||
local query = ""
|
||||
|
||||
if hasClickHouseSupport() then
|
||||
query = string.format("ALTER TABLE %s DELETE WHERE type='%s' AND ifid=%s AND last_seen<%s AND last_seen != 0", table_name, type, ifid, last_seen)
|
||||
query = string.format(
|
||||
"ALTER TABLE %s DELETE WHERE type='%s' AND ifid=%s AND last_seen<%s AND last_seen != 0",
|
||||
table_name, type, ifid, last_seen)
|
||||
else
|
||||
query = string.format("DELETE FROM %s WHERE type='%s' AND ifid=%s AND last_seen<%s AND last_seen != 0", table_name, type, ifid, last_seen)
|
||||
query = string.format(
|
||||
"DELETE FROM %s WHERE type='%s' AND ifid=%s AND last_seen<%s AND last_seen != 0",
|
||||
table_name, type, ifid, last_seen)
|
||||
end
|
||||
|
||||
interface.alert_store_query(query)
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function asset_utils.updateLastSeen()
|
||||
local query = nil
|
||||
|
||||
if hasClickHouseSupport() then
|
||||
query = string.format(
|
||||
"INSERT INTO %s SELECT type, key, ifid, ip, mac, vlan, network, name, device_type, manufacturer, first_seen, now() AS last_seen, gateway_mac, json_info, version + 1 AS version FROM %s WHERE last_seen != 0",
|
||||
table_name, table_name)
|
||||
else
|
||||
query = string.format(
|
||||
"UPDATE %s SET last_seen = DATETIME('now') WHERE last_seen != 0",
|
||||
table_name)
|
||||
end
|
||||
interface.alert_store_query(query)
|
||||
end
|
||||
|
||||
-- ####### SECTION DEDICATED TO THE ASSETS DASHBOARD #######
|
||||
-- #########################################################
|
||||
|
||||
-- Return the lists of assets from the DB
|
||||
function asset_utils.getAllAssetsOverview(ifid, filters)
|
||||
if not ifid then ifid = interface.getId() end
|
||||
local asset_type = "host"
|
||||
local where = build_where(ifid, filters)
|
||||
|
||||
local query = nil
|
||||
if hasClickHouseSupport() then
|
||||
query = string.format("SELECT count(*) as assets, " ..
|
||||
"SUM(JSONHas(json_info, 'dns_server')) AS dns_server, " ..
|
||||
"SUM(JSONHas(json_info, 'dhcp_server')) AS dhcp_server, " ..
|
||||
"SUM(JSONHas(json_info, 'smtp_server')) AS smtp_server, " ..
|
||||
"SUM(JSONHas(json_info, 'imap_server')) AS imap_server, " ..
|
||||
"SUM(JSONHas(json_info, 'pop_server')) AS pop_server, " ..
|
||||
"SUM(JSONHas(json_info, 'ntp_server')) AS ntp_server, " ..
|
||||
"SUM(last_seen != 0) AS offline_asset, " ..
|
||||
"SUM(last_seen == 0) AS online_asset " ..
|
||||
"FROM (SELECT type, json_info, last_seen, key, MAX(version) AS max_version FROM %s WHERE type='%s' AND ifid=%d %s GROUP BY type, key, json_info, last_seen) AS latest",
|
||||
table_name, asset_type, -- Only hosts here
|
||||
tonumber(ifid), where)
|
||||
end
|
||||
|
||||
return interface.alert_store_query(query)
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
-- Return the lists of manufacturers from the DB
|
||||
function asset_utils.getManufacturers(ifid, filters)
|
||||
if not ifid then ifid = interface.getId() end
|
||||
local asset_type = "host"
|
||||
local where = build_where(ifid, filters)
|
||||
|
||||
local query = nil
|
||||
if hasClickHouseSupport() then
|
||||
query = string.format(
|
||||
"SELECT count(*) as count, " .. "manufacturer " ..
|
||||
"FROM (SELECT type, manufacturer, key, MAX(version) AS max_version FROM %s WHERE type='%s' AND ifid=%d %s GROUP BY type, key, manufacturer) " ..
|
||||
"GROUP BY manufacturer ORDER BY count DESC", table_name,
|
||||
asset_type, -- Only hosts here
|
||||
tonumber(ifid), where)
|
||||
end
|
||||
|
||||
return interface.alert_store_query(query)
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
-- Return the lists of devices from the DB
|
||||
function asset_utils.getDeviceTypes(ifid, filters)
|
||||
if not ifid then ifid = interface.getId() end
|
||||
local asset_type = "host"
|
||||
local where = build_where(ifid, filters)
|
||||
|
||||
local query = nil
|
||||
if hasClickHouseSupport() then
|
||||
query = string.format("SELECT count(*) as count, " .. "device_type " ..
|
||||
"FROM (SELECT type, device_type, key, MAX(version) AS max_version FROM %s WHERE type='%s' AND ifid=%d %s GROUP BY type, key, device_type) " ..
|
||||
"GROUP BY device_type ORDER BY count DESC",
|
||||
table_name, asset_type, -- Only hosts here
|
||||
tonumber(ifid), where)
|
||||
end
|
||||
|
||||
return interface.alert_store_query(query)
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
-- Return the lists of OSes from the DB
|
||||
function asset_utils.getOSes(ifid, filters)
|
||||
if not ifid then ifid = interface.getId() end
|
||||
local asset_type = "host"
|
||||
local where = build_where(ifid, filters)
|
||||
|
||||
local query = nil
|
||||
if hasClickHouseSupport() then
|
||||
query = string.format(
|
||||
"SELECT simpleJSONExtractInt(json_info, 'os_type') AS os_type, " ..
|
||||
"COUNT(*) as count " ..
|
||||
"FROM (SELECT type, json_info, key, MAX(version) AS max_version FROM %s WHERE type='%s' AND ifid=%d %s GROUP BY type, key, json_info) " ..
|
||||
"GROUP BY os_type ORDER BY count DESC", table_name,
|
||||
asset_type, -- Only hosts here
|
||||
tonumber(ifid), where)
|
||||
end
|
||||
|
||||
return interface.alert_store_query(query)
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
-- Return the number of online/offline servers from the DB
|
||||
function asset_utils.getServersOverview(ifid, filters)
|
||||
if not ifid then ifid = interface.getId() end
|
||||
local asset_type = "host"
|
||||
local where = build_where(ifid, filters)
|
||||
local query = nil
|
||||
if hasClickHouseSupport() then
|
||||
query = string.format(
|
||||
"SELECT SUM(JSONHas(json_info, 'dns_server') AND last_seen != 0) AS dns_servers_offline, " ..
|
||||
"SUM(JSONHas(json_info, 'dns_server') AND last_seen == 0) AS dns_servers_online, " ..
|
||||
"SUM(JSONHas(json_info, 'smtp_server') AND last_seen != 0) AS smtp_servers_offline, " ..
|
||||
"SUM(JSONHas(json_info, 'smtp_server') AND last_seen == 0) AS smtp_servers_online, " ..
|
||||
"SUM(JSONHas(json_info, 'imap_server') AND last_seen != 0) AS imap_servers_offline, " ..
|
||||
"SUM(JSONHas(json_info, 'imap_server') AND last_seen == 0) AS imap_servers_online, " ..
|
||||
"SUM(JSONHas(json_info, 'pop_server') AND last_seen != 0) AS pop_servers_offline, " ..
|
||||
"SUM(JSONHas(json_info, 'pop_server') AND last_seen == 0) AS pop_servers_online, " ..
|
||||
"SUM(JSONHas(json_info, 'ntp_server') AND last_seen != 0) AS ntp_servers_offline, " ..
|
||||
"SUM(JSONHas(json_info, 'ntp_server') AND last_seen == 0) AS ntp_servers_online, " ..
|
||||
"SUM(JSONHas(json_info, 'dhcp_server') AND last_seen != 0) AS dhcp_servers_offline, " ..
|
||||
"SUM(JSONHas(json_info, 'dhcp_server') AND last_seen == 0) AS dhcp_servers_online " ..
|
||||
"FROM (SELECT type, json_info, last_seen, key, MAX(version) AS max_version FROM %s WHERE type='%s' AND ifid=%d %s GROUP BY type, key, json_info, last_seen) AS latest",
|
||||
table_name, asset_type, -- Only hosts here
|
||||
tonumber(ifid), where)
|
||||
end
|
||||
|
||||
return interface.alert_store_query(query)
|
||||
end
|
||||
|
||||
return asset_utils
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue