package DataCollectorSys_Client.client.process;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Vector;
import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import DataCollectorSys_Client.client.data.LogRecord;
import DataCollectorSys_Client.client.data.MatchedRecord;
import DataCollectorSys_Client.client.handler.CollectionListener;
import DataCollectorSys_Client.client.handler.LogDealer;
public class DataCollector {
private String ip;
private String sourcefile=null;
private String destinationpath=null;
private String command=null;
private String historyfile=null;
private Properties prop=new Properties();
private CollectionListener logdealer;
public void setLogdealer(CollectionListener logdealer)
{
this.logdealer=logdealer;
}
/*
* 加载配置文件
*/
public DataCollector() throws InitException
{
try {
InputStream in = new FileInputStream("D:\\Workspace\\DMS\\src\\DataCollectorSys_Client\\unix_server.properties");
prop.load(in);
} catch (FileNotFoundException e) {
System.out.println("采集属性文件没有设置");
throw new InitException("采集属性文件没有设置");
} catch (IOException e) {
System.out.println("采集属性文件读取错误");
throw new InitException("采集属性文件读取错误");
}
}
private String getNativeIP()
{
return prop.getProperty("ip");
}
/**
* 创建备份文件目录
* @return
* @throws InitException
*/
private String initlog() throws InitException
{
//获取日期时间,并格式化,便于文件备份使用
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmm");
String time = simpleDateFormat.format(date);
//得到采集的数据目录,在unix服务器上是/usr/adm
sourcefile=prop.getProperty("sourcefile");
destinationpath=prop.getProperty("destinationpath");
command=prop.getProperty("command");
historyfile=prop.getProperty("historyfile");
sourcefile=sourcefile==null?"/usr/adm/wtmpx":sourcefile;//采集源路径没有设置,则用usr/adm作为缺省路径。
destinationpath=destinationpath==null?"../":destinationpath;//备份路径没有设置,则备份到当前路径。
command=command==null?"mv":command;
String destfile = "wtmpx" + time;//备份文件名
String cmdstr=command+" " + sourcefile+" "+ destinationpath + destfile;
System.out.println(cmdstr);
//判定源是否存在
if(! new File(sourcefile).exists()){
System.out.println("采集源不存在!");
throw new InitException("采集源不存在!");
}
//判定目标目录是否存在,不存在就创建。
File tmpdestpath=new File(destinationpath);
if(! tmpdestpath.exists()){
tmpdestpath.mkdirs();
}
Runtime r = Runtime.getRuntime();
try {
//内部指令不能单独启动一个进程,必须依赖shell
//请参考DOS的cmd 与 Unix的sh
Process p=r.exec(cmdstr);
p.waitFor();//等待指令执行完毕,否则后面打开文件的时候,报文件不存在异常.
} catch (IOException e) {
e.printStackTrace();
System.out.println("采集源备份错误!");
throw new InitException("采集源备份错误!");
}
catch(InterruptedException e){
System.out.println("采集源备份过程出错!");
throw new InitException("采集源备份过程出错!");
}
return destinationpath+ destfile;
}
public void collect() throws CollectionException
{
ip=getNativeIP();
if(logdealer==null)
{
logdealer=new LogDealer();
}
try {
String filename=initlog();
MappedByteBuffer logbuffer=mappingLogBuffer(filename);
Vector<LogRecord>logins=null;
logins=activate(historyfile);
System.out.println("上次没有匹配的数据数:"+logins.size());
Vector<LogRecord> logouts = new Vector<LogRecord>();
parseLogBuffer(logbuffer,logins,logouts);
System.out.println("登入数:"+logins.size()+"\t登出数:"+logouts.size());
Vector<MatchedRecord> matches = match(logins, logouts);
System.out.println("匹配成功数:"+matches.size());
if(logins.size()>0){
passivate(logins, historyfile);
System.out.println("保存没有匹配的登入数据:" +logins.size());
}
System.out.println("开始处理数据:通过网络发送到服务器!");
logdealer.deal(matches);
} catch (InitException e) {
// TODO Auto-generated catch block
System.out.println("采集数据环境处理错误");
}catch (MatchedException ex) {
// TODO Auto-generated catch block
System.out.println("采集数据匹配处理错误");
}
}
private void parseLogBuffer(MappedByteBuffer logbuffer,Vector<LogRecord> logins,Vector<LogRecord> logouts)
{
System.out.println("parseLogBuffer:记录数:" +logbuffer.capacity()/372);
try {
while(logbuffer.position()<logbuffer.capacity())
{
byte[]bname=new byte[32];
logbuffer.get(bname);
String user=new String(bname).trim();
logbuffer.get(new byte[4]);
logbuffer.get(new byte[32]);
int pid=logbuffer.getInt();
//登录类型 072-073 2 type of entry
short type = logbuffer.getShort();
//进程终止状态码 074-075 2 process termination
logbuffer.get(new byte[2]); // 跳过无用数据
//进程退出状态码 076-077 2 exit status
logbuffer.get(new byte[2]); // 跳过无用数据
// 2 这是C数据类型补齐产生的空位
logbuffer.get(new byte[2]); // 跳过无用数据
//logbuffer.get(new byte[6]);
//登录日期与时间* 080-083 4 time entry was made seconds
// 084-087 4 and microseconds
long second = logbuffer.getInt()*1000L;//秒
second+=logbuffer.getInt();//累加毫秒
//window进程的会话ID 088-091 4 session ID, used for windowing
logbuffer.get(new byte[4]); // 跳过无用数据
//20个保留字节 092-111 20 reserved for future use
logbuffer.get(new byte[20]); // 跳过无用数据
//IP字符的实际长度 112-113 2 significant length of ut_host
logbuffer.get(new byte[2]); // 跳过无用数据
//客户登录的IP 114-371 257 remote host name
byte[] bip = new byte[257];
logbuffer.get(bip);
String userip = new String(bip).trim();
logbuffer.get(new byte[1]); // 总共372字节要都跳过
LogRecord logRecord = null;
if((type == 7 || type == 8)&& !user.startsWith(".")&& !(user.equals("root"))){//屏蔽root用户
logRecord = new LogRecord();
logRecord.setUser(user);
logRecord.setVisittime(second);
logRecord.setUserip(userip);
logRecord.setLabip(ip);
logRecord.setPid(pid);
}else {
continue;
}
// 过滤掉ut_user以"."开头的,ut_type不是7或8的,ut_line以"ftp"开头的
if (type == 7){// && !user.substring(0, 2).equals(".")
//&& !line.substring(0, 4).equals("ftp")) {
logins.add(logRecord);
} else if (type == 8){// && !user.substring(0, 1).equals(".")
//&& !line.substring(0, 4).equals("ftp")) {
logouts.add(logRecord);
}
}
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
logbuffer.clear();
logbuffer=null;
}
}
/*
* 读到vector
*/
private Vector<LogRecord> activate(String loginFilename)
{
ObjectInputStream ois=null;
Vector<LogRecord>logins=new Vector<LogRecord>();
File f=new File(destinationpath,loginFilename);
if(!f.exists() && f.length()<=0)
{
return logins;
}
else
{
try {
ois=new ObjectInputStream(new FileInputStream(f));
try {
logins = (Vector<LogRecord>) ois.readObject();
return logins;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
if(ois!=null)
{
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
DMS.rar_dms_dms java
共87个文件
class:39个
java:38个
properties:8个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 199 浏览量
2022-09-19
15:44:38
上传
评论
收藏 72KB RAR 举报
温馨提示
dms(struts+spring+hibernate)数据管理系统
资源推荐
资源详情
资源评论
收起资源包目录
DMS.rar (87个子文件)
DMS
bin
DataCollectorSys_Client
client
handler
RecieveException.class 440B
CollectionListener.class 292B
LogDealer.class 7KB
NetException.class 514B
SendException.class 431B
LinkException.class 541B
data
LogRecord.class 2KB
MatchedRecord.class 2KB
process
CollectionThread.class 1KB
DataCollector.class 11KB
MatchedException.class 559B
CollectionException.class 520B
InitException.class 553B
CollectionLaunchor.class 2KB
unix_server.properties 391B
DataCollectorSys_Server
server
db
BasicDAO.class 344B
DAOException.class 495B
Detail.class 2KB
DBEnvInit.class 2KB
DetailDAO.class 4KB
process
CollectionTh.class 3KB
DataRecieverTh.class 2KB
db.properties 213B
ServerLauncher.class 961B
DataSimulate
log
InitProperties.class 2KB
LogChecker.class 1KB
LogRec.class 2KB
LogPool.class 1KB
LogCreater.class 3KB
LogWriter.class 4KB
LogSimulaterCheck.class 1KB
LogSimulaterCreate.class 808B
tools
RandomUtil.class 752B
user.properties 1KB
user
UserUtil.class 2KB
UserLoadException.class 471B
simulate
SimulateLauncher.class 751B
simulation.properties 498B
BuildTable
BuildTables$1.class 3KB
BuildTables.class 4KB
runJar.class 673B
Tools.class 3KB
saveParam.class 2KB
src
DataCollectorSys_Client
client
handler
LinkException.java 302B
SendException.java 160B
LogDealer.java 6KB
NetException.java 307B
CollectionListener.java 226B
RecieveException.java 166B
data
MatchedRecord.java 1KB
LogRecord.java 1KB
process
CollectionException.java 306B
DataCollector.java 10KB
CollectionThread.java 661B
MatchedException.java 318B
InitException.java 313B
CollectionLaunchor.java 911B
unix_server.properties 391B
DataCollectorSys_Server
server
db
DBEnvInit.java 2KB
DetailDAO.java 3KB
DAOException.java 269B
Detail.java 1KB
BasicDAO.java 440B
process
CollectionTh.java 2KB
DataRecieverTh.java 1KB
db.properties 213B
ServerLauncher.java 578B
DataSimulate
log
LogWriter.java 3KB
LogCreater.java 2KB
LogChecker.java 606B
LogPool.java 712B
LogSimulaterCheck.java 599B
LogSimulaterCreate.java 350B
LogRec.java 978B
InitProperties.java 2KB
tools
RandomUtil.java 628B
user.properties 1KB
user
UserLoadException.java 283B
UserUtil.java 2KB
simulate
SimulateLauncher.java 496B
simulation.properties 498B
BuildTable
Tools.java 3KB
saveParam.java 920B
BuildTables.java 4KB
runJar.java 275B
.project 379B
.classpath 347B
共 87 条
- 1
资源评论
JaniceLu
- 粉丝: 78
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功