1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use std::{
net::SocketAddr,
path::{Path, PathBuf},
};
use clap::{Parser, Subcommand};
use miette::Result;
use tracing::{debug, info};
use super::{
TamanuArgs,
config::{TamanuConfig, load_config},
connection_url::ConnectionUrlBuilder,
find_tamanu,
};
use crate::actions::Context;
/// Run the alert daemon
///
/// The alert and target definitions are documented online at:
/// <https://github.com/beyondessential/bestool/blob/main/crates/alertd/ALERTS.md>
/// and <https://github.com/beyondessential/bestool/blob/main/crates/alertd/TARGETS.md>.
///
/// Configuration for database and email is read from Tamanu's config files.
#[derive(Debug, Clone, Parser)]
#[clap(verbatim_doc_comment)]
pub struct AlertdArgs {
#[command(subcommand)]
command: Command,
}
/// Common arguments for running the daemon
#[derive(Debug, Clone, Parser)]
struct DaemonArgs {
/// Glob patterns for alert definitions
///
/// Patterns can match directories (which will be read recursively) or individual files.
/// Can be provided multiple times.
/// Examples: /etc/tamanu/alerts, /opt/*/alerts, /etc/tamanu/alerts/**/*.yml
#[arg(long)]
glob: Vec<String>,
/// Execute all alerts once and quit (ignoring intervals)
#[arg(long)]
dry_run: bool,
/// Disable the HTTP server
#[arg(long)]
no_server: bool,
/// HTTP server bind address(es)
///
/// Can be provided multiple times. The server will attempt to bind to each address
/// in order until one succeeds. Defaults to [::1]:8271 and 127.0.0.1:8271
#[arg(long)]
server_addr: Vec<SocketAddr>,
/// Watchdog timeout in seconds
///
/// If no alert task reports activity within this many seconds, the daemon
/// will exit so the service manager can restart it. Defaults to 600 (10 minutes).
#[arg(long, default_value = "600")]
watchdog_timeout: u64,
/// Disable the watchdog
///
/// By default, the daemon will exit if no alert activity is detected within
/// the watchdog timeout. This flag disables that behavior.
#[arg(long)]
no_watchdog: bool,
}
#[derive(Debug, Clone, Subcommand)]
enum Command {
/// Run the alert daemon
///
/// Starts the daemon which monitors alert definition files and executes alerts
/// based on their configured schedules. The daemon will watch for file changes
/// and automatically reload when definitions are modified.
Run {
#[command(flatten)]
daemon: DaemonArgs,
},
/// Show status and health of a running daemon
///
/// Connects to the running daemon's HTTP API and displays version, uptime,
/// health, and watchdog information. Exits with code 1 if the daemon is unhealthy.
Status {
/// HTTP server address(es) to try
///
/// Can be provided multiple times. Will attempt to connect to each address
/// in order until one succeeds. Defaults to [::1]:8271 and 127.0.0.1:8271
#[arg(long)]
server_addr: Vec<SocketAddr>,
},
/// Send reload signal to running daemon
///
/// Connects to the running daemon's HTTP API and triggers a reload.
/// This is an alternative to SIGHUP that works on all platforms including Windows.
Reload {
/// HTTP server address(es) to try
///
/// Can be provided multiple times. Will attempt to connect to each address
/// in order until one succeeds. Defaults to [::1]:8271 and 127.0.0.1:8271
#[arg(long)]
server_addr: Vec<SocketAddr>,
},
/// List currently loaded alert files
///
/// Connects to the running daemon's HTTP API and retrieves the list of
/// currently loaded alert definition files.
LoadedAlerts {
/// HTTP server address(es) to try
///
/// Can be provided multiple times. Will attempt to connect to each address
/// in order until one succeeds. Defaults to [::1]:8271 and 127.0.0.1:8271
#[arg(long)]
server_addr: Vec<SocketAddr>,
/// Show detailed state information for each alert
#[arg(long)]
detail: bool,
},
/// Temporarily pause an alert
///
/// Pauses an alert until the specified time. The alert will not execute during
/// this period. The pause is lost when the daemon restarts.
PauseAlert {
/// Alert file path to pause
alert: String,
/// Time until which to pause the alert (fuzzy time format)
///
/// Examples: "1 hour", "2 days", "next monday", "2024-12-25T10:00:00Z"
/// Defaults to 1 week from now if not specified.
#[arg(long)]
until: Option<String>,
/// HTTP server address(es) to try
///
/// Can be provided multiple times. Will attempt to connect to each address
/// in order until one succeeds. Defaults to [::1]:8271 and 127.0.0.1:8271
#[arg(long)]
server_addr: Vec<SocketAddr>,
},
/// Validate an alert definition file
///
/// Parses an alert definition file and reports any syntax or validation errors.
/// Uses pretty error reporting to pinpoint the exact location of problems.
/// Requires the daemon to be running.
Validate {
/// Path to the alert definition file to validate
file: PathBuf,
/// HTTP server address(es) to try
///
/// Can be provided multiple times. Will attempt to connect to each address
/// in order until one succeeds. Defaults to [::1]:8271 and 127.0.0.1:8271
#[arg(long)]
server_addr: Vec<SocketAddr>,
},
/// Install the daemon as a Windows service
///
/// Creates a Windows service named 'bestool-alertd' that will start automatically
/// and starts it immediately.
#[cfg(windows)]
Install,
/// Uninstall the Windows service
///
/// Stops the 'bestool-alertd' Windows service if running and then removes it.
#[cfg(windows)]
Uninstall,
/// Configure failure recovery on an existing Windows service
///
/// Updates the 'bestool-alertd' service to automatically restart on failure.
/// This is done automatically on new installs, but can be run separately to
/// update an already-installed service.
#[cfg(windows)]
ConfigureRecovery,
#[cfg(windows)]
#[command(hide = true)]
Service {
#[command(flatten)]
daemon: DaemonArgs,
},
}
pub async fn run(ctx: Context<TamanuArgs, AlertdArgs>) -> Result<()> {
match ctx.args_sub.command {
Command::Status { server_addr } => {
let addrs = resolve_addrs(server_addr);
bestool_alertd::commands::get_status(&addrs).await
}
Command::Validate { file, server_addr } => {
let addrs = resolve_addrs(server_addr);
bestool_alertd::commands::validate_alert(&file, &addrs).await
}
Command::Reload { server_addr } => {
let addrs = resolve_addrs(server_addr);
bestool_alertd::commands::send_reload(&addrs).await
}
Command::LoadedAlerts {
server_addr,
detail,
} => {
let addrs = resolve_addrs(server_addr);
bestool_alertd::commands::get_loaded_alerts(&addrs, detail).await
}
Command::PauseAlert {
alert,
until,
server_addr,
} => {
let addrs = resolve_addrs(server_addr);
bestool_alertd::commands::pause_alert(&alert, until.as_deref(), &addrs).await
}
Command::Run { daemon } => {
let (_, root) = find_tamanu(&ctx.args_top)?;
let config = load_config(&root, None)?;
debug!(?config, "parsed Tamanu config");
let daemon_config = build_config(&root, config, daemon).await?;
bestool_alertd::run(daemon_config).await
}
#[cfg(windows)]
Command::Install => {
use std::ffi::OsString;
bestool_alertd::windows_service::install_service_with_args(&[
OsString::from("tamanu"),
OsString::from("alertd"),
OsString::from("service"),
])
}
#[cfg(windows)]
Command::Uninstall => bestool_alertd::windows_service::uninstall_service(),
#[cfg(windows)]
Command::ConfigureRecovery => bestool_alertd::windows_service::configure_recovery(),
#[cfg(windows)]
Command::Service { daemon } => {
let (_, root) = find_tamanu(&ctx.args_top)?;
let config = load_config(&root, None)?;
debug!(?config, "parsed Tamanu config");
// Check and auto-apply recovery configuration if needed
match bestool_alertd::windows_service::is_recovery_configured() {
Ok(false) => {
info!("failure recovery not configured, applying automatically");
if let Err(e) = bestool_alertd::windows_service::configure_recovery() {
tracing::warn!("failed to auto-configure recovery: {e}");
}
}
Err(e) => {
tracing::warn!("failed to check recovery configuration: {e}");
}
Ok(true) => {}
}
let daemon_config = build_config(&root, config, daemon).await?;
bestool_alertd::windows_service::run_service(daemon_config)
}
}
}
fn resolve_addrs(server_addr: Vec<SocketAddr>) -> Vec<SocketAddr> {
if server_addr.is_empty() {
bestool_alertd::commands::default_server_addrs()
} else {
server_addr
}
}
async fn build_config(
root: &Path,
config: TamanuConfig,
DaemonArgs {
glob,
dry_run,
no_server,
server_addr,
watchdog_timeout,
no_watchdog,
}: DaemonArgs,
) -> Result<bestool_alertd::DaemonConfig> {
let dirs = if glob.is_empty() {
default_dirs(root).await
} else {
glob
};
debug!(?dirs, "alert directories");
if dirs.is_empty() {
return Err(miette::miette!("no alert directories found or specified"));
}
info!("starting alertd daemon");
let database_url = ConnectionUrlBuilder {
username: config.db.username.clone(),
password: Some(config.db.password.clone()),
host: config
.db
.host
.clone()
.unwrap_or_else(|| "localhost".to_string()),
port: config.db.port,
database: config.db.name.clone(),
ssl_mode: None,
}
.build();
let email = config
.mailgun
.as_ref()
.map(|mg| bestool_alertd::EmailConfig {
from: mg.sender.clone(),
mailgun_api_key: mg.api_key.clone(),
mailgun_domain: mg.domain.clone(),
});
let watchdog = if no_watchdog {
None
} else {
Some(std::time::Duration::from_secs(watchdog_timeout))
};
let mut daemon_config = bestool_alertd::DaemonConfig::new(dirs, database_url)
.with_dry_run(dry_run)
.with_no_server(no_server)
.with_server_addrs(server_addr)
.with_watchdog_timeout(watchdog);
if let Some(email) = email {
daemon_config = daemon_config.with_email(email);
}
Ok(daemon_config)
}
async fn default_dirs(root: &std::path::Path) -> Vec<String> {
use futures::future::join_all;
let mut dirs = vec![
PathBuf::from(r"C:\Tamanu\alerts"),
root.join("alerts"),
PathBuf::from("/opt/tamanu-toolbox/alerts"),
PathBuf::from("/etc/tamanu/alerts"),
PathBuf::from("/alerts"),
];
if let Ok(cwd) = std::env::current_dir() {
dirs.push(cwd.join("alerts"));
}
join_all(
dirs.into_iter()
.map(|dir| async { if dir.exists() { Some(dir) } else { None } }),
)
.await
.into_iter()
.flatten()
.map(|p| p.display().to_string())
.collect()
}