CiscoTool_v1/netmiko_socket/detectError.js

310 lines
9.4 KiB
JavaScript
Executable File

import fs from "fs";
import zulip from "zulip-js";
import moment from "moment";
import { exec } from "child_process";
import totp from "totp-generator";
import dotenv from "dotenv";
dotenv.config();
const config = {
username: process.env.ZULIP_BOT_EMAIL,
apiKey: process.env.ZULIP_API_KEY,
realm: process.env.ZULIP_SERVER_ADDRESS,
};
let oldMessage = "";
const sendMessageToZulip = (nameServer, error) => {
(async () => {
const client = await zulip(config);
let params = {
type: "stream",
to: process.env.STREAM_ZULIP,
topic: process.env.TOPIC_ZULIP,
content:
"********************" +
nameServer +
"***********************auto.nswteam.net\n```" +
error +
"```",
};
const result = await client.messages.send(params);
})();
};
const sendMessageToZulipNomal = (content, type) => {
(async () => {
const client = await zulip(config);
let params = {
type: "stream",
to: process.env.STREAM_ZULIP,
topic: process.env.TOPIC_ZULIP,
content: type === "help" ? content : "```\n" + content,
};
const result = await client.messages.send(params);
})();
};
try {
fs.watchFile(
"/root/.pm2/logs/api-error.log",
// fs.watchFile("./utils/listIOS.txt",
{
// Specify the use of big integers
// in the Stats object
bigint: false,
// Specify if the process should
// continue as long as file is
// watched
persistent: true,
// Specify the interval between
// each poll the file
interval: 4000,
},
(curr, prev) => {
console.log("\nThe file was edited");
// Show the time when the file was modified
console.log("Previous Modified Time", prev.mtime);
console.log("Current Modified Time", curr.mtime);
// console.log(
// "The contents of the current file are:",
// fs.readFileSync("example_file.txt", "utf8")
// );
console.log("Cấp báo cấp báo");
fs.readFile("/root/.pm2/logs/api-error.log", "utf8", (err, data) => {
if (err) {
console.error(err);
return;
}
// Process the file content
// console.log('File content:', data);
let data1 = data.split("\n");
sendMessageToZulip(
"API SERVER ERROR",
data1.reverse().slice(0, 50).join("\n")
);
});
exec("pm2 restart api", (error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
sendMessageToZulipNomal(stdout);
});
}
);
fs.watchFile(
"/root/.pm2/logs/socket-error.log",
// fs.watchFile("./utils/listIOS.txt",
{
// Specify the use of big integers
// in the Stats object
bigint: false,
// Specify if the process should
// continue as long as file is
// watched
persistent: true,
// Specify the interval between
// each poll the file
interval: 4000,
},
(curr, prev) => {
console.log("\nThe file was edited");
// Show the time when the file was modified
console.log("Previous Modified Time", prev.mtime);
console.log("Current Modified Time", curr.mtime);
// console.log(
// "The contents of the current file are:",
// fs.readFileSync("example_file.txt", "utf8")
// );
console.log("Cấp báo cấp báo");
fs.readFile("/root/.pm2/logs/socket-error.log", "utf8", (err, data) => {
if (err) {
console.error(err);
return;
}
// Process the file content
// console.log('File content:', data);
let data1 = data.split("\n");
sendMessageToZulip(
"SOCKET SERVER ERROR",
data1.reverse().slice(0, 50).join("\n")
);
});
exec("pm2 restart socket", (error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
sendMessageToZulipNomal(stdout);
});
}
);
setInterval(async() => {
const client = await zulip(config);
let check = false;
const readParams = {
anchor: "newest",
num_before: 1,
num_after: 0,
narrow: [{ operator: "topic", operand: process.env.TOPIC_ZULIP }],
};
// const result = await client.messages.send(params);
let a = await client.messages.retrieve(readParams);
let msg = a.messages[0]?.content?.slice(
3,
a.messages[0]?.content.length - 4
);
if (
msg !== oldMessage &&
msg.search("!!") !== -1 &&
a.messages[0]?.sender_email !== "networktool-bot@zulip.ipsupply.com.au"
) {
if (msg === "!!help") {
check = true;
sendMessageToZulipNomal(
"|NUM| COMMAND\n|:---:|---\n|1| :green_large_square: **Start server:** !!start-server *<server name/all>*|\n|2| :red_circle: **Stop server:** !!stop-server *<server name/all>*|\n|3| :counterclockwise: **Restart server:** !!restart-server *<server name/all>*|\n|4| :document: **List server:** !!list-server|\n|5| :gold_record: **Server memory:** !!memory-server|\n|6| :search: **Read file:** !!read-file <File path>|\n|7| :upper_right: **Send command line:** !!command <command> <OTP>|\n|8| :push_pin: **Get OTP:** [](https://totp.danhersam.com/) --> ENTER YOUR SECRET KEY|",
"help"
);
}
if (msg.search("!!restart-server") !== -1) {
if (msg.split(" ")[1] !== "") {
check = true;
exec(
"pm2 restart " + msg.split(" ")[1],
(error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
sendMessageToZulipNomal(stdout, "res");
}
);
}
}
if (msg.search("!!start-server") !== -1) {
if (msg.split(" ")[1] !== "") {
check = true;
exec("pm2 start " + msg.split(" ")[1], (error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
sendMessageToZulipNomal(stdout, "res");
});
}
}
if (msg.search("!!stop-server") !== -1) {
if (msg.split(" ")[1] !== "") {
check = true;
exec("pm2 stop " + msg.split(" ")[1], (error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
sendMessageToZulipNomal(stdout, "res");
});
}
}
if (msg.search("!!list-server") !== -1) {
check = true;
exec("pm2 ls " + msg.split(" ")[1], (error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
sendMessageToZulipNomal(stdout, "res");
});
}
if (msg.search("!!memory-server") !== -1) {
check = true;
exec("df -h", (error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
if (stderr !== "") {
sendMessageToZulipNomal(stderr, "res");
} else {
sendMessageToZulipNomal(stdout, "res");
}
});
}
if (msg.search("!!read-file") !== -1) {
check = true;
exec("cat " + msg.split(" ")[1], (error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
if (stderr !== "") {
sendMessageToZulipNomal(stderr, "res");
} else {
sendMessageToZulipNomal(stdout, "res");
}
});
}
// console.log(oldMessage)
if (msg.search("!!command") !== -1) {
check = true;
if (
totp(process.env.OTP_KEY, {
digits: 6,
period: 60,
}) === msg.slice(msg.length - 6, msg.length).trim()
) {
exec(
msg.slice(msg.search("!!command") + 10, msg.length - 6).trim(),
(error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
if (stderr !== "") {
sendMessageToZulipNomal(stderr, "res");
} else {
sendMessageToZulipNomal(stdout, "res");
}
}
);
} else {
sendMessageToZulipNomal("**OTP invalid**", "help");
}
}
setTimeout(() => {
if (check === false) {
sendMessageToZulipNomal(
":frown: Sorry! I don't get what you mean. Send ***!!help*** to see the commands.",
"help"
);
}
}, 2000);
} else {
if (
msg !== oldMessage &&
a.messages[0]?.sender_email !==
"networktool-bot@zulip.ipsupply.com.au"
) {
sendMessageToZulipNomal(
":frown: Sorry! I don't get what you mean. Send ***!!help*** to see the commands.",
"help"
);
}
}
oldMessage = a.messages[0]?.content?.slice(
3,
a.messages[0]?.content.length - 4
);
}, 10000);
} catch (error) {
console.log(error);
}