157 lines
4.0 KiB
JavaScript
157 lines
4.0 KiB
JavaScript
let hostname = context.panel.elements.find(e => e.id === "hostname")?.value;
|
|
let group = context.panel.elements.find(e => e.id === "group")?.value;
|
|
let interface = context.panel.elements.find(e => e.id === "interface")?.value;
|
|
let ipa = context.panel.elements.find(e => e.id === "ip")?.value;
|
|
let dns_name = context.panel.elements.find(e => e.id === "dns")?.value;
|
|
let template = context.panel.elements.find(e => e.id === "template")?.value;
|
|
|
|
if (dns_name == "undefined") {
|
|
dns_name = "";
|
|
}
|
|
if (ipa == "undefined") {
|
|
ipa = "";
|
|
}
|
|
|
|
async function ejecutarPeticion() {
|
|
async function getToken() {
|
|
let token = null;
|
|
|
|
let res = await fetch("http://localhost:8080/api_jsonrpc.php", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
jsonrpc: "2.0",
|
|
method: "user.login",
|
|
params: {
|
|
username: "Admin",
|
|
password: "zabbix"
|
|
},
|
|
id: 1
|
|
})
|
|
});
|
|
|
|
const data = await res.json();
|
|
token = data.result;
|
|
return token;
|
|
}
|
|
|
|
let token = await getToken();
|
|
if (interface == "ZBX") {
|
|
return fetch("http://localhost:8080/api_jsonrpc.php", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json-rpc",
|
|
"Authorization": "Bearer " + token
|
|
},
|
|
body: JSON.stringify({
|
|
jsonrpc: "2.0",
|
|
method: "host.create",
|
|
params: {
|
|
host: hostname,
|
|
interfaces: [
|
|
{
|
|
type: 1,
|
|
main: 1,
|
|
useip: 1,
|
|
ip: ipa,
|
|
dns: dns_name,
|
|
port: "10050"
|
|
}
|
|
],
|
|
groups: group.map(g => ({
|
|
groupid: parseInt(g)
|
|
})),
|
|
templates: template.map(g => ({
|
|
templateid: parseInt(g)
|
|
})),
|
|
},
|
|
id: 1
|
|
})
|
|
});
|
|
}
|
|
else if (interface == "snmp_v3") {
|
|
let snmp_sec = context.panel.elements.find(e => e.id === "snmp_sec")?.value;
|
|
let snmp_context = context.panel.elements.find(e => e.id === "snmp_context")?.value;
|
|
|
|
return fetch("http://localhost:8080/api_jsonrpc.php", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json-rpc",
|
|
"Authorization": "Bearer " + token
|
|
},
|
|
body: JSON.stringify({
|
|
jsonrpc: "2.0",
|
|
method: "host.create",
|
|
params: {
|
|
host: hostname,
|
|
interfaces: [
|
|
{
|
|
type: 2,
|
|
main: 1,
|
|
useip: 1,
|
|
ip: ipa,
|
|
dns: dns_name,
|
|
port: "161",
|
|
details: {
|
|
version: 3,
|
|
bulk: 0,
|
|
securityname: snmp_sec,
|
|
contextname: "" + snmp_context,
|
|
securitylevel: 1
|
|
}
|
|
}
|
|
],
|
|
groups: group.map(g => ({
|
|
groupid: parseInt(g)
|
|
})),
|
|
templates: template.map(g => ({
|
|
templateid: parseInt(g)
|
|
})),
|
|
},
|
|
id: 1
|
|
})
|
|
});
|
|
}
|
|
else if (interface == "snmp_v2") {
|
|
let snmp_com = context.panel.elements.find(e => e.id === "snmp_com")?.value;
|
|
|
|
return fetch("http://localhost:8080/api_jsonrpc.php", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json-rpc",
|
|
"Authorization": "Bearer " + token
|
|
},
|
|
body: JSON.stringify({
|
|
jsonrpc: "2.0",
|
|
method: "host.create",
|
|
params: {
|
|
host: hostname,
|
|
interfaces: [
|
|
{
|
|
type: 2,
|
|
main: 1,
|
|
useip: 1,
|
|
ip: ipa,
|
|
dns: dns_name,
|
|
port: "161",
|
|
details: {
|
|
version: 2,
|
|
bulk: 0,
|
|
community: snmp_com
|
|
}
|
|
}
|
|
],
|
|
groups: group.map(g => ({
|
|
groupid: parseInt(g)
|
|
})),
|
|
templates: template.map(g => ({
|
|
templateid: parseInt(g)
|
|
})),
|
|
},
|
|
id: 1
|
|
})
|
|
});
|
|
}
|
|
}
|
|
|
|
return ejecutarPeticion(); |