/*
* MainNetFlowPanel.java
*
* Created on 19 November 2005, 16:34
*
* AsItHappens - real-time network monitor
* Copyright (C) 2006 Mark Abrahams
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
package nz.co.abrahams.asithappens.netflow;
import nz.co.abrahams.asithappens.core.DataType;
import nz.co.abrahams.asithappens.storage.DataSets;
import nz.co.abrahams.asithappens.storage.Device;
import nz.co.abrahams.asithappens.flow.FlowOptions;
import nz.co.abrahams.asithappens.snmputil.SNMPException;
import nz.co.abrahams.asithappens.core.DBException;
import nz.co.abrahams.asithappens.core.Configuration;
import nz.co.abrahams.asithappens.cartgraph.TimeSeriesContext;
import nz.co.abrahams.asithappens.cartgraph.DataGraph;
import nz.co.abrahams.asithappens.uiutil.ErrorHandler;
import nz.co.abrahams.asithappens.uiutil.PortsSelectorPanel;
import javax.swing.*;
import java.net.UnknownHostException;
import org.apache.log4j.Logger;
/**
* The graphical pane for creating NetFlow graphs. NetFlow is a Cisco
* technology for storing information about network traffic traversing a network
* device.
*
* @author mark
*/
public class MainNetFlowPanel extends javax.swing.JPanel {
/** Direction */
public static final String[] DIRECTIONS = { "In", "Out", "Both" };
/** Logging provider */
private static Logger logger = Logger.getLogger(MainNetFlowPanel.class);
/** Ports selector interface */
private PortsSelectorPanel portsSelectorPanel;
/** Criteria for flows to include for collection */
private NetFlowMatchCriteria criteria;
/** Creates new form MainNetflowPanel */
public MainNetFlowPanel() {
criteria = new NetFlowMatchCriteria();
initComponents();
initComponentsFinish();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
pollLabel = new javax.swing.JLabel();
pollField = new javax.swing.JTextField();
pollUnitsLabel = new javax.swing.JLabel();
directionLabel = new javax.swing.JLabel();
directionCombo = new JComboBox(MainNetFlowPanel.DIRECTIONS);
categorizeLabel = new javax.swing.JLabel();
ipProtocolCheckBox = new javax.swing.JCheckBox();
sourceAddressCheckBox = new javax.swing.JCheckBox();
destinationAddressCheckBox = new javax.swing.JCheckBox();
tosCheckBox = new javax.swing.JCheckBox();
sourcePortCheckBox = new javax.swing.JCheckBox();
destinationPortCheckBox = new javax.swing.JCheckBox();
storeDataCheckBox = new javax.swing.JCheckBox();
netflowButton = new javax.swing.JButton();
setMatchCriteriaButton = new javax.swing.JButton();
setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
pollLabel.setText("Poll Interval");
add(pollLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 10, 80, 20));
pollField.setText("2");
add(pollField, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 10, 60, -1));
pollUnitsLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
pollUnitsLabel.setText("s");
add(pollUnitsLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(610, 10, 20, 20));
directionLabel.setText("Direction");
add(directionLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 40, 90, 24));
add(directionCombo, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 40, 80, -1));
categorizeLabel.setText("Categorize by");
add(categorizeLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 80, -1, -1));
ipProtocolCheckBox.setSelected(true);
ipProtocolCheckBox.setText("IP protocol");
ipProtocolCheckBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ipProtocolCheckBoxActionPerformed(evt);
}
});
add(ipProtocolCheckBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 100, -1, -1));
sourceAddressCheckBox.setSelected(true);
sourceAddressCheckBox.setText("Source IP address");
add(sourceAddressCheckBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 120, -1, -1));
destinationAddressCheckBox.setSelected(true);
destinationAddressCheckBox.setText("Destination IP address");
add(destinationAddressCheckBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 140, -1, -1));
tosCheckBox.setSelected(true);
tosCheckBox.setText("TOS byte");
add(tosCheckBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 160, -1, -1));
sourcePortCheckBox.setSelected(true);
sourcePortCheckBox.setText("Source TCP/UDP port");
add(sourcePortCheckBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 190, -1, -1));
destinationPortCheckBox.setSelected(true);
destinationPortCheckBox.setText("Destination TCP/UDP port");
add(destinationPortCheckBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 210, -1, -1));
storeDataCheckBox.setText("Store collected data");
add(storeDataCheckBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 290, 180, -1));
netflowButton.setText("Netflow Graph");
netflowButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
netflowButtonActionPerformed(evt);
}
});
add(netflowButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 330, 620, 30));
setMatchCriteriaButton.setText("Set Match Criteria");
setMatchCriteriaButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
setMatchCriteriaButtonActionPerformed(evt);
}
});
add(setMatchCriteriaButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 250, 190, 20));
}// </editor-fold>//GEN-END:initComponents
public void initComponentsFinish() {
portsSelectorPanel = new PortsSelectorPanel(true, ListSelectionModel.SINGLE_SELECTION);
add(portsSelectorPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));
}
/**
* Enables/disables the options based on IP protocol when the "IP protocol"
* checkbox is selected/deselected.
*/
private void ipProtocolCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ipProtocolCheckBoxActionPerformed
if ( ipProtocolCheckBox.isSelected() ) {
sourcePortCheckBox.setEnabled(true);
destinationPortCheckBox.setEnabled(true);
} else {
sourcePortCheckBox.setSelected(false);
sourcePortCheckBox.setEnabled(false);
destinationPortCheckBox.setSelected(false);
destinationPortCheckBox.setEnabled(false);
}
}//GEN-LAST:event_ipProtocolCheckBoxActionPerformed
/** Opens the NetFlow match criteria dialog. */
private void setMatchCriteriaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setMatchCriteriaButtonActionPerformed
NetFlowMatchDialog dialog;
dialog = new NetFlowMatchDialog(criteria);
dialog.setVisible(true);
}//GEN-LAST:event_setMatchCriteriaButtonActionPerformed
/** Creates a new NetFlow graph. */
private void netflowButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_netflowButtonActionPerformed
Device device;
int ifIndex;
String portString;
FlowOptions options;
NetFlowSNMP snmp;
NetFlowCollector collector;
DataSets data;
TimeSeriesContext context;
DataGraph graphFrame;
if ( ! portsSelectorPanel.getModel().hasEnumerated() ) {
ErrorHandler.modalError(this, "Please enumerate ports on a device and select a port",
"No port selected");
return;
} else if ( portsSelectorPanel.rowsSelected().length == 0 ) {
ErrorHandler.modalError(this, "Please select a port", "No port selected");
return;
}
device = portsSelectorPanel.getModel().getDevice();
try {
snmp = new NetFlowSNMP(device);
if ( snmp.getNetFlowTopFlowsTopNTable() > 0 ) {
logger.debug("NetFlow Top-N table already exists");
if ( JOptionPane.showConfirmDialog(this, "A Netflow Top-N table already exists - overwrite this?",
"Confirm NetFlow Top-N table creation", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.CANCEL_OPTION)
return;
}
/*
ifIndex = device.getPortsIndex()[((JList)(portsPane.getViewport().getView())).getSelectedIndex()];
portString = (String)((JList)portsPane.getViewport().getView()).getSelectedValue();
*/
ifIndex = portsSelectorPanel.getIfIndex();
portString = portsSelectorPanel.getIfDescription();
options = new FlowOptions(ipProtocolCheckBox.isSelected(), sourceAddressCheckBox.isSelected(),
destinationAddressCheckBox.isSelected(), tosCheckBox.isSelected(), sourcePortCheckBox.isSelected(), destinationPortCheckBox.isSelected());
collector = new NetFlowCollector(snmp, ifIndex, portString, directionCombo.getSelectedIndex() + 1, Configuration.getPropertyInt("collector.netflow.table.size"), Integer.parseInt(pollField.getText()) * 1000, criteria, options);
data = new DataSets(DataType.NETFLOW, collector, device, Integer.parseInt(pollField.getText()) * 1000, portString, directionCombo.getSelectedIndex() + 1, null, storeDataCheckBox.isSelected());
//data = new DataSets(3, device, Integer.parseInt(pollField.getText()) * 1000, ifIndex, portString, directionCombo.getSelectedIndex() + 1, "NetFlow Graph", storeDataCheckBox.isSelected());
context = new TimeSeriesContext(data);
graphFrame = new DataGraph(context);
} catch (DBException e) {
ErrorHandler.modalError(this, "Please ensure that database is running and accessible",
"Error opening database connection", e);
} catch (UnknownHostException e) {
ErrorHandler.modalError(this, "Please ensure that device name \"" + device.getName() + "\" is valid",
"Unknown host " + device.getName(), e);
} catch (SNMPException e) {
ErrorHandler.modalError(this, "Please ensure that device name and community string are correct",
"Cannot access SNMP service on device " + device.getName(), e);
} catch (ClassCastException e) {
ErrorHandler.modalError(this, "Please ensure that device supports Netflow MIB Top-N feature",
"Cannot find Netflow Top-N information on device " + device.getName(), e);
}
}//GEN-LAST:event_netflowButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel categorizeLabel;
private javax.swing.JCheckBox destinationAddressCheckBox;
private javax.swing.JCheckBox destinationPortCheckBox;
private javax.swing.JComboBox directionCombo;
private javax.swing.JLabel directionLabel;
private javax.swing.JCheckBox ipProtocolCheckBox;
private javax.swing.JButton netflowButton;
private javax.swing.JTextField pollField;
private javax.swing.JLabel pollLabel;
private javax.swing.JLabel pollUnitsLabel;
private javax.swing.JButton setMatchCriteriaButton;
private javax.swing.JCheckBox sourceAddressCheckBox;
private javax.swing.JCheckBox sourcePortCheckBox;
private javax.swing.JCheckBox storeDataCheckBox;
private javax.swing.JCheckBox tosCheckBox;
// End of variables declaration//GEN-END:variables
}