Skip to content

Commit

Permalink
add cpu usage check
Browse files Browse the repository at this point in the history
  • Loading branch information
sarfraznawaz2005 committed Dec 8, 2019
1 parent 910f993 commit 493107e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The package comes with following checks out of the box. Checks can be divided in

- :white_check_mark: Required PHP extensions are installed
- :white_check_mark: Disk Space Enough
- :white_check_mark: Average CPU Usage
- :white_check_mark: FTP Connection Works
- :white_check_mark: SFTP Connection Works
- :white_check_mark: SSL Certificate Valid
Expand Down
48 changes: 48 additions & 0 deletions src/Checks/Server/AvgCpuUsage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Created by PhpStorm.
* User: Sarfraz
* Date: 7/10/2019
* Time: 2:39 PM
*/

namespace Sarfraznawaz2005\ServerMonitor\Checks\Server;

use Sarfraznawaz2005\ServerMonitor\Checks\Check;

class AvgCpuUsage implements Check
{
private $percent;

/**
* Perform the actual verification of this check.
*
* @param array $config
* @return bool
*/
public function check(array $config): bool
{
$this->percent = round($this->getCPUUsagePercentage(), 2);

$failPercentage = $config ['fail_percentage'];

return !($this->percent >= $failPercentage);
}

protected function getCPUUsagePercentage()
{
$cpu = shell_exec("grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}'");

return (float)$cpu;
}

/**
* The error message to display in case the check does not pass.
*
* @return string
*/
public function message(): string
{
return 'Average CPU usage at: ' . $this->percent . '%';
}
}
3 changes: 3 additions & 0 deletions src/Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
\Sarfraznawaz2005\ServerMonitor\Checks\Server\DiskSpaceEnough::class => [
'fail_percentage' => 90
],
\Sarfraznawaz2005\ServerMonitor\Checks\Server\AvgCpuUsage::class => [
'fail_percentage' => 90
],

/*
\Sarfraznawaz2005\ServerMonitor\Checks\Server\CheckPhpIniValues::class => [
Expand Down
4 changes: 2 additions & 2 deletions src/ServerMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function runChecks(): array
'name' => getCheckerName($check, $config),
'status' => $status,
'error' => $error,
'time' => sprintf("%dms", $eTime),
'time' => sprintf('%dms', $eTime),
];
}
}
Expand Down Expand Up @@ -159,7 +159,7 @@ public function runCheck($checkClass): array
'name' => getCheckerName($check, $config),
'status' => $status,
'error' => $error,
'time' => sprintf("%dms", $eTime),
'time' => sprintf('%dms', $eTime),
];
}
}
Expand Down

0 comments on commit 493107e

Please sign in to comment.