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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
|
--
-- (C) 2013-16 - ntop.org
--
require "lua_utils"
require "db_utils"
require "historical_utils"
top_rrds = {
["bytes.rrd"] = "Traffic",
["packets.rrd"] = "Packets",
["drops.rrd"] = "Packet Drops",
["num_flows.rrd"] = "Active Flows",
["num_hosts.rrd"] = "Active Hosts",
["num_http_hosts.rrd"] = "Active HTTP Servers",
["tcp_lost.rrd"] = "TCP Packets Lost",
["tcp_ooo.rrd"] = "TCP Packets Out-Of-Order",
["tcp_retransmissions.rrd"] = "TCP Retransmitted Packets",
}
-- ########################################################
if(ntop.isPro()) then
package.path = dirs.installdir .. "/pro/scripts/lua/modules/?.lua;" .. package.path
require "nv_graph_utils"
end
-- ########################################################
function getProtoVolume(ifName, start_time, end_time)
ifId = getInterfaceId(ifName)
path = fixPath(dirs.workingdir .. "/" .. ifId .. "/rrd/")
rrds = ntop.readdir(path)
ret = { }
for rrdFile,v in pairs(rrds) do
if((string.ends(rrdFile, ".rrd")) and (top_rrds[rrdFile] == nil)) then
rrdname = getRRDName(ifId, nil, rrdFile)
if(ntop.notEmptyFile(rrdname)) then
local fstart, fstep, fnames, fdata = ntop.rrd_fetch(rrdname, 'AVERAGE', start_time, end_time)
if(fstart ~= nil) then
local num_points_found = table.getn(fdata)
accumulated = 0
for i, v in ipairs(fdata) do
for _, w in ipairs(v) do
if(w ~= w) then
-- This is a NaN
v = 0
else
--io.write(w.."\n")
v = tonumber(w)
if(v < 0) then
v = 0
end
end
end
accumulated = accumulated + v
end
if(accumulated > 0) then
rrdFile = string.sub(rrdFile, 1, string.len(rrdFile)-4)
ret[rrdFile] = accumulated
end
end
end
end
end
return(ret)
end
-- ########################################################
function navigatedir(url, label, base, path, go_deep, print_html, ifid, host, start_time, end_time)
local shown = false
local to_skip = false
local ret = { }
local do_debug = false
local printed = false
-- io.write(debug.traceback().."\n")
rrds = ntop.readdir(path)
table.sort(rrds)
for k,v in pairsByKeys(rrds, asc) do
if(v ~= nil) then
p = fixPath(path .. "/" .. v)
if(ntop.isdir(p)) then
if(go_deep) then
r = navigatedir(url, label.."/"..v, base, p, print_html, ifid, host, start_time, end_time)
for k,v in pairs(r) do
ret[k] = v
if(do_debug) then print(v.."<br>\n") end
end
end
else
rrd = singlerrd2json(ifid, host, v, start_time, end_time, true, false)
if((rrd.totalval ~= nil) and (rrd.totalval > 0)) then
if(top_rrds[v] == nil) then
if(label == "*") then
to_skip = true
else
if(not(shown) and not(to_skip)) then
if(print_html) then
if(not(printed)) then print('<li class="divider"></li>\n') printed = true end
print('<li class="dropdown-submenu"><a tabindex="-1" href="#">'..label..'</a>\n<ul class="dropdown-menu">\n')
end
shown = true
end
end
what = string.sub(path.."/"..v, string.len(base)+2)
label = string.sub(v, 1, string.len(v)-4)
label = l4Label(string.gsub(label, "_", " "))
ret[label] = what
if(do_debug) then print(what.."<br>\n") end
if(print_html) then
if(not(printed)) then print('<li class="divider"></li>\n') printed = true end
print("<li> <A HREF="..url..what..">"..label.."</A> </li>\n")
end
end
end
end
end
end
if(shown) then
if(print_html) then print('</ul></li>\n') end
end
return(ret)
end
-- ########################################################
function breakdownBar(sent, sentLabel, rcvd, rcvdLabel, thresholdLow, thresholdHigh)
if((sent+rcvd) > 0) then
sent2rcvd = round((sent * 100) / (sent+rcvd), 0)
--print(sent.."/"..rcvd.."/"..sent2rcvd)
if((thresholdLow == nil) or (thresholdLow < 0)) then thresholdLow = 0 end
if((thresholdHigh == nil) or (thresholdHigh > 100)) then thresholdHigh = 100 end
if(sent2rcvd < thresholdLow) then sentLabel = '<i class="fa fa-warning fa-lg"></i> '..sentLabel
elseif(sent2rcvd > thresholdHigh) then rcvdLabel = '<i class="fa fa-warning fa-lg""></i> '..rcvdLabel end
print('<div class="progress"><div class="progress-bar progress-bar-warning" aria-valuenow="'.. sent2rcvd..'" aria-valuemin="0" aria-valuemax="100" style="width: ' .. sent2rcvd.. '%;">'..sentLabel)
print('</div><div class="progress-bar progress-bar-info" aria-valuenow="'.. (100 -sent2rcvd)..'" aria-valuemin="0" aria-valuemax="100" style="width: ' .. (100-sent2rcvd) .. '%;">' .. rcvdLabel .. '</div></div>')
else
print(' ')
end
end
-- ########################################################
function percentageBar(total, value, valueLabel)
if((total ~= nil) and (total > 0)) then
pctg = round((value * 100) / total, 0)
print('<div class="progress"><div class="progress-bar progress-bar-warning" aria-valuenow="'.. pctg..'" aria-valuemin="0" aria-valuemax="100" style="width: ' .. pctg.. '%;">'..valueLabel)
print('</div></div>')
else
print(' ')
end
end
-- ########################################################
-- host_or_network: host or network name.
-- If network, must be prefixed with 'net:'
-- If profile, must be prefixed with 'profile:'
function getRRDName(ifid, host_or_network, rrdFile)
if host_or_network ~= nil and string.starts(host_or_network, 'net:') then
host_or_network = string.gsub(host_or_network, 'net:', '')
rrdname = fixPath(dirs.workingdir .. "/" .. ifid .. "/subnetstats/")
elseif host_or_network ~= nil and string.starts(host_or_network, 'profile:') then
host_or_network = string.gsub(host_or_network, 'profile:', '')
rrdname = fixPath(dirs.workingdir .. "/" .. ifid .. "/profilestats/")
elseif host_or_network ~= nil and string.starts(host_or_network, 'vlan:') then
host_or_network = string.gsub(host_or_network, 'vlan:', '')
rrdname = fixPath(dirs.workingdir .. "/" .. ifid .. "/vlanstats/")
else
rrdname = fixPath(dirs.workingdir .. "/" .. ifid .. "/rrd/")
end
if(host_or_network ~= nil) then
rrdname = rrdname .. getPathFromKey(host_or_network) .. "/"
end
return(rrdname..rrdFile)
end
-- ########################################################
zoom_vals = {
{ "1m", "now-60s", 60 },
{ "5m", "now-300s", 60*5 },
{ "10m", "now-600s", 60*10 },
{ "1h", "now-1h", 60*60*1 },
{ "3h", "now-3h", 60*60*3 },
{ "6h", "now-6h", 60*60*6 },
{ "12h", "now-12h", 60*60*12 },
{ "1d", "now-1d", 60*60*24 },
{ "1w", "now-1w", 60*60*24*7 },
{ "2w", "now-2w", 60*60*24*14 },
{ "1M", "now-1mon", 60*60*24*31 },
{ "6M", "now-6mon", 60*60*24*31*6 },
{ "1Y", "now-1y", 60*60*24*366 }
}
function getZoomAtPos(cur_zoom, pos_offset)
local pos = 1
local new_zoom_level = cur_zoom
for k,v in pairs(zoom_vals) do
if(zoom_vals[k][1] == cur_zoom) then
if (pos+pos_offset >= 1 and pos+pos_offset < 13) then
new_zoom_level = zoom_vals[pos+pos_offset][1]
break
end
end
pos = pos + 1
end
return new_zoom_level
end
-- ########################################################
function getZoomDuration(cur_zoom)
for k,v in pairs(zoom_vals) do
if(zoom_vals[k][1] == cur_zoom) then
return(zoom_vals[k][3])
end
end
return(180)
end
-- ########################################################
function zoomLevel2sec(zoomLevel)
if(zoomLevel == nil) then zoomLevel = "1h" end
for k,v in ipairs(zoom_vals) do
if(zoom_vals[k][1] == zoomLevel) then
return(zoom_vals[k][3])
end
end
return(3600) -- NOT REACHED
end
-- ########################################################
function drawPeity(ifid, host, rrdFile, zoomLevel, selectedEpoch)
rrdname = getRRDName(ifid, host, rrdFile)
if(zoomLevel == nil) then
zoomLevel = "1h"
end
nextZoomLevel = zoomLevel;
epoch = tonumber(selectedEpoch);
for k,v in ipairs(zoom_vals) do
if(zoom_vals[k][1] == zoomLevel) then
if(k > 1) then
nextZoomLevel = zoom_vals[k-1][1]
end
if(epoch) then
start_time = epoch - zoom_vals[k][3]/2
end_time = epoch + zoom_vals[k][3]/2
else
start_time = zoom_vals[k][2]
end_time = "now"
end
end
end
--print("=> Found "..rrdname.."<p>\n")
if(ntop.notEmptyFile(rrdname)) then
--io.write("=> Found ".. start_time .. "|" .. end_time .. "<p>\n")
local fstart, fstep, fnames, fdata = ntop.rrd_fetch(rrdname, 'AVERAGE', start_time..", end_time..")
if(fstart ~= nil) then
local max_num_points = 512 -- This is to avoid having too many points and thus a fat graph
local num_points_found = table.getn(fdata)
local sample_rate = round(num_points_found / max_num_points)
local num_points = 0
local step = 1
local series = {}
if(sample_rate < 1) then
sample_rate = 1
end
-- print("=> "..num_points_found.."[".. sample_rate .."]["..fstart.."]<p>")
id = 0
num = 0
total = 0
sample_rate = sample_rate-1
points = {}
for i, v in ipairs(fdata) do
timestamp = fstart + (i-1)*fstep
num_points = num_points + 1
local elemId = 1
for _, w in ipairs(v) do
if(w ~= w) then
-- This is a NaN
v = 0
else
v = tonumber(w)
if(v < 0) then
v = 0
end
end
value = v*8 -- bps
total = total + value
if(id == sample_rate) then
points[num] = round(value)..""
num = num+1
id = 0
else
id = id + 1
end
elemId = elemId + 1
end
end
end
end
print("<td class=\"text-right\">"..round(total).."</td><td> <span class=\"peity-line\">")
for i=0,10 do
if(i > 0) then print(",") end
print(points[i])
end
print("</span>\n")
end
-- ########################################################
function drawRRD(ifid, host, rrdFile, zoomLevel, baseurl, show_timeseries,
selectedEpoch, selected_epoch_sanitized, topArray)
local debug_rrd = false
if(zoomLevel == nil) then zoomLevel = "1h" end
if((selectedEpoch == nil) or (selectedEpoch == "")) then
-- Refresh the page every minute unless:
-- ** a specific epoch has been selected or
-- ** the user is browsing historical top talkers and protocols
print[[
<script>
setInterval(function() {
var talkers_loaded, protocols_loaded;
if($('a[href="#historical-top-talkers"]').length){
talkers_loaded = $('a[href="#historical-top-talkers"]').attr("loaded");
}
if($('a[href="#historical-top-apps"]').length){
protocols_loaded = $('a[href="#historical-top-apps"]').attr("loaded");
}
if(typeof talkers_loaded == 'undefined' && typeof protocols_loaded == 'undefined'){
window.location.reload();
}
}, 60*1000);
</script>]]
end
if ntop.isPro() then
_ifstats = interface.getStats()
if(_ifstats.isView == true) then topArray = nil end
drawProGraph(ifid, host, rrdFile, zoomLevel, baseurl, show_timeseries, selectedEpoch, selected_epoch_sanitized, topArray)
return
end
dirs = ntop.getDirs()
rrdname = getRRDName(ifid, host, rrdFile)
names = {}
series = {}
if(zoomLevel == nil) then
zoomLevel = "1h"
end
nextZoomLevel = zoomLevel;
epoch = tonumber(selectedEpoch);
for k,v in ipairs(zoom_vals) do
if(zoom_vals[k][1] == zoomLevel) then
if(k > 1) then
nextZoomLevel = zoom_vals[k-1][1]
end
if(epoch) then
start_time = epoch - zoom_vals[k][3]/2
end_time = epoch + zoom_vals[k][3]/2
else
start_time = zoom_vals[k][2]
end_time = "now"
end
end
end
prefixLabel = l4Label(string.gsub(rrdFile, ".rrd", ""))
-- io.write(prefixLabel.."\n")
if(prefixLabel == "Bytes") then
prefixLabel = "Traffic"
end
if(ntop.notEmptyFile(rrdname)) then
print [[
<style>
#chart_container {
display: inline-block;
font-family: Arial, Helvetica, sans-serif;
}
#chart {
float: left;
}
#legend {
float: left;
margin-left: 15px;
color: black;
background: white;
}
#y_axis {
float: left;
width: 40px;
}
</style>
<div>
<div class="container-fluid">
<ul class="nav nav-tabs" role="tablist" id="historical-tabs-container">
<li class="active"> <a href="#historical-tab-chart" role="tab" data-toggle="tab"> Chart </a> </li>
]]
if ntop.getPrefs().is_dump_flows_to_mysql_enabled
-- hide historical tabs for networks
and not string.starts(host, 'net:')
then
print('<li><a href="#ipv4" role="tab" data-toggle="tab" id="tab-ipv4"> IPv4 Flows </a> </li>\n')
print('<li><a href="#ipv6" role="tab" data-toggle="tab" id="tab-ipv6"> IPv6 Flows </a> </li>\n')
end
print[[
</ul>
<div class="tab-content">
<div class="tab-pane fade active in" id="historical-tab-chart">
<br>
<table border=0>
<tr><td valign="top">
]]
if(show_timeseries == 1) then
print [[
<div class="btn-group">
<button class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">Timeseries <span class="caret"></span></button>
<ul class="dropdown-menu">
]]
for k,v in pairs(top_rrds) do
rrdname = getRRDName(ifid, host, k)
if(ntop.notEmptyFile(rrdname)) then
rrd = singlerrd2json(ifid, host, k, start_time, end_time, true, false)
if((rrd.totalval ~= nil) and (rrd.totalval > 0)) then
print('<li><a href="'..baseurl .. '&rrd_file=' .. k .. '&graph_zoom=' .. zoomLevel .. '&epoch=' .. (selectedEpoch or '') .. '">'.. v ..'</a></li>\n')
end
end
end
dirs = ntop.getDirs()
p = dirs.workingdir .. "/" .. purifyInterfaceName(ifid) .. "/rrd/"
if(host ~= nil) then
p = p .. getPathFromKey(host)
end
d = fixPath(p)
go_deep = false
navigatedir(baseurl .. '&graph_zoom=' .. zoomLevel .. '&epoch=' .. (selectedEpoch or '')..'&rrd_file=',
"*", d, d, go_deep, true, ifid, host, start_time, end_time)
print [[
</ul>
</div><!-- /btn-group -->
]]
end -- show_timeseries == 1
print(' Timeframe: <div class="btn-group" data-toggle="buttons" id="graph_zoom">\n')
for k,v in ipairs(zoom_vals) do
-- display 1 minute button only for networks and interface stats
-- but exclude applications. Application statistics are gathered
-- every 5 minutes
local net_or_profile = false
if host and (string.starts(host, 'net:') or string.starts(host, 'profile:')) then
net_or_profile = true
end
if zoom_vals[k][1] == '1m' and (net_or_profile or (not net_or_profile and not top_rrds[rrdFile])) then
goto continue
end
print('<label class="btn btn-link ')
if(zoom_vals[k][1] == zoomLevel) then
print("active")
end
print('">')
print('<input type="radio" name="options" id="zoom_level_'..k..'" value="'..baseurl .. '&rrd_file=' .. rrdFile .. '&graph_zoom=' .. zoom_vals[k][1] .. '">'.. zoom_vals[k][1] ..'</input></label>\n')
::continue::
end
print [[
</div>
</div>
<script>
$('input:radio[id^=zoom_level_]').change( function() {
window.open(this.value,'_self',false);
});
</script>
<br />
<p>
<div id="legend"></div>
<div id="chart_legend"></div>
<div id="chart" style="margin-right: 50px; margin-left: 10px; display: table-cell"></div>
<p><font color=lightgray><small>NOTE: Click on the graph to zoom.</small></font>
</td>
<td rowspan=2>
<div id="y_axis"></div>
<div style="margin-left: 10px; display: table">
<div id="chart_container" style="display: table-row">
]]
if(string.contains(rrdFile, "num_")) then
formatter_fctn = "fint"
else
formatter_fctn = "fpackets"
end
if (topArray ~= nil) then
print [[
<table class="table table-bordered table-striped" style="border: 0; margin-right: 10px; display: table-cell">
]]
print(' <tr><th> </th><th>Time</th><th>Value</th></tr>\n')
rrd = rrd2json(ifid, host, rrdFile, start_time, end_time, true, false) -- the latest false means: expand_interface_views
if(string.contains(rrdFile, "num_") or string.contains(rrdFile, "tcp_") or string.contains(rrdFile, "packets") or string.contains(rrdFile, "drops")) then
print(' <tr><th>Min</th><td>' .. os.date("%x %X", rrd.minval_time) .. '</td><td>' .. formatValue(rrd.minval) .. '</td></tr>\n')
print(' <tr><th>Max</th><td>' .. os.date("%x %X", rrd.maxval_time) .. '</td><td>' .. formatValue(rrd.maxval) .. '</td></tr>\n')
print(' <tr><th>Last</th><td>' .. os.date("%x %X", rrd.lastval_time) .. '</td><td>' .. formatValue(round(rrd.lastval), 1) .. '</td></tr>\n')
print(' <tr><th>Average</th><td colspan=2>' .. formatValue(round(rrd.average, 2)) .. '</td></tr>\n')
print(' <tr><th>Total Number</th><td colspan=2>' .. formatValue(round(rrd.totalval)) .. '</td></tr>\n')
else
formatter_fctn = "fbits"
print(' <tr><th>Min</th><td>' .. os.date("%x %X", rrd.minval_time) .. '</td><td>' .. bitsToSize(rrd.minval) .. '</td></tr>\n')
print(' <tr><th>Max</th><td>' .. os.date("%x %X", rrd.maxval_time) .. '</td><td>' .. bitsToSize(rrd.maxval) .. '</td></tr>\n')
print(' <tr><th>Last</th><td>' .. os.date("%x %X", rrd.lastval_time) .. '</td><td>' .. bitsToSize(rrd.lastval) .. '</td></tr>\n')
print(' <tr><th>Average</th><td colspan=2>' .. bitsToSize(rrd.average*8) .. '</td></tr>\n')
print(' <tr><th>Total Traffic</th><td colspan=2>' .. bytesToSize(rrd.totalval) .. '</td></tr>\n')
end
print(' <tr><th>Selection Time</th><td colspan=2><div id=when></div></td></tr>\n')
print(' <tr><th>Minute<br>Interface<br>Top Talkers</th><td colspan=2><div id=talkers></div></td></tr>\n')
print [[
</table>
]]
end -- topArray ~= nil
print[[</div></td></tr></table>
</div> <!-- closes div id "historical-tab-chart "-->
]]
if ntop.getPrefs().is_dump_flows_to_mysql_enabled
-- hide historical tabs for networks and profiles
and not string.starts(host, 'net:')
then
if tonumber(start_time) ~= nil and tonumber(end_time) ~= nil then
-- if both start_time and end_time are vaid epoch we can print finer-grained top flows
printTopFlows(ifid, (host or ''), start_time, end_time, rrdFile, '', '', '', 5, 5)
else
printGraphTopFlows(ifid, (host or ''), _GET["epoch"], zoomLevel, rrdFile)
end
end
print[[
</div> <!-- closes div class "tab-content" -->
</div> <!-- closes div class "container-fluid" -->
<script>
var palette = new Rickshaw.Color.Palette();
var graph = new Rickshaw.Graph( {
element: document.getElementById("chart"),
width: 600,
height: 300,
renderer: 'area',
series:
]]
print(rrd.json)
print [[
} );
graph.render();
var chart_legend = document.querySelector('#chart_legend');
function fdate(when) {
var epoch = when*1000;
var d = new Date(epoch);
return(d);
}
function fbits(bits) {
var sizes = ['bps', 'Kbit/s', 'Mbit/s', 'Gbit/s', 'Tbit/s'];
if(bits == 0) return 'n/a';
var i = parseInt(Math.floor(Math.log(bits) / Math.log(1000)));
return Math.round(bits / Math.pow(1000, i), 2) + ' ' + sizes[i];
}
function capitaliseFirstLetter(string)
{
return string.charAt(0).toUpperCase() + string.slice(1);
}
/**
* Convert number of bytes into human readable format
*
* @param integer bytes Number of bytes to convert
* @param integer precision Number of digits after the decimal separator
* @return string
*/
function formatBytes(bytes, precision)
{
var kilobyte = 1024;
var megabyte = kilobyte * 1024;
var gigabyte = megabyte * 1024;
var terabyte = gigabyte * 1024;
if((bytes >= 0) && (bytes < kilobyte)) {
return bytes + ' B';
} else if((bytes >= kilobyte) && (bytes < megabyte)) {
return (bytes / kilobyte).toFixed(precision) + ' KB';
} else if((bytes >= megabyte) && (bytes < gigabyte)) {
return (bytes / megabyte).toFixed(precision) + ' MB';
} else if((bytes >= gigabyte) && (bytes < terabyte)) {
return (bytes / gigabyte).toFixed(precision) + ' GB';
} else if(bytes >= terabyte) {
return (bytes / terabyte).toFixed(precision) + ' TB';
} else {
return bytes + ' B';
}
}
var Hover = Rickshaw.Class.create(Rickshaw.Graph.HoverDetail, {
graph: graph,
xFormatter: function(x) { return new Date( x * 1000 ); },
yFormatter: function(bits) { return(]] print(formatter_fctn) print [[(bits)); },
render: function(args) {
var graph = this.graph;
var points = args.points;
var point = points.filter( function(p) { return p.active } ).shift();
if(point.value.y === null) return;
var formattedXValue = fdate(point.value.x); // point.formattedXValue;
var formattedYValue = ]]
print(formatter_fctn)
print [[(point.value.y); // point.formattedYValue;
var infoHTML = "";
]]
if(topArray ~= nil and topArray["top_talkers"] ~= nil) then
print[[
infoHTML += "<ul>";
$.ajax({
type: 'GET',
url: ']]
print(ntop.getHttpPrefix().."/lua/top_generic.lua?m=top_talkers&epoch='+point.value.x+'&addvlan=true&id="..tostring(ifid))
print [[',
data: { epoch: point.value.x },
async: false,
success: function(content) {
var info = jQuery.parseJSON(content);
$.each(info, function(i, n) {
if (n.length > 0)
infoHTML += "<li>"+capitaliseFirstLetter(i)+" [Avg Traffic/sec]<ol>";
var items = 0;
var other_traffic = 0;
$.each(n, function(j, m) {
if(items < 3) {
infoHTML += "<li><a href='host_details.lua?host="+m.address+"'>"+abbreviateString(m.label ? m.label : m.address,24);
infoHTML += "</a>";
if (m.vlan != "0") infoHTML += " ("+m.vlanm+")";
infoHTML += " ("+fbits((m.value*8)/60)+")</li>";
items++;
} else
other_traffic += m.value;
});
if (other_traffic > 0)
infoHTML += "<li>Other ("+fbits((other_traffic*8)/60)+")</li>";
if (n.length > 0)
infoHTML += "</ol></li>";
});
infoHTML += "</ul></li></li>";
}
});
infoHTML += "</ul>";]]
end -- topArray
print [[
this.element.innerHTML = '';
this.element.style.left = graph.x(point.value.x) + 'px';
/*var xLabel = document.createElement('div');
xLabel.setAttribute("style", "opacity: 0.5; background-color: #EEEEEE; filter: alpha(opacity=0.5)");
xLabel.className = 'x_label';
xLabel.innerHTML = formattedXValue + infoHTML;
this.element.appendChild(xLabel);
*/
$('#when').html(formattedXValue);
$('#talkers').html(infoHTML);
var item = document.createElement('div');
item.className = 'item';
item.innerHTML = this.formatter(point.series, point.value.x, point.value.y, formattedXValue, formattedYValue, point);
item.style.top = this.graph.y(point.value.y0 + point.value.y) + 'px';
this.element.appendChild(item);
var dot = document.createElement('div');
dot.className = 'dot';
dot.style.top = item.style.top;
dot.style.borderColor = point.series.color;
this.element.appendChild(dot);
if(point.active) {
item.className = 'item active';
dot.className = 'dot active';
}
this.show();
if(typeof this.onRender == 'function') {
this.onRender(args);
}
// Put the selected graph epoch into the legend
//chart_legend.innerHTML = point.value.x; // Epoch
this.selected_epoch = point.value.x;
//event
}
} );
var hover = new Hover( { graph: graph } );
var legend = new Rickshaw.Graph.Legend( {
graph: graph,
element: document.getElementById('legend')
} );
//var axes = new Rickshaw.Graph.Axis.Time( { graph: graph } ); axes.render();
var yAxis = new Rickshaw.Graph.Axis.Y({
graph: graph,
tickFormat: ]] print(formatter_fctn) print [[
});
yAxis.render();
$("#chart").click(function() {
if(hover.selected_epoch)
window.location.href = ']]
print(baseurl .. '&rrd_file=' .. rrdFile .. '&graph_zoom=' .. nextZoomLevel .. '&epoch=')
print[['+hover.selected_epoch;
});
</script>
]]
else
print("<div class=\"alert alert-danger\"><img src=".. ntop.getHttpPrefix() .. "/img/warning.png> File "..rrdname.." cannot be found</div>")
end
end
-- ########################################################
function create_rrd(name, step, ds)
if(not(ntop.exists(name))) then
if(enable_second_debug == 1) then io.write('Creating RRD ', name, '\n') end
local prefs = ntop.getPrefs()
ntop.rrd_create(
name,
step, -- step
'DS:' .. ds .. ':DERIVE:5:U:U',
'RRA:AVERAGE:0.5:1:'..tostring(prefs.intf_rrd_raw_days*24*60*60), -- raw: 1 day = 86400
'RRA:AVERAGE:0.5:60:'..tostring(prefs.intf_rrd_1min_days*24*60), -- 1 min resolution = 1 month
'RRA:AVERAGE:0.5:3600:'..tostring(prefs.intf_rrd_1h_days*24), -- 1h resolution (3600 points) 2400 hours = 100 days
'RRA:AVERAGE:0.5:86400:'..tostring(prefs.intf_rrd_1d_days) -- 1d resolution (86400 points) 365 days
-- 'RRA:HWPREDICT:1440:0.1:0.0035:20'
)
end
end
function create_rrd_num(name, ds)
if(not(ntop.exists(name))) then
if(enable_second_debug == 1) then io.write('Creating RRD ', name, '\n') end
local prefs = ntop.getPrefs()
ntop.rrd_create(
name,
1, -- step
'DS:' .. ds .. ':GAUGE:5:0:U',
'RRA:AVERAGE:0.5:1:'..tostring(prefs.intf_rrd_raw_days*24*60*60), -- raw: 1 day = 86400
'RRA:AVERAGE:0.5:3600:'..tostring(prefs.intf_rrd_1h_days*24), -- 1h resolution (3600 points) 2400 hours = 100 days
'RRA:AVERAGE:0.5:86400:'..tostring(prefs.intf_rrd_1d_days) -- 1d resolution (86400 points) 365 days
-- 'RRA:HWPREDICT:1440:0.1:0.0035:20'
)
end
end
function makeRRD(basedir, ifname, rrdname, step, value)
local name = fixPath(basedir .. "/" .. rrdname .. ".rrd")
if(string.contains(rrdname, "num_")) then
create_rrd_num(name, rrdname)
else
create_rrd(name, 1, rrdname)
end
ntop.rrd_update(name, "N:".. tolongint(value))
if(enable_second_debug == 1) then io.write('Updating RRD ['.. ifname..'] '.. name .. " " .. value ..'\n') end
end
function createRRDcounter(path, step, verbose)
if(not(ntop.exists(path))) then
if(verbose) then print('Creating RRD ', path, '\n') end
local prefs = ntop.getPrefs()
ntop.rrd_create(
path,
step, -- step
'DS:sent:DERIVE:600:U:U',
'DS:rcvd:DERIVE:600:U:U',
'RRA:AVERAGE:0.5:1:'..tostring(prefs.other_rrd_raw_days*24*300), -- raw: 1 day = 1 * 24 = 24 * 300 sec = 7200
'RRA:AVERAGE:0.5:12:'..tostring(prefs.other_rrd_1h_days*24), -- 1h resolution (12 points) 2400 hours = 100 days
'RRA:AVERAGE:0.5:288:'..tostring(prefs.other_rrd_1d_days) -- 1d resolution (288 points) 365 days
--'RRA:HWPREDICT:1440:0.1:0.0035:20'
)
end
end
-- ########################################################
function createSingleRRDcounter(path, step, verbose)
if(not(ntop.exists(path))) then
if(verbose) then print('Creating RRD ', path, '\n') end
local prefs = ntop.getPrefs()
ntop.rrd_create(
path,
step, -- step
'DS:num:DERIVE:600:U:U',
'RRA:AVERAGE:0.5:1:'..tostring(prefs.other_rrd_raw_days*24*300), -- raw: 1 day = 1 * 24 = 24 * 300 sec = 7200
'RRA:AVERAGE:0.5:12:'..tostring(prefs.other_rrd_1h_days*24), -- 1h resolution (12 points) 2400 hours = 100 days
'RRA:AVERAGE:0.5:288:'..tostring(prefs.other_rrd_1d_days), -- 1d resolution (288 points) 365 days
'RRA:HWPREDICT:1440:0.1:0.0035:20')
end
end
-- ########################################################
-- this method will be very likely used when saving subnet rrd traffic statistics
function createTripleRRDcounter(path, step, verbose)
if(not(ntop.exists(path))) then
if(verbose) then io.write('Creating RRD '..path..'\n') end
local prefs = ntop.getPrefs()
ntop.rrd_create(
path,
step, -- step
'DS:ingress:DERIVE:600:U:U',
'DS:egress:DERIVE:600:U:U',
'DS:inner:DERIVE:600:U:U',
'RRA:AVERAGE:0.5:1:'..tostring(prefs.other_rrd_raw_days*24*300), -- raw: 1 day = 1 * 24 = 24 * 300 sec = 7200
'RRA:AVERAGE:0.5:12:'..tostring(prefs.other_rrd_1h_days*24), -- 1h resolution (12 points) 2400 hours = 100 days
'RRA:AVERAGE:0.5:288:'..tostring(prefs.other_rrd_1d_days) -- 1d resolution (288 points) 365 days
--'RRA:HWPREDICT:1440:0.1:0.0035:20'
)
end
end
-- ########################################################
function dumpSingleTreeCounters(basedir, label, host, verbose)
what = host[label]
if(what ~= nil) then
for k,v in pairs(what) do
for k1,v1 in pairs(v) do
-- print("-->"..k1.."/".. type(v1).."<--\n")
if(type(v1) == "table") then
for k2,v2 in pairs(v1) do
dname = fixPath(basedir.."/"..label.."/"..k.."/"..k1)
if(not(ntop.exists(dname))) then
ntop.mkdir(dname)
end
fname = dname..fixPath("/"..k2..".rrd")
createSingleRRDcounter(fname, 300, verbose)
ntop.rrd_update(fname, "N:"..toint(v2))
if(verbose) then print("\t"..fname.."\n") end
end
else
dname = fixPath(basedir.."/"..label.."/"..k)
if(not(ntop.exists(dname))) then
ntop.mkdir(dname)
end
fname = dname..fixPath("/"..k1..".rrd")
createSingleRRDcounter(fname, 300, verbose)
ntop.rrd_update(fname, "N:"..toint(v1))
if(verbose) then print("\t"..fname.."\n") end
end
end
end
end
end
function printGraphTopFlows(ifId, host, epoch, zoomLevel, l7proto)
-- Check if the DB is enabled
rsp = interface.execSQLQuery("show tables")
if(rsp == nil) then return end
if((epoch == nil) or (epoch == "")) then epoch = os.time() end
local d = getZoomDuration(zoomLevel)
epoch_end = epoch
epoch_begin = epoch-d
local tot_flows = { }
local versions = { ['IPv4']=4, ['IPv6']=6 }
for k,v in pairs(versions) do
local res = getNumFlows(ifId, v, (host or ''), '', '', (l7proto or ''), '', epoch_begin, epoch_end)
for _,flow in pairs(res) do
tot_flows[v] = flow['TOT_FLOWS']
end
end
printTopFlows(ifId, host, epoch_begin, epoch_end, l7proto, '', '', '', tot_flows[4], tot_flows[6])
end
function printTopFlows(ifId, host, epoch_begin, epoch_end, l7proto, l4proto, port, info, limitv4, limitv6)
url_update = ntop.getHttpPrefix().."/lua/get_db_flows.lua?ifId="..ifId.. "&host="..(host or '') .. "&epoch_begin="..(epoch_begin or '').."&epoch_end="..(epoch_end or '').."&l4proto="..(l4proto or '').."&port="..(port or '').."&info="..(info or '')
if(l7proto ~= "") then
if(not(isnumber(l7proto))) then
local id
-- io.write(l7proto.."\n")
l7proto = string.gsub(l7proto, "%.rrd", "")
if(string.ends(l7proto, ".rrd")) then l7proto = string.sub(l7proto, 1, -5) end
id = interface.getnDPIProtoId(l7proto)
if(id ~= -1) then
l7proto = id
title = "Top "..l7proto.." Flows"
else
l7proto = ""
end
end
if(l7proto ~= "") then
url_update = url_update.."&l7proto="..l7proto
end
end
if((host == "") and (l4proto == "") and (port == "")) then
title = "Top Flows ["..formatEpoch(epoch_begin).." - "..formatEpoch(epoch_end).."]"
else
title = ""
end
if(host ~= nil) then
local chunks = {host:match("(%d+)%.(%d+)%.(%d+)%.(%d+)")}
if(#chunks == 4) then
limitv6="0"
end
end
-- prepare some attributes that will be attached to divs
local div_data = ""
if epoch_begin ~= "" and epoch_begin ~= nil then
div_data = div_data..' epoch_begin="'..tostring(epoch_begin)..'" '
end
if epoch_end ~= "" and epoch_end ~= nil then
div_data = div_data..' epoch_end="'..tostring(epoch_end)..'" '
end
if host ~= "" and host ~= nil then
div_data = div_data..' host="'..tostring(host)..'" '
end
if l7proto ~= "" and l7proto ~= nil then
div_data = div_data..' l7_proto_id="'..l7proto..'" '
end
if l4proto ~= "" and l4proto ~= nil then
div_data = div_data..' l4_proto_id="'..l4proto..'" '
end
if port ~= "" and port ~= nil then
div_data = div_data..' port="'..port..'" '
end
local ipv4_enabled = not(limitv4 == nil or limitv4 == "" or limitv4 == "0")
local ipv6_enabled = not(limitv6 == nil or limitv6 == "" or limitv6 == "0")
if ipv4_enabled == true then
print [[
<div class="tab-pane fade" id="ipv4" ]] print(div_data) print[[">
<div id="table-flows4"></div>
]] historicalDownloadButtonsBar('flows_v4', 'ipv4',
ipv4_enabled,
ipv6_enabled) print[[
</div>
]]
else
print[[
<script>
$('#tab-ipv4').remove()
</script>
]]
end
if ipv6_enabled == true then
print[[
<div class="tab-pane fade" id="ipv6" ]] print(div_data) print[[">
<div id="table-flows6"></div>
]] historicalDownloadButtonsBar('flows_v6', 'ipv6',
ipv4_enabled,
ipv6_enabled
) print[[
</div>
]]
else
print[[
<script>
$('#tab-ipv6').remove()
</script>
]]
end
print [[
<script>
$('a[href="#ipv4"]').on('shown.bs.tab', function (e) {
if ($('a[href="#ipv4"]').attr("loaded") == 1){
// do nothing if the tab has already been computed and populated
return;
}
// if here, then we actually have to load the datatable
$('a[href="#ipv4"]').attr("loaded", 1);
]]
if(not((limitv4 == nil) or (limitv4 == "") or (limitv4 == "0"))) then
print [[
var url_update4 = "]] print(url_update.."&limit="..limitv4) print [[&version=4";
var graph_options4 = {
url: url_update4,
perPage: 5, ]]
if(title ~= "") then print('title: "IPv4 '..title..'",\n') else print("title: '',\n") end
print [[
showFilter: true,
showPagination: true,
sort: [ [ "BYTES","desc"] ],
columns: [
{
title: "Key",
field: "idx",
hidden: true,
},
]]
if(ntop.isPro()) then
print [[
{
title: "",
field: "FLOW_URL",
sortable: false,
css: {
textAlign: 'center'
}
},
]]
end
print [[
{
title: "Application",
field: "L7_PROTO",
sortable: true,
css: {
textAlign: 'center'
}
},
{
title: "L4 Proto",
field: "PROTOCOL",
sortable: true,
css: {
textAlign: 'center'
}
},
{
title: "Client",
field: "CLIENT",
sortable: false,
},
{
title: "Server",
field: "SERVER",
sortable: false,
},
{
title: "Begin",
field: "FIRST_SWITCHED",
sortable: true,
css: {
textAlign: 'center'
}
},
{
title: "End",
field: "LAST_SWITCHED",
sortable: true,
css: {
textAlign: 'center'
}
},
{
title: "Traffic Sent",
field: "IN_BYTES",
sortable: true,
css: {
textAlign: 'right'
}
},
{
title: "Traffic Received",
field: "OUT_BYTES",
sortable: true,
css: {
textAlign: 'right'
}
},
{
title: "Total Traffic",
field: "BYTES",
sortable: true,
css: {
textAlign: 'right'
}
},
{
title: "Info",
field: "INFO",
sortable: true,
css: {
textAlign: 'left'
}
},
{
title: "Avg Thpt",
field: "AVG_THROUGHPUT",
sortable: false,
css: {
textAlign: 'right'
}
}
]
};
var table4 = $("#table-flows4").datatable(graph_options4);
}); // closes the event handler on shown.bs.tab
]]
end
if((limitv6 == nil) or (limitv6 == "") or (limitv6 == "0")) then print("</script>") return end
print [[
$('a[href="#ipv6"]').on('shown.bs.tab', function (e) {
if ($('a[href="#ipv6"]').attr("loaded") == 1){
// do nothing if the tab has already been computed and populated
return;
}
// if here, then we actually have to load the datatable
$('a[href="#ipv6"]').attr("loaded", 1);
var url_update6 = "]] print(url_update.."&limit="..limitv6) print [[&version=6";
var graph_options6 = {
url: url_update6,
perPage: 5, ]]
if(title ~= "") then print('title: "IPv6 '..title..'",\n') else print("title: '',\n") end
print [[
showFilter: true,
showPagination: true,
sort: [ [ "BYTES","desc"] ],
columns: [
{
title: "Key",
field: "idx",
hidden: true,
},
]]
if(ntop.isPro()) then
print [[
{
title: "",
field: "FLOW_URL",
sortable: false,
css: {
textAlign: 'center'
}
},
]]
end
print [[
{
title: "Application",
field: "L7_PROTO",
sortable: true,
css: {
textAlign: 'center'
}
},
{
title: "L4 Proto",
field: "PROTOCOL",
sortable: true,
css: {
textAlign: 'center'
}
},
{
title: "Client",
field: "CLIENT",
sortable: false,
},
{
title: "Server",
field: "SERVER",
sortable: false,
},
{
title: "Begin",
field: "FIRST_SWITCHED",
sortable: true,
css: {
textAlign: 'center'
}
},
{
title: "End",
field: "LAST_SWITCHED",
sortable: true,
css: {
textAlign: 'center'
}
},
{
title: "Traffic Sent",
field: "IN_BYTES",
sortable: true,
css: {
textAlign: 'right'
}
},
{
title: "Traffic Received",
field: "OUT_BYTES",
sortable: true,
css: {
textAlign: 'right'
}
},
{
title: "Total Traffic",
field: "BYTES",
sortable: true,
css: {
textAlign: 'right'
}
},
{
title: "Avg Thpt",
field: "AVG_THROUGHPUT",
sortable: false,
css: {
textAlign: 'right'
}
}
]
};
var table6 = $("#table-flows6").datatable(graph_options6);
}); // closes the event handler on shown.bs.tab
</script>
]]
end
-- ########################################################
-- reads one or more RRDs and returns a json suitable to feed rickshaw
function singlerrd2json(ifid, host, rrdFile, start_time, end_time, rickshaw_json, append_ifname_to_labels)
local rrdname = getRRDName(ifid, host, rrdFile)
local names = {}
local names_cache = {}
local series = {}
local prefixLabel = l4Label(string.gsub(rrdFile, ".rrd", ""))
-- with a scaling factor we can stretch or shrink rrd values
-- by default we set this to a value of 8, in order to convert bytes
-- rrds into bits.
local scaling_factor = 8
--io.write(prefixLabel.."\n")
if(prefixLabel == "Bytes" or string.starts(rrdFile, 'categories/')) then
prefixLabel = "Traffic"
end
if(string.contains(rrdFile, "num_") or string.contains(rrdFile, "tcp_") or string.contains(rrdFile, "packets") or string.contains(rrdFile, "drops")) then
-- do not scale number, packets, and drops
scaling_factor = 1
end
if(not ntop.notEmptyFile(rrdname)) then return '{}' end
local fstart, fstep, fnames, fdata = ntop.rrd_fetch(rrdname, 'AVERAGE', start_time, end_time)
if(fstart == nil) then return '{}' end
--[[
io.write('start time: '..start_time..' end_time: '..end_time..'\n')
io.write('fstart: '..fstart..' fstep: '..fstep..' rrdname: '..rrdname..'\n')
io.write('len(fdata): '..table.getn(fdata)..'\n')
--]]
local max_num_points = 600 -- This is to avoid having too many points and thus a fat graph
local num_points_found = table.getn(fdata)
local sample_rate = round(num_points_found / max_num_points)
if(sample_rate < 1) then sample_rate = 1 end
-- prepare rrd labels
for i, n in ipairs(fnames) do
-- handle duplicates
if (names_cache[n] == nil) then
local extra_info = ''
names_cache[n] = true
if append_ifname_to_labels then
extra_info = getInterfaceName(ifid)
end
if host ~= nil and not string.starts(host, 'profile:') and not string.starts(rrdFile, 'categories/') then
extra_info = extra_info.." ".. firstToUpper(n)
end
if extra_info ~= "" then
names[#names+1] = prefixLabel.." ("..trimSpace(extra_info)..")"
else
names[#names+1] = prefixLabel
end
end
end
local minval, maxval, lastval = 0, 0, 0
local maxval_time, minval_time, lastval_time = nil, nil, nil
local sampling = 1
local s = {}
local totalval, avgval = {}, {}
for i, v in ipairs(fdata) do
local instant = fstart + (i-1)*fstep -- this is the instant in time corresponding to the datapoint
s[0] = instant -- s holds the instant and all the values
totalval[instant] = 0 -- totalval holds the sum of all values of this instant
avgval[instant] = 0
local elemId = 1
for _, w in ipairs(v) do
if(w ~= w) then
-- This is a NaN
w = 0
else
--io.write(w.."\n")
w = tonumber(w)
if(w < 0) then
w = 0
end
end
-- update the total value counter, which is the non-scaled integral over time
totalval[instant] = totalval[instant] + w * fstep
-- also update the average val (do not multiply by fstep, this is not the integral)
avgval[instant] = avgval[instant] + w
-- and the scaled current value (remember that these are derivatives)
w = w * scaling_factor
-- the scaled current value w goes into its own element elemId
if (s[elemId] == nil) then s[elemId] = 0 end
s[elemId] = s[elemId] + w
--if(s[elemId] > 0) then io.write("[".. elemId .. "]=" .. s[elemId] .."\n") end
elemId = elemId + 1
end
-- stops every sample_rate samples, or when there are no more points
if(sampling == sample_rate or num_points_found == i) then
local sample_sum = 0
for elemId=1,#s do
-- calculate the average in the sampling period
s[elemId] = s[elemId] / sampling
sample_sum = sample_sum + s[elemId]
end
-- update last instant
if lastval_time == nil or instant > lastval_time then
lastval = sample_sum
lastval_time = instant
end
-- possibly update maximum value (grab the most recent in case of a tie)
if maxval_time == nil or (sample_sum >= maxval and instant > maxval_time) then
maxval = sample_sum
maxval_time = instant
end
-- possibly update the minimum value (grab the most recent in case of a tie)
if minval_time == nil or (sample_sum <= minval and instant > minval_time) then
minval = sample_sum
minval_time = instant
end
series[#series+1] = s
sampling = 1
s = {}
else
sampling = sampling + 1
end
end
local tot = 0
for k, v in pairs(totalval) do tot = tot + v end
totalval = tot
tot = 0
for k, v in pairs(avgval) do tot = tot + v end
local average = tot / num_points_found
local percentile = 0.95*maxval
local colors = {
'#1f77b4',
'#ff7f0e',
'#2ca02c',
'#d62728',
'#9467bd',
'#8c564b',
'#e377c2',
'#7f7f7f',
'#bcbd22',
'#17becf',
-- https://github.com/mbostock/d3/wiki/Ordinal-Scales
'#ff7f0e',
'#ffbb78',
'#1f77b4',
'#aec7e8',
'#2ca02c',
'#98df8a',
'#d62728',
'#ff9896',
'#9467bd',
'#c5b0d5',
'#8c564b',
'#c49c94',
'#e377c2',
'#f7b6d2',
'#7f7f7f',
'#c7c7c7',
'#bcbd22',
'#dbdb8d',
'#17becf',
'#9edae5'
}
if(names ~= nil) then
json_ret = ''
if(rickshaw_json) then
for elemId=1,#names do
if(elemId > 1) then
json_ret = json_ret.."\n,\n"
end
local name = names[elemId]
json_ret = json_ret..'{"name": "'.. name .. '",\n'
json_ret = json_ret..'color: \''.. colors[elemId] ..'\',\n'
json_ret = json_ret..'"data": [\n'
n = 0
for key, value in pairs(series) do
if(n > 0) then
json_ret = json_ret..',\n'
end
json_ret = json_ret..'\t{ "x": '.. value[0] .. ', "y": '.. value[elemId] .. '}'
n = n + 1
end
json_ret = json_ret.."\n]}\n"
end
else
-- NV3
local num_entries = 0;
for elemId=1,#names do
num_entries = num_entries + 1
if(elemId > 1) then
json_ret = json_ret.."\n,\n"
end
name = names[elemId]
json_ret = json_ret..'{"key": "'.. name .. '",\n'
-- json_ret = json_ret..'"color": "'.. colors[num_entries] ..'",\n'
json_ret = json_ret..'"area": true,\n'
json_ret = json_ret..'"values": [\n'
n = 0
for key, value in pairs(series) do
if(n > 0) then
json_ret = json_ret..',\n'
end
json_ret = json_ret..'\t[ '..value[0] .. ', '.. value[elemId] .. ' ]'
--json_ret = json_ret..'\t{ "x": '.. value[0] .. ', "y": '.. value[elemId] .. '}'
n = n + 1
end
json_ret = json_ret.."\n] }\n"
end
if(false) then
json_ret = json_ret..",\n"
num_entries = num_entries + 1
json_ret = json_ret..'\n{"key": "Average",\n'
json_ret = json_ret..'"color": "'.. colors[num_entries] ..'",\n'
json_ret = json_ret..'"type": "line",\n'
json_ret = json_ret..'"values": [\n'
n = 0
for key, value in pairs(series) do
if(n > 0) then
json_ret = json_ret..',\n'
end
--json_ret = json_ret..'\t[ '..value[0] .. ', '.. value[elemId] .. ' ]'
json_ret = json_ret..'\t{ "x": '.. value[0] .. ', "y": '.. average .. '}'
n = n + 1
end
json_ret = json_ret..'\n] },\n'
num_entries = num_entries + 1
json_ret = json_ret..'\n{"key": "95th Percentile",\n'
json_ret = json_ret..'"color": "'.. colors[num_entries] ..'",\n'
json_ret = json_ret..'"type": "line",\n'
json_ret = json_ret..'"yAxis": 1,\n'
json_ret = json_ret..'"values": [\n'
n = 0
for key, value in pairs(series) do
if(n > 0) then
json_ret = json_ret..',\n'
end
--json_ret = json_ret..'\t[ '..value[0] .. ', '.. value[elemId] .. ' ]'
json_ret = json_ret..'\t{ "x": '.. value[0] .. ', "y": '.. percentile .. '}'
n = n + 1
end
json_ret = json_ret..'\n] }\n'
end
end
end
local ret = {}
ret.maxval_time = maxval_time
ret.maxval = round(maxval, 0)
ret.minval_time = minval_time
ret.minval = round(minval, 0)
ret.lastval_time = lastval_time
ret.lastval = round(lastval, 0)
ret.totalval = round(totalval, 0)
ret.percentile = round(percentile, 0)
ret.average = round(average, 0)
ret.json = json_ret
return(ret)
end
-- #################################################
function rrd2json(ifid, host, rrdFile, start_time, end_time, rickshaw_json, expand_interface_views)
local ret = {}
local num = 0
local debug_metric = false
interface.select(getInterfaceName(ifid))
local ifstats = interface.getStats()
local rrd_if_ids = {} -- read rrds for interfaces listed here
rrd_if_ids[1] = ifid -- the default submitted interface
-- interface.select(getInterfaceName(ifid))
if(debug_metric) then
io.write('ifid: '..ifid..' ifname:'..getInterfaceName(ifid)..'\n')
io.write('expand_interface_views: '..tostring(expand_interface_views)..'\n')
io.write('ifstats.isView: '..tostring(ifstats.isView)..'\n')
end
if expand_interface_views and ifstats.isView then
-- expand rrds for views and read each physical interface separately
for iface,_ in pairs(ifstats.interfaces) do
if(debug_metric) then io.write('iface: '..iface..' id: '..getInterfaceId(iface)..'\n') end
rrd_if_ids[#rrd_if_ids+1] = getInterfaceId(iface)
end
end
if(debug_metric) then io.write("RRD File: "..rrdFile.."\n") end
if(rrdFile == "all") then
-- disable expand interface views for rrdFile == all
expand_interface_views=false
local dirs = ntop.getDirs()
local p = dirs.workingdir .. "/" .. ifid .. "/rrd/"
if(debug_metric) then io.write("Navigating: "..p.."\n") end
if(host ~= nil) then
p = p .. getPathFromKey(host)
go_deep = true
else
go_deep = false
end
d = fixPath(p)
rrds = navigatedir("", "*", d, d, go_deep, false, ifid, host, start_time, end_time)
local traffic_array = {}
for key, value in pairs(rrds) do
rsp = singlerrd2json(ifid, host, value, start_time, end_time, rickshaw_json, expand_interface_views)
if(rsp.totalval ~= nil) then total = rsp.totalval else total = 0 end
if(total > 0) then
traffic_array[total] = rsp
if(debug_metric) then io.write("Analyzing: "..value.." [total "..total.."]\n") end
end
end
for key, value in pairsByKeys(traffic_array, rev) do
ret[#ret+1] = value
if(ret[#ret].json ~= nil) then
if(debug_metric) then io.write(key.."\n") end
num = num + 1
if(num >= 10) then break end
end
end
else
num = 0
for _,iface in pairs(rrd_if_ids) do
if(debug_metric) then io.write('iface: '..iface..'\n') end
for i,rrd in pairs(split(rrdFile, ",")) do
if(debug_metric) then io.write("["..i.."] "..rrd..' iface: '..iface.."\n") end
ret[#ret + 1] = singlerrd2json(iface, host, rrd, start_time, end_time, rickshaw_json, expand_interface_views)
if(ret[#ret].json ~= nil) then num = num + 1 end
end
end
end
if(debug_metric) then io.write("#rrds="..num.."\n") end
if(num == 0) then
ret = {}
ret.json = "[]"
return(ret)
end
local i = 1
-- if we are expanding an interface view, we want to concatenate
-- jsons for single interfaces, and not for the view. Since view statistics
-- are in ret[1], it suffices to aggregate jsons from index i >= 2
if expand_interface_views and ifstats.isView then
i = 2
end
local json = "["
local first = true -- used to decide where to append commas
while i <= num do
if(debug_metric) then io.write("->"..i.."\n") end
if not first then json = json.."," end
json = json..ret[i].json
i = i + 1
first = false
end
json = json.."]"
-- the (possibly aggregated) json always goes into ret[1]
-- ret[1] possibly contains aggregated view statistics such as
-- maxval and maxval_time or minval and minval_time
ret[1].json = json
-- io.write(json.."\n")
return(ret[1])
end
|