JFinal結合Sigar、echarts實現後臺服務器監控

這裏寫圖片描述

服務端

    public void cpu() throws SigarException {  
        Sigar sigar = new Sigar();  
        CpuInfo infos[] = sigar.getCpuInfoList();  
        CpuPerc cpuList[] = sigar.getCpuPercList();  
        JSONObject jsonObject = new JSONObject();  
        JSONArray jsonArray = new JSONArray();  
        for (int i = 0, len = infos.length; i < len; i++) {// 不管是單塊CPU還是多CPU都適用  
            CpuInfo info = infos[i];  
            JSONObject jso = new JSONObject();  
            jso.put("mhz", info.getMhz()); //CPU的總量MHz  
            jso.put("company", info.getVendor()); //CPU的廠商  
            jso.put("model", info.getModel()); //CPU型號類別  
            jso.put("cache_size", info.getCacheSize()); // 緩衝緩存數量  
            CpuPerc cpu = cpuList[i];  
            jso.put("freq_user", CpuPerc.format(cpu.getUser())); //CPU的用戶使用率  
            jso.put("freq_sys", CpuPerc.format(cpu.getSys())); //CPU的系統使用率  
            jso.put("freq_wait", CpuPerc.format(cpu.getWait())); //CPU的當前等待率  
            jso.put("freq_nice", CpuPerc.format(cpu.getNice())); //CPU的當前錯誤率  
            jso.put("freq_idle", CpuPerc.format(cpu.getIdle())); //CPU的當前空閒率  
            jso.put("freq_combined", CpuPerc.format(cpu.getCombined())); //CPU總的使用率  
            jsonArray.add(jso);  
        }  
        jsonObject.put("cpu", jsonArray);  
        renderJson(jsonObject);
    }  

    public void jvm() throws UnknownHostException {  
        Runtime r = Runtime.getRuntime();  
        Properties props = System.getProperties();  
        JSONObject jsonObject = new JSONObject();  
        jsonObject.put("jvm_memory_total", r.totalMemory()); //JVM可以使用的總內存  
        jsonObject.put("jvm_memory_free", r.freeMemory()); //JVM可以使用的剩餘內存  
        jsonObject.put("jvm_processor_avaliable", r.availableProcessors()); //JVM可以使用的處理器個數  
        jsonObject.put("jvm_java_version", props.getProperty("java.version")); //Java的運行環境版本  
        jsonObject.put("jvm_java_vendor", props.getProperty("java.vendor")); //Java的運行環境供應商  
        jsonObject.put("jvm_java_home", props.getProperty("java.home")); //Java的安裝路徑  
        jsonObject.put("jvm_java_specification_version", props.getProperty("java.specification.version")); //Java運行時環境規範版本  
        jsonObject.put("jvm_java_class_path", props.getProperty("java.class.path")); //Java的類路徑  
        jsonObject.put("jvm_java_library_path", props.getProperty("java.library.path")); //Java加載庫時搜索的路徑列表  
        jsonObject.put("jvm_java_io_tmpdir", props.getProperty("java.io.tmpdir")); //默認的臨時文件路徑  
        jsonObject.put("jvm_java_ext_dirs", props.getProperty("java.ext.dirs")); //擴展目錄的路徑  
        renderJson(jsonObject);
    }  

    public void memory() throws SigarException {  
        Sigar sigar = new Sigar();  
        Mem mem = sigar.getMem();  
        JSONObject jsonObject = new JSONObject();  
        jsonObject.put("memory_total", mem.getTotal() / (1024 * 1024L));// 內存總量  
        jsonObject.put("memory_used", mem.getUsed() / (1024 * 1024L));// 當前內存使用量  
        jsonObject.put("memory_free", mem.getFree() / (1024 * 1024L));// 當前內存剩餘量  
        Swap swap = sigar.getSwap();  
        jsonObject.put("memory_swap_total", swap.getTotal() / (1024 * 1024L));// 交換區總量  
        jsonObject.put("memory_swap_used", swap.getUsed() / (1024 * 1024L));// 當前交換區使用量  
        jsonObject.put("memory_swap_free", swap.getFree() / (1024 * 1024L));// 當前交換區剩餘量  
        renderJson(jsonObject);
    }  

前端例子,cpu.js

layui.use(['jquery'], function() {
      $ = layui.jquery;

      $(function(){
            var cpu_length=0;
            var date = [];
            var data = [];
            var cpu_echarts=[];
            $.ajax({
                url:"/back/home/cpu",
                type:"get",
                async: false,
                success:function(data){
                cpu_length=data.cpu.length;
                }
            });
             var dd=[];
              for(var j=120;j>0;j--){
                  dd.push(0);
              } 
            for(var i=0;i<cpu_length;i++){
            $("#cpu_show").append('<div id="cpu_'+i+'" class="cpu"></div>');
            cpu_echarts[i]=echarts.init(document.getElementById("cpu_"+i))
            data[i]=dd;
            }
            for(var i=120;i>0;i--){
            date.push(i);
            } 

            var option = {
                animation:false,
                title: {
                    left: 'center',
                    text: 'cpu',
                },

                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: date,
                    axisLabel:{
                        formatter:'{value}s'
                    }
                },
                yAxis: {
                    type: 'value',
                    boundaryGap: false,
                    max:100,
                    axisLabel:{
                        formatter:'{value}%'
                    }
                },
                series: [
                    {
                        name:'cpu數據',
                        type:'line',
                        smooth:true,
                        symbol: 'none',
                        sampling: 'average',
                        itemStyle: {
                            normal: {
                                color: 'rgb(255, 70, 131)'
                            }
                        },
                        areaStyle: {
                            normal: {
                                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                    offset: 0,
                                    color: 'rgb(255, 158, 68)'
                                }, {
                                    offset: 1,
                                    color: 'rgb(255, 70, 131)'
                                }])
                            }
                        },
                        data: []
                    }
                ]
            };
          setInterval(function (){
              $.post("/back/home/cpu",function(result){
                    $("#serverInfo_cpu").empty();
                     $("#serverInfo_cpu").append(JSON.stringify(result));
                     $.each(result.cpu,function(k,v){
                     var ddd=[];
                     for(var i=1;i<120;i++){
                        ddd.push(data[k][i]);
                        }
                     var num=(v.freq_combined).substring(0,(v.freq_combined).length-1);
                      ddd.push(Math.floor(num));
                      data[k]=ddd;
                      option.series[0].data=data[k];
                      option.yAxis.max=100;
                      option.title.text="cpu"+k+"  使用率"+num+"%";
                      cpu_echarts[k].setOption(option);

                     });
                });
        },1000);

        });
});  

附上sigar相關包 免積分下載地址:

http://download.csdn.net/detail/acmjk/9889210

發佈了325 篇原創文章 · 獲贊 143 · 訪問量 61萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章