📄 netstatlogmain.java
字号:
package com.sxit.wap.sncs;
import java.util.*;
import java.sql.*;
import com.sxit.wap.netstat.*;
import com.sxit.wap.netstatlog.*;
public class NetstatLogMain extends Thread {
private Hashtable hashtable = null;
public NetstatLogMain () {
}
public synchronized void logByIp(String ip,boolean isConnected){
if ( isConnected ) {
java.util.Date temp = null;
if ( ( temp = ( java.util.Date ) hashtable.get ( ip ) ) != null ) {
NetstatLogModel netstatLogModel = new NetstatLogModel ();
netstatLogModel.setLogIp ( ip );
netstatLogModel.setLogStarttime ( new java.sql.Timestamp ( temp.getTime () ) );
netstatLogModel.setLogEndtime ( new java.sql.Timestamp ( System.currentTimeMillis () ) );
netstatLogModel.setLogMemo ( "" );
try {
netstatLogModel.setId ( NetstatLogBean.getSequenceNextValue());
netstatLogModel = NetstatLogDao.insert ( netstatLogModel );
} catch ( Exception e1 ) {
}
}
} else {
if ( hashtable.get ( ip ) == null )
hashtable.put ( ip, new java.util.Date () );
}
}
public java.util.Date getNextTime(java.util.Date date){
java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.setTime(date);
calendar.add(java.util.Calendar.MINUTE,15);
return calendar.getTime();
}
public static Connection getConnection () {
Connection conn = null;
try {
//Class.forName ( "oracle.jdbc.driver.OracleDriver" );
//conn = DriverManager.getConnection ( "jdbc:oracle:thin:@192.168.8.3:1521:ora8", "sxitwap", "sxitwap" );
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/wap?useUnicode=true&characterEncoding=gb2312", "root", "");
} catch ( Exception ex ) {
System.out.println ( ex.getMessage () );
}
return conn;
}
public void check () {
boolean isOk = false;
/**循环检测每台被监控的机器是否处于连通状态*/
try {
java.util.Iterator iterator = NetstatDao.queryAll ().iterator();
ProcessCommand prcCommand = new ProcessCommand ();
while (iterator.hasNext() ){
NetstatModel netstatModel = ( NetstatModel ) iterator.next();
isOk = prcCommand.isPingOk(netstatModel.getMachIp());
logByIp(netstatModel.getMachIp(), isOk) ;
if (!isOk)
;//短信通知
}
} catch ( Exception e1 ) {
}
}
public void run () {
while (true) {
try {
//check();
this.sleep ( 1000 * 60 * 10 );//10 分钟
} catch ( Exception e1 ) {
}
}
}
public static void main ( String[] args ) {
NetstatLogMain thread = new NetstatLogMain ();
thread.run();
}
protected static String getInsertSQL(NetstatLogModel model) {
String sql = "";
//if (Database.dbType == DBType.ORACLE) {
sql = "INSERT INTO WAP_NETSTAT_LOG (ID, LOG_IP, LOG_STARTTIME, LOG_ENDTIME, LOG_MEMO) VALUES ("
+ model.getId() + ", '"
+ model.getLogIp() + "', TO_DATE('"
+ model.getLogStarttime() + "', 'YYYY/MM/DD HH24:MI:SS'), TO_DATE('"
+ model.getLogEndtime() + "', 'YYYY/MM/DD HH24:MI:SS'), '"
+ model.getLogMemo() + "')";
//}
/*else if (Database.dbType == DBType.MYSQL) {
sql = "INSERT INTO WAP_NETSTAT_LOG (ID, LOG_IP, LOG_STARTTIME, LOG_ENDTIME, LOG_MEMO) VALUES ("
+ model.getId() + ", '"
+ Function.writeDBEncode(model.getLogIp()) + "', '"
+ Function.DateToString(model.getLogStarttime()) + "', '"
+ Function.DateToString(model.getLogEndtime()) + "', '"
+ Function.writeDBEncode(model.getLogMemo()) + "')";
}*/
return sql;
}
public static String tableName = "WAP_NETSTAT_LOG";
public static void insert ( NetstatLogModel model ) {
String sql = getInsertSQL ( model );
Connection conn = null;
Statement stmt = null;
try {
conn = getConnection ();
stmt = conn.createStatement ();
int resultCount = stmt.executeUpdate ( sql );
if ( resultCount != 1 ) {
throw new SQLException ( "ERROR in INSERT !! resultCount = " + resultCount );
}
} catch ( SQLException e ) {
e.printStackTrace ();
} finally {
try {
stmt.close ();
} catch ( Exception e1 ) {
}
try {
conn.close ();
} catch ( Exception e1 ) {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -