ping IP地址

package com.xd.monitor.service;

import com.xd.monitor.entity.Equipment;
import com.xd.monitor.entity.EquipmentFailure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Component
public class PingService {

    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private EquipmentFailureService equipmentFailureService;

    @Async
    public void ping(String ip, String eq_id) {//传入IP地址和设备ID,以便更新设备状态
        Runtime runtime = Runtime.getRuntime();
        Equipment equipment1 = new Equipment();
        Process process;
        try {
            process = runtime.exec("ping " + ip); //ping IP
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "gb2312"); //字符编码,避免乱码
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            StringBuffer sb = new StringBuffer();
            while ((line = br.readLine()) != null) {
                sb.append(line);  //获取ping IP的结果
            }
            int status_key = getCheckResult(sb);  //根据结果判定是否ping的通

            if (status_key == 1) {  

                        //插入数据库
                        equipment1.setEq_state_key(status_key);
                        equipment1.setEq_id(eq_id);
                        equipmentService.updateEquipment(equipment1);
                        EquipmentFailure eq_failure = new EquipmentFailure();
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                        String times = df.format(new Date());
                        eq_failure.setPk_equipment_failure(getPK(""));
                        eq_failure.setEq_id(eq_id);
                        eq_failure.setEq_state_key(status_key);
                        eq_failure.setFa_time(times);
                        eq_failure.setFa_description(sb.toString());
                        eq_failure.setFa_accepter("Admin");
                        eq_failure.setIp_address(ip);
                        EquipmentFailure ts=equipmentFailureService.getFailureid(eq_id); //判定故障表中是否有该设备的信息,没有的话插入,有的话不插入,避免重复插入
                        if(ts == null){
                            equipmentFailureService.insertEquipmentFailure(eq_failure);
                        }
                }
            is.close();
            isr.close();
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private int getCheckResult(StringBuffer sb) {  // System.out.println("控制台输出的结果为:"+line);
        Pattern pattern = Pattern.compile("(\\d+ms)(\\s+)(TTL=\\d+)",    Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(sb.toString());
        while (matcher.find()) {
            return 0;
        }
        return 1;
    }

//生成故障表的主键,20个字符长度
    private static Long timeMillis=null;
    public static String getPK(String prefix){
        if(prefix==null)
            prefix="";
        if(timeMillis==null)
            timeMillis=System.currentTimeMillis()*10;

        long i=timeMillis++;
        StringBuffer v=new StringBuffer("00000000000000000000");
        while(i>0){
            long t=i%36;
            i=i/36;
            if(t<10)
                v.insert(20,(String.valueOf(t)));
            else
                v.insert(20,(char)('A'+t-10));
        }
        return prefix+v.substring(v.length()-20+prefix.length(), v.length());
    }
}







你可能感兴趣的:(ping,IP)