@@ -65,12 +65,17 @@ private function checkServiceStatus(): array
65
65
private function checkConfigValidity (): array
66
66
{
67
67
$ output = shell_exec ('caddy validate --config /etc/caddy/Caddyfile 2>&1 ' );
68
- $ isValid = strpos ($ output , 'valid ' ) !== false ;
68
+
69
+ // Check if output contains success indicators (case insensitive)
70
+ $ outputLower = strtolower ($ output );
71
+ $ isValid = (strpos ($ outputLower , 'valid configuration ' ) !== false ) ||
72
+ (strpos ($ outputLower , 'configuration is valid ' ) !== false ) ||
73
+ (strpos ($ outputLower , 'valid ' ) !== false && strpos ($ outputLower , 'error ' ) === false );
69
74
70
75
return [
71
76
'status ' => $ isValid ? 'healthy ' : 'unhealthy ' ,
72
77
'message ' => $ isValid ? 'Configuration is valid ' : 'Configuration has errors ' ,
73
- 'details ' => $ output ,
78
+ 'details ' => $ output ?: ' No output from validation command ' ,
74
79
];
75
80
}
76
81
@@ -151,6 +156,115 @@ private function checkLogErrors(): array
151
156
];
152
157
}
153
158
159
+ public function refreshHealth (): void
160
+ {
161
+ $ this ->runHealthCheck ();
162
+ }
163
+
164
+ public function getHealthResults (): array
165
+ {
166
+ // Calculate overall health status and score
167
+ $ healthyCount = 0 ;
168
+ $ warningCount = 0 ;
169
+ $ unhealthyCount = 0 ;
170
+ $ totalChecks = count ($ this ->healthData );
171
+
172
+ foreach ($ this ->healthData as $ check ) {
173
+ switch ($ check ['status ' ]) {
174
+ case 'healthy ' :
175
+ $ healthyCount ++;
176
+ break ;
177
+ case 'warning ' :
178
+ $ warningCount ++;
179
+ break ;
180
+ case 'unhealthy ' :
181
+ $ unhealthyCount ++;
182
+ break ;
183
+ }
184
+ }
185
+
186
+ // Calculate overall score (healthy = 100%, warning = 50%, unhealthy = 0%)
187
+ $ score = $ totalChecks > 0 ? round (($ healthyCount * 100 + $ warningCount * 50 ) / $ totalChecks ) : 0 ;
188
+
189
+ // Determine overall status
190
+ $ overallStatus = 'healthy ' ;
191
+ if ($ unhealthyCount > 0 ) {
192
+ $ overallStatus = 'unhealthy ' ;
193
+ } elseif ($ warningCount > 0 ) {
194
+ $ overallStatus = 'warning ' ;
195
+ }
196
+
197
+ return [
198
+ 'timestamp ' => now ()->format ('Y-m-d H:i:s ' ),
199
+ 'overall ' => [
200
+ 'status ' => $ overallStatus ,
201
+ 'score ' => $ score ,
202
+ 'healthy_count ' => $ healthyCount ,
203
+ 'warning_count ' => $ warningCount ,
204
+ 'unhealthy_count ' => $ unhealthyCount ,
205
+ 'total_checks ' => $ totalChecks ,
206
+ ],
207
+ 'summary ' => [
208
+ 'healthy ' => $ healthyCount ,
209
+ 'warning ' => $ warningCount ,
210
+ 'critical ' => $ unhealthyCount ,
211
+ 'total ' => $ totalChecks ,
212
+ ],
213
+ 'checks ' => [
214
+ 'service ' => [
215
+ [
216
+ 'name ' => 'Caddy Service Status ' ,
217
+ 'status ' => $ this ->healthData ['service_status ' ]['status ' ],
218
+ 'message ' => $ this ->healthData ['service_status ' ]['message ' ],
219
+ 'details ' => $ this ->healthData ['service_status ' ]['details ' ],
220
+ ],
221
+ ],
222
+ 'configuration ' => [
223
+ [
224
+ 'name ' => 'Configuration Validity ' ,
225
+ 'status ' => $ this ->healthData ['config_validity ' ]['status ' ],
226
+ 'message ' => $ this ->healthData ['config_validity ' ]['message ' ],
227
+ 'details ' => $ this ->healthData ['config_validity ' ]['details ' ],
228
+ ],
229
+ ],
230
+ 'network ' => [
231
+ [
232
+ 'name ' => 'Port Availability ' ,
233
+ 'status ' => $ this ->healthData ['port_availability ' ]['status ' ],
234
+ 'message ' => $ this ->healthData ['port_availability ' ]['message ' ],
235
+ 'details ' => $ this ->healthData ['port_availability ' ]['details ' ],
236
+ ],
237
+ [
238
+ 'name ' => 'Apache Connectivity ' ,
239
+ 'status ' => $ this ->healthData ['apache_connectivity ' ]['status ' ],
240
+ 'message ' => $ this ->healthData ['apache_connectivity ' ]['message ' ],
241
+ 'details ' => $ this ->healthData ['apache_connectivity ' ]['details ' ],
242
+ ],
243
+ ],
244
+ 'system ' => [
245
+ [
246
+ 'name ' => 'SSL Certificates ' ,
247
+ 'status ' => $ this ->healthData ['ssl_certificates ' ]['status ' ],
248
+ 'message ' => $ this ->healthData ['ssl_certificates ' ]['message ' ],
249
+ 'details ' => $ this ->healthData ['ssl_certificates ' ]['details ' ],
250
+ ],
251
+ [
252
+ 'name ' => 'Disk Space ' ,
253
+ 'status ' => $ this ->healthData ['disk_space ' ]['status ' ],
254
+ 'message ' => $ this ->healthData ['disk_space ' ]['message ' ],
255
+ 'details ' => $ this ->healthData ['disk_space ' ]['details ' ],
256
+ ],
257
+ [
258
+ 'name ' => 'Log Errors ' ,
259
+ 'status ' => $ this ->healthData ['log_errors ' ]['status ' ],
260
+ 'message ' => $ this ->healthData ['log_errors ' ]['message ' ],
261
+ 'details ' => $ this ->healthData ['log_errors ' ]['details ' ],
262
+ ],
263
+ ],
264
+ ],
265
+ ];
266
+ }
267
+
154
268
public function getTitle (): string
155
269
{
156
270
return 'Caddy Health Check ' ;
0 commit comments