/* $RCSfile: DHCP_Audit.java $ $Revision: 1.0 $ $Rohit: $ $Date: 2007/05/3 11:30:44 $ $ProjectName: DHCP Auditing / $ $ProjectRevision: 1.0 $ $ Created By : Rohit Jaini */ import java.util.Vector; import java.util.StringTokenizer; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.GridLayout; import java.awt.Dimension; import java.awt.Container; import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import java.awt.Insets; import java.awt.FileDialog; import javax.swing.table.DefaultTableModel; import javax.swing.border.BevelBorder; import javax.swing.JTable; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.UIManager; public class DHCP_Audit { public static void main(String[] args) { if(args.length!=2) { printUsage(); return; } try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception exception){ System.err.println("UI Exception" + exception); } innerClass GUI=new innerClass(args[1]); } static void printUsage() { System.out.println("Error: Insufficient parameters"); System.out.println("Usage: "); System.out.println("java DHCP_Audit -f \"/var/lib/dhcp/db/FileName\" "); } } class innerClass extends JFrame implements ActionListener { static String save="Save As.."; static String refresh="Refresh"; static String about="About"; static String filter="Filter"; static String clear_filter="Clear Filter"; static String close="Close"; static String ok="OK"; static String cancel="Cancel"; static String start_date="Start Date"; static String ipAddress="IP Address"; static String hardware="Hardware Address"; static String host_name="Host Name"; static String end_date="End Date"; static String clientID="Client ID"; static String state="Binding State"; static String audit_log="Audit Log"; static String hash="#"; static String LEASE_STR="lease"; static String STARTS_STR="starts"; static String ENDS_STR="ends"; static String HARDWARE_STR="hardware"; static String CLIENTHOSTNAME_STR="client-hostname"; static String BINDING_STR="binding"; static String UID_STR="uid"; static String FAILOVER_STR="failover"; static String ON_STR="on"; static String CLOSECURLY_STR="}"; static String IPADDRESS_STR="ip-Address"; static String NEVER_STR="never"; static String NEXT_STR="next"; static String START_DATE="0"; static String IP_ADDRESS="1"; static String HARDWARE="2"; static String HOST="3"; static String END_DATE="4"; static String STATE="5"; JFrame frame; JButton Button_Save,Button_Refresh,Button_About,Button_Filter,Button_OK,Button_Cancel; JButton Button_Close,Button_Clear; JPanel panel,filter_panel; JTable table; JDialog dialog; JComboBox binding_Box,start_day_Box,start_year_Box,start_month_Box; JComboBox end_day_Box,end_year_Box,end_month_Box; JScrollPane scroll_panel; String leaseFileName; Vector auditLogVec=new Vector(); Vector column_name; DefaultTableModel model; protected String start_Filter_Date=null; protected String end_Filter_Date=null; protected String state_Filter=null; public innerClass(String fileName) { leaseFileName=fileName; frame = new JFrame(audit_log); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel= new JPanel(); panel.setLayout(new GridLayout(1, 6, 4, 4)); Button_Save=new JButton(save); Button_Refresh=new JButton(refresh); Button_About=new JButton(about); Button_Filter=new JButton(filter); Button_Clear=new JButton(clear_filter); Button_Close=new JButton(close); panel.add(Button_Save); panel.add(Button_Filter); panel.add(Button_Refresh); panel.add(Button_About); panel.add(Button_Clear); panel.add(Button_Close); Button_Save.addActionListener(this); Button_Filter.addActionListener(this); Button_Refresh.addActionListener(this); Button_About.addActionListener(this); Button_Clear.addActionListener(this); Button_Close.addActionListener(this); column_name=new Vector(); column_name.add(start_date); column_name.add(ipAddress); column_name.add(hardware); column_name.add(host_name); column_name.add(end_date); column_name.add(state); model = new DefaultTableModel(column_name, 0); table = new JTable( model ) { public boolean isCellEditable(int row, int column) { return false; } }; table.setPreferredScrollableViewportSize(new Dimension(1000, 504)); scroll_panel = new JScrollPane(table); scroll_panel.setBorder(new BevelBorder(BevelBorder.LOWERED)); scroll_panel.setPreferredSize(new Dimension(1000,504)); Container container=frame.getContentPane(); container.setLayout(new GridBagLayout()); GridBagConstraints gridBag = new GridBagConstraints(); gridBag.gridx = 0; gridBag.gridy = 0; gridBag.gridwidth = 1; gridBag.gridheight = 1; gridBag.fill = GridBagConstraints.NONE; gridBag.anchor = GridBagConstraints.NORTH; gridBag.weightx = 4.0; gridBag.weighty = 1.0; gridBag.insets = new Insets(5,5,5,5); ((GridBagLayout) container.getLayout()).setConstraints(scroll_panel, gridBag); container.add(scroll_panel); gridBag.gridx = 0; gridBag.gridy = 1; gridBag.gridwidth = 1; gridBag.gridheight = 1; gridBag.fill = GridBagConstraints.NONE; gridBag.anchor = GridBagConstraints.SOUTH; gridBag.weightx = 1.0; gridBag.weighty = 1.0; ((GridBagLayout) container.getLayout()).setConstraints(panel, gridBag); container.add(panel); Button_OK=new JButton(ok); Button_Cancel=new JButton(cancel); dialog=new JDialog(frame,"Filter Page",true); dialog.getContentPane().setLayout(new GridBagLayout()); Vector vec_day=new Vector(); Vector vec_month=new Vector(); Vector vec_year=new Vector(); for(int value=1;value<10;value++) { vec_day.add("0"+Integer.toString(value)); vec_month.add("0"+Integer.toString(value)); } for(int value=10;value<32;value++) vec_day.add(Integer.toString(value)); for(int value=10;value<13;value++) vec_month.add(Integer.toString(value)); for(int value=1981;value<2020;value++) vec_year.add(Integer.toString(value)); start_year_Box=new JComboBox(vec_year); start_month_Box=new JComboBox(vec_month); start_day_Box=new JComboBox(vec_day); end_year_Box=new JComboBox(vec_year); end_month_Box=new JComboBox(vec_month); end_day_Box=new JComboBox(vec_day); filter_panel= new JPanel(); filter_panel.setLayout(new GridLayout(7, 7)); filter_panel.add(new JLabel(start_date)); filter_panel.add(start_day_Box); filter_panel.add(new JLabel("Day")); filter_panel.add(start_month_Box); filter_panel.add(new JLabel("Month")); filter_panel.add(start_year_Box); filter_panel.add(new JLabel("Year")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel(end_date)); filter_panel.add(end_day_Box); filter_panel.add(new JLabel("Day")); filter_panel.add(end_month_Box); filter_panel.add(new JLabel("Month")); filter_panel.add(end_year_Box); filter_panel.add(new JLabel("Year")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel(state)); Vector combo_Values=new Vector(); combo_Values.add("active"); combo_Values.add("free"); combo_Values.add("both"); binding_Box=new JComboBox(combo_Values); filter_panel.add(binding_Box); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(new JLabel("")); filter_panel.add(Button_OK); filter_panel.add(new JLabel("")); filter_panel.add(Button_Cancel); Button_OK.addActionListener(this); Button_Cancel.addActionListener(this); dialog.getContentPane().add(filter_panel); dialog.setSize(580,220); dialog.setLocationRelativeTo(frame); click_Refresh(); frame.pack(); frame.setVisible(true); } public void actionPerformed(ActionEvent action_event) { if(action_event.getActionCommand().equalsIgnoreCase(save)) click_Save(); else if(action_event.getActionCommand().equalsIgnoreCase(filter)) click_Filter(); else if(action_event.getActionCommand().equalsIgnoreCase(refresh)) click_Refresh(); else if(action_event.getActionCommand().equalsIgnoreCase(about)) click_About(); else if(action_event.getActionCommand().equalsIgnoreCase(clear_filter)) { end_Filter_Date=null; start_Filter_Date=null; state_Filter=null; updateGUI(); } else if(action_event.getActionCommand().equalsIgnoreCase(ok)) { start_Filter_Date=(String)start_year_Box.getSelectedItem()+"/"+start_month_Box.getSelectedItem()+"/"+start_day_Box.getSelectedItem()+" 01:01:01"; end_Filter_Date=(String)end_year_Box.getSelectedItem()+"/"+end_month_Box.getSelectedItem()+"/"+end_day_Box.getSelectedItem()+" 01:01:01"; state_Filter=(String)binding_Box.getSelectedItem(); if(state_Filter.equalsIgnoreCase("both")) state_Filter=null; if(start_Filter_Date.compareTo(end_Filter_Date)>0) JOptionPane.showMessageDialog(frame,"Start date is greater than End date"); else { dialog.setVisible(false); updateGUI(); } } else if(action_event.getActionCommand().equalsIgnoreCase(cancel)) dialog.setVisible(false); else if(action_event.getActionCommand().equalsIgnoreCase(close)) System.exit(0); else System.out.println(action_event.getActionCommand()); } protected void click_About() { String information="The tool is used to get the information of leased database maintain by the DHCP server"; JOptionPane.showMessageDialog(frame,information); } protected void click_Save() { String fileName; FileDialog fileDialog; PrintWriter file; String columnHdr; int index,columnCount,rowCount; fileDialog= new FileDialog(this); fileDialog.setMode(FileDialog.SAVE); fileDialog.show(); if (fileDialog.getFile() == null) return; else fileName = fileDialog.getDirectory() + fileDialog.getFile(); try{ file = new PrintWriter(new BufferedWriter(new FileWriter(fileName))); } catch(IOException ioException){ System.out.println("Not sufficient rights to over write the file."); return; } file.print("\t\t\t" + audit_log); file.println(); file.println(); columnCount=table.getColumnCount(); if (columnCount>0) file.print(table.getColumnName(0)+"\t"); for (index=1; index< columnCount; index++ ) { columnHdr= table.getColumnName(index); file.print("\t" + columnHdr); } file.println(); rowCount=table.getRowCount(); for (int row=0; row< rowCount; row++ ) { file.print((String)table.getValueAt(row,0)); for(int column=1;column not found "); } catch(Exception exception) { System.out.println("Not sufficient rights to read <"+leaseFileName+"> file "); } updateGUI(); } public void updateGUI() { while(model.getRowCount()!=0) model.removeRow(0); int index,row=-1,length=auditLogVec.size(); Vector currentVec=new Vector(); for(index=0;index0)) continue; } if(end_Filter_Date!=null) { int first_index=currentVec.indexOf(END_DATE); if((first_index!=-1)&&(end_Filter_Date.compareTo((String)currentVec.elementAt(first_index+1))<0)) continue; } if(state_Filter!=null) { int first_index=currentVec.indexOf(STATE); if((first_index!=-1)&&(state_Filter.compareTo((String)currentVec.elementAt(first_index+1))!=0)) continue; } model.addRow(new Vector()); row++; for(inner_index=0;inner_index