| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Health\Checks\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Health\Checks\BaseCheck; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Health\Checks\HealthCheckInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Health\Checks\Traits\FormatTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class DiskUsage extends BaseCheck implements HealthCheckInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     use FormatTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * Default disk usage threshold of 1 % | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var integer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     const DEFAULT_THRESHOLD = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Default Path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     const DEFAULT_PATH = '/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @see \Health\Checks\HealthCheckInterface::call() | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     public function call() | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $builder = $this->getBuilder(); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         $path = $this->getParam('path', self::DEFAULT_PATH); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         $threshold = $this->getParam('threshold', self::DEFAULT_THRESHOLD); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         if ($threshold > 100 || $threshold < 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |             return $builder->down()->withData('error', 'Invalid Threshold - ' . $threshold)->build(); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $free = disk_free_space($path); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $total = disk_total_space($path); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $usage = $total - $free; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $percentage = ($usage / $total) * 100; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         if ($percentage >= $threshold) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             $builder->up(); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             $builder->down(); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $builder->withData('free_bytes', $free) | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             ->withData('free_human', $this->formatBytes($free)) | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             ->withData('usage', $usage) | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             ->withData('usage_human', $this->formatBytes($usage)) | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             ->withData('path', $path) | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             ->withData('threshold', $threshold); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         return $builder->build(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  |