Storage
banshee/storage-collection/storage/truenas/https/api-v2.0/
snapshots
The state of each periodic snapshot task of a TrueNAS, to see a task in error, by its REST API v2.0.
par banshee1 installation1 version
v1Dernière
Publiée le 27 octobre 2026. First version.
// The periodic snapshot tasks of a TrueNAS, by its REST API v2.0 (GET /api/v2.0/pool/snapshottask), behind an
// API key: the state of the last run of each, so that a snapshot task in error is seen. The REST API v2.0
// answers on TrueNAS CORE 12 and 13 and on TrueNAS SCALE up to 25.10.
need('protocol:http');
$address = need('host:address');
$token = need('protocol:http:token');
per('task', 'Snapshot tasks');
output('state', 'text', array('label' => 'State', 'per' => 'task'));
watch.state('failed', 'Snapshot task in error', 'state', array('critical' => array('error')));
watch.state('held', 'Snapshot task on hold', 'state', array('warning' => array('hold')));
watch.missing('missing', 'Snapshot task missing', 'state', array('enabled' => false));
// The key goes in a header: the answer is not secret, its body having carried none.
$answer = http.get('https://' . $address . '/api/v2.0/pool/snapshottask', headers=array('Authorization' => 'Bearer ' . $token));
if ($answer['status'] == 401 || $answer['status'] == 403) {
fail('misconfigured', 'The TrueNAS API refuses the key, code ' . $answer['status']);
}
if ($answer['status'] != 200) {
fail('unreachable', 'The TrueNAS API answers code ' . $answer['status'],
'A TrueNAS 26 or later has no REST API: it answers 404.');
}
// A list of tasks: {"id": 3, "dataset": "tank/home", "naming_schema": "auto-%Y-%m-%d_%H-%M", "enabled": true,
// "state": {"state": "FINISHED", ...}}. The states zettarepl gives a task: RUNNING, FINISHED, ERROR, HOLD,
// and a task with no state yet (never run) reads "pending". A task disabled in TrueNAS reads "disabled".
// A task is named after its dataset; two tasks on one dataset (hourly and daily) each keep their own, the
// second carrying its id.
$tasks = json.read($answer['body']);
$seen = array();
foreach ($tasks as $task) {
$name = $task['dataset'];
if (list.has($seen, $name)) {
$name = $name . ' #' . $task['id'];
}
$seen[$name] = true;
$state = 'disabled';
if ($task['enabled']) {
$state = text.lower(list.get($task['state'], 'state', 'pending'));
}
value('state', $state, $name);
}