DirCollThread.java
6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package com.sitech.ismp.coll.busi;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.sitech.ismp.coll.busi.db.dao.TbTmpFileDao;
import com.sitech.ismp.coll.busi.db.domain.TbTmpFile;
import com.sitech.ismp.coll.busi.util.FilesOperation;
import com.sitech.util.DES3;
import com.sitech.util.RandomGUID;
import com.sitech.util.upload.FTPIF;
import com.sitech.util.upload.FTPSrv;
import com.sitech.util.upload.RemoteFile;
import com.sitech.util.upload.SFTPClient;
/**
* 对目录进行文件数变化管理,核查增加和减少,并返回变化的文件名 结果以文件方式返回 文件命名格式:DirColl_GUID.txt 文件格式:
* ---------------------------------- 172.21.1.100
* /bnmsapp2/linxc/masteragent/logs NEW_FILES=null DELETE_FILES=null
* UPDATE_FILES=文件名##上个周期大小##当前大小##上个周期权限##当前权限
* ----------------------------------
*
* @author linxc
*
*/
public class DirCollThread implements Runnable {
private Logger logger = Logger.getLogger(FileFtpCollThread.class);
private String ip;
private String username;
private String password;
private String remoteDir;
private String localDir;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public DirCollThread(String ip, String username, String password,
String remoteDir, String localDir) {
this.ip = ip;
this.username = username;
this.password = password = DES3.decrypt(password);
this.remoteDir = remoteDir;
this.localDir = localDir;
}
public void run() {
logger.info("Start DirCollThread,IP=" + ip + ",USERNAME=" + username
+ ", DIR=" + remoteDir);
localDir += "/" + ip.replace(".", "_");
File file = new File(localDir);
if (!file.exists()) {
file.mkdir();
}
FTPIF ftpClient = getFtpClient();
if (ftpClient == null) {
logger.warn("ftpClient == null, return!");
return;
}
try {
RemoteFile[] list = listRemoteFile(ftpClient);
if (list == null) {
return;
}
String result = compareWithLastCycle(list);
String guid = RandomGUID.getRandomGUID();
String fileName = localDir + "/DIR_" + guid + ".info";
FilesOperation.createFiles(fileName, result);
} catch (Exception e) {
logger.error("Exception while DirCollThread.run().", e);
} finally {
if (ftpClient != null) {
try {
ftpClient.logout();
} catch (Exception e) {
logger.error("Exception while ftpClient.logout()", e);
}
}
}
}
private String compareWithLastCycle(RemoteFile[] list) {
String result = "";
result += "TYPE=DIR\n";
result += "IP_ADDR=" + ip + "\n";
result += "DIRECTORY=" + remoteDir + "\n";
int currFileNum = 0;
Map<String, TbTmpFile> currFileMap = new HashMap<String, TbTmpFile>();
for (RemoteFile currFile : list) {
if (currFile.isDirectory()) {
continue;
}
currFileNum++;
TbTmpFile tbTmpFile = new TbTmpFile(currFile, ip, remoteDir);
currFileMap.put(tbTmpFile.getID(), tbTmpFile);
}
TbTmpFileDao dao = new TbTmpFileDao();
List<TbTmpFile> lastFileMap = dao.getTbTmpFile(ip, remoteDir);
int lastFileNum = lastFileMap.size();
result += "CURRENT_NUM=" + currFileNum + "\n";
result += "LAST_NUM=" + lastFileNum + "\n";
dao.deleteTbTmpFile(ip, remoteDir);
for (TbTmpFile file : currFileMap.values()) {
dao.insertTbTmpFile(file);
}
String addFileStr = "NEW_FILES=";
String deleteFileStr = "DELETE_FILES=";
String updateFileStr = "UPDATE_FILES=";
String add = "";
String delete = "";
String update = "";
for (TbTmpFile lastFile : lastFileMap) {
String id = lastFile.getID();
TbTmpFile currFile = currFileMap.get(id);
if (currFile == null) {
delete += lastFile.getFILE_ANME() + ",";
} else if (!currFile.equals(lastFile)) {
// 文件名##上个周期大小##当前大小##上个周期权限##当前权限
update += currFile.getFILE_ANME() + "##" + lastFile.getSIZE()
+ "##" + currFile.getSIZE() + "##"
+ lastFile.getPERMISSIONS() + "##"
+ currFile.getPERMISSIONS() + "##"
+ dateFormat.format(lastFile.getLAST_MODIFY()) + "##"
+ dateFormat.format(currFile.getLAST_MODIFY()) + ",";
currFileMap.remove(id);
} else {
currFileMap.remove(id);
}
}
if (currFileMap.size() > 0) {
for (TbTmpFile file : currFileMap.values()) {
add += file.getFILE_ANME() + ",";
}
}
if (add.equals("")) {
add = "null";
} else {
add = add.substring(0, add.length() - 1);
}
if (delete.equals("")) {
delete = "null";
} else {
delete = delete.substring(0, delete.length() - 1);
}
if (update.equals("")) {
update = "null";
} else {
update = update.substring(0, update.length() - 1);
}
addFileStr += add;
deleteFileStr += delete;
updateFileStr += update;
result += addFileStr + "\n";
result += deleteFileStr + "\n";
result += updateFileStr + "\n";
result += "COLL_TIME=" + dateFormat.format(new Date()) + "\n";
logger.info("Result:\n" + result);
return result;
}
private RemoteFile[] listRemoteFile(FTPIF ftpClient) {
try {
ftpClient.chdir(remoteDir);
RemoteFile[] list = ftpClient.listRemoteFile(remoteDir);
return list;
} catch (Exception e) {
logger.error("Exception while listRemoteFile()", e);
}
return null;
}
/**
* @return FTP连接
*/
private FTPIF getFtpClient() {
FTPIF ftpClient = new FTPSrv();
try {
logger.info("ftp " + ip + ", LOGIN:" + username + "...");
ftpClient.login(ip, username, password);
} catch (Exception e) {
logger.warn("ftp " + ip + ", LOGIN:" + username + " FAILED!");
try {
ftpClient = new SFTPClient();
logger.info("sftp " + username + "@" + ip);
ftpClient.login(ip, username, password);
} catch (Exception e1) {
logger.warn("sftp " + username + "@" + ip + " FAILED!");
ftpClient = null;
}
}
return ftpClient;
}
public static void main(String[] args) {
String ip = "10.182.15.77";
String username = "ibnms";
String password = DES3.encrypt("?ismp#*2");
String remoteDir = "/iBNMSConsole/topo/data_save";
String localDir = "F:\\";
DirCollThread thread = new DirCollThread(ip, username, password,
remoteDir, localDir);
thread.run();
}
}