Nodejs+expressは実戦でシステムモニタ機能を実現します.
9416 ワード
nodejsはサーバー端末jsスクリプト言語であり、expressはnodejsのウェブフレームであり、システム監視機能を実現することによってさらにnodejsを学習します.主に4つのプログラムファイルで実現します.server.jsはウェブサービスを開始します.routingn.jsルーティング解析は、機能が実現するファイルと方法にジャンプします.sysinfo.jsは現在のシステムの各種情報指標を取得することを実現する.システム情報の要求および表示.結果は下図の通りです.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/');
routingn.js// the method:[get|post|all], default is all
exports.routings = {
'/getSysInfo':{method:'get', file:'/scripts/sysinfo.js', processFunction:'get'},
};
sysinfo.jsvar 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.<!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>