sar监控服务器-js生成报表

2017-01-01 10:46

shell 脚本如下

#!/bin/bash
if (( $# != 1))
then
    echo "please input sum times:"
    exit 1
fi
sar -u 1 $1 | sed '1,3d'|sed '$d' > sar_cpu_1.txt
sar -r 1 $1 |sed '1,3d'|sed '$d' > sar_men_1.txt
sar -b 1 $1 |sed '1,3d'|sed '$d' > sar_io_1.txt
  • 删掉了第1-3行和最后一行,这样有利于js分析

对txt文件的处理

  • 转换KB
  • 除掉问题数据
function to1024(num){
  return parseFloat(parseFloat(num/1024).toFixed(2));
}

function getNotNullArr(arrT){
  var temp= [];
  for(var i =0; i<arrT.length; i++){
    if(arrT[i]!=""){
    temp.push(arrT[i]);
    }
  }
return temp;
}

js展示效果

$(function () {
            var rtps= [];
            var wtps = [];
            var rsk = [];
            var wsk = [];
            var categories = [];
            var temp2;
            $.get( "txt/sar_io.txt", function( data ) {
                var resourceContent = data.toString(); // can be a global variable too...
                var rc = resourceContent.split("\n");
                for(var i=0; i<rc.length; i++){
                    temp2 = getNotNullArr(rc[i].split(" "));
                    rtps.push(parseFloat(temp2[3]));
                    wtps.push(parseFloat(temp2[4]));
                    rsk.push(to1024(temp2[5]));
                    wsk.push(to1024(temp2[6]));
                    categories.push(temp2[0]);
                }
                set_h_title("硬盘IO使用情况 1");
                set_h_sub_title("test.com");
                set_h_xaxis_setp(2);
                set_h_tooltip("");
                set_h_yaxis_title("IO使用情况");
                set_h_xaxis_categories(categories);
                set_h_series( [{name: '读请求',data:rtps},{name: '写请求',data:wtps},{name: '读KB',data:rsk},{name: '写KB',data:wsk}]);
                highchartsinit("#container",get_h_title(),get_h_sub_title(),get_h_xaxis_setp(),get_h_xaxis_categories(),get_h_yaxis_title(),get_h_tooltip(),get_h_series());
            });
        });

Paste_Image.png

更多功能参考我的源码

更多监控的知识参考:监控中间件