CheckConnectionState.java
5.08 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
package com.sitech.ismp.check.downtime;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import java.util.Date;
import java.util.List;
import java.util.UUID;
/**
* @author frank zmm@honggroup.com.cn
* @Description: 检查主机,数据库,中间件的连接状态。
* 其中主机包含 Linux/unix ping ssh 2种状态;Windows ping snmp 2种状态;
* 数据库 ping jdbc 2种状态; 中间件(Tomcat) ping jmx状态
*
* 需要区分 Windows与其他系统。在方法参数上,将map转换成CheckConnectionBean再使用。
* @Package com.sitech.ismp.check.downtime
* @ClassName: com.sitech.ismp.check.downtime.CheckConnectionState
* @date 2017年04月22日 14:17
*/
public class CheckConnectionState {
private static Logger log= Logger.getLogger(CheckConnectionState.class);
CheckConnectionService service=new CheckConnectionService();
//******************************* 获取主机相关状态代码 *******************************//
//******* 主机分为Linux/unix与Windows两大类,连接的时候前者使用ssh后者使用snmp ************//
//******************************* 获取主机相关状态代码 *******************************//
/**
* 获取设备的ping结果,能ping通返回 UP,否则返回 DOWN.
* 需要连续ping3次,将ping的结果插入到 TB_CONNECTION_STATE.
* @param bean
* @return
*/
public String getPingState(ParamterBean bean){
String state=service.getPingState(bean);
String type=bean.getType();
if("windows".equals(type) || "linux".equals(type)|| "unix".equals(type)){
String ip=bean.getDEVICE_IP();
if(!StringUtils.isEmpty(ip)){
ConnectionStateBean stateBean=new ConnectionStateBean();
stateBean.setDEVICE_STATE(state); // 设备状态
stateBean.setDEVICE_ID(UUID.randomUUID().toString());
stateBean.setDEVICE_IP(bean.getDEVICE_IP());
stateBean.setCREATE_DATE(new Date().getTime());
service.addPingConnectionState(stateBean);
}
}
return state;
}
/**
* 根据IP获取ping不通的次数。只取集合中的前3条数据。当都为down的时候表明已经宕机,少于3次不算宕机。
* @param ip
* @return
*/
public String getPingErrorState(String ip){
List<ConnectionStateBean> list=service.getStatByParam(ip);
int temp=0;
String state=null;
ConnectionStateBean bean=null;
if(null !=list && list.size()>0){
for(int i=0;i<3;i++){
bean=list.get(i);
state=bean.getDEVICE_STATE();
if("DOWN".equals(state)){
temp++;
}
}
}
String stat="UP";
if(temp==3){
stat="DOWN";
}
return stat;
}
/**
* 使用 SSH 或 Telnet 协议 获取设备的连接结果,能连接返回 UP,否则返回 DOWN.
* @param bean
*/
public String getConnectionState4SshOrTelnet(ParamterBean bean){
String state=service.getConnectionState4SshOrTelnet(bean);
return state;
}
/**
* 使用 SNMP 协议 获取设备的连接结果,能连接返回 UP,否则返回 DOWN.
* @param bean
*/
public String getConnectionState4Snmp(ParamterBean bean){
String state=service.getConnectionState4Snmp(bean);
log.info("***** getConnectionState4Snmp ***state= "+state+"**************");
return state;
}
/**
* Oracle数据库连接状态
* @param bean
*/
public String getOracleConnectionState(ParamterBean bean){
bean.setType("oracle");
String state=service.getDbConnectionState(bean);
return state;
}
public static void main(String [] args){
CheckConnectionState ss=new CheckConnectionState();
ParamterBean bean1=new ParamterBean();
bean1.setUSER_NAME("Administrator");
bean1.setPASSWORD("0213");
bean1.setPROTOCOL_PORT(161);
bean1.setDEVICE_IP("192.168.1.141");
ss.getConnectionState4Snmp(bean1);
ParamterBean bean=new ParamterBean();
bean.setPASSWORD("tiger");
bean.setUSER_NAME("scott");
bean.setURL("jdbc:oracle:thin:@192.168.1.130:1521:ORCL");
System.out.println(ss.getOracleConnectionState(bean));
}
/**
* Mysql 数据库连接状态
* @param bean
*/
public String getMysqlConnectionState(ParamterBean bean){
bean.setType("Mysql");
String state=service.getDbConnectionState(bean);
return state;
}
/**
* SqlServer 数据库连接状态
* @param bean
*/
public void getSqlServerConnectionState(ParamterBean bean){
bean.setType("SqlServer");
String state=service.getDbConnectionState(bean);
}
/**
* Db2 数据库连接状态
* @param bean
*/
public void getDb2ConnectionState(ParamterBean bean){
bean.setType("Db2");
String state=service.getDbConnectionState(bean);
}
}