Skip to content

Commit 6dd79fd

Browse files
authored
more cs fix changes (#41)
* more cs fix changes * add typing and doc blocks * fix php stand paths * Update maint.php * use cacti php stand and csfixer configs * Update composer.json * Update composer.json * make sure to use the core cacti folder * Update composer.json * Update plugin-ci-workflow.yml * only scan maint plugin folder * . * use proper php stan this sould fix it !! * Update composer.json * Update composer.json
1 parent c1cb584 commit 6dd79fd

6 files changed

Lines changed: 2011 additions & 1761 deletions

File tree

.github/.phpstan.neon

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
parameters:
22
level: 6
33
paths:
4-
- .
4+
- %currentWorkingDirectory%/setup.php
5+
- %currentWorkingDirectory%/functions.php
6+
- %currentWorkingDirectory%/maint.php
57
excludePaths:
68
- vendor
79
- locales

.github/workflows/plugin-ci-workflow.yml

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ jobs:
124124
composer validate --strict || true
125125
fi
126126
127-
- name: Install Composer Dependencies
127+
- name: Install Composer Dependencies for Cacti
128128
run: |
129129
cd ${{ github.workspace }}/cacti
130130
if [ -f composer.json ]; then
@@ -181,22 +181,10 @@ jobs:
181181
exit 1
182182
fi
183183
184-
- name: Verify Maint Plugin Composer Setup
185-
run: |
186-
ls -la ${{ github.workspace }}/cacti/plugins/maint
187-
cat ${{ github.workspace }}/cacti/plugins/maint/composer.json
188-
189-
- name: Install Maint Plugin Composer Dependencies
190-
run: composer install --prefer-dist --no-progress --no-interaction
184+
- name: Install Plugin Composer Dependencies
185+
run: composer install --prefer-dist --no-progress
191186
working-directory: ${{ github.workspace }}/cacti/plugins/maint
192-
env:
193-
COMPOSER_NO_DEV: 0
194187

195-
- name: Verify Maint Plugin Vendor Binaries
196-
run: |
197-
ls -la ${{ github.workspace }}/cacti/plugins/maint/vendor
198-
ls -la ${{ github.workspace }}/cacti/plugins/maint/vendor/bin
199-
200188
- name: Run Linter on base code
201189
run: composer run-script lint
202190
working-directory: ${{ github.workspace }}/cacti/plugins/maint

composer.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
"description": "Cacti maintenance window plugin.",
44
"type": "project",
55
"license": "GPL-2.0-or-later",
6-
"require": {},
76
"require-dev": {
8-
"friendsofphp/php-cs-fixer": "^3.60",
7+
"friendsofphp/php-cs-fixer": "^3.93",
98
"phpstan/phpstan": "^1.11",
109
"overtrue/phplint": "^9.2"
1110
},
1211
"scripts": {
13-
"lint": "./vendor/bin/phplint -c .github/.phplint.yml",
14-
"phpcsfixer": "./vendor/bin/php-cs-fixer fix --dry-run --diff --config .github/.php-cs-fixer.php",
15-
"phpstan": "./vendor/bin/phpstan analyse -c .github/.phpstan.neon"
12+
"lint": "bash -c 'cd ../../ && plugins/maint/vendor/bin/phplint plugins/maint --exclude=vendor'",
13+
"phpcsfixer": "bash -c 'cd ../../ && plugins/maint/vendor/bin/php-cs-fixer fix --dry-run --diff --config=.php-cs-fixer.php plugins/maint'",
14+
"phpstan": "bash -c 'cd ../../ && plugins/maint/vendor/bin/phpstan analyse --level=6 plugins/maint'"
1615
}
1716
}

functions.php

Lines changed: 115 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -24,92 +24,134 @@
2424
+-------------------------------------------------------------------------+
2525
*/
2626

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);
3038
}
3139

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);
3549
}
3650

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);
4060
}
4161

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 *
4773
FROM plugin_maint_hosts
4874
WHERE TYPE = ?
4975
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;
6188
}
6289

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 *
67104
FROM plugin_maint_schedules
68105
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
100139
SET stime = ?, etime = ?
101140
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;
115157
}

0 commit comments

Comments
 (0)