这个 Laravel 5 包将定期监控您的服务器和网站的运行状况。目前,它提供磁盘使用情况的健康/警报状态通知、用于监控外部服务运行状况的 HTTP Ping 功能以及 SSL 证书的验证/过期监控。
安装后,监控您的服务器非常容易。只需发出这个工匠命令:
php artisan monitor:run
您一次只能运行某些监视器:
php artisan monitor:run HttpPing
php artisan monitor:run SSLCertificate,DiskUsage
使用项目中的配置文件,可以配置任意数量的监视器来检查服务器设置是否存在问题。
当从命令行或使用 Laravel 命令调度程序执行monitor:run
artisan 命令时,监视器将运行并在出现问题时发出警报。警报状态是可配置的,警报可以发送到日志,或通过电子邮件、Pushover 和 Slack 发送。
磁盘使用情况监视器检查给定分区上使用的存储空间的百分比,并在该百分比超出可配置的警报百分比时发出警报。
HTTP Ping 监视器执行简单的页面请求,并在 HTTP 状态代码不是200 时发出警报。它们可以选择检查页面源中是否包含特定短语。
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="EricMakesStuffServerMonitorServerMonitorServiceProvider"
发布配置文件后,您可以编辑 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 发送。允许的值为log
、 mail
、 pushover
和slack
。
默认警报配置为:
' events ' => [
' whenDiskUsageHealthy ' => [ ' log ' ],
' whenDiskUsageAlarm ' => [ ' log ' , ' mail ' ],
' whenHttpPingUp ' => [ ' log ' ],
' whenHttpPingDown ' => [ ' log ' , ' mail ' ],
' whenSSLCertificateValid ' => [ ' log ' ],
' whenSSLCertificateInvalid ' => [ ' log ' , ' mail ' ],
' whenSSLCertificateExpiring ' => [ ' log ' , ' mail ' ],
],
执行基本安装后,您可以开始使用 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
更多监控指标。请随意通过问题或拉取请求提交想法!
详细信息请参阅贡献。
如果您发现任何与安全相关的问题,请发送电子邮件至 [email protected],而不是使用问题跟踪器。
麻省理工学院许可证 (MIT)。请参阅许可证文件以获取更多信息。