forked from nemanjakrstic/snitch
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cron job functionality to monitor elastic agent count
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |