ericmakesstuff/laravel-server-monitor

Laravel应用程序的服务器监控命令

1.2.5 2018-09-17 16:08 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:59:03 UTC


README

Latest Version Software License Build Status Quality Score Total Downloads

此Laravel 5包将定期监控您服务器和网站的健康状况。目前,它提供磁盘使用、HTTP Ping功能来监控外部服务的健康状态以及SSL证书的验证/过期监控的健康/警报状态通知。

安装后,监控服务器非常简单。只需执行以下Artisan命令

php artisan monitor:run

一次只能运行某些监控器

php artisan monitor:run HttpPing
php artisan monitor:run SSLCertificate,DiskUsage

工作原理

通过您的项目中的配置文件,可以配置任意数量的监控器来检查服务器配置的问题。

当执行monitor:run artisan命令时,无论是通过命令行还是使用Laravel命令调度器,监控器将运行并在有问题时发出警报。警报状态是可配置的,警报可以通过日志、电子邮件、Pushover和Slack发送。

磁盘使用监控器

磁盘使用监控器检查给定分区上使用的存储空间百分比,如果百分比超过可配置的警报百分比,则发出警报。

HTTP Ping监控器

HTTP Ping监控器执行简单的页面请求,如果HTTP状态码不是200,则发出警报。它们还可以选择检查页面源中是否包含特定的短语。

SSL证书监控器

SSL证书监控器将配置URL的SSL证书拉取出来,并确保该证书对该URL有效。支持通配符和多域名证书。

如果证书无效或已过期,监控器将发出警报,并在到期日期临近时也发出警报。还可以配置在到期前多少天发出警报。

安装和用法

您可以通过composer使用以下命令安装此包

composer require ericmakesstuff/laravel-server-monitor

您需要注册ServiceProvider

// config/app.php

'providers' => [
    // ...
    EricMakesStuff\ServerMonitor\ServerMonitorServiceProvider::class,
];

要发布配置文件到app/config/server-monitor.php,请运行

php artisan vendor:publish --provider="EricMakesStuff\ServerMonitor\ServerMonitorServiceProvider"

监控配置

发布配置文件后,您可以编辑app/config/server-monitor.php中的'monitors'部分。

默认监控器配置是

'monitors' => [
    /*
     * DiskUsage will alert when the free space on the device exceeds the alarmPercentage.
     * path is any valid file path, and the monitor will look at the usage of that disk partition.
     *
     * You may add as many DiskUsage monitors as you require.
     */
    'DiskUsage' => [
        [
            'path' => base_path(),
            'alarmPercentage' => 75,
        ],
    ],
    /*
     * HttpPing will perform an HTTP request to the configured URL and alert if the response code
     * is not 200, or if the optional checkPhrase is not found in the response.
     */
    'HttpPing' => [
        [
            'url' => 'http://www.example.com/',
        ],
        [
            'url' => 'http://www.example.com/',
            'checkPhrase' => 'Example Domain',
            'timeout' => 10,
            'allowRedirects' => false,
        ],
    ],
    /*
     * SSLCertificate will download the SSL Certificate for the URL and validate that the domain
     * is covered and that it is not expired. Additionally, it can warn when the certificate is
     * approaching expiration.
     */
    'SSLCertificate' => [
        [
            'url' => 'https://www.example.com/',
        ],
        [
            'url' => 'https://www.example.com/',
            'alarmDaysBeforeExpiration' => [14, 7],
        ],
    ],

警报配置

警报可以记录到默认日志处理器,或通过电子邮件、Pushover或Slack发送。允许的值是logmailpushoverslack

默认警报配置是

'events' => [
    'whenDiskUsageHealthy'       => ['log'],
    'whenDiskUsageAlarm'         => ['log', 'mail'],
    'whenHttpPingUp'             => ['log'],
    'whenHttpPingDown'           => ['log', 'mail'],
    'whenSSLCertificateValid'    => ['log'],
    'whenSSLCertificateInvalid'  => ['log', 'mail'],
    'whenSSLCertificateExpiring' => ['log', 'mail'],
],

调度

在您完成基本安装后,您可以使用monitor:run命令。在大多数情况下,您会想要安排此命令,以便您不必手动运行monitor:run来了解服务器的健康状况。

您可以在Laravel的console kernel中像其他命令一样安排这些命令。

// app/Console/Kernel.php

protected function schedule(Schedule $schedule)
{
   $schedule->command('monitor:run')->daily()->at('10:00');
   $schedule->command('monitor:run HttpPing')->hourly();
}

当然,上面代码中使用的计划只是示例。请根据您的喜好进行调整。

测试

使用以下命令运行测试

vendor/bin/phpunit

下一步

更多的监控指标。请随时通过问题或拉取请求提交您的想法!

想法
  • 远程服务器磁盘空间使用(通过SSH)
  • NTP偏移量(以秒为单位)

贡献

有关详细信息,请参阅CONTRIBUTING

安全

如果您发现任何安全相关的问题,请通过电子邮件eric@ericmakesstuff.com报告,而不是使用问题跟踪器。

鸣谢

许可证

MIT 许可证(MIT)。有关更多信息,请参阅许可证文件