扩展推荐 —— Laravel 网站和 SSL(HTTPS 认证过期) 监控

file

如今,互联网已成为大多数人不可或缺的收入来源。对于大部分 web 应用来说,即使是几秒钟,宕机也是不可接受的。

Spatie 发布了一个非常方便的包,叫做  Laravel 运行时间监控器,它的目标是提供一个简单而又强大的方式去监控你网站的运行时间以及 SSL 证书过期时间;当你的网站挂掉的时候,它会使用 Laravel 5.3 那套灵活的通知系统来通知你。另外,当你的网站恢复访问的时候,这个包也会友好的通知你本次宕机时长。

就让我们一起来看看怎么设置和使用这个软件包吧。

安装

首先,通过 Composer 安装 Laravel 运行时间监控器:

composer require spatie/laravel-uptime-monitor

config/app.php 文件中添加其服务提供者:

'providers' => [
    ...
    Spatie\UptimeMonitor\UptimeMonitorServiceProvider::class,
];

然后,在终端运行如下命令来发布配置文件:

php artisan vendor:publish --provider="Spatie\UptimeMonitor\UptimeMonitorServiceProvider"

再运行下面这条命令来迁移监控器表到数据库:

php artisan migrate

最后,打开 app/Console/Kernel.php 文件并调度 monitor:check-uptime 和 monitor:check-certificate 命令:

protected function schedule(Schedule $schedule)
{
    $schedule->command('monitor:check-uptime')->everyMinute();
    $schedule->command('monitor:check-certificate')->daily();
}

请确保你的服务器添加了 Laravel Cron 入口 以使运行时间监控器周期性的运行:

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

使用

安装完这个包以后,你就可以通过 monitor:create 命令来添加要监控的网站了。如果你添加的网站是以 https:// 开头的,此包还会监听其 SSL 证书:

php artisan monitor:create https://laravel-news.com

还有一个可选项,它会询问你是否需要在该网站的响应中查找特定字符串,如果没有找到该字符串,那么它就会认为检查正常运行时间失败了。

稍后,再运行命令 php artisan monitor:list

file

它会展示一个所有被监控网站的清单,它们的运行时间,以及带有 SSL 证书详细信息的证书检查结果。

还有 monitor:deletemonitor:enable 和 monitor:disable 这些命令。第一条命令把网站从监控列表中移除,其他两条分别对应启用和禁用对指定网站的监控:

php artisan monitor:delete http://example.com
php artisan monitor:enable http://example.com
php artisan monitor:disable http://example.com

概览

如果你打开 config/laravel-uptime-monitor.php 这个配置文件,那么你就会看到 Laravel 运行时间监控器这个包有很多配置项,并且是高度可定制的。请特别注意以下重点配置项:

  • 首先,你可以通过自定义 options 选项来给本包将会触发的事件(它提供了六种不同的事件类型)添加通知发送频道。你可以轻松的使用任何频道,只要它是 Laravel 5.3 的通知系统所支持的,像 Slack 或者邮件,等等。

  • 如果你在多个地方使用了 Laravel 运行时间监控器,那么你可以使用 location 选项来区分不同的位置, 因为位置将随通知一起发送。

  • resend_uptime_check_failed_notification_every_minutes 选项指定了网站宕机时发送通知的时间间隔,默认是 60 分钟。

  • 然后,还有两个选项,一个用于指定将被包通知的邮件,另一个则用于指定 Slack 的 webhook_url,如果你是使用 Slack 做为通知发送频道的话。

  • The run_interval_in_minutes option is for specifying the delay time between uptime check. Note that if an uptime check fails the package will ignore this option and starts checking that site every minute.

  • The timeout_per_site option is for specifying the period in seconds that the check process should wait before giving up, the default is ten seconds.

  • The fire_monitor_failed_event_after_consecutive_failures determines the number of failed uptime checks before it fires the Spatie\UptimeMonitor\Events\MonitorFailed event, the default is two.

  • Then, you have options for fire_expiring_soon_event_if_certificate_expires_within_days, which will fire a SslExpiresSoon event when the remaining days in your SSL certificate are less than the number of days you specify for this option.


If you want a powerful way to monitor a website's uptime and SSL certificates expiration date, give the Laravel Uptime Monitor a try.

This appeared first on Laravel News

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://laravel-news.com/uptime-monitor

译文地址:https://learnku.com/laravel/t/9593/exten...

本帖已被设为精华帖!
本文为协同翻译文章,如您发现瑕疵请点击「改进」按钮提交优化建议
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!