监控你的Nodejs应用(NPM包)
发布于 8 年前 作者 ncuzp 5172 次浏览 来自 分享

tinymonit(Go Site)

微信小程序是重点(^.^) Build Status Coverage Status License npm Version

可以搜集系统的CPU、内存、负载等信息,以及系统进程的CPU、内存等相关信息可自由组合,当然也支持集群模式,即如果你有多台机器多个进程仍然可以使用该包收集简单的监控信息来监控你的应用。

使用示例

做了一个简单的微信小程序Demo来显示如何在界面中查看监控信息:

tm-wechat

wechat-tinymonit 代码详见: 点击查看wechat ui代码

特性列表

  • promisify 风格的函数
  • 可以收集系统CPU、内存、负载等信息
  • 可以收集系统中进程的CPU、内存等信息
  • 可以设置阀值告警
  • 支持集群模式

安装

$ npm install tinymonit -g

测试

运行测试:

$ npm run test

运行测试覆盖率:

$ npm run cover

使用

获取系统监控信息

const tm = require('tinymonit');
const osstat = tm.osstat;

Promise.resolve()
.then(() => osstat([8122]))
.then((stat) => console.log(r));

创建被监控进程实例(一般有多个)

const Part = require('tinymonit').part;

let part = new Part(3000, {
	timeout: 100,
	pid: process.pid // this is defalut
});

创建收集监控信息实例(一般只有一个)

const Central = require('tinymonit').central;

let ctl = new Central({
	timeout: 100,
	parts:[
		3000,
		[3001, 200]
		//'[remote ip]:[port]'
	]
});

// start collect performance data from 3000, 3001
Promise.resolve()
.then(() => ctl.collect())
.then((allstats) => {
	// do sth
});

超阀值告警

const tm = require('tinymonit');
const pid = process.pid;
const osstat = tm.osstat;
const alarm = tm.alarm;

Promise.resolve()
.then(() => osstat([pid]))
.then((stat) => {
	console.log(alarm.should_cpu_alarm(rstat, 1));
	console.log(alarm.should_mem_alarm(stat));
	console.log(alarm.should_load_alarm(stat));
	console.log(alarm.should_procs_alarm(stat, 2));
});

License

MIT

回到顶部