Showing
7 changed files
with
806 additions
and
0 deletions
1 | +package com.sitech.ismp.check.downtime; | ||
2 | + | ||
3 | +import java.io.Serializable; | ||
4 | +import java.util.Date; | ||
5 | + | ||
6 | +/** | ||
7 | + * @author frank zmm@honggroup.com.cn | ||
8 | + * @Description: 宕机表对应的bean。 | ||
9 | + * @Package com.sitech.ismp.check.downtime | ||
10 | + * @ClassName: com.sitech.ismp.check.downtime.DowntimeBean | ||
11 | + * @date 2017年04月16日 13:30 | ||
12 | + */ | ||
13 | +public class DowntimeBean implements Serializable{ | ||
14 | + | ||
15 | + // 设备ID | ||
16 | + private String DEVICE_ID; | ||
17 | + // 设备IP地址 | ||
18 | + private String DEVICE_IP; | ||
19 | + // 设备别名 | ||
20 | + private String DEVICE_ALIAS; | ||
21 | + // kbp_class 设备的分类 | ||
22 | + private String KBP_CLASS; | ||
23 | + // 连接协议(ssh/telnet) | ||
24 | + private String PROTOCOL; | ||
25 | + // 协议端口 | ||
26 | + private int PROTOCOL_PORT; | ||
27 | + // 用户名 | ||
28 | + private String USER_NAME; | ||
29 | + // 密码 | ||
30 | + private String PASSWORD; | ||
31 | + // 创建日期 | ||
32 | + private Date CREATE_DATE; | ||
33 | + // ping IP 后的结果 | ||
34 | + private boolean pingState; | ||
35 | + // ping后进行ssh连接后的状态。 | ||
36 | + private boolean sshState; | ||
37 | + // 告警级别 1 严重告警;2 重要告警 | ||
38 | + private int warningLevel; | ||
39 | + // ping后进行ssh连接后的结果,即重要告警还是严重告警。 | ||
40 | + private String warningResult; | ||
41 | + | ||
42 | + private String unitId; | ||
43 | + | ||
44 | + public String getDEVICE_ID() { | ||
45 | + return DEVICE_ID; | ||
46 | + } | ||
47 | + | ||
48 | + public void setDEVICE_ID(String DEVICE_ID) { | ||
49 | + this.DEVICE_ID = DEVICE_ID; | ||
50 | + } | ||
51 | + | ||
52 | + public String getDEVICE_IP() { | ||
53 | + return DEVICE_IP; | ||
54 | + } | ||
55 | + | ||
56 | + public void setDEVICE_IP(String DEVICE_IP) { | ||
57 | + this.DEVICE_IP = DEVICE_IP; | ||
58 | + } | ||
59 | + | ||
60 | + public String getDEVICE_ALIAS() { | ||
61 | + return DEVICE_ALIAS; | ||
62 | + } | ||
63 | + | ||
64 | + public void setDEVICE_ALIAS(String DEVICE_ALIAS) { | ||
65 | + this.DEVICE_ALIAS = DEVICE_ALIAS; | ||
66 | + } | ||
67 | + | ||
68 | + public String getKBP_CLASS() { | ||
69 | + return KBP_CLASS; | ||
70 | + } | ||
71 | + | ||
72 | + public void setKBP_CLASS(String KBP_CLASS) { | ||
73 | + this.KBP_CLASS = KBP_CLASS; | ||
74 | + } | ||
75 | + | ||
76 | + public String getPROTOCOL() { | ||
77 | + return PROTOCOL; | ||
78 | + } | ||
79 | + | ||
80 | + public void setPROTOCOL(String PROTOCOL) { | ||
81 | + this.PROTOCOL = PROTOCOL; | ||
82 | + } | ||
83 | + | ||
84 | + public int getPROTOCOL_PORT() { | ||
85 | + return PROTOCOL_PORT; | ||
86 | + } | ||
87 | + | ||
88 | + public void setPROTOCOL_PORT(int PROTOCOL_PORT) { | ||
89 | + this.PROTOCOL_PORT = PROTOCOL_PORT; | ||
90 | + } | ||
91 | + | ||
92 | + public String getUSER_NAME() { | ||
93 | + return USER_NAME; | ||
94 | + } | ||
95 | + | ||
96 | + public void setUSER_NAME(String USER_NAME) { | ||
97 | + this.USER_NAME = USER_NAME; | ||
98 | + } | ||
99 | + | ||
100 | + public String getPASSWORD() { | ||
101 | + return PASSWORD; | ||
102 | + } | ||
103 | + | ||
104 | + public void setPASSWORD(String PASSWORD) { | ||
105 | + this.PASSWORD = PASSWORD; | ||
106 | + } | ||
107 | + | ||
108 | + public Date getCREATE_DATE() { | ||
109 | + return CREATE_DATE; | ||
110 | + } | ||
111 | + | ||
112 | + public void setCREATE_DATE(Date CREATE_DATE) { | ||
113 | + this.CREATE_DATE = CREATE_DATE; | ||
114 | + } | ||
115 | + | ||
116 | + public boolean isPingState() { | ||
117 | + return pingState; | ||
118 | + } | ||
119 | + | ||
120 | + public void setPingState(boolean pingState) { | ||
121 | + this.pingState = pingState; | ||
122 | + } | ||
123 | + | ||
124 | + public boolean isSshState() { | ||
125 | + return sshState; | ||
126 | + } | ||
127 | + | ||
128 | + public void setSshState(boolean sshState) { | ||
129 | + this.sshState = sshState; | ||
130 | + } | ||
131 | + | ||
132 | + public String getWarningResult() { | ||
133 | + return warningResult; | ||
134 | + } | ||
135 | + | ||
136 | + public void setWarningResult(String warningResult) { | ||
137 | + this.warningResult = warningResult; | ||
138 | + } | ||
139 | + | ||
140 | + public int getWarningLevel() { | ||
141 | + return warningLevel; | ||
142 | + } | ||
143 | + | ||
144 | + public void setWarningLevel(int warningLevel) { | ||
145 | + this.warningLevel = warningLevel; | ||
146 | + } | ||
147 | + | ||
148 | + public String getUnitId() { | ||
149 | + return unitId; | ||
150 | + } | ||
151 | + | ||
152 | + public void setUnitId(String unitId) { | ||
153 | + this.unitId = unitId; | ||
154 | + } | ||
155 | + | ||
156 | + @Override | ||
157 | + public String toString() { | ||
158 | + return "DowntimeBean{" + | ||
159 | + " DEVICE_IP='" + DEVICE_IP + '\'' + | ||
160 | + ", pingState=" + pingState + | ||
161 | + ", sshState=" + sshState + | ||
162 | + ", warningResult='" + warningResult + '\'' + | ||
163 | + ", DEVICE_ALIAS='" + DEVICE_ALIAS + '\'' + | ||
164 | + ", USER_NAME='" + USER_NAME + '\'' + | ||
165 | + ", warningLevel=" + warningLevel + | ||
166 | + '}'; | ||
167 | + } | ||
168 | + | ||
169 | +} |
1 | +package com.sitech.ismp.check.downtime; | ||
2 | + | ||
3 | +import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL; | ||
4 | +import org.apache.log4j.Logger; | ||
5 | + | ||
6 | +import java.text.SimpleDateFormat; | ||
7 | +import java.util.*; | ||
8 | + | ||
9 | +/** | ||
10 | + * @author frank zmm@honggroup.com.cn | ||
11 | + * @Description: 宕机检测MBean实现类 | ||
12 | + * @Package com.sitech.ismp.check.downtime | ||
13 | + * @ClassName: com.sitech.ismp.check.downtime.Downtime | ||
14 | + * @date 2017年04月15日 13:43 | ||
15 | + */ | ||
16 | +public class Downtime implements DowntimeMBean { | ||
17 | + | ||
18 | + private static Logger log= Logger.getLogger(Downtime.class); | ||
19 | + | ||
20 | + | ||
21 | + DowntimeDao dao=new DowntimeDao(); | ||
22 | + DowntimeHistoryDao historyDao=new DowntimeHistoryDao(); | ||
23 | + SimpleDateFormat formaF=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | ||
24 | + | ||
25 | + /** | ||
26 | + * 检查主机的连接状态。如果连接异常,需要每隔30秒连续连接3次,如果3次连接都异常,则视为主机宕机,发送严重告警。 | ||
27 | + * 1 将需要监控的设备添加到内存数据库中。 | ||
28 | + * 2 对表中的主机进行ping操作。 | ||
29 | + * 3 对表中的主机进行SSH登录认证 | ||
30 | + * 4 发送告警信息 | ||
31 | + * @param params | ||
32 | + * @return | ||
33 | + */ | ||
34 | + @Override | ||
35 | + public Vector<TblATO_KPIDETAIL> checkConnectState(HashMap<String,String> params){ | ||
36 | + dao.addDowntimeHost(params); | ||
37 | + String ipAddr=params.get("DEVICE_IP"); | ||
38 | + boolean flag=isNeedExecuteCheckMethod(ipAddr); | ||
39 | + if(flag){ | ||
40 | + log.info("*************"+ formaF.format(new Date()) +" execute method checkConnectState ................."); | ||
41 | + // 将需要监控的设备添加到内存数据库中。 | ||
42 | + DowntimeBean bean=dao.map2DowntimeBean(params); | ||
43 | + DowntimeServer server=new DowntimeServer(bean); | ||
44 | + // 包括 2-4 需要执行的内容。 如果监控的设备很多的时候,使用单线程可能一个监控周期都不能ping完所有的主机。 | ||
45 | + server.run(); | ||
46 | + log.info("@@@@@@@@@@@@@@@@@@@@@@***** finish run method *****@@@@@@@@@@@@@@@@@@@@@@@@@@@"); | ||
47 | + log.info(bean.toString()); | ||
48 | + } | ||
49 | + Vector<TblATO_KPIDETAIL> bean = new Vector<TblATO_KPIDETAIL>(); | ||
50 | + return bean; | ||
51 | + } | ||
52 | + | ||
53 | + | ||
54 | + | ||
55 | + /** | ||
56 | + * 是否执行checkConnectState 方法。 | ||
57 | + * 判断当前IP是否发送过告警信息。每天一个主机只发送1条告警信息。 | ||
58 | + * @return | ||
59 | + */ | ||
60 | + private boolean isNeedExecuteCheckMethod(String ipAddr){ | ||
61 | + boolean flag=true; // 是否该ipAddr已经发送过消息。 | ||
62 | + SimpleDateFormat formaF=new SimpleDateFormat("yyyy-MM-dd"); | ||
63 | + String currentDay=formaF.format(new Date()); | ||
64 | + List<DowntimeHistoryBean> list=historyDao.getDowntimeHistoryByIp(ipAddr,currentDay); | ||
65 | + if(null !=list && list.size()>0){ | ||
66 | + flag=false; | ||
67 | + } | ||
68 | + return flag; | ||
69 | + } | ||
70 | + | ||
71 | + | ||
72 | +} |
1 | +package com.sitech.ismp.check.downtime; | ||
2 | + | ||
3 | +import com.sitech.database.dao.BaseDao; | ||
4 | +import org.apache.log4j.Logger; | ||
5 | + | ||
6 | +import java.sql.SQLException; | ||
7 | +import java.util.*; | ||
8 | + | ||
9 | +/** | ||
10 | + * @author frank zmm@honggroup.com.cn | ||
11 | + * @Description: 宕机检测MBean对应dao类 | ||
12 | + * @Package com.sitech.ismp.check.downtime | ||
13 | + * @ClassName: com.sitech.ismp.check.downtime.DowntimeDao | ||
14 | + * @date 2017年04月16日 14:15 | ||
15 | + */ | ||
16 | +public class DowntimeDao extends BaseDao { | ||
17 | + | ||
18 | + private static Logger log=Logger.getLogger(DowntimeDao.class); | ||
19 | + | ||
20 | + /** | ||
21 | + * 添加需要监控宕机情况的设备,如果该设备已经存在,则修改。 | ||
22 | + */ | ||
23 | + public void addDowntimeHost(HashMap<String,String> params){ | ||
24 | + DowntimeBean downtimeBean=map2DowntimeBean(params); | ||
25 | + addDowntimeHost(downtimeBean); | ||
26 | + } | ||
27 | + | ||
28 | + public void addDowntimeHost(DowntimeBean downtimeBean){ | ||
29 | + try { | ||
30 | + int updateSum = sqlmapClient.update("updateDowntimeHostByIp",downtimeBean); | ||
31 | + if(updateSum == 0){ | ||
32 | + sqlmapClient.insert("addDowntimeHost", downtimeBean); | ||
33 | + } | ||
34 | + } catch (Exception e) { | ||
35 | + error.error("Exception while addDowntimeHost.", e); | ||
36 | + } | ||
37 | + } | ||
38 | + | ||
39 | + | ||
40 | + /** | ||
41 | + * 根据IP删除对应的记录。 | ||
42 | + * @param ip | ||
43 | + * @return | ||
44 | + */ | ||
45 | + public int deleteDowntimeHostByIp(String ip){ | ||
46 | + try { | ||
47 | + return sqlmapClient.delete("deleteDowntimeHostByIp", ip); | ||
48 | + } catch (Exception e) { | ||
49 | + error.error("Exception while deleteDowntimeHostByIp.", e); | ||
50 | + } | ||
51 | + return -1; | ||
52 | + } | ||
53 | + | ||
54 | + /** | ||
55 | + * 根据IP查询对应的记录。 | ||
56 | + * @param deviceIp | ||
57 | + * @return | ||
58 | + */ | ||
59 | + public List<DowntimeBean> getDowntimeByIp(String deviceIp){ | ||
60 | + HashMap<String,String> param=new HashMap<String, String>(); | ||
61 | + param.put("DEVICE_IP",deviceIp); | ||
62 | + List<DowntimeBean> beanList=new ArrayList<DowntimeBean>(); | ||
63 | + try { | ||
64 | + beanList=sqlmapClient.queryForList("getDowntimeByIp", param); | ||
65 | + } catch (SQLException e) { | ||
66 | + e.printStackTrace(); | ||
67 | + } | ||
68 | + return beanList; | ||
69 | + } | ||
70 | + | ||
71 | + public DowntimeBean map2DowntimeBean(HashMap<String,String> params){ | ||
72 | + DowntimeBean bean=new DowntimeBean(); | ||
73 | + try { | ||
74 | + bean.setDEVICE_ID(UUID.randomUUID().toString()); | ||
75 | + bean.setDEVICE_IP(params.get("DEVICE_IP")); | ||
76 | + bean.setDEVICE_ALIAS(params.get("DEVICE_ALIAS")); | ||
77 | + bean.setKBP_CLASS(params.get("KBP_CLASS") + "-99"); | ||
78 | + bean.setPROTOCOL(params.get("PROTOCOL")); | ||
79 | + bean.setPROTOCOL_PORT(Integer.parseInt(params.get("PROTOCOL_PORT"))); | ||
80 | + bean.setUSER_NAME(params.get("USER_NAME")); | ||
81 | + bean.setPASSWORD(params.get("PASSWORD")); | ||
82 | + bean.setCREATE_DATE(new Date()); | ||
83 | + }catch (Exception e){ | ||
84 | + log.error("addDowntimeHost, write values to DowntimeBean has error!",e); | ||
85 | + } | ||
86 | + return bean; | ||
87 | + } | ||
88 | + | ||
89 | +} |
1 | +package com.sitech.ismp.check.downtime; | ||
2 | + | ||
3 | +/** | ||
4 | + * @author frank zmm@honggroup.com.cn | ||
5 | + * @Description: 发送告警的历史记录bean。 | ||
6 | + * @Package com.sitech.ismp.check.downtime | ||
7 | + * @ClassName: com.sitech.ismp.check.downtime.DowntimeHistoryBean | ||
8 | + * @date 2017年04月19日 14:04 | ||
9 | + */ | ||
10 | +public class DowntimeHistoryBean { | ||
11 | + // 设备ID | ||
12 | + private String DEVICE_ID; | ||
13 | + // 设备IP地址 | ||
14 | + private String DEVICE_IP; | ||
15 | + // 创建日期 | ||
16 | + private String CREATE_DATE; | ||
17 | + | ||
18 | + public String getDEVICE_ID() { | ||
19 | + return DEVICE_ID; | ||
20 | + } | ||
21 | + | ||
22 | + public void setDEVICE_ID(String DEVICE_ID) { | ||
23 | + this.DEVICE_ID = DEVICE_ID; | ||
24 | + } | ||
25 | + | ||
26 | + public String getDEVICE_IP() { | ||
27 | + return DEVICE_IP; | ||
28 | + } | ||
29 | + | ||
30 | + public void setDEVICE_IP(String DEVICE_IP) { | ||
31 | + this.DEVICE_IP = DEVICE_IP; | ||
32 | + } | ||
33 | + | ||
34 | + public String getCREATE_DATE() { | ||
35 | + return CREATE_DATE; | ||
36 | + } | ||
37 | + | ||
38 | + public void setCREATE_DATE(String CREATE_DATE) { | ||
39 | + this.CREATE_DATE = CREATE_DATE; | ||
40 | + } | ||
41 | + | ||
42 | + @Override | ||
43 | + public String toString() { | ||
44 | + return "DowntimeHistoryBean{" + | ||
45 | + "DEVICE_ID='" + DEVICE_ID + '\'' + | ||
46 | + ", DEVICE_IP='" + DEVICE_IP + '\'' + | ||
47 | + ", CREATE_DATE='" + CREATE_DATE + '\'' + | ||
48 | + '}'; | ||
49 | + } | ||
50 | +} |
1 | +package com.sitech.ismp.check.downtime; | ||
2 | + | ||
3 | +import com.sitech.database.dao.BaseDao; | ||
4 | +import org.apache.log4j.Logger; | ||
5 | + | ||
6 | +import java.sql.SQLException; | ||
7 | +import java.util.ArrayList; | ||
8 | +import java.util.HashMap; | ||
9 | +import java.util.List; | ||
10 | + | ||
11 | +/** | ||
12 | + * @author frank zmm@honggroup.com.cn | ||
13 | + * @Description: 告警历史dao | ||
14 | + * @Package com.sitech.ismp.check.downtime | ||
15 | + * @ClassName: com.sitech.ismp.check.downtime.DowntimeHistoryDao | ||
16 | + * @date 2017年04月19日 14:06 | ||
17 | + */ | ||
18 | +public class DowntimeHistoryDao extends BaseDao { | ||
19 | + private static Logger log=Logger.getLogger(DowntimeHistoryDao.class); | ||
20 | + | ||
21 | + /** | ||
22 | + * 添加告警信息 | ||
23 | + * @param bean | ||
24 | + */ | ||
25 | + public void addDowntimeHistory(DowntimeHistoryBean bean){ | ||
26 | + try { | ||
27 | + sqlmapClient.insert("addDowntimeHistory", bean); | ||
28 | + } catch (SQLException e) { | ||
29 | + log.error("Exception while addDowntimeHistory.",e); | ||
30 | + } | ||
31 | + } | ||
32 | + | ||
33 | + /** | ||
34 | + * | ||
35 | + * @param deviceIp | ||
36 | + * @param createDate | ||
37 | + * @return | ||
38 | + */ | ||
39 | + public List<DowntimeHistoryBean> getDowntimeHistoryByIp(String deviceIp,String createDate){ | ||
40 | + HashMap<String,String> param=new HashMap<String, String>(); | ||
41 | + param.put("DEVICE_IP",deviceIp); | ||
42 | + param.put("CREATE_DATE",createDate); | ||
43 | + List<DowntimeHistoryBean> beanList=new ArrayList<DowntimeHistoryBean>(); | ||
44 | + try { | ||
45 | + beanList=sqlmapClient.queryForList("getDowntimeHistoryByIp", param); | ||
46 | + } catch (SQLException e) { | ||
47 | + e.printStackTrace(); | ||
48 | + } | ||
49 | + return beanList; | ||
50 | + } | ||
51 | + | ||
52 | + /** | ||
53 | + * 删除表中的所有数据。 | ||
54 | + * @return | ||
55 | + */ | ||
56 | + public int deleteDowntimeHistory(){ | ||
57 | + try { | ||
58 | + return sqlmapClient.delete("deleteDowntimeHistory", null); | ||
59 | + } catch (Exception e) { | ||
60 | + error.error("Exception while deleteDowntimeHistory.", e); | ||
61 | + } | ||
62 | + return -1; | ||
63 | + } | ||
64 | + | ||
65 | +} |
1 | +package com.sitech.ismp.check.downtime; | ||
2 | + | ||
3 | +import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL; | ||
4 | + | ||
5 | +import java.util.HashMap; | ||
6 | +import java.util.Vector; | ||
7 | + | ||
8 | +/** | ||
9 | + * @author frank zmm@honggroup.com.cn | ||
10 | + * @Description: 宕机检测MBean接口 | ||
11 | + * @Package com.sitech.ismp.check.downtime | ||
12 | + * @ClassName: com.sitech.ismp.check.downtime.DowntimeMBean | ||
13 | + * @date 2017年04月15日 13:42 | ||
14 | + */ | ||
15 | +public interface DowntimeMBean { | ||
16 | + /** | ||
17 | + * 检查主机的连接状态。如果连接异常,需要每隔30秒连续连接3次,如果3次连接都异常,则视为主机宕机,发送严重告警。 | ||
18 | + * @param params | ||
19 | + * @return | ||
20 | + */ | ||
21 | + public Vector<TblATO_KPIDETAIL> checkConnectState(HashMap<String,String> params); | ||
22 | + | ||
23 | + | ||
24 | +} |
1 | +package com.sitech.ismp.check.downtime; | ||
2 | + | ||
3 | +import com.sitech.ismp.app.event.KPI2Event; | ||
4 | +import com.sitech.ismp.coll.basic.TblATO_EVENT; | ||
5 | +import com.sitech.util.Base62Util; | ||
6 | +import com.sitech.util.RandomGUID; | ||
7 | +import com.sitech.util.upload.RomoteController; | ||
8 | +import com.sitech.util.upload.SSHThread; | ||
9 | +import com.sitech.util.upload.TelnetThread; | ||
10 | +import org.apache.commons.lang.StringUtils; | ||
11 | +import org.apache.log4j.Logger; | ||
12 | + | ||
13 | +import java.io.BufferedReader; | ||
14 | +import java.io.IOException; | ||
15 | +import java.io.InputStreamReader; | ||
16 | +import java.text.ParseException; | ||
17 | +import java.text.SimpleDateFormat; | ||
18 | +import java.util.Date; | ||
19 | +import java.util.Random; | ||
20 | + | ||
21 | +/** | ||
22 | + * @author frank zmm@honggroup.com.cn | ||
23 | + * @Description: 宕机检测MBean对应Server类。 | ||
24 | + * ping的布尔结果 true:能ping通, false:ping不通 | ||
25 | + * ssh连接的布尔结果 true:能ssh连接, false:ssh连接异常 | ||
26 | + * @Package com.sitech.ismp.check.downtime | ||
27 | + * @ClassName: com.sitech.ismp.check.downtime.DowntimeServer | ||
28 | + * @date 2017年04月12日 16:22 | ||
29 | + */ | ||
30 | +public class DowntimeServer implements Runnable{ | ||
31 | + | ||
32 | + private static Logger log=Logger.getLogger(DowntimeServer.class); | ||
33 | + private DowntimeBean bean; | ||
34 | + DowntimeHistoryDao historyDao=new DowntimeHistoryDao(); | ||
35 | + | ||
36 | + public DowntimeServer(DowntimeBean bean){ | ||
37 | + this.bean=bean; | ||
38 | + } | ||
39 | + | ||
40 | + /** | ||
41 | + * 1 对表中的主机进行ping操作。 | ||
42 | + * 2 对表中的主机进行SSH登录认证。 | ||
43 | + * 3 发送告警信息。 | ||
44 | + */ | ||
45 | + @Override | ||
46 | + public void run(){ | ||
47 | + getPingResult(bean); | ||
48 | + getSshResult(bean); | ||
49 | + sendEvent2Workstation(bean); | ||
50 | + } | ||
51 | + | ||
52 | + /** | ||
53 | + * 将每个IP ping后的结果设置到pingState属性上。 | ||
54 | + * @param bean | ||
55 | + * @return | ||
56 | + */ | ||
57 | + public void getPingResult(DowntimeBean bean){ | ||
58 | + String ipAddr=null; | ||
59 | + log.info("============================== ping all parames ======================"); | ||
60 | + log.info(bean.toString()); | ||
61 | + ipAddr=bean.getDEVICE_IP(); | ||
62 | + boolean temp=pingIpAddress(ipAddr); | ||
63 | + bean.setPingState(temp); | ||
64 | + | ||
65 | + } | ||
66 | + | ||
67 | + /** | ||
68 | + * 进行SSH连接,并设置对应告警的内容。 | ||
69 | + * @param bean | ||
70 | + * @return | ||
71 | + */ | ||
72 | + public void getSshResult(DowntimeBean bean){ | ||
73 | + boolean pingState=bean.isPingState(); // true:能ping通, false:ping不通 | ||
74 | + log.info("========== pingState=== "+pingState+"=========="); | ||
75 | + boolean sshState=contentWithSsh(bean); | ||
76 | + log.info("========== sshState=== "+sshState+"=========="); | ||
77 | + if(!pingState){ // ping 不通 false | ||
78 | + if(!sshState){ // ssh 不能连接 严重告警 | ||
79 | + bean.setWarningLevel(1); | ||
80 | + bean.setWarningResult(bean.getDEVICE_IP()+" 的主机连接异常,请检查网络是否正常或主机宕机!"); | ||
81 | + log.info("========== bean.setWarningResult sshState ="+sshState+",=ip is ["+bean.getDEVICE_IP()+"] host connect error"); | ||
82 | + } | ||
83 | + }else{ //能ping 通 | ||
84 | + if(!sshState){ // ssh 不能连接 重要告警 | ||
85 | + bean.setWarningLevel(2); | ||
86 | + bean.setWarningResult(bean.getDEVICE_IP()+" 的主机不能进行登录,请检查用户名或密码!!"); | ||
87 | + log.info("========== bean.setWarningResult sshState ="+sshState+",=ip is["+bean.getDEVICE_IP()+"] host login error"); | ||
88 | + } | ||
89 | + } | ||
90 | + } | ||
91 | + | ||
92 | + /** | ||
93 | + * 将告警信息发送到Workstation。 | ||
94 | + * 当ping的结果为false的时候才发送。 | ||
95 | + * @param bean | ||
96 | + */ | ||
97 | + private void sendEvent2Workstation(DowntimeBean bean){ | ||
98 | + log.info("******************************************************"); | ||
99 | + log.info("*************** sendEvent2Workstation ****************"); | ||
100 | + log.info("******************************************************"); | ||
101 | + log.info(bean.toString()); | ||
102 | + if(!bean.isSshState()){ | ||
103 | + KPI2Event event=new KPI2Event(); | ||
104 | + TblATO_EVENT tblato_event = new TblATO_EVENT(); | ||
105 | + String eventId= RandomGUID.getRandomGUID(); | ||
106 | + try { | ||
107 | + tblato_event.setEVENT_ID(eventId); // 随即生成一个GUID作为唯一键 | ||
108 | + tblato_event.setUNIT_ID(bean.getUnitId()); // UNIT_ID在页面显示的为平台类型。 | ||
109 | + tblato_event.setKPI_ID("FM-00-01-001-999"); | ||
110 | + tblato_event.setKPI_VALUE(bean.getWarningResult()); | ||
111 | + tblato_event.setEVENT_TITLE(bean.getWarningResult()); | ||
112 | + tblato_event.setEVENT_CLASS(bean.getWarningLevel()); // 告警级别 | ||
113 | + tblato_event.setCLL_TIME(new java.util.Date()); | ||
114 | + tblato_event.setGENERANT_TIME(new java.util.Date()); | ||
115 | + tblato_event.setCFG_GUID(RandomGUID.getRandomGUID()); | ||
116 | + log.info("hava finish set values to TblATO_EVENT!"); | ||
117 | + } catch (ParseException e) { | ||
118 | + log.error("set values to TblATO_EVENT hava error!",e); | ||
119 | + } | ||
120 | + try { | ||
121 | + event.sendEvent2Workstation(tblato_event); | ||
122 | + addDowntimeHistory(bean); | ||
123 | + log.info("have write object(TblATO_EVENT) to Workstation!"); | ||
124 | + } catch (Exception e) { | ||
125 | + log.error("have write object(TblATO_EVENT) to Workstation have error!",e); | ||
126 | + } | ||
127 | + } | ||
128 | + } | ||
129 | + | ||
130 | + | ||
131 | + private void addDowntimeHistory(DowntimeBean bean){ | ||
132 | + DowntimeHistoryBean historyBean=new DowntimeHistoryBean(); | ||
133 | + historyBean.setDEVICE_ID(bean.getDEVICE_ID()); | ||
134 | + historyBean.setDEVICE_IP(bean.getDEVICE_IP()); | ||
135 | + SimpleDateFormat formaF=new SimpleDateFormat("yyyy-MM-dd"); | ||
136 | + String date=formaF.format(new Date())+""; | ||
137 | + date=date.substring(0,10); | ||
138 | + historyBean.setCREATE_DATE(date); | ||
139 | + historyDao.addDowntimeHistory(historyBean); | ||
140 | + } | ||
141 | + | ||
142 | + | ||
143 | + /** | ||
144 | + * 获取ping 后的结果,是否可以平通指定的IP。 | ||
145 | + * @param ipAddr | ||
146 | + * @return | ||
147 | + */ | ||
148 | + private boolean pingIpAddress(String ipAddr){ | ||
149 | + int pingErrorCount=0; // ping 不通的次数,ping3次。 | ||
150 | + int interval=8; // 时间间隔8秒。 | ||
151 | + boolean flag=false; | ||
152 | + boolean temp=false; | ||
153 | + for(int i=0;i<3;i++){ | ||
154 | + temp=executeCommand(ipAddr); // ping不通返回false。 | ||
155 | + log.info("["+ipAddr+"]ping result is ["+temp+"],(true:can connect;false:can't connect)"); | ||
156 | + if(!temp){ | ||
157 | + pingErrorCount++; | ||
158 | + } | ||
159 | + try { | ||
160 | + Thread.sleep(interval*1000); | ||
161 | + log.info("ping ["+ipAddr+"] trread sleep "+interval+" secound........"); | ||
162 | + } catch (InterruptedException e) { | ||
163 | + e.printStackTrace(); | ||
164 | + } | ||
165 | + } | ||
166 | + if(pingErrorCount == 3){ | ||
167 | + log.info("["+ipAddr+"] can't connect 3 times."); | ||
168 | + }else{ | ||
169 | + flag=true; | ||
170 | + log.info("["+ipAddr+"] can connect."); | ||
171 | + } | ||
172 | + return flag; | ||
173 | + } | ||
174 | + | ||
175 | + | ||
176 | + /** | ||
177 | + * 进行ssh连接,判断是否可以连接成功。 | ||
178 | + * windows操作系统使用 TelnetThread类连接。 Windows的客户端需要配置并开启Telnet相关的服务。 | ||
179 | + * @return | ||
180 | + */ | ||
181 | + private boolean contentWithSsh(DowntimeBean bean){ | ||
182 | + String protocol=bean.getPROTOCOL(); | ||
183 | + String ipAddr=bean.getDEVICE_IP(); | ||
184 | + String deviceAlias=bean.getDEVICE_ALIAS(); | ||
185 | + int protocolPort=bean.getPROTOCOL_PORT(); | ||
186 | + String userName=bean.getUSER_NAME(); | ||
187 | + String password=bean.getPASSWORD(); | ||
188 | + bean.setUnitId(getUnitId(deviceAlias)); | ||
189 | + log.info("execute contentWithSsh method ......"); | ||
190 | + boolean temp=false; | ||
191 | + RomoteController tt = null; | ||
192 | + log.info("host params is: ipAddr="+ipAddr+",protocol="+protocol+",protocolPort="+protocolPort+",userName="+userName); | ||
193 | + if(loginParamterIsOk(protocol,ipAddr,protocolPort,userName,password,deviceAlias)){ | ||
194 | + try { | ||
195 | + if (protocol != null && protocol.equalsIgnoreCase("telnet")) { | ||
196 | + tt = new TelnetThread(ipAddr, protocolPort, userName, password); | ||
197 | + } else if (protocol != null && protocol.equalsIgnoreCase("ssh")) { | ||
198 | + tt = new SSHThread(ipAddr, protocolPort, userName, password); | ||
199 | + }else{ | ||
200 | + log.error("===================== unknown protocol ====================="); | ||
201 | + } | ||
202 | + }catch (Exception e){ | ||
203 | + log.error(protocol+"connect have exception!"); | ||
204 | + } | ||
205 | + tt.initial(); | ||
206 | + boolean flag=tt.isAuthorized(); | ||
207 | + log.info("******************* tt.isAuthorized() result is "+flag); | ||
208 | + if (flag){ // 认证通过 | ||
209 | + log.info(ipAddr+" SSH connect,Authorized result is true authorized success!"); | ||
210 | + temp=true; | ||
211 | + bean.setSshState(true); | ||
212 | + }else{ | ||
213 | + log.info(ipAddr+" SSH connect,Authorized result is false authorized fail!!"); | ||
214 | + bean.setSshState(false); | ||
215 | + } | ||
216 | + } | ||
217 | + return temp; | ||
218 | + } | ||
219 | + | ||
220 | + | ||
221 | + /** | ||
222 | + * 判断所有的参数是否为空。 | ||
223 | + * @param protocol 协议类型 | ||
224 | + * @param ipAddr IP地址 | ||
225 | + * @param protocolPort 协议端口 | ||
226 | + * @param userName 用户名 | ||
227 | + * @param password 密码 | ||
228 | + * @param deviceAlias 主机别名 | ||
229 | + * @return | ||
230 | + */ | ||
231 | + private boolean loginParamterIsOk(String protocol,String ipAddr,int protocolPort,String userName, | ||
232 | + String password,String deviceAlias){ | ||
233 | + boolean flag=true; | ||
234 | + if(StringUtils.isEmpty(protocol)){ | ||
235 | + flag=false; | ||
236 | + } | ||
237 | + if(StringUtils.isEmpty(ipAddr)){ | ||
238 | + flag=false; | ||
239 | + } | ||
240 | + if(StringUtils.isEmpty(protocolPort+"")){ | ||
241 | + flag=false; | ||
242 | + } | ||
243 | + if(StringUtils.isEmpty(userName)){ | ||
244 | + flag=false; | ||
245 | + } | ||
246 | + if(StringUtils.isEmpty(password)){ | ||
247 | + flag=false; | ||
248 | + } | ||
249 | + if(StringUtils.isEmpty(deviceAlias)){ | ||
250 | + flag=false; | ||
251 | + } | ||
252 | + return flag; | ||
253 | + } | ||
254 | + | ||
255 | + | ||
256 | + private String getUnitId(String deviceAlias){ | ||
257 | + StringBuffer bf=new StringBuffer("10-10-99-98:"); | ||
258 | + bf.append(deviceAlias).append("_").append(getEncryptStr()); | ||
259 | + return bf.toString(); | ||
260 | + } | ||
261 | + | ||
262 | + /** | ||
263 | + * 返回加密后的一个唯一字符串。 | ||
264 | + * @return | ||
265 | + */ | ||
266 | + private String getEncryptStr(){ | ||
267 | + SimpleDateFormat forma=new SimpleDateFormat("yyMMddHHmmss"); | ||
268 | + Random rr= new Random(); | ||
269 | + String str=rr.nextInt(999)+forma.format(new Date())+rr.nextInt(999); | ||
270 | + return Base62Util.encode(Long.parseLong(str)); | ||
271 | + } | ||
272 | + | ||
273 | + | ||
274 | + /** | ||
275 | + * 返回ping后的结果。 | ||
276 | + * @param ipAddr ip 地址。 | ||
277 | + * @return | ||
278 | + */ | ||
279 | + private boolean executeCommand(String ipAddr){ | ||
280 | + String command = "/bin/ping -c 3 -w 3 " + ipAddr;; | ||
281 | + // 执行command 命令。 | ||
282 | + boolean flag=false; | ||
283 | + String line = null; | ||
284 | + StringBuilder sb = new StringBuilder(); | ||
285 | + Runtime runtime = Runtime.getRuntime(); | ||
286 | + Process process = null; | ||
287 | + long startTime = new Date().getTime(); | ||
288 | + try { | ||
289 | + process = runtime.exec(command); | ||
290 | + } catch (IOException e) { | ||
291 | + log.error("execute command ["+command+"] has some error!"); | ||
292 | + } | ||
293 | + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); | ||
294 | + try { | ||
295 | + while ((line = bufferedReader.readLine()) != null) { | ||
296 | + sb.append(line + "\n"); | ||
297 | + } | ||
298 | + } catch (IOException e) { | ||
299 | + log.error("======== bufferedReader.readLine() has some error!"); | ||
300 | + } | ||
301 | + | ||
302 | + String pingResult=sb.toString(); | ||
303 | + /** | ||
304 | + * 当执行结果出现下面字符说明已经执行完成。 | ||
305 | + * ping 通的时候 结果包含【min/avg/max/mdev】 | ||
306 | + * ping 不通的时候 结果包含【100% packet loss】 | ||
307 | + */ | ||
308 | + if(pingResult.contains("100% packet loss") || (pingResult.contains("min/avg/max/mdev"))){ | ||
309 | + process.destroy(); | ||
310 | + log.info("execute command ["+command+"] finish!"); | ||
311 | + } | ||
312 | + | ||
313 | + | ||
314 | + String os = System.getProperty("os.name").toLowerCase(); | ||
315 | + log.info("------------ operator system is :" + os); | ||
316 | + | ||
317 | + // 解析command命令结果,包含【100% packet loss】 意味着ping不通,返回 false。 | ||
318 | + if(os.indexOf("linux") >= 0){ | ||
319 | + if (!StringUtils.isEmpty(pingResult)) { | ||
320 | + if(!pingResult.contains("100% packet loss")){ | ||
321 | + flag = true; | ||
322 | + } | ||
323 | + } | ||
324 | + } | ||
325 | + return flag; | ||
326 | + } | ||
327 | + | ||
328 | + | ||
329 | + /** | ||
330 | + * 添加已经发送的告警信息。 | ||
331 | + * @param bean,取出对象中的ID与IP,外加时间(年月日) | ||
332 | + */ | ||
333 | + public void addSendedEvent(DowntimeBean bean){ | ||
334 | + | ||
335 | + } | ||
336 | + | ||
337 | +} |
-
Please register or login to post a comment