用nodejs和IgniteUI监控cpu和内存变化
css代码 [code="css"] #chart { width: 700px; height: 500px; float: left; } #legend { float: left; } [/code] html代码 [code="html"] cpu和内存使用监控
[/code] chart.js [code="javascript"] $(function () { var cpuData = []; function toDisplayCPU(v) { return v.toFixed(2); } function toDisplayMem(v) { if (v >= (1024 * 1024 * 1024)) { v /= (1024 * 1024 * 1024); return v.toFixed(2) + "GB"; } if (v >= (1024 * 1024)) { v /= (1024 * 1024); return v.toFixed(2) + "MB"; } if (v >= (1024)) { v /= (1024); return v.toFixed(2) + "KB"; } return v; } function renderChart() { var chartOptions = { dataSource: cpuData, width: "700px", height: "500px", title: "System Performance", subtitle: "CPU utilization over time until present", horizontalZoomable: true, verticalZoomable: true, rightMargin: 30, legend: { element: "legend" }, axes: [{ type: "categoryX", name: "xAxis", label: "displayTime", labelAngle: 45 }, { type: "numericY", name: "yAxis", title: "CPU Utilization", minimumValue: 0, maximumValue: 100, formatLabel: toDisplayCPU }, { type: "numericY", name: "yAxisMemory", title: "Memory Utilization", labelLocation: "outsideRight", minimumValue: 0, maximumValue: 8 * 1024 * 1024 * 1024, interval: 1024 * 1024 * 1024, formatLabel: toDisplayMem, majorStroke: "transparent" }], series: [{ name: "cpu", type: "line", xAxis: "xAxis", yAxis: "yAxis", valueMemberPath: "cpuUsage", showTooltip: true, tooltipTemplate: "
CPU: ${item.displayCPU}
", title: "CPU Utilization" }, { name: "mem", type: "line", xAxis: "xAxis", yAxis: "yAxisMemory", valueMemberPath: "usedMem", showTooltip: true, tooltipTemplate: "
Memory: ${item.displayMem}
", title: "Memory Utilization" }, { name: "itemToolTips", type: "itemToolTipLayer", useInterpolation: false, transitionDuration: 300 }] }; $("#chart").igDataChart(chartOptions); } renderChart(); var socket = io.connect("http://localhost:8080"); socket.on("cpuUpdate", function (update) { var currTime = new Date(); var displayString = currTime.toLocaleTimeString(); update.displayCPU = toDisplayCPU(update.cpuUsage); update.displayMem = toDisplayMem(update.usedMem); update.displayTime = displayString; cpuData.push(update); $("#chart").igDataChart("notifyInsertItem", cpuData, cpuData.length - 1, update); }); }); [/code] 后台用nodejs、express和socket.io获取系统的cpu和内存使用 [code="JavaScript"] var express = require("express"); var app = express(); var server = require("http").createServer(app); var io = require("socket.io").listen(server); var os = require("os"); var osUtils = require("os-utils"); var interval = -1; var currCPU = 0; app.use(express.static(__dirname)); server.listen(8080); io.sockets.on('connection', function () {//连接事件 if (interval < 0) { interval = setInterval(function () { var freeMem = os.freemem(); var totalMem = os.totalmem(); io.sockets.emit("cpuUpdate", { cpuUsage: currCPU * 100.0, freeMem: freeMem, totalMem: totalMem, usedMem: totalMem - freeMem }); }, 10);//每隔10ms取系统数据 } }); function updateCPU() { setTimeout(function () { osUtils.cpuUsage(function (value) { currCPU = value; updateCPU(); }); }, 0); } updateCPU(); console.log("服务器已启动"); [/code] 启动server [img]http://dl2.iteye.com/upload/attachment/0109/5223/73621274-8790-3405-96de-ad67b2d43ad3.jpg[/img] 效果如下: [img]http://dl2.iteye.com/upload/attachment/0109/5225/fe7aa99d-5de3-3a6b-af48-71ab5a221e54.jpg[/img]