Explorar el Código

服務器監控指標采集

demon hace 8 años
padre
commit
2afa5bf5ac

+ 111 - 89
src/main/java/com/yihu/hos/common/SigarUtil.java

@ -9,7 +9,8 @@ import java.text.DecimalFormat;
import java.util.*;
/**
 *  sigar 服务器硬件指标 工具类
 * sigar 服务器硬件指标 工具类
 *
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/10/10.
@ -17,6 +18,7 @@ import java.util.*;
public class SigarUtil {
    public final static Sigar sigar = initSigar();
    private static Sigar initSigar() {
        try {
            //此处只为得到依赖库文件的目录,可根据实际项目自定义
@ -39,7 +41,7 @@ public class SigarUtil {
        }
    }
    public URL getUrl(){
    public URL getUrl() {
        return this.getClass().getResource("/");
    }
@ -53,9 +55,10 @@ public class SigarUtil {
    /**
     * 是否是windows系统
     *
     * @return
     */
    public static boolean isOSWin(){//OS 版本判断
    public static boolean isOSWin() {//OS 版本判断
        String OS = System.getProperty("os.name").toLowerCase();
        if (OS.indexOf("win") >= 0) {
            return true;
@ -66,6 +69,7 @@ public class SigarUtil {
    /**
     * 系统信息  jvm等
     *
     * @throws UnknownHostException
     */
    private static void property() throws UnknownHostException {
@ -117,41 +121,43 @@ public class SigarUtil {
    /**
     * 内存信息
     *
     * @return 返回单位 :G
     * @throws SigarException
     */
    public static Map<String,Object> memory() throws SigarException {
    public static Map<String, Object> memory() throws SigarException {
        Mem mem = sigar.getMem();
        Swap swap = sigar.getSwap();
        Map<String,Object> memory = new HashMap<>();
        memory.put("total",mem.getTotal() / 1024L/ 1024L/ 1024L);
        memory.put("used",mem.getUsed() / 1024L / 1024L/ 1024L);
        memory.put("free",mem.getFree() / 1024L / 1024L/ 1024L);
        memory.put("freePerc",mem.getFreePercent());
        memory.put("usedPerc",mem.getUsedPercent());
        Map<String, Object> memory = new HashMap<>();
        memory.put("total", getDouble((double)mem.getTotal() / 1024L / 1024L / 1024L));
        memory.put("used",  getDouble((double)mem.getUsed() / 1024L / 1024L / 1024L));
        memory.put("free",  getDouble((double)mem.getFree() / 1024L / 1024L / 1024L));
        memory.put("freePerc", getDouble(mem.getFreePercent() / 1024L / 1024L / 1024L));
        memory.put("usedPerc", getDouble(mem.getUsedPercent() / 1024L / 1024L / 1024L));
        //交换区
        memory.put("swapTotal",swap.getTotal() / 1024L / 1024L / 1024L);
        memory.put("swapUsed",swap.getUsed() / 1024L / 1024L / 1024L);
        memory.put("swapFree",swap.getFree() / 1024L / 1024L / 1024L);
        memory.put("swapTotal", getDouble((double)swap.getTotal() / 1024L / 1024L / 1024L));
        memory.put("swapUsed",  getDouble((double)swap.getUsed() / 1024L / 1024L / 1024L));
        memory.put("swapFree", getDouble((double) swap.getFree() / 1024L / 1024L / 1024L));
        return memory;
    }
    /**
     * cpu使用情况
     *
     * @return
     * @throws SigarException
     */
    public static Map<String,Object> cpu() throws SigarException {
        Map<String,Object> cpuMap = new HashMap<>();
    public static Map<String, Object> cpu() throws SigarException {
        Map<String, Object> cpuMap = new HashMap<>();
        CpuInfo infos[] = sigar.getCpuInfoList();
        CpuPerc cpuPerc =  sigar.getCpuPerc();
        CpuPerc cpuPerc = sigar.getCpuPerc();
        CpuInfo info1 = infos[0];
        //cpu信息
        cpuMap.put("quantity",infos.length);
        cpuMap.put("model",info1.getModel());
        cpuMap.put("totalCores",info1.getTotalCores());
        cpuMap.put("totalSockets",info1.getTotalSockets());
        cpuMap.put("Mhz",info1.getMhz());
        cpuMap.put("quantity", infos.length);
        cpuMap.put("model", info1.getModel());
        cpuMap.put("totalCores", info1.getTotalCores());
        cpuMap.put("totalSockets", info1.getTotalSockets());
        cpuMap.put("Mhz", info1.getMhz());
        //cpu使用率
        String totalPerc = String.format("%.2f", cpuPerc.getCombined() * 100);
@ -161,12 +167,12 @@ public class SigarUtil {
        String nice = String.format("%.2f", cpuPerc.getNice() * 100);
        String freePerc = String.format("%.2f", cpuPerc.getIdle() * 100);
        cpuMap.put("totalPerc",Double.parseDouble(totalPerc));
        cpuMap.put("userPerc",Double.parseDouble(userPerc));
        cpuMap.put("sysPerc",Double.parseDouble(sysPerc));
        cpuMap.put("wait",Double.parseDouble(wait));
        cpuMap.put("nice",Double.parseDouble(nice));
        cpuMap.put("freePerc",Double.parseDouble(freePerc));
        cpuMap.put("totalPerc", getDouble(cpuPerc.getCombined() * 100));
        cpuMap.put("userPerc", getDouble(cpuPerc.getUser() * 100));
        cpuMap.put("sysPerc", getDouble(cpuPerc.getSys() * 100));
        cpuMap.put("wait", getDouble(cpuPerc.getWait() * 100));
        cpuMap.put("nice", getDouble(cpuPerc.getNice() * 100));
        cpuMap.put("freePerc", getDouble(cpuPerc.getIdle() * 100));
        return cpuMap;
    }
@ -215,51 +221,51 @@ public class SigarUtil {
    /**
     * 硬盘信息
     */
    public static List<Map<String,Object>> file()  {
        List<Map<String,Object>> files = new ArrayList<>();
        Map<String,Object> fileMap = null;
    public static List<Map<String, Object>> file() {
        List<Map<String, Object>> files = new ArrayList<>();
        Map<String, Object> fileMap = null;
        FileSystem fslist[] = new FileSystem[0];
        try {
            fslist = sigar.getFileSystemList();
        for (int i = 0; i < fslist.length; i++) {
            FileSystem fs = fslist[i];
            fileMap = new HashMap<>();
            for (int i = 0; i < fslist.length; i++) {
                FileSystem fs = fslist[i];
                fileMap = new HashMap<>();
            switch (fs.getType()) {
                case 0: // TYPE_UNKNOWN :未知
                    break;
                case 1: // TYPE_NONE
                    break;
                case 2: // TYPE_LOCAL_DISK : 本地硬盘
                    FileSystemUsage usage  = sigar.getFileSystemUsage(fs.getDirName());
                    fileMap.put("devName",fs.getDevName());
                    fileMap.put("dirName",fs.getDirName());
                    fileMap.put("flags",fs.getFlags());
                    fileMap.put("sysTypeName",fs.getSysTypeName());
                    fileMap.put("typeName",fs.getTypeName());
                    fileMap.put("type",fs.getType());
                switch (fs.getType()) {
                    case 0: // TYPE_UNKNOWN :未知
                        break;
                    case 1: // TYPE_NONE
                        break;
                    case 2: // TYPE_LOCAL_DISK : 本地硬盘
                        FileSystemUsage usage = sigar.getFileSystemUsage(fs.getDirName());
                        fileMap.put("devName", fs.getDevName());
                        fileMap.put("dirName", fs.getDirName());
                        fileMap.put("flags", fs.getFlags());
                        fileMap.put("sysTypeName", fs.getSysTypeName());
                        fileMap.put("typeName", fs.getTypeName());
                        fileMap.put("type", fs.getType());
                    fileMap.put("total",usage.getTotal()  / 1024L / 1024L);
                    fileMap.put("free",usage.getFree() / 1024L / 1024L );
                    fileMap.put("aVall",usage.getAvail() / 1024L / 1024L );//可用大小
                    fileMap.put("used",usage.getUsed() / 1024L / 1024L );
                    fileMap.put("usePercent", usage.getUsePercent() * 100D );
                        fileMap.put("total", usage.getTotal() / 1024L / 1024L);
                        fileMap.put("free", usage.getFree() / 1024L / 1024L);
                        fileMap.put("aVall", usage.getAvail() / 1024L / 1024L);//可用大小
                        fileMap.put("used", usage.getUsed() / 1024L / 1024L);
                        fileMap.put("usePercent", usage.getUsePercent() * 100D);
                    fileMap.put("diskRead", usage.getDiskReads() );
                    fileMap.put("diskWrite", usage.getDiskWrites() );
                    files.add(fileMap);
                    break;
                case 3:// TYPE_NETWORK :网络
                    break;
                case 4:// TYPE_RAM_DISK :闪存
                    break;
                case 5:// TYPE_CDROM :光驱
                    break;
                case 6:// TYPE_SWAP :页面交换
                    break;
            }
                        fileMap.put("diskRead", usage.getDiskReads());
                        fileMap.put("diskWrite", usage.getDiskWrites());
                        files.add(fileMap);
                        break;
                    case 3:// TYPE_NETWORK :网络
                        break;
                    case 4:// TYPE_RAM_DISK :闪存
                        break;
                    case 5:// TYPE_CDROM :光驱
                        break;
                    case 6:// TYPE_SWAP :页面交换
                        break;
                }
        }
            }
        } catch (SigarException e) {
            e.printStackTrace();
        }
@ -268,24 +274,29 @@ public class SigarUtil {
    /**
     * 网络信息
     *
     * @throws Exception
     */
    public static Map<String,Object> net() throws Exception {
        List<Map<String,Object>> netList = new ArrayList<>();
        Map<String,Object> netMap = new HashMap<>();
        NetInterfaceConfig netConfig = sigar.getNetInterfaceConfig();
        NetInterfaceStat netIfStat = sigar.getNetInterfaceStat(netConfig.getName());
        Map<String,Object> bps = populate(netConfig.getName());//bps
        netMap.put("address",netConfig.getAddress());
        netMap.put("broadcast",netConfig.getBroadcast());
        netMap.put("netmask",netConfig.getNetmask());
        netMap.put("rxPackets",netIfStat.getRxPackets());
        netMap.put("txPackets",netIfStat.getTxPackets());
        netMap.put("rxBytes",netIfStat.getRxBytes());
        netMap.put("txBytes",netIfStat.getTxBytes());
        netMap.put("txPackets",netIfStat.getTxPackets());
        netMap.put("txbps",bps.get("txbps"));
        netMap.put("rxbps",bps.get("rxbps"));
    public static Map<String, Object> net() {
        Map<String, Object> netMap = new HashMap<>();
        NetInterfaceConfig netConfig = null;
        try {
            netConfig = sigar.getNetInterfaceConfig();
            NetInterfaceStat netIfStat = sigar.getNetInterfaceStat(netConfig.getName());
            Map<String, Object> bps = populate(netConfig.getName());//bps
            netMap.put("address", netConfig.getAddress());
            netMap.put("broadcast", netConfig.getBroadcast());
            netMap.put("netmask", netConfig.getNetmask());
            netMap.put("rxPackets", netIfStat.getRxPackets());
            netMap.put("txPackets", netIfStat.getTxPackets());
            netMap.put("rxBytes", netIfStat.getRxBytes());
            netMap.put("txBytes", netIfStat.getTxBytes());
            netMap.put("txPackets", netIfStat.getTxPackets());
            netMap.put("txbps", bps.get("txbps"));
            netMap.put("rxbps", bps.get("rxbps"));
        } catch (SigarException e) {
            e.printStackTrace();
        }
        return netMap;
    }
@ -305,17 +316,18 @@ public class SigarUtil {
            System.out.println(cfg.getName() + "网卡类型" + cfg.getType());//
     }
        }
    }
    /**
     * 網絡傳輸速率 bps
     *
     * @param name
     * @throws SigarException
     */
    public static Map<String,Object> populate(String name)
    public static Map<String, Object> populate(String name)
            throws SigarException {
        Map<String,Object> bps = new HashMap<>();
        Map<String, Object> bps = new HashMap<>();
        try {
            long start = System.currentTimeMillis();
            NetInterfaceStat statStart = sigar.getNetInterfaceStat(name);
@ -326,15 +338,25 @@ public class SigarUtil {
            NetInterfaceStat statEnd = sigar.getNetInterfaceStat(name);
            long rxBytesEnd = statEnd.getRxBytes();
            long txBytesEnd = statEnd.getTxBytes();
            long rxbps = ((rxBytesEnd - rxBytesStart)*8/1024) / ((end-start)/1000);
            long txbps = ((txBytesEnd - txBytesStart)*8/1024) / ((end-start)/1000);
            bps.put("rxbps",rxbps);
            bps.put("txbps",txbps);
            System.out.println("#######rxbps: "+rxbps +" txbps: "+txbps);
            long rxbps = ((rxBytesEnd - rxBytesStart) * 8 / 1024) / ((end - start) / 1000);
            long txbps = ((txBytesEnd - txBytesStart) * 8 / 1024) / ((end - start) / 1000);
            bps.put("rxbps", rxbps);
            bps.put("txbps", txbps);
            System.out.println("#######rxbps: " + rxbps + " txbps: " + txbps);
        } catch (SigarException e) {
        } catch (Exception e) {
        }
        return bps;
    }
    /**
     * 获取两位小数的double值
     * @param value
     * @return
     */
    public static double getDouble(double value){
        String total = String.format("%.2f", value );
        return Double.parseDouble(total);
    }
}

+ 80 - 0
src/main/java/com/yihu/hos/scheduler/EnvScheduler.java

@ -0,0 +1,80 @@
package com.yihu.hos.scheduler;
import com.yihu.hos.common.MongodbUtil;
import com.yihu.hos.common.SigarUtil;
import com.yihu.hos.core.datatype.DateUtil;
import com.yihu.hos.core.log.Logger;
import com.yihu.hos.core.log.LoggerFactory;
import com.yihu.hos.crawler.service.DataCollectDispatcher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;
/**
 *  服务器性能数据采集定时器
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/10/11.
 */
@Component
public class EnvScheduler {
    private static Logger logger = LoggerFactory.getLogger(EnvScheduler.class);
    private static DataCollectDispatcher dispatch = DataCollectDispatcher.getInstance();
    @Scheduled(cron="0 0/1 * * * ?") //每分钟执行一次
    public void statusCheck() {
        System.out.println("每分钟执行一次。开始============================================");
        //TODO 采集服务器健康监控指标数据 statusTask.healthCheck();
        collectEnvHealth();
        System.out.println("每分钟执行一次。结束。");
    }
    public String collectEnvHealth(){
        MongodbUtil monoEnv = new MongodbUtil("envHealth");
        try {
            JSONObject result = new JSONObject();
            result.put("create_date", DateUtil.getCurrentString(DateUtil.DEFAULT_YMDHMSDATE_FORMAT));
            //cpu
            JSONObject cpu = JSONObject.fromObject( SigarUtil.cpu());
            result.put("data",cpu);
            monoEnv.insert("cpu",result);
            //内存
            JSONObject memory = JSONObject.fromObject( SigarUtil.memory());
            result.put("data",memory);
            monoEnv.insert("memory",result);
            //硬盘
            List<JSONObject> files = JSONArray.fromObject( SigarUtil.file());
            result.put("data",files);
            monoEnv.insert("files", result);
            //网络
            JSONObject net = JSONObject.fromObject( SigarUtil.net());
            result.put("data",net);
            monoEnv.insert("net",result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
//    @Scheduled(fixedRate=10000)
//    public void testTasks() {
//        System.out.println("每10秒执行一次。开始……");
//        //statusTask.healthCheck();
//        System.out.println("每10秒执行一次。结束。");
//    }
}

+ 86 - 0
src/main/java/com/yihu/hos/server/controller/ServerMetricsController.java

@ -0,0 +1,86 @@
package com.yihu.hos.server.controller;
import com.yihu.hos.server.service.ServerMetricsService;
import com.yihu.hos.web.framework.model.ActionResult;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.hyperic.sigar.SigarException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
 *  服务器健康监控
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/10/14.
 */
@RestController("ServerMetricsController")
@RequestMapping("/serverMetrics")
public class ServerMetricsController {
    @Autowired
    private ServerMetricsService serverMetricsService;
    @RequestMapping(value = "/searchHistory", method = RequestMethod.GET)
    @ApiOperation(value = "获取服务器历史监控数据", response = ActionResult.class, produces = "application/json", notes = "获取服务器历史监控数据")
    public ActionResult getHistory(
            @ApiParam(name = "type", value = "类型")
            @RequestParam(value = "type") String type,
            @ApiParam(name = "beginTime", value = "开始时间")
            @RequestParam(value = "beginTime", required = false) String beginTime,
            @ApiParam(name = "endTime", value = "结束时间")
            @RequestParam(value = "endTime", required = false) String endTime) {
        try {
            JSONArray data  = serverMetricsService.getHistory(type, beginTime, endTime);
            ActionResult result = new ActionResult(true,"查询成功");
            result.setData(data);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        ActionResult result = new ActionResult(false,"查询异常");
        return result;
    }
    @RequestMapping(value = "/searchCurrent", method = RequestMethod.GET)
    @ApiOperation(value = "获取服务器当前健康指标", response = ActionResult.class, produces = "application/json", notes = "获取服务器当前健康指标")
    public ActionResult searchCurrent(
            @ApiParam(name = "type", value = "类型")
            @RequestParam(value = "type") String type) {
        JSONObject data = null;
        try {
            data = serverMetricsService.getCurrent(type);
            ActionResult result = new ActionResult(true,"查询成功");
            result.setData(data);
            return result;
        } catch (SigarException e) {
            e.printStackTrace();
        }
        ActionResult result = new ActionResult(false,"查询异常");
        return result;
    }
    @RequestMapping(value = "/searchHardware", method = RequestMethod.GET)
    @ApiOperation(value = "获取服务器硬件信息", response = ActionResult.class, produces = "application/json", notes = "获取服务器硬件信息")
    public ActionResult searchHardware(
            @ApiParam(name = "type", value = "类型")
            @RequestParam(value = "type") String type) {
        //TODO 可以和上一個接口可共用
        return null;
    }
}

+ 107 - 0
src/main/java/com/yihu/hos/server/service/ServerMetricsService.java

@ -0,0 +1,107 @@
package com.yihu.hos.server.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.ehr.dbhelper.common.DBList;
import com.yihu.ehr.dbhelper.common.MongodbQuery;
import com.yihu.ehr.dbhelper.common.QueryCondition;
import com.yihu.ehr.dbhelper.common.QueryEntity;
import com.yihu.hos.common.SigarUtil;
import com.yihu.hos.crawler.model.transform.EhrCondition;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.hyperic.sigar.SigarException;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
 * 服务器指标 业务类
 *
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/10/14.
 */
@Component
public class ServerMetricsService {
    /**
     * 获取历史服务器监控指标
     *
     * @param type  监控类型(cpu,内存等)
     * @param begin 开始时间
     * @param end   结束时间
     * @return
     */
    public JSONArray getHistory(String type, String begin, String end) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        JSONArray result = new JSONArray();
        List<EhrCondition> queryParams = new ArrayList<>();
        queryParams.add(new EhrCondition(" > ", "create_date", begin));
        queryParams.add(new EhrCondition(" < ", "create_date", end));
        result = collectionData(type, objectMapper.writeValueAsString(queryParams));
        return result;
    }
    /**
     * mongodb 查询方法
     *
     * @param tableCode
     * @param condition
     * @return
     * @throws Exception
     */
    public JSONArray collectionData(String tableCode, String condition) throws Exception {
        JSONArray result = new JSONArray();
        try {
            MongodbQuery mdb = new MongodbQuery("envHealth");//TODO 提取到配置文件中
            QueryEntity qe = new QueryEntity(tableCode);
            //设置参数
            if (!StringUtils.isEmpty(condition) && !"{}".equals(condition)) {
                JSONArray ar = JSONArray.fromObject(condition);
                for (int i = 0; i < ar.size(); i++) {
                    JSONObject jo = (JSONObject) ar.get(i);
                    String andOr = String.valueOf(jo.get("andOr"));
                    String field = String.valueOf(jo.get("field"));
                    String cond = String.valueOf(jo.get("condition"));
                    String value = String.valueOf(jo.get("value"));
                    qe.addCondition(new QueryCondition(andOr, cond, field, value));
                }
            }
            DBList list = mdb.query(qe);
            result = JSONArray.fromObject(list.getList().toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 获取当前 服务器监控指标
     * @param type
     * @return
     * @throws SigarException
     */
    public JSONObject getCurrent(String type) throws SigarException {
        JSONObject result = new JSONObject();
        switch (type) {
            case "cpu" : // cpu
                result = JSONObject.fromObject(SigarUtil.cpu());
                break;
            case "memory" : // 内存
                result = JSONObject.fromObject(SigarUtil.memory());
                break;
            case "files" : // 硬盘
                result = JSONObject.fromObject(SigarUtil.file());
                break;
            case "net" :// 网络
                result = JSONObject.fromObject(SigarUtil.net());
                break;
        }
        return result;
    }
}

+ 22 - 0
src/main/resources/banner.txt

@ -0,0 +1,22 @@
                                            _ooOoo_
                                           o8888888o
                                           88" . "88
                                           (| -_- |)
                                            O\ = /O
                                        ____/`---'\____
                                      .   ' \\| |// `.
                                       / \\||| : |||// \
                                     / _||||| -:- |||||- \
                                       | | \\\ - /// | |
                                     | \_| ''\---/'' | |
                                      \ .-\__ `-` ___/-. /
                                   ___`. .' /--.--\ `. . __
                                ."" '< `.___\_<|>_/___.' >'"".
                               | | : `- \`.;`\ _ /`;.`/ - ` : | |
                                 \ \ `-. \_ __\ /__ _/ .-` / /
                         ======`-.____`-.___\_____/___.-`____.-'======
                                            `=---='
                         .............................................
                         佛祖保佑                               永无BUG
**************************spring-boot.version: ${spring-boot.version}*****************************