1
/* NTOP JDBC interface for mySQL
3
* Command line invocation requires a text file parameter to store SQL commands into for debug:
5
* java MySQLServer [ntopSQL.txt]
7
* The SQL capture file write may be commented out.
8
* SQL time stamps are generated for the SQL capture file only.
10
* This routine checks for and corrects the invalid "DELETE *" command generated by NTOP
11
* It does NOT generate the missing "FIRSTSEEN" values.
12
* Both of the delete and firstseen problems should be fixed in the NTOP C-code.
14
* Author Dave Moore <davem@mitre.org>
15
* (Based upon Java ODBC sample for MS Access) (Don't forget to set you userID & password.)
23
import java.lang.System;
25
public class MySQLServer {
26
private Connection connection;
28
private boolean checkForWarning (SQLWarning warn) {
31
// If an SQLWarning object was given, display the
32
// warning messages. Note that there could be
33
// multiple warnings chained together
36
System.out.println ("\n *** Warning ***\n");
38
while (warn != null) {
39
System.out.println ("SQLState: " +
41
System.out.println ("Message: " +
43
System.out.println ("");
44
warn = warn.getNextWarning ();
48
} catch(Exception e) {
53
public MySQLServer(String dbName) {
55
Class.forName("org.gjt.mm.mysql.Driver");
56
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ntop?user=myid&password=mypass");
57
checkForWarning(connection.getWarnings());
58
//connection.setAutoCommit(true); // Automatic transaction commitment
59
connection.setAutoCommit(true); // Automatic transaction commitment
60
} catch (Exception e) {
67
public void executeStatement(String statement) {
68
PreparedStatement stmt;
72
stmt = connection.prepareStatement(statement); // Insert record in the HostInfo table
73
rc = stmt.executeUpdate();
74
stmt.close(); // Close the statement
75
} catch(Exception ex) {
80
private String replace_substring(String original, String delimiter, String replacement) {
81
// Given an "original" input string, find all values of the "delimiter" string
82
// and replace them with the "replacement" string.
84
int delimiter_length = delimiter.length();
85
int replacement_length = replacement.length();
88
if (original != null) {
89
while (index_2 >= 0) {
90
index_2 = original.indexOf(delimiter, index_1); // next delimiter position from previous
92
if (index_2 == index_1) { //very next character ?
93
result = result + replacement;
96
result = result + original.substring(index_1, index_2) + replacement;
97
//log("found " + delimiter + " replacing with " + replacement + ".");
99
index_1 = index_2 + delimiter_length; //skip over delimiter.
102
result = result + original.substring(index_1, original.length()); //pick up tail end.
108
// only argument is a text file to echo SQL commands to for debug.
109
public static void main( String args[] ) throws IOException
112
int bufLength = 1514;
114
MySQLServer jdbcServer;
115
DatagramSocket socket = null;
118
// SimpleDateFormat DateFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss:SSS zzz");
119
SimpleDateFormat DateFormatter = new SimpleDateFormat("EEE, HH:mm:ss.SSS");
120
java.util.Date currentTime_1;
122
System.out.println("Starting up...");
125
socket = new DatagramSocket(4000);
126
} catch (Exception e) {
131
if (args.length != 1)
133
System.out.println("SQL capture file name parameter is required");
137
// debug SQL commands out to listout
138
PrintWriter listout = new PrintWriter(new FileWriter(args[0]), true); //autoflush set to true
140
buffer = new byte[bufLength];
141
pkt = new DatagramPacket(buffer, buffer.length);
143
// comment out to stop database access
144
jdbcServer = new MySQLServer("NTOP");
149
String statement = new String(buffer, 0, pkt.getLength());
151
//Fix SQL syntax(1): Change "DELETE *" to just "DELETE".
152
statement = jdbcServer.replace_substring(statement, "DELETE *", "DELETE");
154
currentTime_1 = new java.util.Date();
155
String dateString = DateFormatter.format(currentTime_1);
156
cmndline = "["+pkt.getLength()+"] '"+statement+"'";
158
// comment out to eliminate debug dump file
159
listout.println(dateString + " : " + cmndline);
161
// comment out to block database access
162
jdbcServer.executeStatement(statement);
164
pkt.setLength(bufLength);
165
} catch (Exception e) {
172
System.out.println("Done.");