Make nmon easier to install and run without editing YAML by hand: - Settings: point-and-click forms over config.yaml (GET/PUT /api/v1/settings/form); the YAML editor moves to /settings/yaml. - First install: with no sign-in configured nmon starts in install mode, serves only the setup wizard (sign-in, basics, devices, alerting) and closes it for good once sign-in is saved. A self-signed certificate is generated when no TLS source is given. poller.interval becomes a config setting. - Reverse proxies: opt-in --http-listen plain-HTTP listener beside HTTPS; sign-in stays required. - Webhooks: Generic, Teams Adaptive Card, Slack and Discord presets, template preview and test send. - Help: the operations, coverage and profile docs served at /help. - Polling: per-device "Poll now", a dry-run test poll returning an SNMP trace, and time-limited in-memory SNMP debug recording, in the UI and as `nmon device poll`. - Ansible: deploys into install mode when no sign-in is set, and no longer overwrites the config.yaml the wizard wrote. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017TuKgyKve2XLFtrrXWMi2w
105 lines
4.9 KiB
TypeScript
105 lines
4.9 KiB
TypeScript
// Stateful mock of the typed settings form (GET/PUT /settings/form) and write-only secrets.
|
|
import type { components } from '../../src/api/schema.d.ts'
|
|
import type { Reply } from './groups.ts'
|
|
|
|
type S = components['schemas']
|
|
type View = S['SettingsFormView']
|
|
|
|
const RESTART = new Set(['retention', 'timezone', 'sites', 'proxmox', 'checks', 'auth'])
|
|
|
|
function defaults(): View {
|
|
return {
|
|
retention: { hot: '30d', fiveminute: '120d', long: '2y' },
|
|
timezone: 'UTC',
|
|
retired_purge_after: '1y',
|
|
poller_interval: '5m',
|
|
sites: [],
|
|
alerting: { outbound_allow: [], evaluation_interval: '30s' },
|
|
receivers: [],
|
|
auth: { break_glass_file: '', break_glass_user: 'breakglass', trusted_proxies: [], session: { idle: '1h', absolute: '12h' } },
|
|
backup: { at: '', dir: '', keep: 7 },
|
|
disk: { alert_used: 0.8, admit_used: 0.9, reserve_free: 0.05 },
|
|
l2: { interval: '15m', keep: '90d', edge_max_macs: 8, concurrency: 4, device_timeout: '2m' },
|
|
oui: { fetch: true, refresh: '7d' },
|
|
proxmox: { clusters: [], interval: '5m', timeout: '1m', retire_grace: '1d', workers: 4 },
|
|
checks: { workers: 32, timeout: '10s', tick: '5s' },
|
|
}
|
|
}
|
|
|
|
function seeded(): View {
|
|
const v = defaults()
|
|
v.timezone = 'America/Toronto'
|
|
v.poller_interval = ''
|
|
v.retention = { hot: '30d', fiveminute: '120d', long: '2y' }
|
|
v.sites = [{ name: 'yyz1', timezone: 'America/Toronto' }]
|
|
v.backup = { at: '02:30', dir: '/var/backups/nmon', keep: 7 }
|
|
v.receivers = [{ name: 'ops-hook', kind: 'webhook', url: 'https://hooks.example.net/nmon', headers: {}, template: '', smtp: { host: '', port: 0, from: '', to: [], username: '', login_stored: false, starttls: false } }]
|
|
v.auth.trusted_proxies = ['10.0.0.0/8']
|
|
v.proxmox.clusters = [{ name: 'pve-yyz1', url: 'https://pve-yyz1.example.net:8006', token_id: 'nmon@pve!ro', secret_name: 'pve-yyz1-token', ca_file: '', insecure_skip_verify: false }]
|
|
return v
|
|
}
|
|
|
|
/** "30d" / "12h" / "5m" in hours; NaN when it does not parse. */
|
|
function hours(s: string): number {
|
|
const m = /^(\d+)([ymdhs])$/.exec(s)
|
|
if (!m) return Number.NaN
|
|
const unit = { y: 8760, d: 24, h: 1, m: 1 / 60, s: 1 / 3600 }[m[2] as 'y']
|
|
return Number(m[1]) * unit
|
|
}
|
|
|
|
function invalid(loc: string, msg: string): Reply {
|
|
return { status: 422, body: { type: 'about:blank', title: 'Unprocessable Entity', status: 422, detail: msg, errors: [{ message: msg, location: `body.${loc}` }] } }
|
|
}
|
|
|
|
function validate(p: S['SettingsFormPatch']): Reply | undefined {
|
|
const r = p.retention
|
|
if (r && Number.isNaN(hours(r.hot))) return invalid('retention.hot', `"${r.hot}" is not a duration (examples: 30d, 5m, 1h30m)`)
|
|
if (r && hours(r.hot) < 24) return invalid('retention.hot', 'retention.hot must be at least 1d')
|
|
if (r && hours(r.long) > 10 * 8760) return invalid('retention.long', 'retention.long must be at most 10y')
|
|
if (p.backup?.at && !/^([01]\d|2[0-3]):[0-5]\d$/.test(p.backup.at)) return invalid('backup.at', `backup.at "${p.backup.at}": want HH:MM (24 hour)`)
|
|
return undefined
|
|
}
|
|
|
|
/** What the server reads back: credentials are write-only, so header values are blank and the password is only flagged. */
|
|
function viewOfReceivers(rs: S['ReceiverForm'][]): S['ReceiverView'][] {
|
|
return rs.map((r) => {
|
|
const { password, clear_password: _clear, ...smtp } = r.smtp
|
|
return { ...r, headers: Object.fromEntries(Object.keys(r.headers).map((k) => [k, ''])), smtp: { ...smtp, login_stored: password !== '' } }
|
|
})
|
|
}
|
|
|
|
export class SettingsFormStore {
|
|
private form = seeded()
|
|
private restart: string[] = []
|
|
/** Secrets stored through PUT /secrets/{name} (names only: values are write-only). */
|
|
readonly secrets: string[] = []
|
|
|
|
route(method: string, p: string, json: () => unknown): Reply | undefined {
|
|
const secret = /^\/secrets\/([^/]+)$/.exec(p)
|
|
if (secret && method === 'PUT') {
|
|
this.secrets.push(decodeURIComponent(secret[1] as string))
|
|
return { status: 204, body: undefined }
|
|
}
|
|
if (p !== '/settings/form') return undefined
|
|
if (method === 'GET') return { status: 200, body: { form: this.form, defaults: defaults(), restart_required: this.restart } satisfies S['SettingsFormState'] }
|
|
if (method !== 'PUT') return undefined
|
|
return this.put(json() as S['SettingsFormPatch'])
|
|
}
|
|
|
|
private put(patch: S['SettingsFormPatch']): Reply {
|
|
const bad = validate(patch)
|
|
if (bad) return bad
|
|
const changed: string[] = []
|
|
const next = { ...this.form } as Record<string, unknown>
|
|
for (const [k, w] of Object.entries(patch)) {
|
|
const v = k === 'receivers' ? viewOfReceivers(w as S['ReceiverForm'][]) : w
|
|
if (JSON.stringify(this.form[k as keyof View]) !== JSON.stringify(v)) changed.push(k)
|
|
next[k] = v
|
|
}
|
|
this.form = next as unknown as View
|
|
this.restart = [...new Set([...this.restart, ...changed.filter((k) => RESTART.has(k))])]
|
|
const applied = changed.filter((k) => !RESTART.has(k))
|
|
return { status: 200, body: { form: this.form, applied, restart_required: this.restart } satisfies S['SettingsFormApplied'] }
|
|
}
|
|
}
|