-
Notifications
You must be signed in to change notification settings - Fork 0
/
is-server-alive.js
72 lines (63 loc) · 2.54 KB
/
is-server-alive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
process.env["NTBA_FIX_319"] = 1
const ping = require('ping')
const TelegramBot = require('node-telegram-bot-api');
const chalk = require('chalk')
module.exports = {
isServerAlive: function isServerAlive(config) {
const chatId = config.chatId
const token = config.token
const bot = new TelegramBot(token, {
polling: true
});
const targets = config.targets
const options = config.options
const intervalTimeout = config.intervalTimeout
async function isAlive() {
try {
targets.forEach(function (host) {
ping.promise.probe(host, options).then(function (res) {
if (res.alive == false) sendMessagePromise(host, res)
})
// ping.sys.probe(host, function (isAlive) {
// if (isAlive != true) sendMessage(host, isAlive)
// }, options);
// Add here your code to check if your service is alive, may be using fetch-node or something similar.
})
} catch (err) {
handleFatalError(err)
}
}
setInterval(isAlive, intervalTimeout)
function sendMessagePromise(host, res) {
try {
console.log("The Host: [" + host + "] with IP [" + res.numeric_host + "] is NOT Alive. " + new Date())
bot.sendMessage(chatId, "The Host: [" + host + "] with IP [" + res.numeric_host + "] is NOT Alive. " + new Date())
} catch (err) {
handleFatalError(err)
}
}
function sendMessage(host, isAlive) {
try {
console.log("The Host: [" + host + "] is NOT Alive. " + new Date())
bot.sendMessage(chatId, "The Host: [" + host + "] is NOT Alive. " + new Date())
} catch (err) {
handleFatalError(err)
}
}
function handleFatalError(err) {
console.error(`${chalk.red('[fatal error]')} ${err.message}`)
console.error(err.stack)
}
},
showMyChatId: function showMyChatId(config) {
const token = config
const bot = new TelegramBot(token, {
polling: true
})
bot.on('message', (msg) => {
const chatId = msg.chat.id
console.log('Your ChatID is: ' + chatId)
bot.sendMessage(chatId, 'Your ChatID is: ' + chatId)
})
}
}