CiscoTool_v1/netmiko_Api/utils/callPython.js

1782 lines
66 KiB
JavaScript

const { stdout } = require("process");
const Device = require("../models/DeviceModel");
const spawnSync = require("child_process").spawnSync;
const fs = require("fs");
const result = [];
let error = "";
const callPython = async (infoStation, action, listLine, count) => {
console.log(count);
if (count === 0) {
result.length = 0;
}
const time = "./public/filesTest/" + Date.now().toString();
let commands = [];
const clearLine = spawnSync(
"python3",
[
"./utils/netmiko_test.py",
"clear line " + listLine.line_clear,
infoStation.sta_ip,
23,
"clear",
],
{ timeout: 14000, encoding: "utf-8" }
);
console.log(clearLine.stderr);
if (
clearLine.stdout.search("not available for clearing") !== -1 ||
clearLine.stderr !== ""
) {
console.log("Loi: ", clearLine.stdout);
if (clearLine.stderr !== "") {
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: clearLine.stderr,
status: "Error",
});
fs.writeFile(time, clearLine.stderr, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
} else {
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: clearLine.stdout,
status: "Error",
});
fs.writeFile(time, clearLine.stdout, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
}
await Device.addLog(
listLine.id_line,
time,
"Error",
Date.now(),
"Admin",
"",
""
);
if (count + 1 < listLine.length) {
callPython(infoStation, action, listLine, count + 1);
}
} else {
console.log("OK: ", clearLine.stdout);
const checkPass = spawnSync(
"python3",
[
"./utils/checkPassword.py",
"\n",
infoStation.sta_ip,
listLine.line_port,
],
{ timeout: 25000, encoding: "utf-8" }
);
if (checkPass.stdout.search("Pass") !== -1) {
let checkModel = spawnSync(
"python3",
[
"./utils/netmiko_test.py",
"show inv",
infoStation.sta_ip,
listLine.line_port,
"check",
],
{ timeout: 30000, encoding: "utf-8" }
);
console.log(checkModel.stdout);
if (checkModel.stderr !== "") {
//checkModel error
console.log("****" + count + "**Err****");
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: checkModel.stderr,
status: "Error",
});
fs.writeFile(time, checkModel.stderr, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Error",
Date.now(),
"Admin",
"",
""
);
if (count + 1 < listLine.length) {
callPython(infoStation, action, listLine, count + 1);
}
} else {
if (checkModel.stderr === "" && checkModel.stdout === "") {
//Time out
console.log("****" + count + "**Time out****");
console.log("stdout", checkModel.stdout);
checkModel = spawnSync(
"python3",
[
"./utils/netmiko_test.py",
"show inv",
infoStation.sta_ip,
listLine.line_port,
"check",
],
{ timeout: 30000, encoding: "utf-8" }
);
if (checkModel.stderr === "" && checkModel.stdout === "") {
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: "Time out",
status: "Error",
});
fs.writeFile(time, "Time out", function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Error",
Date.now(),
"Admin",
"",
""
);
} else {
//Error python exception
if (checkModel.stdout.includes("Error")) {
console.log("****" + count + "**Error python exception****");
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: checkModel.stdout,
status: "Error",
});
fs.writeFile(time, checkModel.stdout, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Error",
Date.now(),
"Admin",
"",
""
);
if (count + 1 < listLine.length) {
callPython(infoStation, action, listLine, count + 1);
}
} else {
//Good
console.log("****" + count + "**Good****");
let PID = "";
let SN = "";
if (checkModel.stdout.split(",")[1] !== undefined) {
const findModel = checkModel.stdout.indexOf("PID:");
const findSN = checkModel.stdout.indexOf("SN:");
PID = checkModel.stdout
.substr(findModel + 5, 30)
.split(",")[0]
.trim();
SN = checkModel.stdout
.substr(findSN + 4, 17)
.split("\n")[0]
.trim();
const model = PID.split("-")[0] + "-" + PID.split("-")[1];
console.log("*********SN: ", SN);
console.log("*********model: ", model);
const cmdModel = await Device.getAllCmdOfModel(model);
if (cmdModel.pData.length > 0) {
cmdModel.pData.map((u) => {
commands.push(u.cmd_message);
});
} else {
const res = await Device.getAllGroupOfModel(model);
if (res.pData.length > 0) {
console.log("-------MODEL exist--------");
//get command group
const group_info = res.pData.filter(
(item) =>
item.group_action.toLocaleLowerCase() ===
action.toLocaleLowerCase()
);
console.log(res);
const res1 = await Device.getAllCmdOfGroup(
group_info[0].id_group
);
res1.pData.map((u) => {
commands.push(u.cmd_message);
});
console.log(commands);
} else {
console.log("--------MODEL not exist----------");
const res0 = await Device.getAllGroupOfModel(PID);
console.log(res0.pData);
if (res0.pData.length > 0) {
console.log(
"--------PID exist and have a group----------"
);
//get command group default with action
const res = await Device.getAllGroup();
const res_1 = await Device.getAllModel();
//get command of group default with "action"
const res1 = await Device.getAllCmdOfGroup(
res0.pData[0].id_group
);
res1.pData.map((u) => {
commands.push(u.cmd_message);
});
console.log(commands);
} else {
console.log(
"--------PID exist and have not a group----------"
);
const res = await Device.getAllGroup();
const res_1 = await Device.getAllModel();
//get group default with "action"
const groupDefault = res.pData.filter(
(i) =>
i.group_name.split("_")[0].toLocaleLowerCase() ===
"default" &&
i.group_action.toLocaleLowerCase() ===
action.toLocaleLowerCase()
);
//get command of group default with "action"
const res1 = await Device.getAllCmdOfGroup(
groupDefault[0].id_group
);
res1.pData.map((u) => {
commands.push(u.cmd_message);
});
console.log(commands);
if (
res_1.pData.filter((i) => i.id_model === PID).length > 0
) {
console.log("--------Add group for PID----------");
await Device.addModelForGroup(
groupDefault[0].id_group,
PID
);
} else {
console.log(
"--------PID not exist - add PID----------"
);
await Device.addModel(
PID,
groupDefault[0].id_group,
"Group",
Date.now()
);
}
}
}
}
}
const output = spawnSync(
"python3",
[
"./utils/netmiko_test.py",
commands,
infoStation.sta_ip,
listLine.line_port,
"check",
],
{ timeout: commands.length * 12000, encoding: "utf-8" }
);
if (output.stderr !== "") {
//output error
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: output.stderr,
status: "Error",
});
fs.writeFile(time, output.stderr, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Error",
Date.now(),
"Admin",
PID,
SN
);
if (count + 1 < listLine.length) {
callPython(infoStation, action, listLine, count + 1);
}
} else {
//output good
let faulty = [
"err",
"fail",
"fault",
"trace",
"error",
"crit",
"invalid",
];
let modelSpecial = [
"AIM-",
"PWR-4",
"EHWIC-",
'NIM-',
"ISM-",
"PVDM2-",
"PVDM3-",
'PVDM4-',
"DFC4-",
"WS-X68",
"3CXL",
"ISR",
"800G2-POE-2",
"WS-C3560V2-"]
let RAMspecial = [
"29929471K",
"7213055K",
"7081983K",
"6598655K",
"7057407K",
"7341807K",
"7393215K",
"8388608K",
"14888959K",
"14659583K",
"16777216K",
"33554432K",
"131072K"
]
let arrayLine = output.stdout.toLocaleLowerCase().split("\n");
let arrayFilter = arrayLine
.filter((i) => i !== "")
.filter((i) => i.search("#") === -1);
let listExcludeErrForAll = [];
let listExcludeErrForModel = [];
let listErrorFinal = [];
let excludeError = [];
let modelCheck = ""
let RAMcheck = ""
let fail = "Faulty: \n";
modelSpecial.map((u) => {
if (output.stdout.search(u) !== -1) {
modelCheck = output.stdout.slice(output.stdout.search(u), output.stdout.search(u) + u.length)
}
})
RAMspecial.map((u) => {
if (output.stdout.search(u) !== -1) {
console.log(output.stdout.slice(output.stdout.search(u), output.stdout.search(u) + u.length));
RAMcheck = output.stdout.slice(output.stdout.search(u), output.stdout.search(u) + u.length)
}
})
const excludeErr = await Device.getAllExcludeError();
excludeErr.pData.map((u) => {
if (u.err_model.toLocaleLowerCase() === "all") {
listExcludeErrForAll.push(u.err_message);
} else {
listExcludeErrForModel.push(u);
}
});
for (let i = 0; i < faulty.length; i++) {
for (let j = 0; j < arrayFilter.length; j++) {
if (arrayFilter[j].search(faulty[i]) !== -1) {
listErrorFinal.push(arrayFilter[j]);
}
}
}
for (let i = 0; i < listErrorFinal.length; i++) {
if (listExcludeErrForAll.length > 0) {
for (let j = 0; j < listExcludeErrForAll.length; j++) {
if (
listErrorFinal[i].search(
listExcludeErrForAll[j].toLocaleLowerCase()
) !== -1
) {
excludeError.push(listErrorFinal[i]);
} else {
for (let k = 0; k < listExcludeErrForModel.length; k++) {
if (
listErrorFinal[i].search(
listExcludeErrForModel[
k
].err_message.toLocaleLowerCase()
) !== -1 &&
PID.search(listExcludeErrForModel[k].err_model) !== -1
) {
excludeError.push(listErrorFinal[i]);
}
}
}
}
} else {
for (let k = 0; k < listExcludeErrForModel.length; k++) {
if (
listErrorFinal[i].search(
listExcludeErrForModel[
k
].err_message.toLocaleLowerCase()
) !== -1 &&
PID.search(listExcludeErrForModel[k].err_model) !== -1
) {
excludeError.push(listErrorFinal[i]);
}
}
}
}
let uniqueArr = listErrorFinal.filter((value) => {
return !excludeError.includes(value);
});
uniqueArr.map((u) => (fail = fail + u + "\n\n"));
let report = fail + "======================================================================\n" + output.stdout;
if (fail !== "Faulty: \n") {
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
PID: PID,
SN: SN,
modelCheck: modelCheck,
RAMcheck: RAMcheck,
output: report,
status: "Faulty",
});
fs.writeFile(time, report, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Faulty",
Date.now(),
"Admin",
PID,
SN
);
} else {
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
PID: PID,
SN: SN,
modelCheck: modelCheck,
RAMcheck: RAMcheck,
output: output.stdout,
status: "Good",
});
console.log("OUTTTT", output.stdout);
console.log("IP", infoStation.sta_ip);
console.log("PORT", listLine.line_port);
fs.writeFile(time, output.stdout, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Good",
Date.now(),
"Admin",
PID,
SN
);
}
if (count + 1 < listLine.length) {
callPython(infoStation, action, listLine, count + 1);
}
}
}
}
if (count + 1 < listLine.length) {
callPython(infoStation, action, listLine, count + 1);
}
} else {
//Error python exception
if (checkModel.stdout.includes("Error")) {
console.log("****" + count + "**Error python exception****");
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: checkModel.stdout,
status: "Error",
});
fs.writeFile(time, checkModel.stdout, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Error",
Date.now(),
"Admin",
"",
""
);
if (count + 1 < listLine.length) {
callPython(infoStation, action, listLine, count + 1);
}
} else {
//Good
console.log("****" + count + "**Good****");
let PID = "";
let SN = "";
if (checkModel.stdout.split(",")[1] !== undefined) {
const findModel = checkModel.stdout.indexOf("PID:");
const findSN = checkModel.stdout.indexOf("SN:");
PID = checkModel.stdout
.substr(findModel + 5, 30)
.split(",")[0]
.trim();
SN = checkModel.stdout
.substr(findSN + 4, 17)
.split("\n")[0]
.trim();
const model = PID.split("-")[0] + "-" + PID.split("-")[1];
console.log("*********SN: ", SN);
console.log("*********model: ", model);
const cmdModel = await Device.getAllCmdOfModel(model);
if (cmdModel.pData.length > 0) {
cmdModel.pData.map((u) => {
commands.push(u.cmd_message);
});
} else {
const res = await Device.getAllGroupOfModel(model);
if (res.pData.length > 0) {
console.log("-------MODEL exist--------");
//get command group
const group_info = res.pData.filter(
(item) =>
item.group_action.toLocaleLowerCase() ===
action.toLocaleLowerCase()
);
console.log(res);
const res1 = await Device.getAllCmdOfGroup(
group_info[0].id_group
);
res1.pData.map((u) => {
commands.push(u.cmd_message);
});
console.log(commands);
} else {
console.log("--------MODEL not exist----------");
const res0 = await Device.getAllGroupOfModel(PID);
console.log(res0.pData);
if (res0.pData.length > 0) {
console.log("--------PID exist and have a group----------");
//get command group default with action
const res = await Device.getAllGroup();
const res_1 = await Device.getAllModel();
//get command of group default with "action"
const res1 = await Device.getAllCmdOfGroup(
res0.pData[0].id_group
);
res1.pData.map((u) => {
commands.push(u.cmd_message);
});
console.log(commands);
} else {
console.log(
"--------PID exist and have not a group----------"
);
const res = await Device.getAllGroup();
const res_1 = await Device.getAllModel();
//get group default with "action"
const groupDefault = res.pData.filter(
(i) =>
i.group_name.split("_")[0].toLocaleLowerCase() ===
"default" &&
i.group_action.toLocaleLowerCase() ===
action.toLocaleLowerCase()
);
//get command of group default with "action"
const res1 = await Device.getAllCmdOfGroup(
groupDefault[0].id_group
);
res1.pData.map((u) => {
commands.push(u.cmd_message);
});
console.log(commands);
if (
res_1.pData.filter((i) => i.id_model === PID).length > 0
) {
console.log("--------Add group for PID----------");
await Device.addModelForGroup(
groupDefault[0].id_group,
PID
);
} else {
console.log("--------PID not exist - add PID----------");
await Device.addModel(
PID,
groupDefault[0].id_group,
"Group",
Date.now()
);
}
}
}
}
}
const output = spawnSync(
"python3",
[
"./utils/netmiko_test.py",
commands,
infoStation.sta_ip,
listLine.line_port,
"check",
],
{ timeout: commands.length * 12000, encoding: "utf-8" }
);
if (output.stderr !== "") {
//output error
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: output.stderr,
status: "Error",
});
fs.writeFile(time, output.stderr, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Error",
Date.now(),
"Admin",
PID,
SN
);
if (count + 1 < listLine.length) {
callPython(infoStation, action, listLine, count + 1);
}
} else {
//output good
let faulty = [
"err",
"fail",
"fault",
"trace",
"error",
"crit",
"invalid",
];
let modelSpecial = [
"AIM-",
"PWR-4",
"EHWIC-",
'NIM-',
"ISM-",
"PVDM2-",
"PVDM3-",
'PVDM4-',
"DFC4-",
"WS-X68",
"3CXL",
"ISR",
"800G2-POE-2",
"WS-C3560V2-"]
let RAMspecial = [
"29929471K",
"7213055K",
"7081983K",
"6598655K",
"7057407K",
"7341807K",
"7393215K",
"8388608K",
"14888959K",
"14659583K",
"16777216K",
"33554432K",
"131072K"
]
let arrayLine = output.stdout.toLocaleLowerCase().split("\n");
let arrayFilter = arrayLine
.filter((i) => i !== "")
.filter((i) => i.search("#") === -1);
let listExcludeErrForAll = [];
let listExcludeErrForModel = [];
let listErrorFinal = [];
let excludeError = [];
let modelCheck = ""
let RAMcheck = ""
let fail = "Faulty: \n";
modelSpecial.map((u) => {
if (output.stdout.search(u) !== -1) {
modelCheck = output.stdout.slice(output.stdout.search(u), output.stdout.search(u) + u.length)
}
})
RAMspecial.map((u) => {
if (output.stdout.search(u) !== -1) {
console.log(output.stdout.slice(output.stdout.search(u), output.stdout.search(u) + u.length));
RAMcheck = output.stdout.slice(output.stdout.search(u), output.stdout.search(u) + u.length)
}
})
const excludeErr = await Device.getAllExcludeError();
excludeErr.pData.map((u) => {
if (u.err_model.toLocaleLowerCase() === "all") {
listExcludeErrForAll.push(u.err_message);
} else {
listExcludeErrForModel.push(u);
}
});
for (let i = 0; i < faulty.length; i++) {
for (let j = 0; j < arrayFilter.length; j++) {
if (arrayFilter[j].search(faulty[i]) !== -1) {
listErrorFinal.push(arrayFilter[j]);
}
}
}
for (let i = 0; i < listErrorFinal.length; i++) {
if (listExcludeErrForAll.length > 0) {
for (let j = 0; j < listExcludeErrForAll.length; j++) {
if (
listErrorFinal[i].search(
listExcludeErrForAll[j].toLocaleLowerCase()
) !== -1
) {
excludeError.push(listErrorFinal[i]);
} else {
for (let k = 0; k < listExcludeErrForModel.length; k++) {
if (
listErrorFinal[i].search(
listExcludeErrForModel[
k
].err_message.toLocaleLowerCase()
) !== -1 &&
PID.search(listExcludeErrForModel[k].err_model) !== -1
) {
excludeError.push(listErrorFinal[i]);
}
}
}
}
} else {
for (let k = 0; k < listExcludeErrForModel.length; k++) {
if (
listErrorFinal[i].search(
listExcludeErrForModel[
k
].err_message.toLocaleLowerCase()
) !== -1 &&
PID.search(listExcludeErrForModel[k].err_model) !== -1
) {
excludeError.push(listErrorFinal[i]);
}
}
}
}
let uniqueArr = listErrorFinal.filter((value) => {
return !excludeError.includes(value);
});
uniqueArr.map((u) => (fail = fail + u + "\n\n"));
let report = fail + "======================================================================\n" + output.stdout;
if (fail !== "Faulty: \n") {
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
PID: PID,
SN: SN,
modelCheck: modelCheck,
RAMcheck: RAMcheck,
output: report,
status: "Faulty",
});
fs.writeFile(time, report, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Faulty",
Date.now(),
"Admin",
PID,
SN
);
} else {
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
PID: PID,
SN: SN,
modelCheck: modelCheck,
RAMcheck: RAMcheck,
output: output.stdout,
status: "Good",
});
console.log("OUTTTT", output.stdout);
console.log("IP", infoStation.sta_ip);
console.log("PORT", listLine.line_port);
fs.writeFile(time, output.stdout, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Good",
Date.now(),
"Admin",
PID,
SN
);
}
if (count + 1 < listLine.length) {
callPython(infoStation, action, listLine, count + 1);
}
}
}
}
}
} else {
const checkPass = spawnSync(
"python3",
[
"./utils/checkPassword.py",
"\n",
infoStation.sta_ip,
listLine.line_port,
],
{ timeout: 25000, encoding: "utf-8" }
);
if (checkPass.stdout.search("Pass") !== -1) {
let checkModel = spawnSync(
"python3",
[
"./utils/netmiko_test.py",
"show inv",
infoStation.sta_ip,
listLine.line_port,
"check",
],
{ timeout: 30000, encoding: "utf-8" }
);
console.log(checkModel.stdout);
if (checkModel.stderr !== "") {
//checkModel error
console.log("****" + count + "**Err****");
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: checkModel.stderr,
status: "Error",
});
fs.writeFile(time, checkModel.stderr, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Error",
Date.now(),
"Admin",
"",
""
);
if (count + 1 < listLine.length) {
callPython(infoStation, action, listLine, count + 1);
}
} else {
if (checkModel.stderr === "" && checkModel.stdout === "") {
//Time out
console.log("****" + count + "**Time out****");
console.log("stdout", checkModel.stdout);
checkModel = spawnSync(
"python3",
[
"./utils/netmiko_test.py",
"show inv",
infoStation.sta_ip,
listLine.line_port,
"check",
],
{ timeout: 30000, encoding: "utf-8" }
);
if (checkModel.stderr === "" && checkModel.stdout === "") {
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: "Time out",
status: "Error",
});
fs.writeFile(time, "Time out", function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Error",
Date.now(),
"Admin",
"",
""
);
} else {
//Error python exception
if (checkModel.stdout.includes("Error")) {
console.log("****" + count + "**Error python exception****");
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: checkModel.stdout,
status: "Error",
});
fs.writeFile(time, checkModel.stdout, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Error",
Date.now(),
"Admin",
"",
""
);
if (count + 1 < listLine.length) {
callPython(infoStation, action, listLine, count + 1);
}
} else {
//Good
console.log("****" + count + "**Good****");
let PID = "";
let SN = "";
if (checkModel.stdout.split(",")[1] !== undefined) {
const findModel = checkModel.stdout.indexOf("PID:");
const findSN = checkModel.stdout.indexOf("SN:");
PID = checkModel.stdout
.substr(findModel + 5, 30)
.split(",")[0]
.trim();
SN = checkModel.stdout
.substr(findSN + 4, 17)
.split("\n")[0]
.trim();
const model = PID.split("-")[0] + "-" + PID.split("-")[1];
console.log("*********SN: ", SN);
console.log("*********model: ", model);
const cmdModel = await Device.getAllCmdOfModel(model);
if (cmdModel.pData.length > 0) {
cmdModel.pData.map((u) => {
commands.push(u.cmd_message);
});
} else {
const res = await Device.getAllGroupOfModel(model);
if (res.pData.length > 0) {
console.log("-------MODEL exist--------");
//get command group
const group_info = res.pData.filter(
(item) =>
item.group_action.toLocaleLowerCase() ===
action.toLocaleLowerCase()
);
console.log(res);
const res1 = await Device.getAllCmdOfGroup(
group_info[0].id_group
);
res1.pData.map((u) => {
commands.push(u.cmd_message);
});
console.log(commands);
} else {
console.log("--------MODEL not exist----------");
const res0 = await Device.getAllGroupOfModel(PID);
console.log(res0.pData);
if (res0.pData.length > 0) {
console.log(
"--------PID exist and have a group----------"
);
//get command group default with action
const res = await Device.getAllGroup();
const res_1 = await Device.getAllModel();
//get command of group default with "action"
const res1 = await Device.getAllCmdOfGroup(
res0.pData[0].id_group
);
res1.pData.map((u) => {
commands.push(u.cmd_message);
});
console.log(commands);
} else {
console.log(
"--------PID exist and have not a group----------"
);
const res = await Device.getAllGroup();
const res_1 = await Device.getAllModel();
//get group default with "action"
const groupDefault = res.pData.filter(
(i) =>
i.group_name.split("_")[0].toLocaleLowerCase() ===
"default" &&
i.group_action.toLocaleLowerCase() ===
action.toLocaleLowerCase()
);
//get command of group default with "action"
const res1 = await Device.getAllCmdOfGroup(
groupDefault[0].id_group
);
res1.pData.map((u) => {
commands.push(u.cmd_message);
});
console.log(commands);
if (
res_1.pData.filter((i) => i.id_model === PID).length > 0
) {
console.log("--------Add group for PID----------");
await Device.addModelForGroup(
groupDefault[0].id_group,
PID
);
} else {
console.log(
"--------PID not exist - add PID----------"
);
await Device.addModel(
PID,
groupDefault[0].id_group,
"Group",
Date.now()
);
}
}
}
}
}
const output = spawnSync(
"python3",
[
"./utils/netmiko_test.py",
commands,
infoStation.sta_ip,
listLine.line_port,
"check",
],
{ timeout: commands.length * 12000, encoding: "utf-8" }
);
if (output.stderr !== "") {
//output error
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: output.stderr,
status: "Error",
});
fs.writeFile(time, output.stderr, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Error",
Date.now(),
"Admin",
PID,
SN
);
if (count + 1 < listLine.length) {
callPython(infoStation, action, listLine, count + 1);
}
} else {
//output good
let faulty = [
"err",
"fail",
"fault",
"trace",
"error",
"crit",
"invalid",
];
let modelSpecial = [
"AIM-",
"PWR-4",
"EHWIC-",
'NIM-',
"ISM-",
"PVDM2-",
"PVDM3-",
'PVDM4-',
"DFC4-",
"WS-X68",
"3CXL",
"ISR",
"800G2-POE-2",
"WS-C3560V2-"]
let RAMspecial = [
"29929471K",
"7213055K",
"7081983K",
"6598655K",
"7057407K",
"7341807K",
"7393215K",
"8388608K",
"14888959K",
"14659583K",
"16777216K",
"33554432K",
"131072K"
]
let arrayLine = output.stdout.toLocaleLowerCase().split("\n");
let arrayFilter = arrayLine
.filter((i) => i !== "")
.filter((i) => i.search("#") === -1);
let listExcludeErrForAll = [];
let listExcludeErrForModel = [];
let listErrorFinal = [];
let excludeError = [];
let modelCheck = ""
let RAMcheck = ""
let fail = "Faulty: \n";
modelSpecial.map((u) => {
if (output.stdout.search(u) !== -1) {
modelCheck = output.stdout.slice(output.stdout.search(u), output.stdout.search(u) + u.length)
}
})
RAMspecial.map((u) => {
if (output.stdout.search(u) !== -1) {
console.log(output.stdout.slice(output.stdout.search(u), output.stdout.search(u) + u.length));
RAMcheck = output.stdout.slice(output.stdout.search(u), output.stdout.search(u) + u.length)
}
})
const excludeErr = await Device.getAllExcludeError();
excludeErr.pData.map((u) => {
if (u.err_model.toLocaleLowerCase() === "all") {
listExcludeErrForAll.push(u.err_message);
} else {
listExcludeErrForModel.push(u);
}
});
for (let i = 0; i < faulty.length; i++) {
for (let j = 0; j < arrayFilter.length; j++) {
if (arrayFilter[j].search(faulty[i]) !== -1) {
listErrorFinal.push(arrayFilter[j]);
}
}
}
for (let i = 0; i < listErrorFinal.length; i++) {
if (listExcludeErrForAll.length > 0) {
for (let j = 0; j < listExcludeErrForAll.length; j++) {
if (
listErrorFinal[i].search(
listExcludeErrForAll[j].toLocaleLowerCase()
) !== -1
) {
excludeError.push(listErrorFinal[i]);
} else {
for (let k = 0; k < listExcludeErrForModel.length; k++) {
if (
listErrorFinal[i].search(
listExcludeErrForModel[
k
].err_message.toLocaleLowerCase()
) !== -1 &&
PID.search(listExcludeErrForModel[k].err_model) !== -1
) {
excludeError.push(listErrorFinal[i]);
}
}
}
}
} else {
for (let k = 0; k < listExcludeErrForModel.length; k++) {
if (
listErrorFinal[i].search(
listExcludeErrForModel[
k
].err_message.toLocaleLowerCase()
) !== -1 &&
PID.search(listExcludeErrForModel[k].err_model) !== -1
) {
excludeError.push(listErrorFinal[i]);
}
}
}
}
let uniqueArr = listErrorFinal.filter((value) => {
return !excludeError.includes(value);
});
uniqueArr.map((u) => (fail = fail + u + "\n\n"));
let report = fail + "======================================================================\n" + output.stdout;
if (fail !== "Faulty: \n") {
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
PID: PID,
SN: SN,
modelCheck: modelCheck,
RAMcheck: RAMcheck,
output: report,
status: "Faulty",
});
fs.writeFile(time, report, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Faulty",
Date.now(),
"Admin",
PID,
SN
);
} else {
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
PID: PID,
SN: SN,
modelCheck: modelCheck,
RAMcheck: RAMcheck,
output: output.stdout,
status: "Good",
});
console.log("OUTTTT", output.stdout);
console.log("IP", infoStation.sta_ip);
console.log("PORT", listLine.line_port);
fs.writeFile(time, output.stdout, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Good",
Date.now(),
"Admin",
PID,
SN
);
}
if (count + 1 < listLine.length) {
callPython(infoStation, action, listLine, count + 1);
}
}
}
}
if (count + 1 < listLine.length) {
callPython(infoStation, action, listLine, count + 1);
}
} else {
//Error python exception
if (checkModel.stdout.includes("Error")) {
console.log("****" + count + "**Error python exception****");
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: checkModel.stdout,
status: "Error",
});
fs.writeFile(time, checkModel.stdout, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Error",
Date.now(),
"Admin",
"",
""
);
if (count + 1 < listLine.length) {
callPython(infoStation, action, listLine, count + 1);
}
} else {
//Good
console.log("****" + count + "**Good****");
let PID = "";
let SN = "";
if (checkModel.stdout.split(",")[1] !== undefined) {
const findModel = checkModel.stdout.indexOf("PID:");
const findSN = checkModel.stdout.indexOf("SN:");
PID = checkModel.stdout
.substr(findModel + 5, 30)
.split(",")[0]
.trim();
SN = checkModel.stdout
.substr(findSN + 4, 17)
.split("\n")[0]
.trim();
const model = PID.split("-")[0] + "-" + PID.split("-")[1];
console.log("*********SN: ", SN);
console.log("*********model: ", model);
const cmdModel = await Device.getAllCmdOfModel(model);
if (cmdModel.pData.length > 0) {
cmdModel.pData.map((u) => {
commands.push(u.cmd_message);
});
} else {
const res = await Device.getAllGroupOfModel(model);
if (res.pData.length > 0) {
console.log("-------MODEL exist--------");
//get command group
const group_info = res.pData.filter(
(item) =>
item.group_action.toLocaleLowerCase() ===
action.toLocaleLowerCase()
);
console.log(res);
const res1 = await Device.getAllCmdOfGroup(
group_info[0].id_group
);
res1.pData.map((u) => {
commands.push(u.cmd_message);
});
console.log(commands);
} else {
console.log("--------MODEL not exist----------");
const res0 = await Device.getAllGroupOfModel(PID);
console.log(res0.pData);
if (res0.pData.length > 0) {
console.log("--------PID exist and have a group----------");
//get command group default with action
const res = await Device.getAllGroup();
const res_1 = await Device.getAllModel();
//get command of group default with "action"
const res1 = await Device.getAllCmdOfGroup(
res0.pData[0].id_group
);
res1.pData.map((u) => {
commands.push(u.cmd_message);
});
console.log(commands);
} else {
console.log(
"--------PID exist and have not a group----------"
);
const res = await Device.getAllGroup();
const res_1 = await Device.getAllModel();
//get group default with "action"
const groupDefault = res.pData.filter(
(i) =>
i.group_name.split("_")[0].toLocaleLowerCase() ===
"default" &&
i.group_action.toLocaleLowerCase() ===
action.toLocaleLowerCase()
);
//get command of group default with "action"
const res1 = await Device.getAllCmdOfGroup(
groupDefault[0].id_group
);
res1.pData.map((u) => {
commands.push(u.cmd_message);
});
console.log(commands);
if (
res_1.pData.filter((i) => i.id_model === PID).length > 0
) {
console.log("--------Add group for PID----------");
await Device.addModelForGroup(
groupDefault[0].id_group,
PID
);
} else {
console.log("--------PID not exist - add PID----------");
await Device.addModel(
PID,
groupDefault[0].id_group,
"Group",
Date.now()
);
}
}
}
}
}
const output = spawnSync(
"python3",
[
"./utils/netmiko_test.py",
commands,
infoStation.sta_ip,
listLine.line_port,
"check",
],
{ timeout: commands.length * 12000, encoding: "utf-8" }
);
if (output.stderr !== "") {
//output error
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: output.stderr,
status: "Error",
});
fs.writeFile(time, output.stderr, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Error",
Date.now(),
"Admin",
PID,
SN
);
if (count + 1 < listLine.length) {
callPython(infoStation, action, listLine, count + 1);
}
} else {
//output good
let faulty = [
"err",
"fail",
"fault",
"trace",
"error",
"crit",
"invalid",
];
let modelSpecial = [
"AIM-",
"PWR-4",
"EHWIC-",
'NIM-',
"ISM-",
"PVDM2-",
"PVDM3-",
'PVDM4-',
"DFC4-",
"WS-X68",
"3CXL",
"ISR",
"800G2-POE-2",
"WS-C3560V2-"]
let RAMspecial = [
"29929471K",
"7213055K",
"7081983K",
"6598655K",
"7057407K",
"7341807K",
"7393215K",
"8388608K",
"14888959K",
"14659583K",
"16777216K",
"33554432K",
"131072K"
]
let arrayLine = output.stdout.toLocaleLowerCase().split("\n");
let arrayFilter = arrayLine
.filter((i) => i !== "")
.filter((i) => i.search("#") === -1);
let listExcludeErrForAll = [];
let listExcludeErrForModel = [];
let listErrorFinal = [];
let excludeError = [];
let modelCheck = ""
let RAMcheck = ""
let fail = "Faulty: \n";
modelSpecial.map((u) => {
if (output.stdout.search(u) !== -1) {
modelCheck = output.stdout.slice(output.stdout.search(u), output.stdout.search(u) + u.length)
}
})
RAMspecial.map((u) => {
if (output.stdout.search(u) !== -1) {
console.log(output.stdout.slice(output.stdout.search(u), output.stdout.search(u) + u.length));
RAMcheck = output.stdout.slice(output.stdout.search(u), output.stdout.search(u) + u.length)
}
})
const excludeErr = await Device.getAllExcludeError();
excludeErr.pData.map((u) => {
if (u.err_model.toLocaleLowerCase() === "all") {
listExcludeErrForAll.push(u.err_message);
} else {
listExcludeErrForModel.push(u);
}
});
for (let i = 0; i < faulty.length; i++) {
for (let j = 0; j < arrayFilter.length; j++) {
if (arrayFilter[j].search(faulty[i]) !== -1) {
listErrorFinal.push(arrayFilter[j]);
}
}
}
for (let i = 0; i < listErrorFinal.length; i++) {
if (listExcludeErrForAll.length > 0) {
for (let j = 0; j < listExcludeErrForAll.length; j++) {
if (
listErrorFinal[i].search(
listExcludeErrForAll[j].toLocaleLowerCase()
) !== -1
) {
excludeError.push(listErrorFinal[i]);
} else {
for (let k = 0; k < listExcludeErrForModel.length; k++) {
if (
listErrorFinal[i].search(
listExcludeErrForModel[
k
].err_message.toLocaleLowerCase()
) !== -1 &&
PID.search(listExcludeErrForModel[k].err_model) !== -1
) {
excludeError.push(listErrorFinal[i]);
}
}
}
}
} else {
for (let k = 0; k < listExcludeErrForModel.length; k++) {
if (
listErrorFinal[i].search(
listExcludeErrForModel[
k
].err_message.toLocaleLowerCase()
) !== -1 &&
PID.search(listExcludeErrForModel[k].err_model) !== -1
) {
excludeError.push(listErrorFinal[i]);
}
}
}
}
let uniqueArr = listErrorFinal.filter((value) => {
return !excludeError.includes(value);
});
uniqueArr.map((u) => (fail = fail + u + "\n\n"));
let report = fail + "======================================================================\n" + output.stdout;
if (fail !== "Faulty: \n") {
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
PID: PID,
SN: SN,
modelCheck: modelCheck,
RAMcheck: RAMcheck,
output: report,
status: "Faulty",
});
fs.writeFile(time, report, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Faulty",
Date.now(),
"Admin",
PID,
SN
);
} else {
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
PID: PID,
SN: SN,
modelCheck: modelCheck,
RAMcheck: RAMcheck,
output: output.stdout,
status: "Good",
});
console.log("OUTTTT", output.stdout);
console.log("IP", infoStation.sta_ip);
console.log("PORT", listLine.line_port);
fs.writeFile(time, output.stdout, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
await Device.addLog(
listLine.id_line,
time,
"Good",
Date.now(),
"Admin",
PID,
SN
);
}
if (count + 1 < listLine.length) {
callPython(infoStation, action, listLine, count + 1);
}
}
}
}
}
} else {
if (checkPass.stdout.search("NetmikoAuthenticationException") !== -1) {
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: "Password exist",
status: "Error",
});
fs.writeFile(time, "Password exist", function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
} else {
result.push({
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: checkPass.stdout,
status: "Error",
});
fs.writeFile(time, checkPass.stdout, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
}
await Device.addLog(
listLine.id_line,
time,
"Error",
Date.now(),
"Admin",
"",
""
);
if (count + 1 < listLine.length) {
callPython(infoStation, action, listLine, count + 1);
}
}
}
}
return result;
};
module.exports = callPython;