fibermonitor/index.js

95 lines
2.1 KiB
JavaScript
Raw Normal View History

2024-11-21 00:23:16 +00:00
const cheerio = require("cheerio");
const axios = require("axios");
const fastify = require("fastify")({
logger: true,
});
// Fiber Model URL
const fiberUrl = "http://192.168.1.254/cgi-bin/fiberstat.ha";
// Get Fiber Model Page
// implement prometheus client
const client = require("prom-client");
const fiberTx = new client.Gauge({
name: "fiber_tx",
help: "Fiber Tx Power",
});
const fiberRx = new client.Gauge({
name: "fiber_rx",
help: "Fiber Rx Power",
});
const fiberBias = new client.Gauge({
name: "fiber_bias",
help: "Fiber Bias",
});
const fiberVcc = new client.Gauge({
name: "fiber_vcc",
help: "Fiber Vcc",
});
const fiberTemp = new client.Gauge({
name: "fiber_temp",
help: "Fiber Temperature",
});
// Register the metrics
fastify.get("/metrics", async (req, res) => {
const data = await getFiberData();
res.header("Content-Type", "text/plain");
res.send(await client.register.metrics());
});
async function getFiberData() {
const modemData = await axios.get(fiberUrl);
const $ = cheerio.load(modemData.data);
// Parse the fiber and split to have Temp, Vcc, TX Bias, TX Power and RX Power
// Look for a H1 header that has the text Tx Power in it
const TxPower = $("h1")
.filter((i, el) => $(el).text().includes("Tx Power"))
.text()
.split("Currently ")[1];
const Bias = $("h1")
.filter((i, el) => $(el).text().includes("Bias"))
.text()
.split("Currently ")[1];
const RxPower = $("h1")
.filter((i, el) => $(el).text().includes("Rx Power"))
.text()
.split("Currently ")[1];
const Vcc = $("h1")
.filter((i, el) => $(el).text().includes("Vcc"))
.text()
.split("Currently ")[1];
const Temp = $("h1")
.filter((i, el) => $(el).text().includes("Temperature"))
.text()
.split("Currently ")[1];
fiberTx.set(parseFloat(TxPower));
fiberRx.set(parseFloat(RxPower));
fiberBias.set(parseFloat(Bias));
fiberVcc.set(parseFloat(Vcc));
fiberTemp.set(parseFloat(Temp));
}
const start = async () => {
try {
await fastify.listen({ port: 3000 });
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();