|
24 | 24 | +-------------------------------------------------------------------------+ |
25 | 25 | */ |
26 | 26 |
|
27 | | -function plugin_maint_check_cacti_host($host) |
28 | | -{ |
29 | | - return plugin_maint_check_host(1, $host); |
| 27 | +/** |
| 28 | + * Check if a Cacti host is in maintenance |
| 29 | + * |
| 30 | + * This is called via the 'is_device_in_maintenance' hook. |
| 31 | + * |
| 32 | + * @param int $host Host ID to check |
| 33 | + * |
| 34 | + * @return bool True if host is in active maintenance, false otherwise |
| 35 | + */ |
| 36 | +function plugin_maint_check_cacti_host(int $host): bool { |
| 37 | + return plugin_maint_check_host(1, $host); |
30 | 38 | } |
31 | 39 |
|
32 | | -function plugin_maint_check_webseer_url($host) |
33 | | -{ |
34 | | - return plugin_maint_check_host(2, $host); |
| 40 | +/** |
| 41 | + * Check if a WebSeer URL is in maintenance |
| 42 | + * |
| 43 | + * @param int $host WebSeer URL ID to check |
| 44 | + * |
| 45 | + * @return bool True if URL is in active maintenance, false otherwise |
| 46 | + */ |
| 47 | +function plugin_maint_check_webseer_url(int $host): bool { |
| 48 | + return plugin_maint_check_host(2, $host); |
35 | 49 | } |
36 | 50 |
|
37 | | -function plugin_maint_check_servcheck_test($host) |
38 | | -{ |
39 | | - return plugin_maint_check_host(3, $host); |
| 51 | +/** |
| 52 | + * Check if a Servcheck test is in maintenance |
| 53 | + * |
| 54 | + * @param int $host Servcheck test ID to check |
| 55 | + * |
| 56 | + * @return bool True if test is in active maintenance, false otherwise |
| 57 | + */ |
| 58 | +function plugin_maint_check_servcheck_test(int $host): bool { |
| 59 | + return plugin_maint_check_host(3, $host); |
40 | 60 | } |
41 | 61 |
|
42 | | - |
43 | | -function plugin_maint_check_host($type, $host) |
44 | | -{ |
45 | | - $schedules = db_fetch_assoc_prepared( |
46 | | - 'SELECT * |
| 62 | +/** |
| 63 | + * Check if a host is in maintenance based on type |
| 64 | + * |
| 65 | + * @param int $type Host type (1=Cacti host, 2=WebSeer URL, 3=Servcheck test) |
| 66 | + * @param int $host Host/URL/Test ID to check |
| 67 | + * |
| 68 | + * @return bool True if host is in active maintenance schedule, false otherwise |
| 69 | + */ |
| 70 | +function plugin_maint_check_host(int $type, int $host): bool { |
| 71 | + $schedules = db_fetch_assoc_prepared( |
| 72 | + 'SELECT * |
47 | 73 | FROM plugin_maint_hosts |
48 | 74 | WHERE TYPE = ? |
49 | 75 | AND (host = ? OR host = 0)', |
50 | | - [$type, $host], |
51 | | - ); |
52 | | - |
53 | | - if (!empty($schedules)) { |
54 | | - foreach ($schedules as $s) { |
55 | | - if (plugin_maint_check_schedule($s['schedule'])) { |
56 | | - return true; |
57 | | - } |
58 | | - } |
59 | | - } |
60 | | - return false; |
| 76 | + [$type, $host], |
| 77 | + ); |
| 78 | + |
| 79 | + if (!empty($schedules)) { |
| 80 | + foreach ($schedules as $s) { |
| 81 | + if (plugin_maint_check_schedule($s['schedule'])) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return false; |
61 | 88 | } |
62 | 89 |
|
63 | | -function plugin_maint_check_schedule($schedule) |
64 | | -{ |
65 | | - $sc = db_fetch_row_prepared( |
66 | | - 'SELECT * |
| 90 | +/** |
| 91 | + * Check if a maintenance schedule is currently active |
| 92 | + * |
| 93 | + * Handles both one-time and recurring schedules. |
| 94 | + * For recurring schedules that have passed, automatically calculates |
| 95 | + * and updates the next occurrence. |
| 96 | + * |
| 97 | + * @param int $schedule Schedule ID to check |
| 98 | + * |
| 99 | + * @return bool True if schedule is active now, false otherwise |
| 100 | + */ |
| 101 | +function plugin_maint_check_schedule(int $schedule): bool { |
| 102 | + $sc = db_fetch_row_prepared( |
| 103 | + 'SELECT * |
67 | 104 | FROM plugin_maint_schedules |
68 | 105 | WHERE enabled = \'on\' AND id = ?', |
69 | | - [$schedule], |
70 | | - ); |
71 | | - |
72 | | - if (!empty($sc)) { |
73 | | - $t = time(); |
74 | | - switch ($sc['mtype']) { |
75 | | - case 1: |
76 | | - if ($t > $sc['stime'] && $t < $sc['etime']) { |
77 | | - return true; |
78 | | - } |
79 | | - break; |
80 | | - |
81 | | - case 2: // Recurring |
82 | | - /* past, calculate next */ |
83 | | - if ($sc['etime'] < $t) { |
84 | | - /* convert start and end to local so that hour stays same for add days across daylight saving time change */ |
85 | | - $starttimelocal = (new DateTime('@' . strval($sc['stime'])))->setTimezone(new DateTimeZone(date_default_timezone_get())); |
86 | | - $endtimelocal = (new DateTime('@' . strval($sc['etime'])))->setTimezone(new DateTimeZone(date_default_timezone_get())); |
87 | | - $nowtime = new DateTime(); |
88 | | - /* add interval days */ |
89 | | - $addday = new DateInterval('P' . strval($sc['minterval'] / 86400) . 'D'); |
90 | | - while ($endtimelocal < $nowtime) { |
91 | | - $starttimelocal = $starttimelocal->add($addday); |
92 | | - $endtimelocal = $endtimelocal->add($addday); |
93 | | - } |
94 | | - |
95 | | - $sc['stime'] = $starttimelocal->getTimestamp(); |
96 | | - $sc['etime'] = $endtimelocal->getTimestamp(); |
97 | | - /* save next interval so not need to recalculate */ |
98 | | - db_execute_prepared( |
99 | | - 'UPDATE plugin_maint_schedules |
| 106 | + [$schedule], |
| 107 | + ); |
| 108 | + |
| 109 | + if (!empty($sc)) { |
| 110 | + $t = time(); |
| 111 | + |
| 112 | + switch ($sc['mtype']) { |
| 113 | + case 1: |
| 114 | + if ($t > $sc['stime'] && $t < $sc['etime']) { |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + break; |
| 119 | + case 2: // Recurring |
| 120 | + // past, calculate next |
| 121 | + if ($sc['etime'] < $t) { |
| 122 | + // convert start and end to local so that hour stays same for add days across daylight saving time change |
| 123 | + $starttimelocal = (new DateTime('@' . strval($sc['stime'])))->setTimezone(new DateTimeZone(date_default_timezone_get())); |
| 124 | + $endtimelocal = (new DateTime('@' . strval($sc['etime'])))->setTimezone(new DateTimeZone(date_default_timezone_get())); |
| 125 | + $nowtime = new DateTime(); |
| 126 | + // add interval days |
| 127 | + $addday = new DateInterval('P' . strval($sc['minterval'] / 86400) . 'D'); |
| 128 | + |
| 129 | + while ($endtimelocal < $nowtime) { |
| 130 | + $starttimelocal = $starttimelocal->add($addday); |
| 131 | + $endtimelocal = $endtimelocal->add($addday); |
| 132 | + } |
| 133 | + |
| 134 | + $sc['stime'] = $starttimelocal->getTimestamp(); |
| 135 | + $sc['etime'] = $endtimelocal->getTimestamp(); |
| 136 | + // save next interval so not need to recalculate |
| 137 | + db_execute_prepared( |
| 138 | + 'UPDATE plugin_maint_schedules |
100 | 139 | SET stime = ?, etime = ? |
101 | 140 | WHERE id = ?', |
102 | | - [$sc['stime'], $sc['etime'], $schedule], |
103 | | - ); |
104 | | - /* format yyyy-mm-dd hh:mm */ |
105 | | - cacti_log('INFO: Maintenance schedule "' . $sc['name'] . '" Next start ' . $starttimelocal->format('Y-m-d H:i') |
106 | | - . ' End ' . $endtimelocal->format('Y-m-d H:i'), false, 'MAINT'); |
107 | | - } |
108 | | - if ($t > $sc['stime'] && $t < $sc['etime']) { |
109 | | - return true; |
110 | | - } |
111 | | - break; |
112 | | - } |
113 | | - } |
114 | | - return false; |
| 141 | + [$sc['stime'], $sc['etime'], $schedule], |
| 142 | + ); |
| 143 | + // format yyyy-mm-dd hh:mm |
| 144 | + cacti_log('INFO: Maintenance schedule "' . $sc['name'] . '" Next start ' . $starttimelocal->format('Y-m-d H:i') |
| 145 | + . ' End ' . $endtimelocal->format('Y-m-d H:i'), false, 'MAINT'); |
| 146 | + } |
| 147 | + |
| 148 | + if ($t > $sc['stime'] && $t < $sc['etime']) { |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + break; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return false; |
115 | 157 | } |
0 commit comments