require "lua_utils" local pcap_status_url = ntop.getHttpPrefix().."/lua/get_nbox_data.lua?action=status" local pcap_request_url = ntop.getHttpPrefix().."/lua/get_nbox_data.lua?action=schedule" local favourites_url = ntop.getHttpPrefix().."/lua/get_historical_favourites.lua" local flows_download_url = ntop.getHttpPrefix().."/lua/get_db_flows.lua" function commonJsUtils() print[[ function hideAll(cla){ $('.' + cla).hide(); } function showOne(cla, id){ $('.' + cla).not('#' + id).hide(); $('#' + id).show(); } function disableAllDropdowns(){ $("select").each(function() { $(this).prop("disabled", true); }); } function enableAllDropdowns(){ $("select").each(function() { $(this).prop("disabled", false); }); } function hostkey2hostid(host_key) { var info; var hostinfo = []; host_key = host_key.replace(/\:/g, "____"); host_key = host_key.replace(/\//g, "___"); host_key = host_key.replace(/\./g, "__"); info = host_key.split("@"); return(info); } function buildRequestData(source_div_id){ var epoch_begin = $('#' + source_div_id).attr("epoch_begin"); var epoch_end = $('#' + source_div_id).attr("epoch_end"); var ifname = $('#' + source_div_id).attr("ifname"); var ifId = "]] print(tostring(ifId)) print [["; var host = $('#' + source_div_id).attr("host"); var peer = $('#' + source_div_id).attr("peer"); var l7_proto_id = $('#' + source_div_id).attr("l7_proto_id"); var l4_proto_id = $('#' + source_div_id).attr("l4_proto_id"); var port = $('#' + source_div_id).attr("port"); var res = {epoch_begin: epoch_begin, epoch_end: epoch_end}; if (typeof ifname != 'undefined') res.ifname = ifname; if (typeof ifId != 'undefined') res.ifId = ifId; if (typeof host != 'undefined') res.host = host; if (typeof peer != 'undefined') res.peer = peer; if (typeof port != 'undefined') res.port = port; if (typeof l7_proto_id != 'undefined'){ res.l7_proto_id = l7_proto_id; res.l7proto = l7_proto_id; }; if (typeof l4_proto_id != 'undefined'){ res.l4_proto_id = l4_proto_id; res.l4proto = l4_proto_id; }; return res; } function addToFavourites(source_div_id, stats_type, favourite_type, select_id){ $.ajax({type:'GET',url:"]]print(favourites_url)print[[?action=set&stats_type=" + stats_type + "&favourite_type=" + favourite_type, data:buildRequestData(source_div_id), success:function(data){ data=jQuery.parseJSON(data); populateFavourites(source_div_id, stats_type, favourite_type, select_id); }, error:function(){ perror('An HTTP error occurred.'); } }); } function removeFromFavourites(source_div_id, stats_type, favourite_type, select_id){ $.ajax({type:'GET',url:"]]print(favourites_url)print[[?action=del&stats_type=" + stats_type + "&favourite_type=" + favourite_type, data:buildRequestData(source_div_id), success:function(data){ data=jQuery.parseJSON(data); populateFavourites(source_div_id, stats_type, favourite_type, select_id); }, error:function(){ perror('An HTTP error occurred.'); } }); } function populateFavourites(source_div_id, stats_type, favourite_type, select_id){ var multival_separator = " <---> "; $('#'+select_id).find('option').remove(); $.ajax({type:'GET',url:"]]print(favourites_url)print[[?action=get&stats_type=" + stats_type + "&favourite_type=" + favourite_type, data:buildRequestData(source_div_id), success:function(data){ data=jQuery.parseJSON(data); // if no favourite has been added, we hide the div that contains the dropdown if(Object.keys(data).length == 0){ $('#' + select_id).parent().closest('div').hide(); // alternatively, we ajax data to the dropdown menu } else { $('#' + select_id).parent().closest('div').show(); $('').appendTo('#' + select_id); $.each(data, function(key, value){ if (key.split(',').length == 1){ var option_data = ''; }else if (key.split(',').length == 2) { var option_data = ''; } $(option_data).appendTo('#'+select_id); }); $('#' + select_id).change(function() { if (stats_type == "top_talkers"){ var host = $(this).find(':selected').val(); if (host == "noaction"){ return; } host = host.split(','); if (host.length == 1){ populateHostTopTalkersTable(host[0]); } else if (host.length == 2){ populateAppsPerHostsPairTable(host[0], host[1]); } } else if (stats_type == "top_applications"){ var value = $(this).find(':selected').val(); var human_readable = $(this).find(':selected').text(); if (value == "noaction"){ return; } value = value.split(','); if (value.length == 1){ // only the l7 protocol id in value[0] var proto = human_readable; $('#historical-apps-container').attr("proto", proto); $('#historical-apps-container').attr("proto_id", value[0]); populateAppTopTalkersTable(value[0]); } else if (value.length == 2){ // both the l7 protocol id and the peer have been selected var proto = human_readable.split(multival_separator)[0]; var addr = human_readable.split(multival_separator)[1]; $('#historical-apps-container').attr("proto", proto); $('#historical-apps-container').attr("proto_id", value[0]); $('#historical-apps-container').attr("host", addr); populatePeersPerHostByApplication(value[1], value[0]); } } // finally, put the dropdown in the default position // after waiting a couple of seconds to give the user the feeling its // choice has had an impact setTimeout(function(){ $('#'+select_id + '>option:eq(0)').prop("selected", true); }, 2000); }); } }, error:function(){ perror('An HTTP error occurred.'); } }); } function removeAllFavourites(stats_type, favourite_type, select_id){ $.ajax({type:'GET',url:"]]print(favourites_url)print[[?action=del_all&stats_type=" + stats_type + "&favourite_type=" + favourite_type, success:function(data){ // remove all the exising options... $('#'+select_id).find('option').remove(); // and hide the container div... $('#' + select_id).parent().closest('div').hide(); // refresh the right breacrumb if (stats_type == "top_talkers"){ if(favourite_type == "talker"){ $('.bc-item-add.talker').show(); $('.bc-item-remove.talker').hide(); } else if (favourite_type == "apps_per_host_pair"){ $('.bc-item-add.host-pair').show(); $('.bc-item-remove.host-pair').hide(); } } else if (stats_type == "top_applications"){ if(favourite_type == "host_peers_by_app"){ $('.bc-app-item-add.host-peers-by-app').show(); $('.bc-app-item-remove.host-peers-by-app').hide(); } else if (favourite_type == "app"){ $('.bc-app-item-add.app').show(); $('.bc-app-item-remove.app').hide(); } } }, error:function(){ perror('An HTTP error occurred.'); } }); } ]] end function historicalDownloadButtonsBar(button_id, pcap_request_data_container_div_id) if not ntop.isPro() then return end -- integrate only in the Pro version print [[