jcergolj/extra-checks-for-spatie-laravel-server-monitor

为 Spatie laravel-server-monitor 包提供的附加自定义检查

v1.4 2023-05-24 10:23 UTC

This package is auto-updated.

Last update: 2024-08-30 01:20:48 UTC


README

首先,您需要安装 Spatie Server Monitor 包

然后,您需要安装此包

composer require jcergolj/extra-checks-for-spatie-laravel-server-monitor

最后,将以下检查添加到 config\server-monitor.php 文件中

'checks' => [
    'diskspace' => Spatie\ServerMonitor\CheckDefinitions\Diskspace::class,
    'elasticsearch' => Spatie\ServerMonitor\CheckDefinitions\Elasticsearch::class,
    'memcached' => Spatie\ServerMonitor\CheckDefinitions\Memcached::class,
    'mysql' => Spatie\ServerMonitor\CheckDefinitions\MySql::class,
    'cpu-load' => Jcergolj\CustomChecks\CpuLoadCheck::class,
    'redis' => Jcergolj\CustomChecks\RedisCheck::class,
    'redis-memory' => Jcergolj\CustomChecks\RedisMemoryCheck::class,
    'horizon-artisan-command' => Jcergolj\CustomChecks\HorizonArtisanCommandCheck::class,
    'horizon-supervisor' => Jcergolj\CustomChecks\HorizonSupervisorCheck::class,
    'horizon-worker' => Jcergolj\CustomChecks\HorizonWorkerCheck::class,
    'queue-worker' => Jcergolj\CustomChecks\QueueWorkerCheck::class,
    'db-connection-count' => Jcergolj\CustomChecks\DbConnectionCountCheck::class,
],

自定义检查概述

CpuLoadCheck

它检查过去 1、5 和 15 分钟的服务器负载

它将在服务器上执行此命令:uptime

您可以在 server-monitor.php 配置文件中指定 CPU 负载阈值。如果没有提供,默认值为 1.3。

// config/server-monitor.php
'cpu_load' => [
    'one_minute_threshold' => 1.6,
    'five_minute_threshold' => 1.2,
    'fifteen_minute_threshold' => 1.1,
]

RedisCheck

它检查 Redis 是否正在运行。

它将在服务器上执行此命令:redis-cli ping

RedisMemoryCheck

它检查当前 Redis 内存消耗。

它将在服务器上执行此命令:redis-cli info memory

您可以在 server-monitor.php 配置文件中指定 Redis 内存阈值。默认值为 5MB。

// config/server-monitor.php
'redis' => [
    'memory_threshold' => 6000000,
]

HorizonArtisanCommandCheck

它检查 horizon artisan 进程是否正在运行。

它将在服务器上执行此命令:ps aux | grep -E ".*php([0-9]\.[0-9])? .*artisan horizon$" | grep -v grep

您可以在 server-monitor.php 配置文件中指定服务器上应运行的 horizon 进程数。默认值为 1。

// config/server-monitor.php
'horizon' => [
    'artisan_command_processes' => 2,
]

HorizonSupervisorCheck

它检查 horizon supervisor 进程是否正在运行。

它将在服务器上执行此命令:ps aux | grep -E ".*php([0-9]\.[0-9])? .*artisan horizon:supervisor" | grep -v grep

您可以在 server-monitor.php 配置文件中指定服务器上应运行的 horizon supervisor 进程数。默认值为 1。

// config/server-monitor.php
'horizon' => [
    'supervisor_processes' => 2,
]

HorizonWorkerCheck

它检查 horizon worker 进程是否正在运行。

它将在服务器上执行此命令:ps aux | grep -E ".*php([0-9]\.[0-9])? .*artisan horizon:work" | grep -v grep

您可以在 server-monitor.php 配置文件中指定服务器上应运行的 min 和 max horizon worker 进程数。默认值是两者都是 1。

// config/server-monitor.php
'horizon' => [
    'min_worker_processes' => 2,
    'max_worker_processes' => 2,
]

QueueWorkerCheck

它检查 queue worker 进程是否正在运行。

它将在服务器上执行此命令:ps aux | grep -E ".*php([0-9]\.[0-9])? .*artisan queue:work" | grep -v grep

您可以在 server-monitor.php 配置文件中指定服务器上应运行的 queue worker 进程数。默认值为 1。

// config/server-monitor.php
'queue' => [
    'worker_processes' => 2,
]

DbConnectionCountCheck

它检查 mysql 连接数。

它将在服务器上执行此命令:netstat -an | grep 3306 | grep ESTABLISHED

您可以在 server-monitor.php 配置文件中指定 db 连接阈值。默认值为 40。

// config/server-monitor.php
'mysql' => [
    'connections' => 50,
]

扩展 server-monitor.php 配置文件

<?php

return [

    /*
     * These are the checks that can be performed on your servers. You can add your own
     * checks. The only requirement is that they should extend the
     * `Spatie\ServerMonitor\Checks\CheckDefinitions\CheckDefinition` class.
     */
    'checks' => [
        'diskspace' => Spatie\ServerMonitor\CheckDefinitions\Diskspace::class,
        'elasticsearch' => Spatie\ServerMonitor\CheckDefinitions\Elasticsearch::class,
        'memcached' => Spatie\ServerMonitor\CheckDefinitions\Memcached::class,
        'mysql' => Spatie\ServerMonitor\CheckDefinitions\MySql::class,
    ],

    /*
     * The default value for how often the checks will run,
     * after the last successful one.
     */
    'next_run_in_minutes' => env('SERVER_MONITOR_NEXT_RUN_IN_MINUTES', 10),

    /*
     * The performance of the package can be increased by allowing a high number
     * of concurrent ssh connections. Set this to a lower value if you're
     * getting weird errors running the check.
     */
    'concurrent_ssh_connections' => 5,

    /*
     * This string will be prepended to the ssh command generated by the package.
     */
    'ssh_command_prefix' => '',

    /*
     * This string will be appended to the ssh command generated by the package.
     */
    'ssh_command_suffix' => '',

    'notifications' => [

        'notifications' => [
            Spatie\ServerMonitor\Notifications\Notifications\CheckSucceeded::class => [],
            Spatie\ServerMonitor\Notifications\Notifications\CheckRestored::class => ['slack'],
            Spatie\ServerMonitor\Notifications\Notifications\CheckWarning::class => ['slack'],
            Spatie\ServerMonitor\Notifications\Notifications\CheckFailed::class => ['slack'],
        ],

        /*
         * To avoid burying you in notifications, we'll only send one every given amount
         * of minutes when a check keeps emitting warning or keeps failing.
         */
        'throttle_failing_notifications_for_minutes' => 60,

        // Separate the email by , to add many recipients
        'mail' => [
            'to' => 'your@email.com',
        ],

        'slack' => [
            'webhook_url' => env('SERVER_MONITOR_SLACK_WEBHOOK_URL'),
        ],

        /*
         * Here you can specify the notifiable to which the notifications should be sent. The default
         * notifiable will use the variables specified in this config file.
         */
        'notifiable' => \Spatie\ServerMonitor\Notifications\Notifiable::class,

        /*
         * The date format used in notifications.
         */
        'date_format' => 'd/m/Y',
    ],

    /*
     * To add or modify behaviour to the `Host` model you can specify your
     * own model here. The only requirement is that they should
     * extend the `Host` model provided by this package.
     */
    'host_model' => Spatie\ServerMonitor\Models\Host::class,

    /*
     * To add or modify behaviour to the `Check` model you can specify your
     * own model here. The only requirement is that they should
     * extend the `Check` model provided by this package.
     */
    'check_model' => Spatie\ServerMonitor\Models\Check::class,

    /*
     * Right before running a check it's process will be given to this class. Here you
     * can perform some last minute manipulations on it before it will
     * actually be run.
     *
     * This class should implement Spatie\ServerMonitor\Manipulators\Manipulator
     */
    'process_manipulator' => Spatie\ServerMonitor\Manipulators\Passthrough::class,

    /*
     * Thresholds for disk space's alert.
     */
    'diskspace_percentage_threshold' => [
        'warning' => 80,
        'fail' => 90,
    ],

    'cpu_load' => [
        'one_minute_threshold' => 1.4,
        'five_minute_threshold' => 1.4,
        'fifteen_minute_threshold' => 1.4,
    ],

    'redis' => [
        'memory_threshold' => 5000000,
    ],

    'horizon' => [
        'artisan_command_processes' => 1,
        'supervisor_processes' => 1,
        'min_worker_processes' => 1,
        'max_worker_processes' => 1,
    ],

    'queue' => [
        'worker_processes' => 1,
    ],

    'mysql' => [
        'connections' => 40,
    ],

    // host specific config values
    // you can override all or only some values on a host to host basis
    // replace dots in host name string with underscores
    // host.name.com => host_name_com
    'host_name_one_com' => [
        'cpu_load' => [
            'one_minute_threshold' => 0.9,
        ],
    ],

    'host_name_two_com' => [
        'cpu_load' => [
            'one_minute_threshold' => 1.2,
            'five_minute_threshold' => 1.2,
            'fifteen_minute_threshold' => 1.2,
        ],

        'redis' => [
            'memory_threshold' => 40000,
        ],

        'horizon' => [
            'artisan_command_processes' => 2,
            'supervisor_processes' => 2,
            'min_worker_processes' => 1,
            'max_worker_processes' => 2,
        ],

        'queue' => [
            'worker_processes' => 2,
        ],

        'mysql' => [
            'connections' => 10,
        ]
    ]
];

为每个主机提供其自己的额外自定义选项

自定义值是如何获得的?首先,我们检查是否存在于主机特定值,然后是配置中的通用值,最后如果没有,则使用默认值。