-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
227 lines (205 loc) Β· 8.88 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const { Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const axios = require('axios');
const dotenv = require('dotenv');
dotenv.config();
const client = new Client({
puppeteer: {
args: ['--no-sandbox'],
},
authStrategy: new LocalAuth()
});
client.on('qr', (qr) => {
qrcode.generate(qr, { small: true });
});
client.on('ready', () => {
console.log('Client is ready!');
});
client.on('message', async (message) => {
if (message.body === '!check 1') {
const serverUrl = process.env.SERVER_1;
const monitorUrl = `http://${serverUrl}/monitor.php`;
const response = await axios.get(monitorUrl);
const data = response.data;
const reply = `CPU USAGE = ${(data.cpuUsage * 100.0).toFixed(0)}%\nUp time = ${data.uptime}\nram usage = ${data.usedRam}`;
message.reply(reply);
}
if (message.body === '!check 2') {
const serverUrl = process.env.SERVER_2;
const monitorUrl = `http://${serverUrl}/monitor.php`;
const response = await axios.get(monitorUrl);
const data = response.data;
const reply = `CPU USAGE = ${(data.cpuUsage * 100.0).toFixed(0)}%\nUp time = ${data.uptime}\nram usage = ${data.usedRam}`;
message.reply(reply);
}
if (message.body === '!forta-server1') {
const serverUrl = process.env.SERVER_1;
const fortaUrl = `http://${serverUrl}/nit.php`;
const response = await axios.get(fortaUrl);
let reply = `Ip = ${serverUrl}\n`;
const statusList = response.data.split('\n');
for (const status of statusList) {
reply += `${status.trim()}\n`;
}
message.reply(reply);
}
if (message.body === '!forta-server2') {
const serverUrl = process.env.SERVER_2;
const fortaUrl = `http://${serverUrl}/nit.php`;
const response = await axios.get(fortaUrl);
let reply = '';
const statusList = response.data.split('\n');
for (const status of statusList) {
reply += `${status.trim()}\n`;
}
message.reply(reply);
}
if (message.body === '!forta-sla') {
try {
const scannerAddress = process.env.WALLET_1;
const scannerAddress2 = process.env.WALLET_2;
const network = process.env.NETWORK_WALLET_1;
const network2 = process.env.NETWORK_WALLET_2;
const slaUrl = `https://api.forta.network/stats/sla/scanner/${scannerAddress}`;
const slaUrl2 = `https://api.forta.network/stats/sla/scanner/${scannerAddress2}`;
const response = await axios.get(slaUrl);
const response2 = await axios.get(slaUrl2);
const data = response.data;
const data2 = response2.data;
// Determine score emoji based on lowest score
const score1 = data.lowestScores[0].score;
const score2 = data2.lowestScores[0].score;
const scoreEmoji1 = score1 < 0.7 ? 'π΄' : 'π’';
const scoreEmoji2 = score2 < 0.7 ? 'π΄' : 'π’';
// Generate reply string
const reply = `Scanner ID = ${scannerAddress}\n` +
`Network = ${network}\n` +
`SLA = ${scoreEmoji1}${score1}\n`+
`Statistik = min: ${data.statistics.min}, max: ${data.statistics.max}, p50: ${data.statistics.p50}, avg: ${data.statistics.avg}\n\n` +
`Scanner ID = ${scannerAddress2}\n` +
`Network = ${network2}\n` +
`SLA = ${scoreEmoji2}${score2}\n` +
`Statistik = min: ${data2.statistics.min}, max: ${data2.statistics.max}, p50: ${data2.statistics.p50}, avg: ${data2.statistics.avg}\n`;
message.reply(reply);
} catch (error) {
console.error(error);
message.reply('Terjadi kesalahan saat memproses permintaan Anda');
}
}
});
client.initialize();
const sendSLAData = async () => {
try {
const scannerAddress = process.env.WALLET_1;
const scannerAddress2 = process.env.WALLET_2;
const network = process.env.NETWORK_WALLET_1;
const network2 = process.env.NETWORK_WALLET_2;
const slaUrl = `https://api.forta.network/stats/sla/scanner/${scannerAddress}`;
const slaUrl2 = `https://api.forta.network/stats/sla/scanner/${scannerAddress2}`;
const response = await axios.get(slaUrl);
const response2 = await axios.get(slaUrl2);
const data = response.data;
const data2 = response2.data;
// Determine score emoji based on lowest score
const score1 = data.lowestScores[0].score;
const score2 = data2.lowestScores[0].score;
const scoreEmoji1 = score1 < 0.7 ? 'π΄' : 'π’';
const scoreEmoji2 = score2 < 0.7 ? 'π΄' : 'π’';
// Generate reply string
const reply = `Scanner ID = ${scannerAddress}\n` +
`Network = ${network}\n` +
`SLA = ${scoreEmoji1}${score1}\n`+
`Statistik = min: ${data.statistics.min}, max: ${data.statistics.max}, p50: ${data.statistics.p50}, avg: ${data.statistics.avg}\n\n` +
`Scanner ID = ${scannerAddress2}\n` +
`Network = ${network2}\n` +
`SLA = ${scoreEmoji2}${score2}\n` +
`Statistik = min: ${data2.statistics.min}, max: ${data2.statistics.max}, p50: ${data2.statistics.p50}, avg: ${data2.statistics.avg}\n`;
// Send the reply to the user
const chat = await client.getChatById(process.env.YOUR_NUMBER + '@c.us');
chat.sendMessage(reply);
const interval = 2 * 60 * 60 * 1000;
const remainingTime = interval - (new Date().getTime() % interval);
console.log(`Next SLA data check in ${Math.floor(remainingTime / (60 * 1000))} minutes`);
} catch (error) {
console.error(error);
}
};
const SendServerData = async () => {
try {
const serverUrl = process.env.SERVER_1;
const serverUrl2 =process.env.SERVER_2;
const monitorUrl = `http://${serverUrl}/monitor.php`;
const monitorUrl2 = `http://${serverUrl2}/monitor.php`;
const response = await axios.get(monitorUrl);
const response2 = await axios.get(monitorUrl2);
const data = response.data;
const data2 = response2.data;
const reply = `Ip = ${serverUrl} \nCPU USAGE = ${(data.cpuUsage * 100.0).toFixed(0)}%\nUp time = ${data.uptime}\nram usage = ${data.usedRam}\n\nIp = ${serverUrl2} \nCPU USAGE = ${(data2.cpuUsage * 100.0).toFixed(0)}%\nUp time = ${data2.uptime}\nram usage = ${data2.usedRam}\n\n`;
// Send the reply to the user
const chat = await client.getChatById(process.env.YOUR_NUMBER + '@c.us');
chat.sendMessage(reply);
const interval = 2 * 60 * 60 * 1000;
const remainingTime = interval - (new Date().getTime() % interval);
console.log(`Next Server data check in ${Math.floor(remainingTime / (60 * 1000))} minutes`);
} catch (error) {
console.error(error);
}
};
const SendFortaStatus1 = async () => {
try {
const serverUrl = process.env.SERVER_1;
const network = process.env.NETWORK_WALLET_1;
const fortaUrl = `http://${serverUrl}/nit.php`;
const response = await axios.get(fortaUrl);
let reply = `Ip : ${serverUrl}\nNetwork : ${network}\n`;
const statusList = response.data.split('\n');
for (const status of statusList) {
reply += `${status.trim()}\n`;
}
// Send the reply to the user
const chat = await client.getChatById(process.env.YOUR_NUMBER + '@c.us');
chat.sendMessage(reply);
const interval = 2 * 60 * 60 * 1000;
const remainingTime = interval - (new Date().getTime() % interval);
console.log(`Next Forta Status check in ${Math.floor(remainingTime / (60 * 1000))} minutes`);
} catch (error) {
console.error(error);
}
};
const SendFortaStatus2 = async () => {
try {
const serverUrl = process.env.SERVER_2;
const network = process.env.NETWORK_WALLET_2;
const fortaUrl = `http://${serverUrl}/nit.php`;
const response = await axios.get(fortaUrl);
let reply = `Ip : ${serverUrl}\nNetwork : ${network}\n`;
const statusList = response.data.split('\n');
for (const status of statusList) {
reply += `${status.trim()}\n`;
}
// Send the reply to the user
const chat = await client.getChatById(process.env.YOUR_NUMBER + '@c.us');
chat.sendMessage(reply);
} catch (error) {
console.error(error);
}
};
// Send the SLA data every 2 hours
setInterval(SendServerData, 2 * 60 * 60 * 1000);
setInterval(SendFortaStatus1, 2 * 60 * 60 * 1000);
setInterval(SendFortaStatus2, 2 * 60 * 60 * 1000);
setInterval(sendSLAData, 2 * 60 * 60 * 1000);
const interval = 2 * 60 * 60 * 1000;
let remainingTime = interval - (new Date().getTime() % interval);
const countdown = setInterval(() => {
remainingTime -= 1000;
if (remainingTime < 0) {
clearInterval(countdown);
console.log("Data Auto check is now running");
} else {
const seconds = Math.floor((remainingTime / 1000) % 60);
const minutes = Math.floor((remainingTime / (1000 * 60)) % 60);
const hours = Math.floor(remainingTime / (1000 * 60 * 60));
// console.log(`Next Data Auto check in ${hours} hours ${minutes} minutes ${seconds} seconds`);
}
}, 1000);