henrygd / beszel

Lightweight server monitoring hub with historical data, docker stats, and alerts.
MIT License
2.42k stars 73 forks source link

Apply alerts to multiple hosts at once #123

Closed ghost closed 1 week ago

ghost commented 2 months ago

It would be great if we could apply and adjust alerts in bulk, for example setting the same alert up for all servers at once.

However, we still need the ability to fine tune alerts on each individual server.

henrygd commented 2 months ago

Maybe in a future release, but for now you can accomplish this with the REST API.

Here's an example using the official Pocketbase JS SDK with Bun / TS:

import PocketBase from 'pocketbase'

const pb = new PocketBase('http://localhost:8090')

const userId = '04zda3gvpbc5l6o'

// authenticate as admin
await pb.admins.authWithPassword(process.env.EMAIL!, process.env.PASSWORD!)

// get all systems accessible to the user
const systems = await pb.collection('systems').getFullList({
    fields: 'id',
    filter: `users.id ?= '${userId}'`,
})

// get current alerts for the user
const currentAlerts = await pb.collection('alerts').getFullList({
    fields: 'name,system',
    filter: `user='${userId}'`,
})

// loop through systems and create alerts if they don't exist
for (const system of systems) {
    // create status alert
    if (!currentAlerts.find((alert) => alert.name === 'Status' && alert.system === system.id)) {
        await pb.collection('alerts').create({
            user: userId,
            system: system.id,
            name: 'Status',
        })
    }
    // create cpu alert
    if (!currentAlerts.find((alert) => alert.name === 'CPU' && alert.system === system.id)) {
        await pb.collection('alerts').create({
            user: userId,
            system: system.id,
            name: 'CPU',
            value: 60,
        })
    }
}
henrygd commented 1 week ago

This has been added in 0.6.1