From 720ad56a7a32ba2a6a63aed35e702fd3b7441d21 Mon Sep 17 00:00:00 2001 From: Donny Date: Fri, 30 Aug 2024 09:00:52 -0600 Subject: [PATCH] Added a endpoint for zabbix --- app.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/app.js b/app.js index a0c4284..a4d231c 100644 --- a/app.js +++ b/app.js @@ -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; +}; +