kuma-tts/index.js
2024-11-13 20:42:46 -07:00

61 lines
1.8 KiB
JavaScript

require("dotenv").config()
const UptimeKuma = require("uptimekuma-api");
const kuma = new UptimeKuma(process.env.SERVER);
const { exec } = require("child_process");
function runCommand(command, stdin) {
return new Promise((resolve, reject) => {
const child = exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
reject(error);
} else {
resolve(stdout);
}
});
if (stdin) {
child.stdin.write(stdin);
child.stdin.end();
}
});
}
kuma.status(process.env.STATUSPAGE).then((status) => {
let onlineCount = 0;
let totalCount = 0;
let offline = [];
for (let x of status) {
for (let monitor of x.monitors) {
totalCount++; // Increment total services count
// Check if the service is online
if (monitor.heartbeats.slice(-1)[0].status) {
onlineCount++; // Increment online services count
} else {
offline.push(monitor.name)
}
}
}
// Calculate the percentage of online services
const onlinePercentage = (onlineCount / totalCount) * 100;
// Output the result
console.log(`Total Services: ${totalCount}`);
console.log(`Online Services: ${onlineCount}`);
console.log(`Online Percentage: ${onlinePercentage.toFixed(0)}%`);
let statusString = `Service Status at ${onlinePercentage.toFixed(0)}%. `;
if (offline.length > 0) {
statusString += `${offline.length} of ${totalCount} services are offline. Offline services follow, ${offline.join(". ")}`
} else {
statusString += `No outages detected. All ${totalCount} services are online.`
}
runCommand(process.env.TTS, statusString).then((output) => {
//console.log(output)
runCommand(`ffmpeg -y -i /tmp/kuma-status.wav -ar 8000 -ac 1 -c:a pcm_s16le /tmp/kuma-status-final.wav`)
}).catch(err => {
console.log(err)
})
});