Skip to content

Commit

Permalink
Merge pull request #240 from TripleSD/dev
Browse files Browse the repository at this point in the history
New version 0.1.9 (build 029)
  • Loading branch information
AntonMZ authored May 26, 2020
2 parents fc5e2b1 + 9d1b5c7 commit 84848e4
Show file tree
Hide file tree
Showing 26 changed files with 262 additions and 167 deletions.
15 changes: 12 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
[v.0.1.8 (Build.028)](https://github.com/TripleSD/moring/releases/tag/0.1.6)
- **(New)** - Added treshold pings values of sites.
[v.0.1.9 (Build.029)](https://github.com/TripleSD/moring/releases/tag/0.1.9)
- **(Upd)** - Refactoring sites repository.
- **(Upd)** - Refactoring old migrations.
- **(Upd)** - Refactoring templates.
- **(Upd)** - Upgraded ping commands.
- **(Upd)** - Refactoring SitesChecker. Added options (web/cli/debug). Removed pcntl_fork & added curl for sites check.
- **(Fix)** - Refactoring SitesChecker. Added check only enabled sites.
- **(Fix)** - Refactoring SitesSslChecker. Added check only enabled sites.

[v.0.1.8 (Build.028)](https://github.com/TripleSD/moring/releases/tag/0.1.8)
- **(New)** - Added threshold pings values of sites.
- **(New)** - Added showing site's ip address.
- **(Upd)** - Updated some templates.
- **(Upd)** - Updated values of .env file.

[v.0.1.7 (Build.027)](https://github.com/TripleSD/moring/releases/tag/0.1.6)
[v.0.1.7 (Build.027)](https://github.com/TripleSD/moring/releases/tag/0.1.7)
- **(New)** - Added showing errors list for SNMP device.
- **(New)** - Added showing errors counts for SNMP devices list.
- **(New)** - Added Telegram notifications for SNMP device (up/down).
Expand Down
132 changes: 99 additions & 33 deletions app/Console/Commands/SitesChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\SitesPhpVersions;
use App\Models\SitesWebServers;
use Carbon\Carbon;
use Exception;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Str;
Expand All @@ -20,7 +21,7 @@ class SitesChecker extends Command
*
* @var string
*/
protected $signature = 'SitesChecker';
protected $signature = 'SitesChecker {--cli} {--web} {--debug}';

/**
* The console command description.
Expand All @@ -39,36 +40,56 @@ public function __construct()
$this->telegramConnector = new TelegramConnector();
}

public function handle(int $site_id = null)
public function handle(int $site_id = null, $mode = null)
{
$cli = false;
$debug = false;

if ($mode === null) {
$cli = $this->option('cli');
$debug = $this->option('debug');
}

if ($cli) {
$this->info('Start script.');
$this->line('------------------------------------');

$start = microtime(true);
}

if ($site_id === null) {
$sites = Sites::get();
$sites = Sites::where('enabled', 1)->get();
$tgMessage = 0;
} else {
$sites[] = Sites::find($site_id);
$tgMessage = 1;
}

/*
* http_response_code()
* true - web
* false - cli
*/

foreach ($sites as $site) {
if (http_response_code() == false) {
$fork = pcntl_fork();
if ($cli) {
$this->warn("Start check: $site->url");
$startTaskTime = microtime(true);
}
self::ckeckSite($site, $cli, $debug);

if ($fork) {
echo $site->title . PHP_EOL;
self::ckeckSite($site);
die(0);
}
} else {
self::ckeckSite($site);
if ($cli) {
$endTaskTime = microtime(true);
$delta = $endTaskTime - $startTaskTime;

$this->warn('End check!');
$this->warn('Time: ' . $delta . 'sec.');
$this->line('------------------------------------');
}
}

if ($cli) {
$finish = microtime(true);
$delta = $finish - $start;

$this->info('Finish!');
$this->info("Total time: $delta sec");
}

if ($this->settingsController->getTelegramStatus() === 1) {
try {
$date = Carbon::now()->format('Y-m-d H:i:s');
Expand All @@ -93,10 +114,9 @@ public function handle(int $site_id = null)
}
}

private function ckeckSite($site): void
private function ckeckSite($site, $cli = false, $debug = false): void
{
Sites::where('id', $site->id)->update(['ip_address' => gethostbyname($site->url)]);

try {
if ($site->checksList->use_file === 1) {
$httpClient = new Client();
Expand All @@ -109,27 +129,58 @@ private function ckeckSite($site): void
$webServerType = $responseArray['web-server'];
$phpBranch = $responseArray['php-branch'];
} else {
$httpClient = new Client();
$url = ($site->https === 1 && $site->checksList->check_https === 1) ? 'https://' . $site->url : 'http://' . $site->url;
$response = $httpClient->request('GET', $url, ['allow_redirects' => false]);
$phpVersion = $response->getHeader('X-Powered-By');
$webServerType = $response->getHeader('server');
$url = ($site->https === 1 && $site->checksList->check_https === 1) ? 'https://' . $site->url : 'http://' . $site->url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

if ($webServerType != null) {
$webServerType = $webServerType[0];
} else {
$output = curl_exec($ch);
$headers = [];
$output = rtrim($output);
$output = strtolower($output);
$data = explode("\n", $output);
$headers['status'] = $data[0];

array_shift($data);

foreach ($data as $part) {
//some headers will contain ":" character (Location for example), and the part after ":" will be lost, Thanks to @Emanuele
$middle = explode(':', $part, 2);

//Supress warning message if $middle[1] does not exist, Thanks to @crayons
if (! isset($middle[1])) {
$middle[1] = null;
}

$headers[trim($middle[0])] = trim($middle[1]);
}

if ($cli) {
print_r($headers);
}

$phpVersion = $headers['x-powered-by'] ?? null;
$webServerType = $headers['server'];

if ($webServerType === null) {
$webServerType = 0;
}

if (preg_match('/^[0-9]*/', $response->getStatusCode())) {
$statusCode = $response->getStatusCode();
if (preg_match('/^[0-9]*/', $headers['status'])) {
preg_match('/[\d][\d][\d]/', $headers['status'], $rawStatusCode);
$statusCode = $rawStatusCode[0];
} else {
$statusCode = 999;
}

if ($phpVersion != null) {
if (preg_match('/^PHP/', $phpVersion[0])) {
preg_match('/[\d][.]\d{1,2}[.]\d{1,2}/', $phpVersion[0], $rawPhpVersion);
if (preg_match('/^PHP|php/', $phpVersion)) {
preg_match('/[\d][.]\d{1,2}[.]\d{1,2}/', $phpVersion, $rawPhpVersion);
$phpVersion = $rawPhpVersion[0];
$phpBranchRaw = explode('.', $phpVersion);
$phpBranchRaw = $phpBranchRaw[0] * 10000 + $phpBranchRaw[1] * 100 + $phpBranchRaw[2];
Expand All @@ -147,6 +198,18 @@ private function ckeckSite($site): void
$ssl = new SitesSSLChecker();
$ssl->handle($site->id);

// HTTP code saving process
$http = SitesHttpCodes::where('site_id', $site->id)->first();
if (isset($http)) {
$http->http_code = $statusCode;
} else {
$fillable = ['site_id' => $site->id, 'http_code' => $statusCode];
$http = new SitesHttpCodes($fillable);
}

$ssl = new SitesSSLChecker();
$ssl->handle($site->id);

// HTTP code saving process
$http = SitesHttpCodes::where('site_id', $site->id)->first();
if (isset($http)) {
Expand Down Expand Up @@ -187,7 +250,10 @@ private function ckeckSite($site): void
$pending->pending = 0;
$pending->save();
}
} catch (\Exception $e) {
} catch (Exception $e) {
if ($debug) {
$this->warn($e->getMessage());
}
}
}
}
20 changes: 18 additions & 2 deletions app/Console/Commands/SitesSSLChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,25 @@ public function __construct()
public function handle(int $site_id = null)
{
if ($site_id === null) {
$checksList = SitesChecksList::where('check_ssl', 1)->with('site')->get();
$checksList = SitesChecksList::with('site')
->where('check_ssl', 1)
->whereHas(
'site',
function ($q) {
$q->where('enabled', 1);
}
)
->get();
} else {
$checksList = SitesChecksList::where([['check_ssl', 1], ['site_id', $site_id]])->with('site')->get();
$checksList = SitesChecksList::with('site')
->where([['check_ssl', 1], ['site_id', $site_id]])
->whereHas(
'site',
function ($q) {
$q->where('enabled', 1);
}
)
->get();
}

foreach ($checksList as $check) {
Expand Down
11 changes: 7 additions & 4 deletions app/Console/Ping.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ class Ping

public static function pingTarget(string $host): array
{
if (self::OS != 'WINNT') {
exec("ping -c 3 $host", $output, $return_var);
} else {
if (self::OS === 'Darwin') {
exec("ping -c 3 -i 0.5 $host", $output, $return_var);
} elseif (self::OS === 'WINNT') {
exec("ping -n 3 $host", $output, $return_var);
} else {
exec("ping -c 3 -A $host", $output, $return_var);
}

if (count($output) > 3) {
$filter = [];
$filter = [];
$pre_final = array_reduce(
$output,
function ($filter, $string) {
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/Admin/Sites/SitesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function store(StoreSiteRequest $request)
if ($result) {
// Run first site check
$check = new SitesChecker();
$check->handle((int) ($result->id));
$check->handle((int) ($result->id), 'web');

// Run first site ping as well
$ping = new SitesPings();
Expand Down Expand Up @@ -222,8 +222,8 @@ public function refresh($id)
$site->update(['ip_address' => gethostbyname($site->url)]);

// Starting checks
$check = new SitesChecker($id);
$check->handle($id);
$check = new SitesChecker();
$check->handle($id, 'web');
$ping = new SitesPings();
$ping->handle($id);

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/Sites/StoreSiteRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function rules()
'check_php' => 'integer | max:1',
'check_ssl' => 'integer | max:1',
'comment' => 'nullable | max:255',
'ping_trashold' => 'integer | nullable',
'ping_threshold' => 'integer | nullable',
];
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/Sites/UpdateSiteRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function rules()
'check_php' => 'integer | max:1',
'check_ssl' => 'integer | max:1',
'comment' => 'nullable | max:255',
'ping_trashold' => 'integer | nullable',
'ping_threshold' => 'integer | nullable',
];
}

Expand Down
2 changes: 1 addition & 1 deletion app/Models/Sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Sites extends Model
'enabled',
'pending',
'ip_address',
'ping_trashold',
'ping_threshold',
];

use SoftDeletes;
Expand Down
3 changes: 2 additions & 1 deletion app/Repositories/AdminSitesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public function index($request)
public function store(array $fillable)
{
// Now we check, if checkbox https selected otherwise we set check_ssl and check_https to zero
if ((int) ($fillable['https']) === 0) {
if (isset($fillable['https']) === false) {
$fillable['https'] = 0;
$fillable['check_ssl'] = 0;
$fillable['check_https'] = 0;
} else {
Expand Down
10 changes: 7 additions & 3 deletions app/Repositories/Sites/SitesBackendRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

namespace App\Repositories\Sites;

use App\Console\Commands\SitesChecker;
use App\Console\Commands\SitesSSLChecker;
use App\Repositories\Repository;
use Illuminate\Support\Facades\Artisan;

class SitesBackendRepository extends Repository
{
public function refreshList()
{
Artisan::call('SitesChecker');
Artisan::call('SitesSSLChecker');
$siteChecker = new SitesChecker();
$siteChecker->handle(null, 'web');

$siteSslChecker = new SitesSSLChecker();
$siteSslChecker->handle();
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"laravel/ui": "^1.1",
"laravelcollective/html": "^6.0",
"spatie/ssl-certificate": "^1.16",
"ext-pcntl": "*"
"ext-curl": "*"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.2",
Expand Down
4 changes: 2 additions & 2 deletions config/moring.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

return [
'build' => '028',
'version' => '0.1.8',
'build' => '029',
'version' => '0.1.9',
'createYear' => '2019',
'bridgeUrl' => 'http://api.moring.ru/v1',
'bridgeCurrentMoringVersionUrl' => '/moring/version',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function up()
'sites_checks_list',
function (Blueprint $table) {
$table->boolean('check_php')
->default(1)
->default(0)
->after('http_code');
}
);
Expand Down
Loading

0 comments on commit 84848e4

Please sign in to comment.