OPNsense Forum

English Forums => General Discussion => Topic started by: hboetes on November 06, 2026, 12:07:38 PM

Title: Cleaning up expired firewall schedule entries (with a cron job)
Post by: hboetes on November 06, 2026, 12:07:38 PM
Hi all,

A schedule's time ranges only store a month and a day. There is no year. So every one-off maintenance window you ever added will happily open again next year on the same date. We had about forty of those piled up, some going back a year. Issue #7350 runs into the same thing from another angle.

There's no API for schedules, so I wrote a small PHP script that runs on the firewall itself. It removes all dates that are in the past, keeps anything still coming up, and leaves weekday-based ranges alone. Since a schedule needs at least one range, it adds a placeholder on 31 December from 0:00 to 0:00. I tested it: a 0:00-0:00 range is an empty window, so the rules never become active from it.

It saves through write_config(), so the change shows up in the config history like any other and you can roll it back from there. Without options it's a dry run.

Put this in /usr/local/sbin/schedule_cleanup.php:

<?php
/*
* Remove expired dates from OPNsense firewall schedules and give every
* schedule exactly one empty placeholder entry ("keep", 31 Dec 0:00-0:00).
*
*   php schedule_cleanup.php            dry run, shows what would change
*   php schedule_cleanup.php --write    saves via write_config()
*   php schedule_cleanup.php --cron     saves and reloads the filter
*                                       (only when something changed)
*/
require_once("config.inc");
require_once(
"util.inc");

$cron = in_array("--cron", $argv);
$write = $cron || in_array("--write", $argv);
$today = (int)date("n") * 100 + (int)date("j");
$keep = ["month" => "12", "day" => "31", "hour" => "0:00-0:00", "rangedescr" => "keep"];

if (empty(
$config["schedules"]["schedule"])) {
   exit(
"no schedules found\n");
}
$scheds = &$config["schedules"]["schedule"];
if (isset(
$scheds["name"])) {
   
$scheds = [$scheds];
}

$changed = false;
foreach (
$scheds as &$s) {
   
$ranges = $s["timerange"] ?? [];
   if (isset(
$ranges["hour"])) {
       
$ranges = [$ranges];
   }
   
$new = [];
   
$msg = [];
   foreach (
$ranges as $t) {
       
$hour = $t["hour"] ?? "";
       
$descr = $t["rangedescr"] ?? "";
       if (
$hour === "0:00-0:00") {
           
$msg[] = sprintf("%-24s drop placeholder   %s %s/%s %s\n", $s["name"], $hour,
               
$t["month"] ?? "-", $t["day"] ?? "-", $descr);
           continue;
       }
       if (empty(
$t["month"])) {
           
$new[] = $t;            // weekday based, untouched
           
continue;
       }
       
$m = explode(",", $t["month"]);
       
$d = explode(",", $t["day"]);
       
$km = [];
       
$kd = [];
       
$gone = [];
       foreach (
$m as $i => $mm) {
           if ((int)
$mm * 100 + (int)$d[$i] < $today) {
               
$gone[] = "$mm/{$d[$i]}";
           } else {
               
$km[] = $mm;
               
$kd[] = $d[$i];
           }
       }
       if (
count($gone) == 0) {
           
$new[] = $t;
           continue;
       }
       if (
count($km) == 0) {
           
$msg[] = sprintf("%-24s drop expired       %s %s %s\n", $s["name"], $hour,
               
implode(" ", $gone), $descr);
           continue;
       }
       
$msg[] = sprintf("%-24s trim               %s removed %s, kept %s %s\n", $s["name"],
           
$hour, implode(" ", $gone), implode(" ", array_map(
               fn(
$a, $b) => "$a/$b", $km, $kd)), $descr);
       
$t["month"] = implode(",", $km);
       
$t["day"] = implode(",", $kd);
       
$new[] = $t;
   }
   
$new[] = $keep;
   if (
$new == $ranges) {
       continue;                  
// already clean
   
}
   echo
implode("", $msg);
   
printf("%-24s add keep           12/31 0:00-0:00\n\n", $s["name"]);
   
$s["timerange"] = $new;
   
$changed = true;
}
unset(
$s);

if (!
$changed) {
   echo
"nothing to do.\n";
   exit(
0);
}
if (!
$write) {
   echo
"\ndry run, nothing written (use --write)\n";
   exit(
0);
}
write_config("schedule cleanup: expired dates removed, keep entry added");
if (!
$cron) {
   echo
"\nwritten; run: `configctl filter reload` to activate the new schedule\n";
   exit(
0);
}
echo
"\nwritten; reloading filter\n";
exec("/usr/local/sbin/configctl filter reload 2>&1", $out, $rc);
echo
implode("\n", $out), "\n";
exit(
$rc);

Run it once without options and read what it wants to do before you add --write.

To run it from cron, the cron page only lets you pick configd actions, so you need a small action file. Put this in /usr/local/opnsense/service/conf/actions.d/actions_schedulecleanup.conf:

[run]
command:/usr/local/bin/php /usr/local/sbin/schedule_cleanup.php --cron
parameters:
type:script_output
message:removing expired schedule entries
description:Remove expired firewall schedule entries

Then do a service configd restart and test it with configctl schedulecleanup run. After that "Remove expired firewall schedule entries" shows up under System > Settings > Cron. I run it once a month, which is plenty since an expired date only comes back a year later.

Two things that cost me some time: write_config() needs util.inc as well, otherwise you get "Call to undefined function shell_safe()". And the parameter field in the cron job does nothing unless the action file passes it on with %s, which is why --cron is hardcoded in the command line.

Of course it would be much nicer if schedules had a year, or a "one-off" option, and if they had an API. Until then, maybe this is useful to someone.

Han