CollUnion.java
3.29 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
/**
* 功能:并发监控agent所在主机的主机状态
* 作者:毛志荣
* 日期:2008-5-2 12:34
* 版本:v1.00
*
*/
package com.sitech.ismp.coll;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import sitech.www.frame.jdbc.SqlQuery;
import sitech.www.frame.util.DataSourceUtils;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CollUnion {
private static ExecutorService executorService;
static List hosts = new ArrayList();
public static String host_type;
public static String times;
public static String count;
public static String swap_up_cmd;
public static String swap_down_cmd;
public static String kpi_id = "FM-00-01-001-01";
private static long interval;
/**
* 根据unit_id与kpi_id得到相应的值
*/
static {
ResourceBundle config = ResourceBundle.getBundle("conf");
host_type = config.getString("host_type");
times = config.getString("ping_times");
count = config.getString("count");
swap_up_cmd = config.getString("swap_up_cmd");
swap_down_cmd = config.getString("swap_down_cmd");
interval = Integer.parseInt(config.getString("interval"));
String filterString = getFilterNet();
String sql = "select t.ip_addr, SUBSTR(t.unit_id, 0, instr(t.unit_id, ':')-1) || '-10' || SUBSTR(t.unit_id, instr(t.unit_id, ':') ) || '-total',t.flag from "
+ "tb_asset_host t where t.enable = '1'" + filterString;
System.out.println("[监控主机状态:" + sql + "]");
java.sql.Connection conn = DataSourceUtils.getConnection();
System.out.println("数据库连接" + conn);
try {
List list = SqlQuery.findList(sql);
int size = list.size();
for (int i = 0; i < size; i++) {
hosts.add((String[]) list.get(i));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DataSourceUtils.closeConnection(conn);
conn = null;
}
}
private static String getFilterNet() {
String result = "";
ResourceBundle resourceBundle = ResourceBundle.getBundle("netfilter");
result = resourceBundle.getString("net");
if (result.equals("")) {
return "";
} else {
String temp = "";
String[] strs = result.split(",");
for (int i = 0; i < strs.length; i++) {
temp = temp + " and t.ip_addr not like '" + strs[i] + "%'";
}
result = temp;
}
return result;
}
/** --- 主机状态监控--- */
public void start() {
executorService = Executors.newFixedThreadPool(20);
int count = hosts.size();
System.out.println("[主机台数: " + count + "]");
while (true) {
for (int index = 0; index < count; index++) {
String[] db_msg = (String[]) hosts.get(index);
String host_ip = db_msg[0];
String unit_id = db_msg[1];
String flag = db_msg[2];
System.out.println(unit_id);
executorService.execute(new CollUnionWorker(host_ip, unit_id,
flag));
try {
Thread.sleep(1000L); // 缓冲,停留一秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Runtime.getRuntime().freeMemory();
Runtime.getRuntime().gc();
Thread.sleep(1000 * interval); // 隔inter
System.gc();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print("=");
}
}
public static void main(String[] args) {
CollUnion moni_db = new CollUnion();
moni_db.start();
}
}