【发布时间】:2021-05-08 17:04:04
【问题描述】:
我尝试创建简单的监控工具,用于 ping 配置文件中的所有服务。对所有项目同时运行 ping 命令。目前我可以按索引 ping 单个元素,如何为每个人执行此操作?
config/host.yml
default:
server:
port: 3000
hosts:
type: 'CaP AC'
development:
hosts:
type: 'CaP AC'
model: 'RBcAPGi-5acD2nD'
location: 'Office'
list:
- host: '10.10.10.10'
user: 'admin'
password: 'password'
name: 'cap-1'
- host: '10.10.4.5'
user: 'admin'
password: 'password'
name: 'cap-2'
控制器/update.js
const Hosts = require('./hosts');
const Monitor = require('ping-monitor');
module.exports = {
Ping: function (config) {
return new Promise(function (resolve, reject) {
config.hosts.list.forEach(cap => {
let pong = new Monitor({
website: "http://"+config.host,
interval: 1,
config: {
intervalUnits: "seconds"
},
ignoreSSL: true
})
pong.on('up', function (res, state) {
console.log('Service up!');
});
pong.on('error', function (error, res) {
console.error(error);
});
})
})
}
控制器/hosts.js
var hosts = [];
module.exports = hosts;
index.js
const config = yaml_config.load(__dirname + '/config/host.yml');
const Update = require('./controllers/update');
const Hosts = require('./controllers/hosts');
Update.Ping(config).then(result => {
result.forEach(cap => {
console.log(cap +result)
}).catch((err) => {
console.log(err)
});
})
【问题讨论】:
-
你的问题不是很清楚。如果我理解正确,您想用一个 ping-monitor 实例 ping 所有主机。这是不可能的,因为包不支持那种接口。无论哪种方式,包都需要为每个主机发送单独的请求。
标签: javascript node.js arrays list