Files
Varaverk/Plugin/usr/local/emhttp/plugins/varaverk/include/config.php
T
Gmer4Lfe 57d55c647c varaverk: replace User Scripts dependency with native job tracking
run_job.sh: new wrapper called by scheduler cron for every job.
  Writes /var/log/varaverk/<id>.json (status/start/end/exit/pid)
  and /var/log/varaverk/<id>.log (latest run output, overwritten).
  Maps exit 0→ok, 1→warn, 2+→error so watchdog "took action" runs
  show warn not error.

scheduler: cron now calls bash run_job.sh instead of inline bash -c.
  Added vv_job_stat_path(). LOG_DIR moved to config.php (shared).

monitor: vv_scripts_status() now reads /var/log/varaverk/*.json and
  */*.json — no User Scripts tmpScripts dependency at all.
  Stale running detection via /proc/<pid> check.
  Returns error_count in addition to running/ok/warn counts.

pages/monitor.php: added error pill (red ✗), error icon/color in
  list, duration shown next to age for each script entry.
2026-05-24 17:51:31 -04:00

78 lines
2.6 KiB
PHP

<?php
// Config file parser and writer.
// Reads master.conf and the appropriate host*.conf based on running host.
define('PLUGIN_CFG', '/boot/config/plugins/varaverk/varaverk.cfg');
$_vv_cfg = @parse_ini_file(PLUGIN_CFG) ?: [];
define('SCRIPTS_DIR', $_vv_cfg['SCRIPTS_DIR'] ?? '/mnt/user/appdata/unraid_scripts');
define('CONF_DIR', SCRIPTS_DIR . '/Configurations');
define('LOG_DIR', '/var/log/varaverk');
unset($_vv_cfg);
function vv_get_hostname(): string {
return trim(shell_exec('hostname -s') ?: '');
}
function vv_detect_host(): string {
// Reads master.conf to find HOST1/HOST2 hostnames, returns 'host1', 'host2', or 'unknown'
$master = vv_read_conf_raw('master.conf');
preg_match('/^\s*HOST1_NAME\s*=\s*["\']?(\S+?)["\']?\s*$/m', $master, $m1);
preg_match('/^\s*HOST2_NAME\s*=\s*["\']?(\S+?)["\']?\s*$/m', $master, $m2);
$hostname = vv_get_hostname();
if (!empty($m1[1]) && strcasecmp($hostname, $m1[1]) === 0) return 'host1';
if (!empty($m2[1]) && strcasecmp($hostname, $m2[1]) === 0) return 'host2';
return 'unknown';
}
function vv_is_owner(): bool {
return vv_detect_host() === 'host1';
}
function vv_read_conf_raw(string $filename): string {
$path = CONF_DIR . '/' . $filename;
return file_exists($path) ? file_get_contents($path) : '';
}
function vv_write_conf_raw(string $filename, string $content): bool {
$path = CONF_DIR . '/' . $filename;
return file_put_contents($path, $content) !== false;
}
function vv_get_conf_files(): array {
// Returns conf files this host is allowed to view/edit
$host = vv_detect_host();
$files = [];
if ($host === 'host1') {
$files[] = 'master.conf';
$files[] = 'host1.conf';
} elseif ($host === 'host2') {
$files[] = 'host2.conf';
} else {
// Unknown host — show all for dev/debug
foreach (glob(CONF_DIR . '/*.conf') as $f) {
$files[] = basename($f);
}
}
return $files;
}
// Parse conf into key=>value map for $VAR substitution in docs
function vv_conf_vars(): array {
$vars = [];
$files = ['master.conf'];
$host = vv_detect_host();
if ($host === 'host1') $files[] = 'host1.conf';
if ($host === 'host2') $files[] = 'host2.conf';
foreach ($files as $f) {
$raw = vv_read_conf_raw($f);
// Match: VAR_NAME="value" or VAR_NAME=value (no quotes)
preg_match_all('/^\s*([A-Z0-9_]+)\s*=\s*["\']?([^"\'#\n]*?)["\']?\s*(?:#.*)?$/m', $raw, $m);
foreach ($m[1] as $i => $key) {
$vars[$key] = trim($m[2][$i]);
}
}
return $vars;
}