cheng
2024-01-28 31016f01ec27432295e77d1720b19cd5fd37ce72
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
package com.product.data.center.service;
 
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.data.center.config.CmnConst;
import com.product.data.center.config.ErrorCode;
import com.product.util.BaseUtil;
 
import net.minidev.json.JSONObject;
 
/**
 * 网络监测
 * 
 * @author 86151
 *
 */
@Component
public class NetWorkMonitorService extends AbstractBaseService {
 
    @Autowired
    BaseDao baseDao;
 
    /**
     *    监测IP列表(左侧树)
     * @return
     */
    public DataTableEntity listNetWork() {
        DataTableEntity dtIp = baseDao.listTable("product_sys_network_monitor");
        return dtIp;
    }
 
    /**
     *     监测IP视图(右侧模块)
     * @return
     * @throws IOException 
     * @throws UnknownHostException 
     */
    public List<JSONObject> monitorIp(FieldSetEntity fse) throws UnknownHostException, IOException {
        List<JSONObject> listData=new ArrayList<>();
        String queryIp=fse.getString("ip_address");
        if (!BaseUtil.strIsNull(queryIp)) {
            JSONObject jsonResult=new JSONObject();
            jsonResult.put("ip", queryIp);
            jsonResult.put("status", pingHost(queryIp));
            listData.add(jsonResult);
        }else {
            DataTableEntity dtIp=listNetWork();
            if (!BaseUtil.dataTableIsEmpty(dtIp)) {
                for (int i = 0; i < dtIp.getRows(); i++) {
                    FieldSetEntity fseIp=dtIp.getFieldSetEntity(i);
                    JSONObject jsonResult=new JSONObject();
                    jsonResult.put("ip", fseIp.getString("ip_address"));
                    jsonResult.put("status", pingHost(fseIp.getString("ip_address")));
                    listData.add(jsonResult);
                }
            }
        }
        return listData;
    }
    
    /**
     *     java ping
     * @param ipAddress
     * @return
     * @throws IOException 
     * @throws UnknownHostException 
     */
    public String pingHost(String ipAddress) throws UnknownHostException, IOException {
        int  timeOut =  3000 ;  //超时应该在3钞以上        
        boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut);     // 当返回值是true时,说明host是可用的,false则不可。
        if (status) {
            return "成功";
        }
        return "失败";
    }
    
    /**
     *     监测IP新增
     * @param fseNetWork
     * @return
     */
    public String addNetWork(FieldSetEntity fseNetWork) {
        FieldSetEntity fseExist=baseDao.getFieldSetEntityByFilter("product_sys_network_monitor", "ip_address=?", new Object[] {fseNetWork.getString("ip_address")}, false);
        if (fseExist!=null) {
            throw new BaseException(ErrorCode.NERWORK_HAS_EXIST.getValue(), ErrorCode.NERWORK_HAS_EXIST.getText());
        }
        fseNetWork.setValue(CmnConst.CREATED_UTC_DATETIME, new Date());
        fseNetWork.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
        return baseDao.add(fseNetWork);
    }
    
    /**
     *     监测IP删除
     * @param fseNetWork
     * @return
     */
    public boolean deleteNetWork(String ipAddress) {
        return baseDao.delete("product_sys_network_monitor", "ip_address=?", new Object[] {ipAddress});
    }
}