?? uas5000syslogreceiver.java
字號:
/*
* 文件:UAS5000SyslogReceiver.java.java
* 功能:SYSLOG消息處理類
* 作者:茹海燕
* 時間:2003年06月20日
* 項目:ZXNM01數據網絡管理
* 版權:深圳市中興通訊股份有限公司網絡事業部
*/
package com.zte.e10.ems.uas.zteuas.config;
import java.net.*;
import java.util.*;
import java.io.*;
import com.adventnet.nms.topodb.SnmpNode;
import com.adventnet.nms.util.*;
import com.adventnet.snmp.beans.*;
import com.adventnet.snmp.mibs.MibOperations;
import com.adventnet.snmp.snmp2.*;
import com.zte.e10.ems.general.PubTools;
import com.zte.e10.ems.mibdata.zteuas.*;
import com.zte.e10.ems.uas.UASConst;
import com.zte.e10.ems.uas.zteuas.discovery.discoveryconst;
/**
* UDP數據包監聽,接收之后,構造呈SYSLOG信息,寫入數據庫
*/
public class UAS5000SyslogReceiver extends Uas5000ConfigBase implements java.lang.Runnable
{
private static String debugTitle = "<Syslog.UAS5000SyslogReceiver>";
private SyslogInfo syslog = null;
public static void main(String[] args)
{
UAS5000SyslogReceiver dc = new UAS5000SyslogReceiver();
dc.listen();
}
private void listen()
{
InetAddress addr = null;
DatagramSocket ds = null;
try
{
ds = new DatagramSocket(SyslogInfo.SYSLOG_PORT);
}
catch(Exception ex)
{
PubTools.DebugLog(debugTitle, "construct a new DatagramSocket error. " + ex);
ex.printStackTrace();
return;
}
byte[] buffer = new byte[SyslogInfo.UDP_DATA_LENGTH];
DatagramPacket dp = new DatagramPacket(buffer,SyslogInfo.UDP_DATA_LENGTH);
String data = "";
while(true)
{
try
{
ds.receive(dp);
data = new String(dp.getData());
String ip = dp.getAddress().getHostAddress();
String moname = discoveryconst.NENAME_PREFIX + ip;
PubTools.DebugLog(debugTitle, "receive data : " + data);
if (data.length() != 0)
{
syslog = createSysLogInfo(data);
if (syslog == null)
{
PubTools.DebugLog(debugTitle, "createSysLogInfo failed! the info is " + data );
break;
}
else if (!super.db.addSysLogInfotoDb(moname,syslog))
{
PubTools.DebugLog(debugTitle,
"addSysLogInfotoDb failed! the syslog is " + syslog.toString() );
return;
}
}
}
catch(Exception ex)
{
PubTools.DebugLog(debugTitle, "receive a new datagram packet error. " + ex);
ex.printStackTrace();
return;
}
}
}
/**
* 構造SyslogInfo類
* @see com.zte.e10.ems.mibdata.zteuas.SyslogInfo
*/
private SyslogInfo createSysLogInfo(String info)
{
int index1 = info.indexOf("<");
int index2 = info.indexOf(">");
if ((index1 < 0) || (index2 < 0) || (index1 >= index2))
{
PubTools.DebugLog(debugTitle,
"createSysLogInfo failed! the info error: " + info);
return null;
}
String temp = info.substring(index1+1, index2);
SyslogInfo log = new SyslogInfo();
try
{
int number = Integer.parseInt(temp);
if (number<0)
{
PubTools.DebugLog(debugTitle,
"createSysLogInfo failed! the number error: " + number);
return null;
}
log.time = new Date().getTime();
log.moduleNo = number/SyslogInfo.DIVISOR;
log.severityLevel = number%SyslogInfo.DIVISOR;
log.information = info.substring(index2+1).trim();
//PubTools.DebugLog(debugTitle, "SysLogInfo: " + log.toString());
}
catch(Exception ex)
{
PubTools.DebugLog(debugTitle, "createSysLogInfo() failed. " + ex);
ex.printStackTrace();
return null;
}
return log;
}
/**
* 線程入口,啟動一個線程監聽。
*/
public void run()
{
UAS5000SyslogReceiver dc = new UAS5000SyslogReceiver();
dc.listen();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -