--
-- (C) 2013-16 - ntop.org
--
dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
if(ntop.isPro()) then
package.path = dirs.installdir .. "/pro/scripts/lua/modules/?.lua;" .. package.path
require "snmp_utils"
end
require "lua_utils"
require "graph_utils"
require "alert_utils"
require "historical_utils"
debug_hosts = false
page = _GET["page"]
protocol_id = _GET["protocol"]
host_info = url2hostinfo(_GET)
host_ip = host_info["host"]
host_name = hostinfo2hostkey(host_info)
host_vlan = host_info["vlan"] or 0
always_show_hist = _GET["always_show_hist"]
ntopinfo = ntop.getInfo()
active_page = "hosts"
interface.select(ifname)
ifstats = interface.getStats()
ifId = ifstats.id
is_packetdump_enabled = isLocalPacketdumpEnabled()
host = nil
family = nil
prefs = ntop.getPrefs()
local hostkey = hostinfo2hostkey(host_info, nil, true --[[ force show vlan --]])
if((host_name == nil) or (host_ip == nil)) then
sendHTTPHeader('text/html; charset=iso-8859-1')
ntop.dumpFile(dirs.installdir .. "/httpdocs/inc/header.inc")
dofile(dirs.installdir .. "/scripts/lua/inc/menu.lua")
print("
Host '.. hostinfo2hostkey(host_info) .. ' cannot be found. ')
if((json ~= nil) and (json ~= "")) then
print(' Click
here to restore it from cache.\n')
else
print(purgedErrorString())
end
print("
")
dofile(dirs.installdir .. "/scripts/lua/inc/footer.lua")
return
end
else
sendHTTPHeader('text/html; charset=iso-8859-1')
ntop.dumpFile(dirs.installdir .. "/httpdocs/inc/header.inc")
dofile(dirs.installdir .. "/scripts/lua/inc/menu.lua")
-- Added global javascript variable, in order to disable the refresh of pie chart in case
-- of historical interface
print('\n\n')
if(host["ip"] ~= nil) then
host_name = hostinfo2hostkey(host)
host_info["host"] = host["ip"]
end
if(_GET["custom_name"] ~=nil) then
setHostAltName(hostinfo2hostkey(host_info), _GET["custom_name"])
end
host["label"] = getHostAltName(hostinfo2hostkey(host_info))
hostbase = dirs.workingdir .. "/" .. ifId .. "/rrd/" .. getPathFromKey(hostinfo2hostkey(host_info))
rrdname = hostbase .. "/bytes.rrd"
-- print(rrdname)
print [[
\n")
if(host["ip"] ~= nil) then
if(host["mac"] ~= "00:00:00:00:00:00") then
print("| (Router) MAC Address | " ..get_symbolic_mac(host["mac"]).. " "..getHostIcon(host["mac"]).." | ")
else
if(host["localhost"] == true and is_packetdump_enabled) then
print(" |
|---|
| Traffic Dump | ")
end
end
if(host["localhost"] == true and is_packetdump_enabled) then
dump_status = host["dump_host_traffic"]
if(_GET["dump_traffic"] ~= nil) then
if(_GET["dump_traffic"] == "true") then
dump_status = true
else
dump_status = false
end
interface.select(ifname) -- if we submitted a form, nothing is select()ed
interface.setHostDumpPolicy(dump_status, host_info["host"], host_vlan)
end
if(dump_status) then
dump_traffic_checked = 'checked="checked"'
dump_traffic_value = "false" -- Opposite
else
dump_traffic_checked = ""
dump_traffic_value = "true" -- Opposite
end
if(isAdministrator()) then
print [[
')
end
print(' |
')
end
if((host["mac"] ~= "") and (info["version.enterprise_edition"])) then
local ports = find_mac_snmp_ports(host["mac"])
if(ports ~= nil) then
local rsps = 1
for host,port in pairs(ports) do
rsps = rsps + 1
end
if(rsps > 1) then
print("| Host SNMP Location | SNMP Device | Device Port |
\n")
for host,port in pairs(ports) do
print("| "..ntop.getResolvedAddress(host).." | ")
print(""..port.." |
\n")
end
end
end
end
if host.deviceIfIdx ~= nil and host.deviceIfIdx ~= 0 and ntop.isPro() then
print("| Device IP / Port Index | ".. host.deviceIP .."@"..host.deviceIfIdx.." |
\n")
end
print("| IP Address | " .. host["ip"])
historicalProtoHostHref(getInterfaceId(ifname), host["ip"], nil, nil, nil)
if(host["local_network_name"] ~= nil) then
print(" [ ".. host["local_network_name"].." ]")
end
else
if(host["mac"] ~= nil) then
print(" |
|---|
| MAC Address | " .. host["mac"].. " |
\n")
end
end
if((host["city"] ~= nil) and (host["city"] ~= "")) then
print(" [ " .. host["city"] .." "..getFlag(host["country"]).." ]")
end
drop_host_traffic = _GET["drop_host_traffic"]
host_key = hostinfo2hostkey(host_info)
if(drop_host_traffic ~= nil) then
if(drop_host_traffic == "false") then
ntop.delHashCache("ntopng.prefs.drop_host_traffic", host_key)
else
ntop.setHashCache("ntopng.prefs.drop_host_traffic", host_key, drop_host_traffic)
end
interface.updateHostTrafficPolicy(host_info["host"], host_vlan)
else
drop_host_traffic = ntop.getHashCache("ntopng.prefs.drop_host_traffic", host_key)
if(drop_host_traffic == nil) then drop_host_traffic = "false" end
end
if(host["ip"] ~= nil) then
print [[
')
print(' | ')
end
if(ifstats.vlan and (host["vlan"] ~= nil)) then
print("| ")
if(ifstats.sprobe) then
print('Source Id')
else
print('VLAN ID')
end
print(" | "..host["vlan"].." |
\n")
end
if(ifstats.inline and (host.localhost or host.systemhost)) then
print("| Host Traffic Policy | ")
print('')
if(host["bridge.ingress_shaper_id"] ~= nil) then
shaper_key = "ntopng.prefs.".. ifId ..".shaper_max_rate"
ingress_max_rate = ntop.getHashCache(shaper_key, host["bridge.ingress_shaper_id"])
egress_max_rate = ntop.getHashCache(shaper_key, host["bridge.egress_shaper_id"])
print("")
print("| Ingress Policer | "..maxRateToString(ingress_max_rate).." | ")
print("| Egress Policer | "..maxRateToString(egress_max_rate).." | ")
print(" ")
end
print(' | ')
print('')
if(host["localhost"] == true) then
drop_traffic = ntop.getHashCache("ntopng.prefs.drop_host_traffic", host_key)
if(drop_traffic == "true") then
drop_traffic_checked = 'checked="checked"'
drop_traffic_value = "false" -- Opposite
else
drop_traffic_checked = ""
drop_traffic_value = "true" -- Opposite
end
print('')
else
print(' ')
end
print(' |
')
end
if((ifstats.inline and (host.localhost or host.systemhost)) or (host["os"] ~= "")) then
print("")
if(host["os"] ~= "") then
print("| OS | "..mapOS2Icon(host["os"]) .. " | \n")
else
print(" | | \n")
end
if(ifstats.inline and (host.localhost or host.systemhost) and isAdministrator()) then
if(_GET["host_quota"] ~= nil) then
interface.select(ifname) -- if we submitted a form, nothing is select()ed
interface.setHostQuota(tonumber(_GET["host_quota"]), host_info["host"], host_vlan)
end
host_quota_value = host["host_quota_mb"]
if(_GET["host_quota"] ~= nil) then host_quota_value = _GET["host_quota"] end
print [[')
print(' |
')
else
print(" | ")
end
end
local labelKey = host_info["host"].."@"..host_info["vlan"]
if(_GET["custom_icon"] ~=nil) then
setHostIcon(labelKey, _GET["custom_icon"])
end
if((host["asn"] ~= nil) and (host["asn"] > 0)) then
print("| ASN | ")
print(""..host.asname.." [ ASN ".. host.asn.." ] | ")
print('Whois Lookup | ')
print("
\n")
end
if(host["ip"] ~= nil) then
if(host["name"] == nil) then
host["name"] = ntop.getResolvedAddress(host["ip"])
end
print("| Name | ")
if(isAdministrator()) then
print(" ")
else
print(" | ")
end
if(host["ip"] == host["name"]) then
print(" ")
end
print(host["name"] .. " ")
if(host["localhost"] == true) then print('Local') else print('Remote') end
if(host["privatehost"] == true) then print(' Private IP') end
if(host["systemhost"] == true) then print(' System ') end
print(getHostIcon(labelKey))
print(" | \n")
end
if(host["ip"] ~= nil) then
if(isAdministrator()) then
print("")
print [[
|
]]
end
else
-- print(""..host_info["host"].." | ")
end
if(host["num_alerts"] > 0) then
print("| Alerts | "..host["num_alerts"] .. " |
\n")
end
print("| First / Last Seen | " .. formatEpoch(host["seen.first"]) .. " [" .. secondsToTime(os.time()-host["seen.first"]) .. " ago]" .. " | \n")
print("" .. formatEpoch(host["seen.last"]) .. " [" .. secondsToTime(os.time()-host["seen.last"]) .. " ago]" .. " |
\n")
if((host["bytes.sent"]+host["bytes.rcvd"]) > 0) then
print("| Sent vs Received Traffic Breakdown | ")
breakdownBar(host["bytes.sent"], "Sent", host["bytes.rcvd"], "Rcvd", 0, 100)
print(" |
\n")
end
print("| Traffic Sent / Received | " .. formatPackets(host["packets.sent"]) .. " / ".. bytesToSize(host["bytes.sent"]) .. " | " .. formatPackets(host["packets.rcvd"]) .. " / ".. bytesToSize(host["bytes.rcvd"]) .. " |
\n")
local flows_th = "Recently Active Flows / Total"
if interface.isPacketInterface() then
if interface.isPcapDumpInterface() == false then
flows_th = "Active Flows / Total Active / Low Goodput"
else
flows_th = "Flows / Total Active / Low Goodput"
end
end
print("| "..flows_th.." | 'As Client' | 'As Server' |
\n")
print("| " .. formatValue(host["active_flows.as_client"]) .. " \n")
print("/ " .. formatValue(host["flows.as_client"]) .. " \n")
if interface.isPacketInterface() then
print("/ " .. formatValue(host["low_goodput_flows.as_client"]) .. " \n")
end
print(" | ")
print("" .. formatValue(host["active_flows.as_server"]) .. " \n")
print("/ "..formatValue(host["flows.as_server"]) .. " \n")
if interface.isPacketInterface() then
print("/ " .. formatValue(host["low_goodput_flows.as_server"]) .. " \n")
end
print(" |
")
if host["tcp.packets.seq_problems"] == true then
print("| TCP Packets Sent Analysis | Retransmissions | ".. formatPackets(host["tcp.packets.retransmissions"]) .." |
\n")
print("| Out of Order | ".. formatPackets(host["tcp.packets.out_of_order"]) .." |
\n")
print("| Lost | ".. formatPackets(host["tcp.packets.lost"]) .." |
\n")
end
if(host["json"] ~= nil) then print("| JSON | Download |
\n") end
print("
\n")
elseif((page == "packets")) then
print [[
]]
elseif((page == "peers")) then
host_info = url2hostinfo(_GET)
flows = interface.getFlowPeers(host_info["host"], host_info["vlan"])
found = 0
for key, value in pairs(flows) do
found = 1
break
end
if(found) then
print [[
\n")
print("| DNS Breakdown | Queries | Positive Replies | Error Replies | Reply Breakdown |
")
print("| Sent | ".. formatValue(host["dns"]["sent"]["num_queries"]) .." | ")
print("".. formatValue(host["dns"]["sent"]["num_replies_ok"]) .." | ")
print("".. formatValue(host["dns"]["sent"]["num_replies_error"]) .." | ")
breakdownBar(host["dns"]["sent"]["num_replies_ok"], "OK", host["dns"]["sent"]["num_replies_error"], "Error", 0, 100)
print(" |
")
if(host["dns"]["sent"]["num_queries"] > 0) then
print [[
| DNS Query Sent Distribution |
|
]]
end
print("| Rcvd | ".. formatValue(host["dns"]["rcvd"]["num_queries"]) .." | ")
print("".. formatValue(host["dns"]["rcvd"]["num_replies_ok"]) .." | ")
print("".. formatValue(host["dns"]["rcvd"]["num_replies_error"]) .." | ")
breakdownBar(host["dns"]["rcvd"]["num_replies_ok"], "OK", host["dns"]["rcvd"]["num_replies_error"], "Error", 50, 100)
print(" |
")
if(host["dns"]["rcvd"]["num_queries"] > 0) then
print [[
| DNS Rcvd Query Distribution |
|
]]
end
print('| Request vs Reply | ')
breakdownBar(host["dns"]["sent"]["num_queries"], "Queries", host["dns"]["rcvd"]["num_replies_ok"]+host["dns"]["rcvd"]["num_replies_error"], "Replies", 30, 70)
print(' |
\n')
print("
\n")
end
elseif(page == "http") then
if(http ~= nil) then
print("\n")
if(host["sites"] ~= nil) then
old_top_len = table.len(host["sites.old"]) if(old_top_len > 10) then old_top_len = 10 end
top_len = table.len(host["sites"]) if(top_len > 10) then top_len = 10 end
if(old_top_len > top_len) then num = old_top_len else num = top_len end
print("| Top Visited Sites | Current Sites | Contacts | Last 5 Minute Sites | Contacts |
\n")
sites = {}
for k,v in pairsByValues(host["sites"], rev) do
table.insert(sites, { k, v })
end
sites_old = {}
for k,v in pairsByValues(host["sites.old"], rev) do
table.insert(sites_old, { k, v })
end
for i=1,num do
if(sites[i] == nil) then sites[i] = { "", 0 } end
if(sites_old[i] == nil) then sites_old[i] = { "", 0 } end
print("| ")
if(sites[i][1] ~= "") then
print(formatWebSite(sites[i][1]).." | "..sites[i][2].." | \n")
else
print(" | \n")
end
if(sites_old[i][1] ~= "") then
print(""..formatWebSite(sites_old[i][1]).." | "..sites_old[i][2].." |
\n")
else
print(" | \n")
end
end
end
print("| HTTP Queries | Method | Requests | Distribution |
")
print("| GET | ".. formatValue(http["sender"]["query"]["num_get"]) .." | ")
print [[
]]
print(" |
")
print("| POST | ".. formatValue(http["sender"]["query"]["num_post"]) .." |
")
print("| HEAD | ".. formatValue(http["sender"]["query"]["num_head"]) .." |
")
print("| PUT | ".. formatValue(http["sender"]["query"]["num_put"]) .." |
")
print("| Other Method | ".. formatValue(http["sender"]["query"]["num_other"]) .." |
")
print("| |
")
print("| HTTP Responses | Response code | Responses | Distribution |
")
print("| 1xx (Informational) | ".. formatValue(http["receiver"]["response"]["num_1xx"]) .." | ")
print [[
]]
print(" |
")
print("| 2xx (Success) | ".. formatValue(http["receiver"]["response"]["num_2xx"]) .." |
")
print("| 3xx (Redirection) | ".. formatValue(http["receiver"]["response"]["num_3xx"]) .." |
")
print("| 4xx (Client Error) | ".. formatValue(http["receiver"]["response"]["num_4xx"]) .." |
")
print("| 5xx (Server Error) | ".. formatValue(http["receiver"]["response"]["num_5xx"]) .." |
")
vh = http["virtual_hosts"]
if(vh ~= nil) then
local now = os.time()
local ago1h = now - 3600
num = table.len(vh)
if(num > 0) then
local ifId = getInterfaceId(ifname)
print("| Virtual Hosts | Name | Traffic Sent | Traffic Received | Requests Served |
\n")
for k,v in pairsByKeys(vh, asc) do
local j = string.gsub(k, "%.", "___")
print("| "..k.." ")
historicalProtoHostHref(ifId, host, nil, nil, k)
print(" | ")
print(""..bytesToSize(vh[k]["bytes.sent"]).." | ")
print(""..bytesToSize(vh[k]["bytes.rcvd"]).." | ")
print(""..formatValue(vh[k]["http.requests"]).." |
\n")
end
end
end
print("
\n")
end
elseif(page == "flows") then
print [[
\n")
print("| Client Contacts (Initiator) | Server Contacts (Receiver) |
\n")
print("")
if(cnum == 0) then
print("| No client contacts so far | ")
else
print("\n")
print("| Server Address | Contacts | \n")
-- TOFIX VLAN (We need to remove the host vlan and add the client vlan)
-- Client
sortTable = {}
for k,v in pairs(host["contacts"]["client"]) do
sortTable[v]=k
end
num = 0
max_num = 64 -- Do not create huge maps
for _v,k in pairsByKeys(sortTable, rev) do
if(num >= max_num) then break end
num = num + 1
name = interface.getHostInfo(k)
-- TOFIX VLAN (We need to remove the host vlan and add the client vlan)
v = host["contacts"]["client"][k]
info = interface.getHostInfo(k)
if(info ~= nil) then
if(info["name"] ~= nil) then n = info["name"] else n = ntop.getResolvedAddress(info["ip"]) end
url = ""..n..""
else
url = k
end
if(info ~= nil) then
url = url .. getFlag(info["country"]).." "
end
-- print(v.." ")
print("| "..url.." | " .. formatValue(v) .. " | \n")
end
print(" | \n")
end
if(snum == 0) then
print("No server contacts so far | ")
else
print("\n")
print("| Client Address | Contacts | \n")
-- Server
sortTable = {}
for k,v in pairs(host["contacts"]["server"]) do sortTable[v]=k end
for _v,k in pairsByKeys(sortTable, rev) do
v = host["contacts"]["server"][k]
info = interface.getHostInfo(k)
if(info ~= nil) then
if(info["name"] ~= nil) then n = info["name"] else n = ntop.getResolvedAddress(info["ip"]) end
url = ""..n..""
else
url = k
end
if(info ~= nil) then
url = url ..getFlag(info["country"]).." "
end
print("| "..url.." | " .. formatValue(v) .. " | \n")
end
print(" | \n")
end
print("
\n")
print("
\n")
else
print("No contacts for this host")
end
elseif(page == "alerts") then
checkDeleteStoredAlerts()
local tab = _GET["tab"]
local re_arm_minutes = nil
print('