diff --git a/src/monitor-elastic-agents.js b/src/monitor-elastic-agents.js new file mode 100644 index 0000000..ec5bea8 --- /dev/null +++ b/src/monitor-elastic-agents.js @@ -0,0 +1,65 @@ +import _ from "lodash-es"; +import chalk from "chalk"; +import dayjs from "dayjs"; +import config from "config"; +import axios from "axios"; +import * as cheerio from "cheerio"; +import { CronJob } from "cron"; + +const now = () => dayjs().format("HH:mm:ss"); +const url = config.get("go.url") + "/go/admin/status_reports/com.thoughtworks.gocd.elastic-agent.ecs/cluster/CI"; + +const MAX_PENDING_TASKS = 20; +const MAX_HOST_MACHINES = 40; + +async function check() { + const response = await axios.get(url, { auth: { ...config.get("go") } }); + if (response.data) { + const $ = cheerio.load(response.data); + const getCount = (name) => { + return Number($(`div.cluster-properties label:contains('${name}')`).siblings("span").text()); + }; + + console.log(`${now()} GoCD Elastic Agent Status`); + + process.stdout.write(chalk.bold(" Tasks ")); + const runningTasks = getCount("Running tasks"); + const pendingTasks = getCount("Pending tasks"); + console.log(`Running: ${runningTasks} Pending: ${pendingTasks}`); + if (Number(pendingTasks) >= MAX_PENDING_TASKS) { + console.log(" ", chalk.bgRed.white.bold(`Too many pending tasks (${pendingTasks})`)); + } + + process.stdout.write(chalk.bold("Instances ")); + const spot = getCount("Registered linux spot instances"); + const onDemand = getCount("Registered linux on-demand instances"); + const total = spot + onDemand; + const hasTotal = spot > 0 && onDemand > 0; + console.log(` Spot: ${spot} On Demand: ${onDemand} ${hasTotal ? chalk.bold(`Total: ${total}`) : ""}`); + if (total >= MAX_HOST_MACHINES) { + console.log(" ", chalk.bgGreen.white.bold(`Max limit reached: ${total}`)); + } + + // TODO: Get errors from the page when or if they show up. I don't know the selector for that yet + // Guess based on https://github.com/gocd/gocd-ecs-elastic-agent/blob/master/src/main/resources/error.template.ftlh#L45 + if ($(`div.error-container`).length > 0) { + const text = $(`div.error-container`).text(); + console.log(chalk.red("Possible error on status page. Please check.")); + console.log(text); + console.log(); + } + } else { + console.log(response.status); + } + console.log(); +} + +if (process.argv[2] === "start") { + const expr = "*/1 * * * 1-5"; + console.log(`${now()} 🚀 Starting Cron Job (${expr})`); + check(); + const job = new CronJob(expr, check); + job.start(); +} else { + check(); +}