Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 575585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:57:23+00:00 2026-05-13T13:57:23+00:00

I’ve attempted to write a PHP class that gets the uptime of my Linux

  • 0

I’ve attempted to write a PHP class that gets the uptime of my Linux machine. It will get the uptime correctly, but I have an if statement that determines whether or not the load average is “high” or not and set a warning code, and it doesn’t seem to be working (it stays at 0).

I’ve included all of the code here from the class (50 or so lines) because I didn’t know what I could take out but still provide some info about what’s going wrong here.

<?php
class loadavg {

    private $divisor, $status;

    public function __construct($set_divisor = 1){
        $this->divisor = $set_divisor;
    }

    public function __toString(){
        return $this->load(1).', '.$this->load(5).', '.$this->load(15)."\n";
    }

    public function load($time = 1){
        $loadfile = shell_exec('cat /proc/loadavg');
        $split = preg_split('/ /', $loadfile);

        if ($split[1] > (2 * $this->divisor)){
            $this->status = 3;
        } else if ($split[1] > $this->divisor){
            $this->status = 2;
        } else {
            $this->status = 1;
        }

        switch($time){
            case 1:
                return $split[0];
            case 5:
                return $split[1];
            case 15:
            return $split[2];
        }
    }

    public function status_name(){
        switch ($this->status){
            case 3:
                return 'critical';
            case 2:
                return 'warn';
            case 1:
                return 'ok';
            case 0:
                return 'error';
        }
    }
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:57:23+00:00Added an answer on May 13, 2026 at 1:57 pm
    <?
    class loadavg {
    
        private $divisor, $status;
    
        public function __construct($set_divisor = 1){
            $this->divisor = $set_divisor;
        }
    
        public function __toString(){
            return $this->load(1).', '.$this->load(5).', '.$this->load(15)."\n";
        }
    
        public function load($time = 1){
            //$loadfile = shell_exec('cat /proc/loadavg');
            $loadfile = '1.5 1.5 1.5';
            $split = preg_split('/ /', $loadfile);
    
            if ($split[1] > (2 * $this->divisor)){
                $this->status = 3;
            } else if ($split[1] > $this->divisor){
                $this->status = 2;
            } else {
                $this->status = 1;
            }
    
            switch($time){
                case 1:
                    return $split[0];
                case 5:
                    return $split[1];
                case 15:
                return $split[2];
            }
        }
    
        public function status_name(){
            switch ($this->status){
                case 3:
                    return 'critical';
                case 2:
                    return 'warn';
                case 1:
                    return 'ok';
                case 0:
                    return 'error';
            }
        }
    }
    
    $la = new loadavg();
    print($la);
    print($la->status_name()); 
    

    I just executed this, and got the output:

    1.5, 1.5, 1.5
    warn
    

    Isn’t this what is expected? If I take out the print($la) statement, the output is this:

    error
    

    this is because the load() function is never called to set the status. If you want to have the status printed out correctly, you have to run load() first. I assume you’d want to add the time parameter to status_name and call load before returning, like this:

    public function status_name($time =1){
        $this->load($time);
        switch ($this->status){
            case 3:
                return 'critical';
            case 2:
                return 'warn';
            case 1:
                return 'ok';
            case 0:
                return 'error';
        }
    }
    

    which will now enable you to do this:

    $la = new loadavg();
    print($la->status_name(5)); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.