Laravel 服务器与应用健康监控与通知器


README

Laravel 服务器与应用健康监控与通知器

Latest Stable Version License Code Quality

Build Downloads Coverage PHP

此包检查应用资源是否按预期运行并创建服务状态面板。以下是其主要特点:

  • 高度可扩展和可配置:您可以非常容易地创建新的检查器和通知器,并且可以几乎更改它的一切。
  • 易于配置:使用 YAML 作为配置文件
  • 弹性资源检查器:如果框架正在运行并且至少有一个通知渠道,您应该会收到通知消息。
  • 内置通知系统:通过邮件、Slack、Telegram 或您需要的任何其他方式获取通知。
  • 路由:面板、JSON 结果、字符串结果和资源。
  • 可配置的面板设计。
  • 缓存。
  • 计划检查,以便在服务失败时自动接收通知。
  • 在面板中直接查看应用程序错误消息。
  • 错误时,对于 Envoyer 等服务,显示 HTTP 响应代码 200 和 500,以跟踪您的应用程序健康。

内置资源

Health 预配置了以下服务的资源检查器:

  • Adyen
  • AppKey
  • APIs
  • 广播
  • 缓存
  • ConfigurationCached
  • 证书
  • 数据库
  • DebugMode
  • DirectoryPermissions
  • DiskSpace
  • DocuSign
  • ElasticsearchConnectable
  • EnvExists
  • Filesystem
  • Framework
  • Horizon
  • Http
  • Https
  • LaravelServices
  • Latency
  • LocalStorage
  • Mail
  • MailgunConnectable
  • MemcachedConnectable
  • MigrationsUpToDate
  • MySql
  • MySqlConnectable
  • NewrelicDeamon
  • NginxServer
  • PackagesUpToDate
  • Php
  • PostgreSqlConnectable
  • PostgreSqlServer
  • Queue
  • QueueWorkers
  • RebootRequired
  • Redis
  • RedisConnectable
  • RedisServer
  • RoutesCached
  • S3
  • SecurityChecker
  • SeeTickets
  • Sendinblue
  • ServerLoad
  • ServerUptime
  • Sshd
  • Supervisor

但您可以添加任何其他所需的内容,您只需要找到正确的检查器来使用或创建一个新的检查器以检查您的资源。

易于配置

创建新的资源监控器非常简单,只需在应用的 config/health 文件夹中创建一个新的 YAML 文件即可。以下是一些示例:

Amazon S3

name: S3
abbreviation: s3
checker: PragmaRX\Health\Checkers\CloudStorageChecker
notify: true
driver: s3
file: pragmarx-health-s3-testfile.txt
contents: {{ str_random(32) }}
error_message: 'Amazon S3 connection is failing.'
column_size: 4

Nginx

name: NginxServer
abbreviation: ngnxsrvr
checker: PragmaRX\Health\Checkers\ProcessChecker
command: 'pgrep %s'
method: process_count
process_name: nginx
instances:
    minimum:
        count: 4
        message: 'Process "%s" has not enough instances running: it has %s, when should have at least %s'
    maximum:
        count: 8
        message: 'Process "%s" exceeded the maximum number of running instances: it has %s, when should have at most %s'
notify: true
pid_file_missing_error_message: 'Process ID file is missing: %s.'
pid_file_missing_not_locked: 'Process ID file is not being used by any process: %s.'
column_size: 4

屏幕截图

面板

default panel

面板替代设计

如果您要检查许多服务,您可以将默认的面板设计更改为使用更少的空间。

default panel

4 列布局的面板

default panel

错误消息

将鼠标悬停在失败的资源上,即可立即访问错误消息。

default panel

点击资源按钮,您将获得一个显示错误消息的警报。

default panel

Slack 通知

以下是通过 Slack 发送的示例通知。

default panel

Artisan 控制台命令

health:panel

使用 health:panel 命令在控制台查看服务的状态。

health:check

使用 health:check 命令检查所有资源,并在失败时发送通知。

default panel

路由

安装后,您将有权访问以下路由:

/health/panel

主要面板路由。

/health/check

返回一个包含该软件包所知道的所有服务信息的json对象

default panel

/health/string

返回一个包含所有服务状态信息的字符串,当使用其他监控服务时很有用

hlthFAIL-dbFAIL-filesystemOK-frmwrkOK-httpOK-httpsOK-mailOK

/health/resource/{name}

返回一个包含特定服务信息的json对象

default panel

要求

  • PHP 7.1+
  • Laravel 5.6+

安装

使用Composer安装

composer require pragmarx/health

在Laravel上安装

将服务提供者添加到你的 config/app.php

PragmaRX\Health\ServiceProvider::class,

发布配置和视图

php artisan vendor:publish --provider="PragmaRX\Health\ServiceProvider"

访问健康面板

http://yourdomain.com/health/panel

配置所有设置

几乎所有的配置都可以在这个包中轻松完成

  • 面板名称
  • 标题和消息
  • 资源检查器
  • Slack图标
  • 通知渠道
  • 模板位置
  • 路由和前缀
  • 邮件服务器
  • 缓存
  • 调度器

配置二进制文件

一些检查器需要你配置正确的二进制路径才能工作

'services' => [
    'ping' => [
        'bin' => env('HEALTH_PING_BIN', '/sbin/ping'),
    ],

    'composer' => [
        'bin' => env('HEALTH_COMPOSER_BIN', 'composer'),
    ],
],

允许Slack通知

为了通过Slack接收通知,你需要设置Incoming Webhooks并将此方法添加到你的用户模型中,并使用你的webhook

/**
 * Route notifications for the Slack channel.
 *
 * @return string
 */
public function routeNotificationForSlack()
{
    return config('services.slack.webhook_url');
}

缓存

当健康结果被缓存时,你可以通过向URL添加?flush=true来刷新缓存,以便重新处理所有资源

http://yourdomain.com/health/panel?flush=true

事件

如果你更喜欢构建自己的通知系统,你可以禁用它并监听以下事件

PragmaRX\Health\Events\RaiseHealthIssue::class

广播检查器

广播检查器通过ping和pong系统完成。广播检查器将ping你的服务,它必须pong回。基本上你需要做的就是在带有一些数据的URL上进行回调

Redis + Socket.io

var request = require('request');
var server = require('http').Server();
var io = require('socket.io')(server);
var Redis = require('ioredis');
var redis = new Redis();

redis.subscribe('pragmarx-health-broadcasting-channel');

redis.on('message', function (channel, message) {
    message = JSON.parse(message);

    if (message.event == 'PragmaRX\\Health\\Events\\HealthPing') {
        request.get(message.data.callbackUrl + '?data=' + JSON.stringify(message.data));
    }
});

server.listen(3000);

Pusher

<!DOCTYPE html>
<html>
    <head>
        <title>Pusher Test</title>
        <script src="https://js.pusher.com/3.2/pusher.min.js"></script>
        <script>
            var pusher = new Pusher('YOUR-PUSHER-KEY', {
                encrypted: true
            });

            var channel = pusher.subscribe('pragmarx-health-broadcasting-channel');

            channel.bind('PragmaRX\\Health\\Events\\HealthPing', function(data) {
                var request = (new XMLHttpRequest());

                request.open("GET", data.callbackUrl + '?data=' + JSON.stringify(data));

                request.send();
            });
        </script>
    </head>

    <body>
        Pusher waiting for events...
    </body>
</html>

程序化检查资源

$generalHealthState = app('pragmarx.health')->checkResources();

// or 

$databaseHealthy = app('pragmarx.health')->checkResource('database')->isHealthy();

在artisan命令中的检查示例

Artisan::command('database:health', function () {
    app('pragmarx.health')->checkResource('database')->isHealthy()
        ? $this->info('database is healthy')
        : $this->info('database is in trouble')
    ;
})->describe('Check database health');

Lumen

要在Lumen中使用它,你可能在你的bootstrap/app.php中需要做类似这样的操作

$app->instance('path.config', app()->basePath() . DIRECTORY_SEPARATOR . 'config');
$app->instance('path.storage', app()->basePath() . DIRECTORY_SEPARATOR . 'storage');

$app->withFacades();

$app->singleton('Illuminate\Contracts\Routing\ResponseFactory', function ($app) {
    return new \Illuminate\Routing\ResponseFactory(
        $app['Illuminate\Contracts\View\Factory'],
        $app['Illuminate\Routing\Redirector']
    );
});

$app->register(PragmaRX\Health\ServiceProvider::class);

测试

$ composer test

作者

Antonio Carlos Ribeiro

许可证

Health在BSD 3-Clause许可证下授权 - 有关详细信息,请参阅LICENSE文件

贡献

欢迎提交拉取请求和问题。