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 monitorIp(FieldSetEntity fse) throws UnknownHostException, IOException { List 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}); } }