FileFtpCollThread.java
6.14 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
package com.sitech.ismp.coll.busi;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
import com.sitech.ismp.coll.busi.util.FilesOperation;
import com.sitech.util.DES3;
import com.sitech.util.JSONUtil;
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;
/**
* FTP采集配置文件(GX)
* 本地目录命名规则: ${coll.local.path} + "/" + ip.replace(".","_")/
* 例:/bnmsapp2/172_21_1_100/
*
* 1. FTP配置文件到本地目录,文件命名规则为:CFG_$GUID.txt
* 2. 创建对应的描述文件到本地目录,文件命名规则为:CFG_$GUID.info
*
*******Begin CFG_986C5F24.info***************************************
* TYPE=FTP
* IP_ADDR=172.21.1.100
* DIRECTORY=/bnmsapp2
* FILE_NAME=AppCenterProxy_error_check.sh
* PERSSIONS=-rw-r--r--
* SIZE=1279
* UPDATE_TIME=2012-10-17 10:02:38
* USER=bnmsapp2
* COLL_TIME=2012-11-21 13:18:55
*******End CFG_986C5F24.info*****************************************
*
* @author linxc
*/
public class FileFtpCollThread implements Runnable {
private Logger logger = Logger.getLogger(FileFtpCollThread.class);
private String ip;
private String username;
private String password;
private int collHa = 0;
private String ipHa;
private String usernameHa;
private String passwordHa;
private String localDir;
private String[] dir;
private String[] file;
private HashMap<String,String> params;
public FileFtpCollThread(HashMap<String,String> params) {
this.params = params;
}
public void run() {
if (!init()) {
return;
}
collHost(ip, username, password);
if(collHa == 1){
// 采集双机
collHost(ipHa, usernameHa, passwordHa);
}
}
private void collHost(String ip, String username, String password){
logger.info("Start FileFtpCollThread,IP=" + ip + ",USERNAME=" + username);
FTPIF ftpClient = getFtpClient(ip, username, password);
if (ftpClient == null) {
logger.warn("ftpClient == null, return!");
return;
}
try {
for (int i = 0; i < file.length; i++) {
getFile(ftpClient, dir[i], file[i]);
}
} catch (Exception e) {
logger.error("Exception while FileFtpColl.collFile", e);
} finally {
if (ftpClient != null) {
try {
ftpClient.logout();
ftpClient = null;
} catch (Exception e) {
logger.error("Exception while ftpClient.logout()", e);
}
}
}
}
public void getFile(FTPIF ftpClient, String remoteDir, String fileName)
throws Exception {
fileName = fileName.trim();
logger.info("cd " + remoteDir);
ftpClient.chdir(remoteDir);
RemoteFile remoteFile = ftpClient.listRemoteFile(remoteDir, fileName);
if (remoteFile == null || remoteFile.isDirectory()) {
return;
}
String perssions = remoteFile.getPermissons();
long size = remoteFile.getSize();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date updateTime = remoteFile.getLastModifyTime();
String user = remoteFile.getOwner();
String descr = "";
descr += "TYPE=FTP\n";
descr += "IP_ADDR=" + ip + "\n";
descr += "DIRECTORY=" + remoteDir + "\n";
descr += "FILE_NAME=" + fileName + "\n";
descr += "PERSSIONS=" + perssions + "\n";
descr += "SIZE=" + size + "\n";
descr += "UPDATE_TIME=" + dateFormat.format(updateTime) + "\n";
descr += "USER=" + user + "\n";
descr += "COLL_TIME=" + dateFormat.format(new Date()) + "\n";
logger.info("Begin get file[" + fileName + "]...");
String guid = RandomGUID.getRandomGUID();
String tempName = localDir + "/CFG_" + guid + ".load";
String descrFileName = localDir + "/CFG_" + guid + ".info";
File tempFile = new File(tempName);
File localFile = new File(localDir + "/CFG_" + guid + ".txt");
ftpClient.get(fileName, tempName);
tempFile.renameTo(localFile);
logger.info("Get file[" + fileName + "] success!");
FilesOperation.createFiles(descrFileName, descr);
}
private FTPIF getFtpClient(String ip, String username, String password) {
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;
}
/**
* 初始化采集参数
*/
private boolean init(){
try{
this.ip = params.get("IP_ADDR");
this.username = params.get("USERNAME");
this.password = DES3.decrypt(params.get("PASSWORD"));
String haCollFlag = params.get("HA_FLAG");
if(haCollFlag != null && haCollFlag.trim().equals("1")){
this.collHa = Integer.parseInt(haCollFlag);
this.ipHa = params.get("IP_ADDR_HA");
this.usernameHa = params.get("USERNAME_HA");
this.passwordHa = DES3.decrypt(params.get("PASSWORD_HA"));
}
ResourceBundle agentConfig = ResourceBundle.getBundle("agent");
this.localDir = agentConfig.getString("coll.local.path") + "/" + ip.replace(".", "_");
File temp = new File(this.localDir);
if (!temp.exists()) {
temp.mkdir();
}
List<String> fileList = new ArrayList<String>();
for (String key : params.keySet()) {
if (key.indexOf("FILE_") >= 0) {
Map<?,?> tempMap = (Map<?,?>) JSONUtil.fromJSON(params.get(key));
fileList.add((String) tempMap.get("filePath"));
}
}
this.dir = new String[fileList.size()];
this.file = new String[fileList.size()];
for (int i = 0; i < fileList.size(); i++) {
String elem = fileList.get(i);
dir[i] = elem.substring(0, elem.lastIndexOf("/"));
file[i] = elem.substring(elem.lastIndexOf("/") + 1);
}
}catch (Exception e) {
logger.error("初始化文件Ftp采集进程出错!!!", e);
return false;
}
return true;
}
}