How To Check RAM And CPU Usage In Laravel

Websolutionstuff | Jul-29-2020 | Categories : Laravel

In this tutorial, I will show you how to check RAM and CPU usage in laravel in ubuntu OS. Many times we require to check how much RAM and CPU usage of consumption in the ubuntu server. if we have no code then we need to check manually every time. So, here I will give you an example of checking ubuntu server RAM and CPU usage. So, you can get data of RAM and CPU usage in your admin panel.

The sys_getloadavg() function returns the system load average. This function returns an array with three numbers that represent the average system load over the last 1, 5 and 15 minutes.

So, Create a controller and add a function for CPU usage Linux and check ram usage Linux.

public function total_ram_cpu_usage()
{
        //RAM usage
        $free = shell_exec('free'); 
        $free = (string) trim($free);
        $free_arr = explode("\n", $free);
        $mem = explode(" ", $free_arr[1]);
        $mem = array_filter($mem);
        $mem = array_merge($mem);
        $usedmem = $mem[2];
        $usedmemInGB = number_format($usedmem / 1048576, 2) . ' GB';
        $memory1 = $mem[2] / $mem[1] * 100;
        $memory = round($memory1) . '%';
        $fh = fopen('/proc/meminfo', 'r');
        $mem = 0;
        while ($line = fgets($fh)) {
            $pieces = array();
            if (preg_match('/^MemTotal:\s+(\d+)\skB$/', $line, $pieces)) {
                $mem = $pieces[1];
                break;
            }
        }
        fclose($fh);
        $totalram = number_format($mem / 1048576, 2) . ' GB';
        
        //cpu usage
        $cpu_load = sys_getloadavg(); 
        $load = $cpu_load[0] . '% / 100%';
        
        return view('details',compact('memory','totalram','usedmemInGB','load'));
}

 

 

And create details.blade.php and add the below code for view. 

<html>
<head>
  <link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <div class="col-sm-6 col-md-3">
    <h2 class="no-margin text-semibold">Current RAM Usage</h2>
    <div class="progress progress-micro mb-10">
      <div class="progress-bar bg-indigo-400" style="width: {{$memory}}">
        <span class="sr-only">{{$memory}}</span>
      </div>
    </div>
    <span class="pull-right">{{$usedmemInGB}} / {{$totalram}} ({{$memory}})</span>  

  </div>

  <div class="col-sm-6 col-md-3">
    <h2 class="no-margin text-semibold">Current CPU Usage</h2>
    <div class="progress progress-micro mb-10">
      <div class="progress-bar bg-indigo-400" style="width: {{$load}}">
        <span class="sr-only">{{$load}}</span>
      </div>
    </div>
    <span class="pull-right">{{$load}}</span>   
  </div>

</body>
</html>

And you will now get the current RAM usage Linux and CPU usage Linux.

 


You might also like:

Recommended Post
Featured Post
Laravel 10 Scout Search and Algolia Example
Laravel 10 Scout Search and Al...

Hey everyone! Ever wished your Laravel application could deliver lightning-fast search results? Well, I've got great...

Read More

Feb-21-2024

Send Email In Laravel
Send Email In Laravel

In this article, we will explore the process of sending emails in Laravel, covering versions 6, 7, 8, 9, and 10. Email f...

Read More

Sep-02-2020

How To Generate PDF File In Laravel 10
How To Generate PDF File In La...

In this article, we will see how to generate a pdf file in laravel 10. Here, we will learn about laravel 10 ge...

Read More

Mar-10-2023

Laravel Clear Cache Using Artisan Command
Laravel Clear Cache Using Arti...

In this tutorial, I am giving information about laravel artisan command which can help you to clear your application'...

Read More

May-18-2020