package inter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ReadSzthLog extends Thread{
Connection conn = null;
Equipment equipment = null;
public ReadSzthLog(Connection conn,Equipment equipment) {
super();
this.conn=conn;
this.equipment=equipment;
}
LogUtil mylogger = new LogUtil();
public void run() {
//日志目录
String logFilePath = “/data/share/log/”+equipment.getCompanyName()+“/”+equipment.getDevIp()+“/log_info”;
mylogger.info(“logFilePath :”+logFilePath);
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
int newFileFlag=0;
int isNewDay=0;
String perDate = “19970101”;
String newDate = null;
RandomAccessFile randomFile = null;
String sql;
String perFile = null;
String newFile = null;
long lastTimeFileSize = 0; // 上次文件大小
while (true) {
try {
//获取和设备的连接转态
netStatus(isOnline(equipment.getDevIp()));
File logFile = new File(logFilePath);
//判断文件路径是否存在
if(!logFile.exists()){
Thread.sleep(500);
mylogger.info("没有目录:"+logFilePath);
continue;
}
Tools tool = new Tools();
List<String> fileNames = new ArrayList<String>();
//判断是不是新的一天
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String dataStr = formatter.format(new Date());
newDate=dataStr;
if(!dataStr.equals(perDate)){
isNewDay = 1;
mylogger.info("新的一天开始了:"+newDate);
}
if(isNewDay == 1 ){
perFile=null;
perDate=newDate;
fileNames=tool.getFileNames(logFilePath);
for(String fileName:fileNames){
mylogger.info("fileName:"+fileName);
if(fileName.indexOf(newDate) != -1){
//找到一个新的文件
//日志文件的命名格式是 xxxxx年月日.txt(例如: xxxxx20230217.txt)
newFile=logFilePath+"/"+fileName;
File file=new File(newFile);
if(file.isFile()){
isNewDay=0;
newFileFlag=1;
mylogger.info("新的文件为:"+newFile);
break;
}
}
}
}
if(newFile != null && newFileFlag ==1){
perFile = newFile;
newFileFlag = 0;
newFile = null;
if(randomFile != null){
randomFile.close();
randomFile=null;
}
randomFile = new RandomAccessFile(perFile, "r");
File InfoStaFile=new File(logFilePath+"/log_status");
if(InfoStaFile.isFile()){
RandomAccessFile infoRandom = new RandomAccessFile(logFilePath+"/log_status", "r");
String infoLine = null;
String logFileName = null;
String sizeStr = null;
//读取上次打开文件的信息
/*****************************
文件格式为如下:文件名称和文件读取的大小
logFileName = /data/share/log//log_info/xxxxx_20230216.txt
lastTimeFileSize = 24
***********************************/
while ((infoLine = infoRandom.readLine()) != null) {
String infoLine1=new String(infoLine.getBytes("ISO-8859-1"),"utf-8");
mylogger.info("infoLine1 "+infoLine1);
if(infoLine1.indexOf("logFileName") != -1){
logFileName=getValue(infoLine1,"logFileName");
}
if(infoLine.indexOf("lastTimeFileSize") != -1){
sizeStr=getValue(infoLine1,"lastTimeFileSize");
}
}
if(logFileName != null && sizeStr != null){
long size=Long.parseLong(sizeStr);
mylogger.info("logFileName "+logFileName);
mylogger.info("sizeStr "+sizeStr);
if(logFileName.equals(perFile) && size > 0){
lastTimeFileSize=size;
}else{
lastTimeFileSize=0;
}
}else{
lastTimeFileSize=0;
}
}else{
lastTimeFileSize=0;
}
}
if( perFile != null){
//定位到上次读取的位置
randomFile.seek(lastTimeFileSize);
String line = null;
while ((line = randomFile.readLine()) != null) {
String logLine=new String(line.getBytes("ISO-8859-1"),"utf-8");
String[] split = logLine.split("\t");
String time=null;
String type=null;
String information=null;
//日志格式为
//2023-02-14 13:02:02 正常 console connected:/192.168.127.18:54546
mylogger.info("split length is "+split.length);
if (split != null && split.length == 3) {
time = split[0];
type = split[1];
information = split[2];
sql="INSERT INTO log_info (name,ip,hostname,type,time,description) values("+"\'"+equipment.getCompanyName()+"\',\'"+equipment.getDevIp()+"\',\'"+equipment.getDevName()+"\',\'"+type+"\',\'"+time+"\',\'"+information+"\'"+");";
mylogger.info("################"+sql);
//写入数据库
stmt.executeUpdate(sql);
}
Thread.sleep(500);
mylogger.info("line"+"\t"+ logLine);
}
long readFilesSize = randomFile.length();
if(lastTimeFileSize != readFilesSize){
lastTimeFileSize = readFilesSize;
writeLogStatus(perFile,lastTimeFileSize);
}
Thread.sleep(500);
}else{
mylogger.info("没有读取的文件");
}
Thread.sleep(500);
} catch (IOException e) {
mylogger.info("error"+e);
e.printStackTrace();
}
}
}catch (Exception e) {
try {
mylogger.info("error"+e);
stmt.close();
} catch (SQLException e1) {
mylogger.info("error"+e1);
// TODO Auto-generated catch block
e1.printStackTrace();
}
mylogger.info("################"+e);
}
}
private String getValue(String line,String name){
String result=null;
String[] split = line.split(" = ");
if(split != null && split.length == 2){
result=split[1];
}
return result;
}
private void writeLogStatus(String logName,long lastTimeFileSize){
try{
String logFilePath = "/data/share/log/"+equipment.getCompanyName()+"/"+equipment.getDevIp();
FileWriter fw = new FileWriter(new File(logFilePath+"/log_info/log_status"));
BufferedWriter bw=new BufferedWriter(fw);
bw.write("logFileName = "+logName+"\n");
bw.write("lastTimeFileSize = "+lastTimeFileSize+"\n");
bw.close();
fw.close();
}catch (Exception e) {
e.printStackTrace();
mylogger.info("写入"+ logName +"出错!");
}
}
private void netStatus(boolean starus){
try{
String logFilePath = "/data/share/log/"+equipment.getCompanyName()+"/"+equipment.getDevIp();
FileWriter fw = new FileWriter(new File(logFilePath+"/cfg/status"));
BufferedWriter bw=new BufferedWriter(fw);
if(starus){
bw.write("1\n");
}else{
bw.write("0\n");
}
bw.close();
fw.close();
}catch (Exception e) {
e.printStackTrace();
mylogger.info("写入网络状态出错!");
}
}
public static boolean isOnline(String hostname) {
try {
InetAddress ia;
boolean isonline = false;
ia = InetAddress.getByName(hostname);// 例如:www.baidu.com
isonline = ia.isReachable(1500); //超时时间1.5秒
return isonline;
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
System.out.println("address:" + hostname + " is not unknown");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("address:" + hostname + " is not reachable");
}
return false;
}
}