Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1 from io-digital/feature/server-utilization
Browse files Browse the repository at this point in the history
Add cpu and memory usage checks
  • Loading branch information
garethnic authored Jul 26, 2017
2 parents 812dc15 + 911fc27 commit f869810
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 6 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip

## NEXT - 2017-07-26

### Added
- Added check for memory usage
- Added check for cpu usage

### Deprecated
- Nothing

### Fixed
- Nothing

### Removed
- Nothing

### Security
- Nothing

## NEXT - 2017-07-26

### Added
- Added check for SSL
- Created package config file
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ You currently have the options to select which tests to perform:
'ssl' => true,
'database' => true,
'application' => true,

//System information
'memory' => false,
'cpu_usage' => false,
```

## Usage
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@
}
],
"require": {
"php": "~5.6|~7.0",
"illuminate/support": "~5.1",
"php" : "~5.6|~7.0"
"linfo/linfo": "^3.0"
},
"require-dev": {
"phpunit/phpunit" : "~4.0||~5.0||~6.0",
"squizlabs/php_codesniffer": "^2.3"
},
"autoload": {
"psr-4": {
"IoDigital\\HealthCheck\\": "src"
"IoDigital\\HealthCheck\\": "src/"
}
},
"autoload-dev": {
Expand Down
52 changes: 51 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 63 additions & 3 deletions src/HealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

namespace IoDigital\HealthCheck;

require(__DIR__ . '/../vendor/autoload.php');

use Linfo\Linfo;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class HealthCheck
{
/**
* @var Linfo
*/
protected $linfo;

public function __construct()
{
//
$this->linfo = new Linfo();
}

/**
Expand Down Expand Up @@ -89,14 +97,58 @@ private function sslTest()
}
}

/**
* Check memory usage
*
* @return array
*/
private function memoryTest()
{
$info = $this->linfo->getParser();

return [
'memory' => [
'message' => $info->getRam(),
'success' => true
]
];
}

/**
* Check CPU usage
*
* @return array
*/
private function cpuTest()
{
$info = $this->linfo->getParser();
$os = $info->getOS();

if (strcmp($os, 'Linux') === 0) {
return [
'cpu_usage' => [
'message' => $info->getCPUUsage(),
'success' => true
]
];
} else {
return [
'cpu_usage' => [
'message' => 'Not applicable on ' . $os,
'success' => true
]
];
}
}

/**
* Check and perform selected health checks
*
* @return array
*/
private function performChecks()
{
$ssl = $database = $application = [];
$ssl = $database = $application = $memory = $cpu = [];

if (config('healthcheck.ssl') === true) {
$ssl = $this->sslTest();
Expand All @@ -110,6 +162,14 @@ private function performChecks()
$application = $this->applicationTest();
}

return array_merge($application, $database, $ssl);
if (config('healthcheck.cpu_usage') === true) {
$cpu = $this->cpuTest();
}

if (config('healthcheck.memory') === true) {
$memory = $this->memoryTest();
}

return array_merge($application, $database, $ssl, $memory, $cpu);
}
}
4 changes: 4 additions & 0 deletions src/config/healthcheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
'ssl' => true,
'database' => true,
'application' => true,

//System information
'memory' => false,
'cpu_usage' => false,
];

0 comments on commit f869810

Please sign in to comment.