Skip to content

Commit

Permalink
Services as group is now part of the API! YAY!
Browse files Browse the repository at this point in the history
  • Loading branch information
UlisesGascon committed Nov 20, 2018
1 parent 81986fc commit 902d332
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
44 changes: 30 additions & 14 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function icingaRequest (urlPath="", method="GET") {
reject({error, response});
}
});
})
});
}

function stateMapping (code) {
Expand All @@ -35,12 +35,12 @@ function stateMapping (code) {

function outputMapping (output){
const data = output.split(" ");
return {raw: output, ping: data[1], packet_loss: data[6], rta_ms: data[9]}
return {raw: output, ping: data[1], packet_loss: data[6], rta_ms: data[9]};
}

function filterDetails (hostDetails) {
return {
name: hostDetails.name,
name: hostDetails.attrs.display_name,
type: hostDetails.type,
severity: hostDetails.attrs.severity,
next_check: hostDetails.attrs.next_check,
Expand All @@ -49,31 +49,47 @@ function filterDetails (hostDetails) {
last_state_down: hostDetails.attrs.last_state_down,
output: outputMapping(hostDetails.attrs.last_check_result.output),
state: stateMapping(hostDetails.attrs.last_check_result.state),
isActive: hostDetails.attrs.last_check_result.active,
}
isActive: hostDetails.attrs.last_check_result.active
};
}

function allHosts () {
return new Promise ((resolve, reject) => {
icingaRequest('/objects/hosts')
.then((res, body) => {
const data = JSON.parse(res.body)
resolve(data.results.map(filterDetails))
const data = JSON.parse(res.body);
resolve(data.results.map(filterDetails));
})
.catch(reject)
})
.catch(reject);
});
}

function hostDetails (host) {
return new Promise ((resolve, reject) => {
icingaRequest(`/objects/hosts?host=${host}`)
.then((res, body) => {
const data = JSON.parse(res.body)
resolve(data.results.map(filterDetails)[0])
const data = JSON.parse(res.body);
resolve(data.results.map(filterDetails)[0]);
})
.catch(reject)
})
.catch(reject);
});
}

function allGroups () {
return new Promise ((resolve, reject) => {
icingaRequest('/objects/services')
.then((res, body) => {
const finalData = {};
const data = JSON.parse(res.body);
data.results.forEach(item => {
const service = item.name.split("!")[0];
finalData[service] = finalData[service] || [];
finalData[service].push(filterDetails(item));
});
resolve(finalData);
})
.catch(reject);
});
}

module.exports = {allHosts, hostDetails}
module.exports = {allHosts, hostDetails, allGroups};
13 changes: 11 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ app.get('/api/v1/host', [utils.enableCors], (req, res) => {
.then(data => res.status(200).json(data))
.catch(err => {
res.status(500).send();
logger.error(err);
logger.error(JSON.parse(err));
});
});

Expand All @@ -34,7 +34,16 @@ app.get('/api/v1/host/:host', [utils.enableCors], (req, res) => {
.then(data => res.status(200).json(data))
.catch(err => {
res.status(500).send();
logger.error(err);
logger.error(JSON.parse(err));
});
});

app.get('/api/v1/group', [utils.enableCors], (req, res) => {
api.allGroups()
.then(data => res.status(200).json(data))
.catch(err => {
res.status(500).send();
logger.error(JSON.parse(err));
});
});

Expand Down

0 comments on commit 902d332

Please sign in to comment.