Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Choose the way monitor-bot runs
# cron = use a continuous monitor that checks on a set interval
# single = run the monitor one, then quit
RUNNING_METHOD=cron
CHECK_INTERVAL=5000 # in milliseconds

# Local Deployment
LOCAL_CSTATE_PATH=../cstate # Relative path to your Hugo/cState site

Expand Down
21 changes: 21 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
run_monitorbot:
image: oven/bun:alpine
cache:
key: monitorbot-cache
paths:
- node_modules/
- incident_state.json # Caches the incident states between job runs
before_script:
- echo "Installing dependencies..."
# - apk add git # Uncomment when using Git deployment method
- bun install --no-save
script:
- echo "Running monitorbot..."
- bun index.js
rules:
# Run pipeline from a schedule defined in GitLab
- if: '$CI_PIPELINE_SOURCE == "schedule"'
# Run if manually triggered from GitLab UI
- if: '$CI_PIPELINE_SOURCE == "web"'
# Optional: allow running on manual trigger from another job
- when: manual
34 changes: 29 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ Modify the `config.js` file to set up the sites you want to monitor and adjust o

```javascript
export default {
checkInterval: 60 * 1000, // Check interval in milliseconds (60 seconds)
runningMethod: 'cron', // 'cron' for continuous process or 'single' for check once and quit
checkInterval: 5 * 1000, // 5 seconds (adjust as needed), only for cron running method
testMode: true, // Set to false for actual deployment
deployment: {
method: 'local', // Deployment method: local, ftp, or git
Expand Down Expand Up @@ -146,7 +147,7 @@ export default {

## Usage

1. **Test Mode (Dry Run):**
### Test Mode (Dry Run)

To run the checks without creating/modifying incident files, make sure `testMode` is set to `true` in `config.js`. Then run:

Expand All @@ -158,7 +159,7 @@ If you edit the `config.js` file, you will need to restart the script for the ch

This will output the results of the checks to the console in a color-coded format.

2. **Deployment Mode (Create/Update Incidents):**
### Deployment Mode to Host (Create/Update Incidents)

* Set `testMode` to `false` in `config.js`.
* Choose your deployment method (`local`, `ftp`, or `git`) in `config.js`.
Expand All @@ -171,9 +172,9 @@ node index.js

This will perform the checks and create/update incident Markdown files in your Hugo/cState site based on the results.

## Scheduling
#### Scheduling

The bot uses `cron` to run checks periodically. The default interval is set to 60 seconds in `config.js`. You can adjust the `checkInterval` value or modify the cron expression in `index.js` for more advanced scheduling.
When the bot uses the `cron` running method, it uses `cron` to run checks periodically. The default interval is set to 60 seconds in `config.js`. You can adjust the `checkInterval` value or modify the cron expression in `index.js` for more advanced scheduling.

**Example (run every 5 minutes):**

Expand All @@ -188,6 +189,29 @@ const job = new CronJob(
);
```

### Deployment Mode in GitLab Pipelines *(experimental)*

This deployment mode relies on the cron scheduler of GitLab pipelines. The bot will run the checks once in the pipeline, perform an issue deployment if necessary and quit. The *incident state* is transferred between pipeline runs using the pipeline cache.

> **Note**: when a cache miss occurs (e.g. due to a network error), the *incident state* is reset, which may result in false positives in your monitor. Deploying the `incident_state.json` every once in a while should make it more reliable, but for simple monitoring tasks using the cache should suffice most of the times.

* Set `testMode` to `false` and `runningMode` to `single` in `config.js`.
* Choose your deployment method (`local`, `ftp`, or `git`) in `config.js`.
* Make sure your `.env` file is configured correctly for your chosen deployment method.
* When choosing deployment method `git`, make sure to add the git package in the `.gitlab-ci.yml`, by uncommenting the corresponding line.

```yaml
# In .gitlab-ci.yml
before_script:
- echo "Installing dependencies..."
- apk add git # <-- uncomment this line when using Git deployment method
- bun install --no-save
```

* In GitLab, schedule a pipeline for this repository for a chosen interval in **Project** > **Build** > **Pipeline schedules**.

On a small GitLab runner, this pipeline should take about **~20 seconds**, depending on the amount of checks you do.

## Incident Report Format

Incident reports are generated as Markdown files in the `content/issues` directory of your Hugo/cState site. They use the following frontmatter:
Expand Down
3 changes: 2 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import dotenv from 'dotenv';
dotenv.config();

export default {
checkInterval: 5 * 1000, // 60 seconds (adjust as needed)
runningMethod: process.env.RUNNING_METHOD || 'cron', // 'cron' for continuous process or 'single' for check once and quit
checkInterval: process.env.CHECK_INTERVAL || 5 * 1000, // 5 seconds (adjust as needed), only for cron running method
httpMonitor: {
urls: [
{
Expand Down
22 changes: 12 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ async function runChecks() {
// Initial run
runChecks();

// Schedule checks using CronJob
const job = new CronJob(
`*/${config.checkInterval / 1000} * * * * *`, // Run every `checkInterval` seconds
runChecks,
null,
true,
'America/Los_Angeles' // Adjust the time zone as needed
);

job.start();
if (config.runningMethod === 'cron') {
// Schedule checks using CronJob
const job = new CronJob(
`*/${config.checkInterval / 1000} * * * * *`, // Run every `checkInterval` seconds
runChecks,
null,
true,
'America/Los_Angeles' // Adjust the time zone as needed
);

job.start();
};