📄 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 + -