這個 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)。請參閱許可證文件以獲取更多資訊。