Added a endpoint for zabbix

This commit is contained in:
Donny 2024-08-30 09:00:52 -06:00
parent 0956ba1776
commit 720ad56a7a

45
app.js
View File

@ -309,6 +309,51 @@ const getLicenseCounts = async () => {
return records;
};
app.get('/api/zabbix', async (req, res) => {
try {
const programs = await getZabbixCounts();
res.json(programs); // Directly return the reshaped JSON
} catch (error) {
console.log("[ERROR]: There was an error retrieving licenses. More info: " + error);
res.status(500).json({ error: "Internal Server Error" });
}
});
const getZabbixCounts = async () => {
let n = (pollRate / 1000) + ((pollRate / 1000) * 0.25);
let records = {};
try {
const conn = await pool.getConnection();
try {
const query = `
SELECT ul.program, COUNT(*) as activecount, rl.total,
(rl.total - COUNT(*)) as available
FROM usedLicenses ul
JOIN requestedLicenses rl ON ul.program = rl.name
WHERE ul.date >= DATE_SUB(NOW(), INTERVAL ? SECOND)
GROUP BY ul.program, rl.total
`;
const results = await conn.query(query, [n]);
results.forEach(row => {
records[row.program] = {
activecount: String(row.activecount),
total: String(row.total),
available: String(row.available)
};
});
} catch (error) {
console.log("[ERROR]: There was an error reading from the database. More info: " + error);
} finally {
conn.release();
}
} catch (error) {
console.log("[ERROR]: There was an error reading from the database. More info: " + error);
}
return records;
};