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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
--
-- (C) 2013-16 - ntop.org
--
dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
require "lua_utils"
function cleanName(name)
n = string.gsub(name, "'", "_")
-- Cut the name at 128 chars
n = string.sub(n, 1, 128)
return(n)
end
interfaces_id = 0
interfaces_hash = { }
function interface2id(name)
if(interfaces_hash[name] == nil) then
id = interfaces_id
interfaces_hash[name] = id
interfaces_id = interfaces_id + 1
return(id)
else
return(interfaces_hash[name])
end
end
-- #########################
hosts_id = 0
hosts_hash = { }
function host2id(name)
name = cleanName(name)
if(hosts_hash[name] == nil) then
id = hosts_id
hosts_hash[name] = id
hosts_id = hosts_id + 1
return(id)
else
return(hosts_hash[name])
end
end
-- #########################
sendHTTPHeader('text/html; charset=iso-8859-1')
begin = os.clock()
t = os.time() -- -86400
when = os.date("%y%m%d", t)
key_name = when..".keys"
--print(key_name.."\n")
local debug = true
local delete_keys = true
dump_dir = fixPath(dirs.workingdir .. "/datadump/")
ntop.mkdir(dump_dir)
fname = dump_dir .. "20".. when
activities_name = fname .."_activities.csv"
contacts_name = fname .."_contacts.csv"
hosts_name = fname .."_hosts.csv"
interfaces_name = fname .."_interfaces.csv"
tables_name = fname ..".sql"
activities = io.open(activities_name, "w")
contacts = io.open(contacts_name, "w")
hosts = io.open(hosts_name, "w")
interfaces = io.open(interfaces_name, "w")
tables = io.open(tables_name, "w")
print("Please wait: we are dumping data into<ul>\n")
print("<li>"..activities_name.."\n")
print("<li>"..contacts_name.."\n")
print("<li>"..hosts_name.."\n")
print("<li>"..interfaces_name.."\n")
print("<li>"..tables_name.."\n")
print("</ul>\n")
interfaces:write("# idx | interface\n")
hosts:write("# idx | name\n")
contacts:write("# contacted_by=0, contacted_peers=1\n")
contact_idx = 0
idx = 0
repeat
key = ntop.setPopCache(key_name)
if(debug) then print("====> "..key_name.."<br>\n") end
if((key == nil) or (key == "")) then break end
if(debug) then print("=> "..key.."<br>\n") end
k1 = when.."|"..key.."|contacted_by"
v1 = ntop.getHashKeysCache(k1)
if(v1 ~= nil) then
res = split(k1, "|")
if(res[2] == "host_contacts") then
r = 0
else
-- aggregations
r = 1
end
name = host2id(res[4])
activities:write(idx..",".. interface2id(res[3])..","..name..",".. r.."\n")
if(debug) then print("-> (1)"..k1.."\n") end
for k,_ in pairs(v1) do
v = ntop.getHashCache(k1, k)
res = split(k, "@")
if(debug) then print("\t"..k .. "=" .. v.. "\n") end
if((res[1] ~= nil) and (res[2] ~= nil) and (v ~= nil)) then
contacts:write(contact_idx..","..idx..",0,"..host2id(res[1])..",".. res[2]..",".. v.."\n")
contact_idx = contact_idx + 1
end
if(delete_keys) then ntop.delHashCache(k1, k) end
end
idx = idx + 1
end
k2 = when.."|"..key.."|contacted_peers"
v2 = ntop.getHashKeysCache(k2)
if(v2 ~= nil) then
res = split(k1, "|")
if(res[2] == "host_contacts") then
r = 0
else
-- aggregations
r = 1
end
name = host2id(res[4])
activities:write(idx..",".. interface2id(res[3])..",".. name ..",".. r .."\n")
if(debug) then print("-> (2)"..k2.."\n") end
for k,v in pairs(v2) do
v = ntop.getHashCache(k2, k)
res = split(k, "@")
if(debug) then print("\t"..k .. "=" .. v.. "\n") end
if((res[1] ~= nil) and (res[2] ~= nil) and (v ~= nil)) then
contacts:write(contact_idx..","..idx..",1,"..host2id(res[1])..",".. res[2]..",".. v.."\n")
contact_idx = contact_idx + 1
end
if(delete_keys) then ntop.delHashCache(k2, k) end
end
idx = idx + 1
end
until(key == "")
-- Dump Interfaces
print(interfaces_id.." interfaces found<br>\n")
for iface,id in pairs(interfaces_hash) do
interfaces:write(id..","..iface.."\n")
end
-- Dump Hosts
print(hosts_id.." hosts found<br>\n")
for host,id in pairs(hosts_hash) do
hosts:write(id..","..host.."\n")
end
contacts:close()
activities:close()
hosts:close()
-- #########################################################
tables:write("CREATE DATABASE IF NOT EXISTS `20"..when.."`;\nUSE `20"..when.."`;\n\n")
tables:write("\nCREATE TABLE IF NOT EXISTS `interfaces` (\n`idx` int(11) NOT NULL,\n`interface_name` VARCHAR(32) NOT NULL,\nPRIMARY KEY (`idx`)\n) ENGINE=InnoDB DEFAULT CHARSET=latin1;\n\n")
tables:write("\nCREATE TABLE IF NOT EXISTS `hosts` (\n`idx` int(11) NOT NULL,\n`host_name` VARCHAR(128) NOT NULL,\nPRIMARY KEY (`idx`)\n) ENGINE=InnoDB DEFAULT CHARSET=latin1;\n\n")
tables:write("\nCREATE TABLE IF NOT EXISTS `activities` (\n`idx` int(11) NOT NULL,\n`interface_idx` int(11) NOT NULL,\n`host_idx` int(11) NOT NULL,\n`type` INT(8) NOT NULL,\nPRIMARY KEY (`idx`)\n) ENGINE=InnoDB DEFAULT CHARSET=latin1;\n\n")
tables:write("\nCREATE TABLE IF NOT EXISTS `contacts` (\n`idx` int(11) NOT NULL,\n`activity_idx` int(11) NOT NULL,\n`contact_type` int(11) NOT NULL,\n`host_idx` int(11) NOT NULL,\n`contact_family` int(8) DEFAULT NULL,\n`num_contacts` int(8) NOT NULL,\nPRIMARY KEY (`idx`)\n) ENGINE=InnoDB DEFAULT CHARSET=latin1;\n\n")
tables:write('mysql --local-infile=1 -u root -p<pass> 20'..when..'\n')
tables:write('LOAD data local infile "/var/tmp/ntopng/datadump/20'..when..'_interfaces.csv" INTO TABLE interfaces fields terminated by "," ignore 1 lines;\n')
tables:write('LOAD data local infile "/var/tmp/ntopng/datadump/20'..when..'_hosts.csv" INTO TABLE hosts fields terminated by "," ignore 1 lines;\n')
tables:write('LOAD data local infile "/var/tmp/ntopng/datadump/20'..when..'_activities.csv" INTO TABLE activities fields terminated by "," ignore 1 lines;\n')
tables:write('LOAD data local infile "/var/tmp/ntopng/datadump/20'..when..'_contacts.csv" INTO TABLE contacts fields terminated by "," ignore 1 lines;\n')
tables:close()
sec = os.clock() - begin
print(string.format("Elapsed time: %.2f min\n", sec/60).."<br>\n")
print("\nDone.\n")
-- redis-cli KEYS "131129|*" | xargs redis-cli DEL
|