代码改变世界

Nodejs+express 实战,实现系统监控功能

2011-07-18 20:51  依水间  阅读(22858)  评论(4编辑  收藏  举报

nodejs是服务器端js脚本语言, express是nodejs的web框架,通过实现系统监控功能进一步学习nodejs.
主要由四个程序文件实现:server.js启动web服务;routing.js路由解析跳转到功能实现的文件和方法;sysinfo.js实现取当前系统的各项信息指标;sysinfo.html 请求及显示系统信息。
结果如下图:

server.js

/**
 * User: jacky
 * Date: 11-3-30
 * Time: 上午11:11
 * To change this template use File | Settings | File Templates.
 */
var express = require('express');
var app = express.createServer();
var path = require('path');
var fs = require('fs');
var url = require('url');
//configration
//每次启动都会执行的配置
app.configure(function(){
    //console.log('in configure');
    app.use(express.methodOverride());
    app.use(express.bodyParser());
    app.use(app.router);
    app.use(express.logger({ format: ':method :uri' }));
});
//默认方式 $ node ./server.js 为开发模式
app.configure('development', function(){
    //console.log('in configure with development');
    app.use(express.static(__dirname + '/static'));
    //console.log(__dirname + '/static');
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
//以 $NODE_ENV=production node ./server.js 方式运行,即为生产模式
app.configure('production', function(){
  //console.log('in configure with production');
  var oneYear = 31557600000;
  app.use(express.static(__dirname + '/static', { maxAge: oneYear }));
  app.use(express.errorHandler());
});
// routing settings

//重定向静态文件 html,css,js,jpg,png => /static/+req.url
app.all('/*.(html|css|js|jpg|png){1}', function(req, res, next){
    //var static_file_formats = ['.html','.css','.js','.jpg','.png'];
    //console.log(path.extname(req.url));
    var realpath = __dirname + '/static' + url.parse(req.url).pathname;
    //console.log(realpath);
    if(path.existsSync(realpath)){
        res.end(fs.readFileSync(realpath));
    }else{
        res.end('Cannot find request url: '+req.url);
    }
});

//加载配置文件routing.js到变量routings. [/scripts/routing.js]
var routings = require(__dirname + '/scripts/routing.js').routings;
for(var r in routings){
    var pf = require(__dirname + routings[r].file)[routings[r].processFunction];
    if(routings[r].method == 'get')
        app.get(r, pf,function(req, res){});
    else if(routings[r].method == 'post')
        app.post(r,pf);
    else
        app.all(r, pf,function(req, res){});
}

app.listen(8080);
console.log('Server running at http://127.0.0.1:8080/');

routing.js

// the method:[get|post|all], default is all
exports.routings = {
    '/getSysInfo':{method:'get', file:'/scripts/sysinfo.js', processFunction:'get'},
};

sysinfo.js

var os  = require('os');
//过虑掉字符串首尾格式,替换字符串中的多个空格为一个空格
function trim(s){
    return s.replace(/(^\s*)|(\s*$)/g, '').replace(/\s+/g,' ');
}
exports.get = function (req, res, next) {
  //df --total |grep total
  var sysinfo = {'hostname'   : os.hostname(),
                 'systemtype' : os.type(),
                 'release'    : os.release(),
                 'uptime'     : os.uptime(),
                 'loadavg'    : os.loadavg(),
                 'totalmem'   : os.totalmem(),
                 'freemem'    : os.freemem(),
                 'cpus'       : os.cpus(),
                 'disk'       : ''
                };
  var exec = require('child_process').exec;
  exec('df --total |grep total',
      function (error, stdout, stderr) {
        if (error !== null) {
           console.log('exec error: ' + error);
        }else{
           var tmp = trim(stdout).split(' ');
           sysinfo.disk = {total:tmp[1],used:tmp[2],free:tmp[3]};
        }
        res.send(JSON.stringify(sysinfo));
    });
};

sysinfo.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>系统监控</title>
<style type="text/css">
table{
width:600px;
border-collapse:collapse;
border:solid #999;
border-width:1px 1px 1px 1px;
color:#4682B4;
}
table th,table td {border:solid #999;border-width:0 1px 1px 0;padding:2px;}
table td {text-align:center;}
span {color:#8B8989}
.tablecontent {color:#8B8989}
</style>
</head>

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('jquery', '1.5.1');
google.load('visualization', '1', {'packages':['piechart']});
//google.setOnLoadCallback(drawChart);
function drawDiskChart(used,free) {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Space');
data.addColumn('number', 'Space number');
data.addRows([
['未使用'+formatK2GB(free), free],
['已使用'+formatK2GB(used), used]
]);

var chart = new google.visualization.PieChart(document.getElementById('disk_chart_div'));
chart.draw(data, {width: 400, height: 240, is3D: true, title: '磁盘使用情况'});
}
function drawMemChart(used,free) {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Space');
data.addColumn('number', 'Space number');
data.addRows([
['空闲'+formatB2MB(free), free],
['已使用'+formatB2MB(used), used]
]);

var chart = new google.visualization.PieChart(document.getElementById('mem_chart_div'));
chart.draw(data, {width: 400, height: 240, is3D: true, title: '内存使用情况'});
}
</script>
<!--<script type="text/javascript" src="/js/jquery-1.5.1.min.js"></script>-->
<script type="text/javascript">
//过虑掉字符串首尾格式,替换字符串中的多个空格为一个空格。
function trim(s){
return s.replace(/(^\s*)|(\s*$)/g, "").replace(/\s+/g,' ');
}
//转换从字节到兆字节
function formatB2MB(num){
return parseInt(num/(1000*1000))+' MB';
}
//转换从K字节到G字节
function formatK2GB(num){
return parseInt(num/(1000*1000))+' GB';
}
$(document).ready(function(){
//alert($.fn.jquery); //显示jquery版本号
loadSysInfo();
setInterval(loadSysInfo,5000);
});
function loadSysInfo(){
$.ajax({url:"/getSysInfo",
success:function(data){
//alert(data.systemtype);
$('#hostname').html(data.hostname);
$('#systemtype').html(data.systemtype);
$('#systemtype').html(data.systemtype);
$('#release').html(data.release);
$('#uptime').html(parseInt(data.uptime/(60*60))+' Hours');
$('#loadavg').html(data.loadavg.join(','));
$('#totalmem').html(formatB2MB(data.totalmem));
$('#freemem').html(formatB2MB(data.freemem));
$('#cpus').html(data.cpus.length);
$('#disk').html(data.disk.used + ' used,' + data.disk.free +' free');
//填充cup表格
$('.tablecontent').remove();
for(var i in data.cpus){
$('<tr class=\'tablecontent\'><td>cpu'+ i +'</td>' +
'<td>'+ data.cpus[i].model  +'</td>' +
'<td>'+ data.cpus[i].speed  +'</td>' +
'<td>'+ data.cpus[i].times.user  +'</td>' +
'<td>'+ data.cpus[i].times.nice  +'</td>' +
'<td>'+ data.cpus[i].times.sys  +'</td>' +
'<td>'+ data.cpus[i].times.idle  +'</td>' +
'<td>'+ data.cpus[i].times.irq  +'</td>' +
'</tr>').appendTo('#cputable');
}
//图形显示
drawDiskChart(parseInt(data.disk.used),parseInt(data.disk.free));
drawMemChart(data.totalmem - data.freemem, data.freemem);

},
cache:false,
dataType:"json"});
}
</script>
<body>
<strong>@sysinfo.html</strong><br><p>
主机名称:<span id='hostname'></span><br>
操作系统:<span id='systemtype'></span><br>
发行版本:<span id='release'></span><br>
运行时间:<span id='uptime'></span><br>
负载:   <span id='loadavg'></span><br>
内存大小:<span id='totalmem'></span><br>
空闲内存:<span id='freemem'></span><br>
CPUs:  <span id='cpus'></span><br>
硬盘空间:<span id='disk'></span><br>

<table id='cputable'>
<tr><td colspan=8>CPU使用情况</td></tr>
<tr><td>cpu</td>
<td>model</td>
<td>speed</td>
<td>user</td>
<td>nice</td>
<td>sys</td>
<td>idle</td>
<td>irq</td>
</tr>
</table>
<span id='disk_chart_div'></span>
<span id='mem_chart_div'></span>
</body>
</html>