Files
zoneminder/db/zm_update-1.39.42.sql
T
Isaac ConnorandClaude Opus 5 a30665c164 feat: run monitor actions at a time of day refs #4957
that already exist -- lights on, lights off, play a sound -- at set times.

MonitorActions already says what to do and to which device, with triggers for
EventStart, Alarm, Manual and so on. So this adds a trigger rather than a
subsystem: an action whose TriggerOn is 'Schedule' carries a five field cron
spec, and cron runs it.

Cron, rather than a daemon of our own, because the web user owns a crontab it
can rewrite without privilege, and because the line cron runs,

  30 22 * * * /usr/bin/zmcontrol.pl --id 24 --command=lightOff

is the same invocation web/includes/Monitor.php already falls back to when no
zmcontrol daemon is listening. A scheduled action therefore takes the path a
manual one does rather than a parallel one of its own. It also means no new
code runs as root, nothing new has to be supervised, and the feature is a
table column plus a crontab block.

Nothing a form can set reaches a shell. The schedule is refused unless
cronScheduleIsValid() accepts it -- five fields, numbers and the usual
*/,-/ only, with newline, carriage return and % rejected outright because
each would end the line or the command. The command itself comes from
MonitorAction::commandFor()'s fixed list and the ids are integers, so the
whole line is assembled from constrained parts. That is a narrower surface
than Filters.AutoExecuteCmd, which has taken an arbitrary command for years.

The crontab is not ours to own, so ZoneMinder rewrites only a block between
two markers and leaves every other line byte for byte. A block whose end
marker is missing, which is what an interrupted write leaves, is treated as
no block at all rather than swallowing everything after it: losing our own
stale block is the safe failure, losing the admin's entries is not. The block
is rebuilt from the table on every save, so the crontab is a projection of
the database rather than a second place to keep the truth.

zmc recognises the new trigger and skips it, so having one does not warn on
every reload, and nothing fires a scheduled action from the capture process.

An action targeting a monitor on another server is left out and logged: cron
runs zmcontrol.pl locally, and the web ui's proxy through the API has no cron
equivalent. Single server installs are unaffected.

The two pieces that can lose data or run the wrong thing are pure, and
tests/php/test_crontab.php covers them: schedule validation including the
injection cases, and splicing a block in, out and over without disturbing the
lines around it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-10-28 18:01:25 -05:00

50 lines
1.8 KiB
SQL

--
-- This updates a 1.39.41 database to 1.39.42
--
-- Let a monitor action run at a time of day as well as on an event.
--
-- MonitorActions already says what to do (LightOn, AudioPlay, ...) and to
-- which device; all that was missing was "at 22:30" alongside the existing
-- EventStart/Alarm/Manual triggers. So this adds a trigger rather than a
-- table: a scheduled action is an ordinary action whose TriggerOn is
-- 'Schedule' and whose Schedule column holds a five field cron spec.
--
-- The spec is written into the web user's own crontab, which runs
-- zmcontrol.pl exactly as the web ui does for a manual action. Nothing
-- validates it on the way in from the database, so whatever writes here must
-- have passed it through cronScheduleIsValid() first -- see
-- web/includes/CronTab.php.
--
SELECT 'Checking for Schedule in MonitorActions';
SET @s = (SELECT IF(
(SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'MonitorActions'
AND table_schema = DATABASE()
AND column_name = 'Schedule'
) > 0,
"SELECT 'Column Schedule already exists in MonitorActions'",
"ALTER TABLE `MonitorActions` ADD COLUMN `Schedule` varchar(64) NOT NULL default '' AFTER `TriggerOn`"
));
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT 'Checking for Schedule in the MonitorActions.TriggerOn enum';
SET @s = (SELECT IF(
(SELECT LOCATE('Schedule', COLUMN_TYPE)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'MonitorActions'
AND table_schema = DATABASE()
AND column_name = 'TriggerOn'
) > 0,
"SELECT 'TriggerOn already offers Schedule'",
"ALTER TABLE `MonitorActions` MODIFY `TriggerOn` enum('EventStart','EventEnd','Alarm','AlarmEnd','Manual','Schedule') NOT NULL default 'EventStart'"
));
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;