package tarena.dms.client.read;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import tarena.dms.client.data.LogRecord;
import tarena.dms.client.data.MatchRecord;
import tarena.dms.client.interfaces.Match;
import tarena.dms.client.interfaces.Read;
public class CollectionRecord implements Read, Match {
private FileInputStream fis = null;
private List<MatchRecord> conainer = null;
private static Properties p = null;
static {
InputStream is = null;
p = new Properties();
try {
is = new FileInputStream("unix_server.properties");
p.load(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
}
}
}
public CollectionRecord() {
try {
String sourceFile = p.getProperty("tarena.com.client.source_file");
String backFile = p.getProperty("tarena.com.client.back_file");
String readFile = move(sourceFile, backFile);
fis = new FileInputStream(readFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new InitException("原始采集数据文件找不到");
}
}
public List<MatchRecord> read() {
List<LogRecord> logins = new ArrayList<LogRecord>();
List<LogRecord> logouts = new ArrayList<LogRecord>();
LogRecord logRecord = new LogRecord();
MappedByteBuffer dataBuffer = mappingBuffer();
System.out.println("dataBuffer:记录数:" +dataBuffer.capacity()/372);
while (dataBuffer.position() < dataBuffer.capacity()) {
byte[] uname = new byte[32];
dataBuffer.get(uname);
String username = new String(uname).trim();
dataBuffer.get(new byte[4]);
//036-067 32 device name (console, lnxx)
dataBuffer.get(new byte[32]);
//068-071 4 process id
int pid = dataBuffer.getInt();
short type = dataBuffer.getShort();
//074-075 2 process termination
dataBuffer.get(new byte[2]);
//076-077 2 exit status
dataBuffer.get(new byte[2]);
dataBuffer.get(new byte[2]);
// 080-083 4 time entry was made seconds
// 084-087 4 and microseconds
long second = dataBuffer.getInt()*1000L;//
second+=dataBuffer.getInt();//
// 088-091 4 session ID, used for windowing
dataBuffer.get(new byte[4]);
//092-111 20 reserved for future use
dataBuffer.get(new byte[20]);
//IP 112-113 2 significant length of ut_host
dataBuffer.get(new byte[2]);
//IP 114-371 257 remote host name
//logbuffer.get(new byte[26]);
byte[] bip = new byte[257];
dataBuffer.get(bip);
String userip = new String(bip).trim();
dataBuffer.get(new byte[1]);
if ((type == 7 || type == 8)&&!"".equals(username) || !".".equals(username)||!"root".equals(username)) {
logRecord = new LogRecord();
logRecord.setUname(username);
logRecord.setLogIp(userip);
logRecord.setLogTime(second);
logRecord.setType(type);
logRecord.setPid(pid);
} else {
continue;
}
if (type == 7){
logins.add(logRecord);
} else if (type == 8){
logouts.add(logRecord);
}
}
readSaveFile(logins);
System.out.println("logins : " + logins.size());
System.out.println("logouts : " + logouts.size());
return match(logins, logouts);
}
private MappedByteBuffer mappingBuffer() {
MappedByteBuffer mappedBuffer = null;
FileChannel fc = fis.getChannel();
try {
mappedBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fis
.available());
} catch (IOException e) {
e.printStackTrace();
throw new MappingMemoryExcepton("原始文件内容映射到内存中出现问题");
}
return mappedBuffer;
}
private String move(String sourceFile, String destinationFile) {
File dest = new File(destinationFile);
if (!dest.exists()) {
dest.mkdirs();
}
String filename = sourceFile.substring(sourceFile.lastIndexOf("/") + 1,
sourceFile.length());
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
String datetime = sdf.format(new Date());
String destination = destinationFile + filename + datetime;
String command = "mv " + sourceFile + " " + destination;
Process p = null;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
p.destroy();
}
return destination;
}
public List<MatchRecord> match(List<LogRecord> logins, List<LogRecord> logouts) {
conainer = new ArrayList<MatchRecord>();
for (int i = 0; i < logins.size(); i++) {
MatchRecord mr = new MatchRecord();
LogRecord login = logins.get(i);
for (int j = 0; j < logouts.size();j++) {
LogRecord logout = logouts.get(j);
if (login.getPid() == logout.getPid()
&& login.getUname().equals(logout.getUname())) {
long duration = logout.getLogTime() - login.getLogTime();
if (duration < 0) {
//throw new MatchException("匹配数据发生异常");
logouts.remove(j);
j--;
break;
}
mr.setDuration((int) duration);
mr.setLogIp(login.getLogIp());
mr.setLoginTime(login.getLogTime());
mr.setLogoutTime(logout.getLogTime());
mr.setLogName(login.getUname());
mr.setLabIp(p.getProperty("tarena.com.client.lab_ip"));
conainer.add(mr);
logouts.remove(j);
j--;
break;
}
}
logins.remove(i);
i--;
}
if(logins.size() > 0){
store(logins);
}
return conainer;
}
private void store(List<LogRecord> logins) {
String savefile = p.getProperty("tarena.com.client.save_file");
File f = new File(savefile);
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(logins);
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new SaveDataException("写入的存储文件找不到");
} catch (IOException e) {
e.printStackTrace();
throw new SaveDataException("写入数据到存储文件中时发生异常");
} finally {
try {
oos.close();
} catch (IOException e) {
}
}
}
private void readSaveFile(List<LogRecord> logins) {
String savefile = p.getProperty("tarena.com.client.save_file");
File f = new File(savefile);
if (!f.exists())
return;
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(f));
Object obj = ois.readObject();
if (obj instanceof ArrayList) {
logins = (ArrayList<LogRecord>) obj;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new SaveDataException("读出的存储文件找不到");
} catch (IOException e) {
e.printStackTrace();
throw new SaveDataException("从存储文件中读出数据时发生异常");
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new SaveDataException("从存储文件中读出数据时发生异常");
} finally {
try {
ois.close();
} catch (IOException e) {
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
DMS.rar_dms_java data collect_数据采集_采集
共86个文件
class:35个
java:35个
properties:7个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 77 浏览量
2022-09-22
20:24:16
上传
评论
收藏 362KB RAR 举报
温馨提示
DMS数据采集系统,主要用来对数据进行采集
资源推荐
资源详情
资源评论
收起资源包目录
DMS.rar (86个子文件)
DMS
wtmpx 8.55MB
bin
unix_server.properties 194B
db_config.properties 160B
insert_statement.sql 1KB
select.sql 327B
tarena
dms
client
read
SaveDataException.class 470B
MappingMemoryExcepton.class 467B
CollectionRecord.class 9KB
InitException.class 458B
MatchException.class 465B
data
MatchRecord.class 3KB
LogRecord.class 2KB
send
SendDataException.class 474B
Sender.class 2KB
main
ClientCollector.class 730B
interfaces
Match.class 372B
Read.class 233B
Send.class 139B
server
persist
JDBCPersistTwo.class 4KB
JDBCPersist.class 3KB
interfaces
Persist.class 256B
util
JdbcUitl.class 4KB
receive
ServerReceiveDataException.class 503B
Receiver.class 3KB
ServerInitException.class 486B
interfaces
Receive.class 156B
main
Server.class 1KB
integrate
DealWithMonth.class 4KB
IntegratorTwo.class 2KB
Integrator.class 2KB
data
YearTime.class 1KB
MonthTime.class 2KB
DateTime.class 2KB
Detail.class 2KB
DealWithDay.class 4KB
ReadFromDBException.class 490B
DealWithYear.class 4KB
main
ExecIntegrator.class 673B
interfaces
Integrate.class 163B
util
IntegrateUtil.class 2KB
create_table.sql 1KB
scr
unix_server.properties 194B
db_config.properties 160B
insert_statement.sql 1KB
select.sql 327B
tarena
dms
client
read
CollectionRecord.java 7KB
MatchException.java 178B
SaveDataException.java 183B
MappingMemoryExcepton.java 178B
InitException.java 172B
data
LogRecord.java 2KB
MatchRecord.java 2KB
send
Sender.java 2KB
SendDataException.java 185B
main
ClientCollector.java 340B
interfaces
Match.java 287B
Send.java 85B
Read.java 195B
server
persist
JDBCPersist.java 2KB
interfaces
Persist.java 340B
util
JdbcUitl.java 3KB
JDBCPersistTwo.java 3KB
receive
Receiver.java 2KB
interfaces
Receive.java 99B
ServerReceiveDataException.java 212B
ServerInitException.java 195B
main
Server.java 552B
integrate
DealWithYear.java 2KB
ReadFromDBException.java 197B
IntegratorTwo.java 1KB
Integrator.java 3KB
DealWithMonth.java 3KB
data
YearTime.java 919B
MonthTime.java 1KB
DateTime.java 1KB
Detail.java 1KB
main
ExecIntegrator.java 295B
interfaces
Integrate.java 273B
DealWithDay.java 4KB
util
IntegrateUtil.java 1KB
.settings
com.genuitec.eclipse.core.prefs 140B
unix_server.properties 194B
db_config.properties 160B
.project 362B
.classpath 324B
Integrate_config.properties 160B
共 86 条
- 1
资源评论
朱moyimi
- 粉丝: 61
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功