SwapMonitorThread.java
4.34 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
package com.sitech.jmx.manage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import com.sitech.base.AgentProperties;
import com.sitech.base.Config;
import com.sitech.ismp.app.event.KPI2Event;
import com.sitech.ismp.messageObject.PerformanceObject;
import com.sitech.util.Formater;
import com.sitech.util.mq.DataTunnel;
import com.sitech.util.mq.TunnelFactory;
public class SwapMonitorThread extends Thread {
private static Logger logger = Logger.getLogger("SWAP");
public void run() {
// SWAP文件目录
File file = null;
try {
file = new File(AgentProperties.SWAP_PATH);
logger.info("Start SwapMonitorThread, " + file.getAbsolutePath());
} catch (Exception e) {
logger.error("Exception while Start SwapMonitorThread, " + file.getAbsolutePath(), e);
return;
}
while (true) {
try {
File[] filelist = file.listFiles();
if(filelist == null || filelist.length == 0){
logger.debug("sleep 1 second...");
sleep(1000);
continue;
}
for (int i = 0; i < filelist.length / 100 + 1; i++) {
// 100个文件作为一组处理
List<PerformanceObject> result = new ArrayList<PerformanceObject>();
for (int j = i * 100; j < (i + 1) * 100 && j < filelist.length; j++) {
// 解析swap文件
String filepath = filelist[i].getPath();
if (filelist[i].canWrite()) {
try {
result.addAll(parsetxtfile(filepath));
} catch (Exception e) {
logger.error("Exception while parse swap file", e);
} finally {
if (filelist[i] != null && filelist[i].exists()) {
filelist[i].delete();
}
}
}
}
// 100个文件的指标批量进行阀值判断
KPI2Event kpi2event = new KPI2Event();
kpi2event.generation(result);
}
} catch (Exception e) {
logger.error("Exception while parse swap file", e);
}
}
}
private List<PerformanceObject> parsetxtfile(String filestr) {
List<PerformanceObject> result = new ArrayList<PerformanceObject>();
logger.info("Begin parse swap file " + filestr);
String line = "";
DataTunnel kpidetailTunnel = TunnelFactory.getTunnel(Config.Q_PERFORMANCE);
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filestr));
while ((line = br.readLine()) != null) {
String[] elem = line.split("\t");
if (elem.length == 4 || elem.length == 5 || elem.length == 6) {
String unitId = elem[0];
String kpiId = elem[1];
Date cllTime = Formater.stringToDateTime(elem[2]);
String kpiValue = elem[3];
PerformanceObject kpidetail = new PerformanceObject();
kpidetail.setUnitId(unitId);
kpidetail.setKpiId(kpiId);
kpidetail.setKpiValue(kpiValue);
kpidetail.setCllTime(cllTime);
kpidetail.setCllTimeStr(elem[2]);
kpidetail.setExtUnitId(getExtUnitId(unitId));
if (elem.length >= 5) {
kpidetail.setInterval(Long.parseLong(elem[4]));
}
if (elem.length >= 6) {
kpidetail.setKpiDetail(elem[5]);
}
logger.info(line);
//
// kpi2event.generation(unitId, kpiId, cllTime, kpiValue);
kpidetailTunnel.writeData(kpidetail);
result.add(kpidetail);
}else{
logger.error("Parse swap file["+filestr+"] failed: " + line);
}
}// end while
} catch (Exception e) {
logger.error("Exception while parse swap file["+filestr+"].", e);
}finally{
if(br != null){
try {
br.close();
} catch (IOException e) {
logger.error("Exception while parse swap file["+filestr+"].", e);
}
}
}
logger.info("End parse swap file " + filestr);
return result;
}
/**
* 获得扩展UNIT_ID,如:10-10-20:deviceId
* @throws
* @since Ver 1.1
*/
private String getExtUnitId(String unitId){
String extUnitId = "";
if (unitId.startsWith("10")) {
String[] elem = unitId.split(":");
if(elem.length >=2){
String[] kbp = elem[0].split("-");
if (kbp.length >= 3) {
extUnitId = kbp[0] + "-" + kbp[1] + "-" + kbp[2];
}else{
extUnitId = elem[0];
}
String deviceId = elem[1].split("-")[0];
extUnitId += ":" + deviceId;
} else {
extUnitId = "n/r";
}
}else{
// 业务类的EXT_UNIT_ID
String[] elem = unitId.split(":");
extUnitId = elem[0];
}
return extUnitId;
}
}