webvimark/laravel-server-monitor

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

1.2.6 2017-01-24 14:49 UTC

This package is auto-updated.

Last update: 2024-08-29 04:14:37 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,则会发出警报。它们可以选择检查页面源中是否包含特定短语。

注意:如果您提供了应该在响应中出现的checkPhrase,则不会检查HTTP状态码。这样,您可以检查您的403页面返回类似{"error":{"code":403,"message":"Invalid authorization headers"}}的内容。

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],
        ],
    ],

您可以定义匿名函数作为配置。例如

'monitors' => [
    'HttpPing' => function(){
        return \App\Models\Domain::all()->map(function(\App\Models\Domain $domain){
            return [
                'url'=>$domain->url,
                'checkPhrase'=>$domain->check_phrase,
            ];
        })->toArray();
    },
]

警报配置

警报可以记录到默认日志处理器,或通过电子邮件、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的控制台内核中调度这些命令,就像其他命令一样。

// 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)。有关更多信息,请参阅许可证文件