CrontabCollThread.java
5.26 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
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.Map;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
import com.sitech.base.AgentProperties;
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采集Crontab配置到本地,并创建对应的描述文件
* 需要/var/spool/cron或/var/spool/cron/crontabs目录读权限
*
* @author LINXC
*/
public class CrontabCollThread implements Runnable {
private Logger logger = Logger.getLogger(CrontabCollThread.class);
private String ip;
private String username;
private String password;
private String localDir;
private Map<String,String> cronMap = new HashMap<String, String>();
public CrontabCollThread(HashMap<String, String> params) {
init(params);
}
/**
* @param params
* HashMap KEY:[HOSTIP,USERNAME,PASSWORD, CRON_*]
*/
private void init(HashMap<String, String> params) {
ResourceBundle agentConfig = ResourceBundle.getBundle("agent");
localDir = agentConfig.getString("coll.local.path");
ip = params.get("IP_ADDR");
username = params.get("USERNAME");
password = params.get("PASSWORD");
password = DES3.decrypt(password);
for (String key : params.keySet()) {
if (key.startsWith("CRON_")) {
cronMap.put(key, params.get(key));
}
}
}
public void run() {
logger.info("Start CrontabCollThread,IP=" + ip + ",USERNAME=" + username);
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 {
getFile(ftpClient);
} catch (Exception e) {
logger.error("Exception while CrontabColl.collCrontab", e);
} finally {
if (ftpClient != null) {
try {
ftpClient.logout();
ftpClient = null;
} catch (Exception e) {
logger.error("Exception while ftpClient.logout()", e);
}
}
}
// 判断crontab任务是否正常执行
String crontab = "";
for (String guid : cronMap.keySet()) {
String json = cronMap.get(guid);
Map<?, ?> map = (Map<?, ?>) JSONUtil.fromJSON(json);
String cron = map.get("crontabTime") +"";
String crontabLog = map.get("crontabLog") +"";
String crontabUser = map.get("crontabUser") +"";
crontab += cron + " com.sitech.ismp.coll.busi.CrontabCheck "
+ ip+ " " + username + " " + password + " " + guid + " " + crontabUser+ " " +crontabLog.replace("\\", "")
+ "\n";
}
try {
FilesOperation.createFiles(AgentProperties.AGENT_HOME + "/config/gx/crontab", crontab);
} catch (Exception e) {
logger.error("Exception while create crontab file!", e);
}
}
/**
* FTP采集用户crontab配置文件到本地,并创建对应的描述文件
* CRONTAB文件: CRON_${GUID}.txt
*
*
* @param ftpClient
* @throws Exception
*/
public void getFile(FTPIF ftpClient) throws Exception {
logger.info("cd /var/spool/cron/crontabs");
ftpClient.chdir("/var/spool/cron/crontabs");
RemoteFile[] file = ftpClient.listRemoteFile("/var/spool/cron/crontabs");
for (int i = 0; i < file.length; i++) {
if (file[i].isDirectory()) {
continue;
}
String fileName = file[i].getFileName();
String perssions = file[i].getPermissons();
long size = file[i].getSize();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date updateTime = file[i].getLastModifyTime();
String user = file[i].getOwner();
String descr = "";
descr += "TYPE=CROB\n";
descr += "IP_ADDR=" + ip + "\n";
descr += "DIRECTORY=/var/spool/cron\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 + "/CROB_" + guid + ".load";
String descrFileName = localDir + "/CROB_" + guid + ".info";
File tempFile = new File(tempName);
File localFile = new File(localDir + "/CROB_" + guid + ".txt");
ftpClient.get(fileName, tempName);
tempFile.renameTo(localFile);
logger.info("Get file[" + fileName + "] success!");
FilesOperation.createFiles(descrFileName, descr);
}
}
private FTPIF getFtpClient() {
FTPIF ftpClient = new SFTPClient();
try {
logger.info("sftp " + username + "@" + ip);
ftpClient.login(ip, username, password);
} catch (Exception e) {
logger.warn("sftp" + ip + ", LOGIN:" + username + " FAILED!");
try {
ftpClient = new FTPSrv();
logger.info("ftp " + ip + ", LOGIN:" + username + "...");
ftpClient.login(ip, username, password);
} catch (Exception e1) {
logger.warn("ftp " + username + "@" + ip + " FAILED!");
ftpClient = null;
}
}
return ftpClient;
}
}