CiscoTool_v1/netmiko_Api/utils/manualTest/ASAManualTest.js

343 lines
11 KiB
JavaScript

const net = require("net");
const fs = require("fs");
const { io } = require("socket.io-client");
const Device = require("../../models/DeviceModel");
const moment = require("moment");
const { getInfoLog } = require("../autoTest/getInfoLog");
const { detectFaulty } = require("../autoTest/detectFaulty");
const { checkPID } = require("../autoTest/checkPID");
const spawn = require("child_process").spawn;
const ASA_manual_test = async (client, date, time, body) => {
const { infoStation, listLine, stream, topic, valueConfirmPhysical } = body;
let checkTest = false;
let output = "";
let command = [
"show inventory", "show version", "show post", "show logging", "show power", "show diag",
];
try {
console.log("commnad ", command);
const socket1 = new net.Socket();
socket1.connect(infoStation.sta_port_telnet, infoStation.sta_ip, () => {
console.log(
`Connected to ${infoStation.sta_ip}:${infoStation.sta_port_telnet}`
);
socket1.setEncoding("utf-8");
socket1.write("clear line " + listLine.line_clear + "\n");
socket1.write("\n");
socket1.write("\n");
socket1.write("p" + listLine.line_number);
socket1.write("\n");
socket1.write("\n");
setTimeout(() => {
socket1.write("enable\n");
socket1.write("\n");
socket1.write("\n");
socket1.write("\n");
}, 6000);
});
for (let i = 0; i < command.length; i++) {
setTimeout(() => {
socket1.write(command[i] + "\n");
}, 8000);
}
let temp = String.fromCodePoint(8);
const pattern = new RegExp(`${temp} ${temp}`, "g");
const pattern1 = new RegExp(`${temp}`, "g");
socket1.on("data", (data) => {
data = data.toString();
output =
output +
data
.replace(/# sh/g, "###sh").replace(/\0/g, '')
.replace(pattern, "")
.replace(pattern1, "")
.replace(/ --More-- /g, "");
// console.log("output ",data);
if (data.search("-->") !== -1) {
setTimeout(() => {
socket1.write(" \n");
}, 2000);
}
if (data.search("refused") !== -1) {
setTimeout(() => {
socket1.write("p" + listLine.line_number);
socket1.write("\n");
socket1.write("\n");
socket1.write("\n");
socket1.write("\n");
}, 2000);
}
});
setTimeout(async () => {
if (output.search("Error") === -1) {
checkTest = true;
let PID = output
.split("\n")
.filter(
(i) =>
i.search("PID:") !== -1 &&
i.search("VID:") !== -1 &&
i.search("SN:") !== -1
)[0]
?.split("VID:")[0]
?.split(":")[1]
.replace(/,/g, "")
?.trim();
let VID = output
.split("\n")
.filter(
(i) =>
i.search("PID:") !== -1 &&
i.search("VID:") !== -1 &&
i.search("SN:") !== -1
)[0]
?.split("VID:")[1]
?.split("SN:")[0]
.replace(",", "")
.trim();
let SN = output
.split("\n")
.filter(
(i) =>
i.search("PID:") !== -1 &&
i.search("VID:") !== -1 &&
i.search("SN:") !== -1
)[0]
?.split("SN:")[1]
?.trim();
fs.writeFile(time, output, async function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line good " + listLine.line_number);
});
if (output.length > 30) {
// console.log(output)
await Device.addLog(
listLine.id_line,
time,
"DPELP",
date,
"manual",
PID !== undefined ? PID : "",
SN !== undefined ? SN : ""
);
if (
valueConfirmPhysical !== "" &&
valueConfirmPhysical.option !== null
) {
let testLog = "";
setTimeout(async () => {
let res = await Device.getLogByCreatedAt(date);
if (res.pData[0] !== undefined) {
if (valueConfirmPhysical.option === "Issue") {
testLog = testLog + "Issue:\n" + valueConfirmPhysical.value;
await Device.changeCommand(
res.pData[0].id_log,
valueConfirmPhysical.option +
": " +
valueConfirmPhysical.value
);
} else {
await Device.changeCommand(
res.pData[0].id_log,
valueConfirmPhysical.option
);
testLog = testLog + valueConfirmPhysical.option;
}
}
await sendNoteToERP(
PID,
VID,
SN,
testLog,
infoStation.sta_name.search("AU") !== -1 ? "AU" : "US"
);
}, 3000);
}
const faulty = await detectFaulty(output); //faulty.fail.length > 20 ---> ton tai loi
const fail = faulty.fail.length > 20 ? faulty.fail : "not found";
const RAM_FLASH = await getInfoLog(output);
const itemExtra =
faulty.modelCheck !== ""
? "\n- " + faulty.PIDSpecial.join("\n- ")
: "not found";
let shortLog = [];
let shortConfig = output
.split("\n")
?.slice(
output
.split("\n")
?.indexOf(
output
.split("\n")
?.filter(
(i) =>
i.search("show version") !== -1
)[0]
)+1,
output
.split("\n")
?.indexOf(
output
.split("\n")
?.filter(
(i) =>
i
.toLocaleLowerCase()
.search("logging") !== -1
)[0]
)
);
output.split("\n")?.map((u, index) => {
if (
(u.search("PID:") !== -1 &&
u.search("VID:") !== -1 &&
u.search("SN:") !== -1) ||
(u.toLocaleLowerCase().search("descr:") !== -1 &&
u.toLocaleLowerCase().search("name:") !== -1) ||
u.toLocaleLowerCase().search("system image file") !== -1
) {
shortLog.push(u);
}
});
const title =
valueConfirmPhysical === ""
? "**Line " +
listLine.line_number +
": Test Report Summary**\n**"
: valueConfirmPhysical.option === "Issue"
? "**Line " +
listLine.line_number +
": Test Report Summary**\n\n**Physical test: :warning: ISSUE:** \n" +
valueConfirmPhysical.value +
"\n\n**"
: "**Line " +
listLine.line_number +
": Test Report Summary**\n\n**Physical test: :check: " +
valueConfirmPhysical.option +
"**\n\n**";
const content =
title +
output
.split("\n")
.filter(
(i) =>
i.search("PID:") !== -1 &&
i.search("VID:") !== -1 &&
i.search("SN:") !== -1
)[0].replace("\r","") +
"**\n\n" +
RAM_FLASH.split("\n").filter((i) => i.search("RAM: ") !== -1)[0] +
" - " +
RAM_FLASH.split("\n")?.filter(
(i) => i.search("Flash: ") !== -1
)[0] +
"\n\nExtra item: " +
itemExtra +
"\n\nAuto detect issue: \n```\n" +
fail +
"\n```\n\nShorten log: - [View all](" +
process.env.SOCKET_SERVER +
"/api/log?name=" +
time.split("/")[time.split("/").length - 1] +
")\n````\n" +
shortLog.join("\n") +
"\n" +
shortConfig.join("\n") +
"\n````";
let params = {
type: "stream",
to: stream,
topic: topic,
content: content,
};
console.log("PARAMS: ", params)
const result = await client.messages.send(params);
}
} else {
checkTest = true;
let params = {
type: "stream",
to: stream,
topic: topic,
content:
":red_circle: Line " +
listLine.line_number +
" manual test error. Please check!",
};
const result = await client.messages.send(params);
fs.writeFile(time, data, async function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line error2" + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Error",
date,
"manual",
"",
""
);
if (
valueConfirmPhysical !== "" &&
valueConfirmPhysical.option !== null
) {
setTimeout(async () => {
let res = await Device.getLogByCreatedAt(date);
if (res.pData[0] !== undefined) {
if (valueConfirmPhysical.option === "Issue") {
await Device.changeCommand(
res.pData[0].id_log,
valueConfirmPhysical.option +
": " +
valueConfirmPhysical.value
);
} else {
await Device.changeCommand(
res.pData[0].id_log,
valueConfirmPhysical.option
);
}
}
}, 3000);
}
}
socket1.end();
}, command.length * 8000);
setTimeout(async () => {
if (checkTest === false) {
//check timeout
let params = {
type: "stream",
to: stream,
topic: topic,
content:
listLine.line_number + ". Manual test time out. Please check!",
};
const result = await client.messages.send(params);
}
}, command.length * 10000);
} catch (error) {
console.log(error);
}
};
module.exports.ASA_manual_test = ASA_manual_test;