iabdullahsk/laravel-health-nagios 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

iabdullahsk/laravel-health-nagios

Composer 安装命令:

composer require iabdullahsk/laravel-health-nagios

包简介

Nagios integration for spatie/laravel-health

README 文档

README

Latest Version on Packagist Total Downloads Tests

A Nagios integration add-on for spatie/laravel-health. This package extends Laravel Health to provide Nagios-compatible monitoring endpoints and output formatting.

What This Package Does

This add-on package provides:

  • 🔍 Nagios-Compatible Health Check Endpoint - Exposes your Laravel Health checks in a format that Nagios can understand
  • 🛡️ Bearer Token Authentication - Secure your health check endpoint with configurable bearer token authentication
  • 📊 Performance Data Generation - Includes performance metrics for Nagios graphing and trending
  • 🎯 Proper Status Codes - Returns appropriate HTTP status codes (200 for OK, 503 for failures)
  • ⚙️ Configurable Output - Control what information is included in the Nagios output
  • 🔄 Fresh Results Support - Option to force fresh health check execution via ?fresh parameter

Requirements

Installation

You can install the package via composer:

composer require iabdullahsk/laravel-health-nagios

Note: This will automatically install spatie/laravel-health as a dependency if it's not already installed.

Publish Configuration (Optional)

php artisan vendor:publish --tag="nagios-config"
php artisan vendor:publish --tag="health-config" 

The package will automatically register its service provider via Laravel's auto-discovery feature.

Configuration

Environment Variables

Add these environment variables to your .env file:

# Enable/disable the Nagios endpoint
NAGIOS_ENABLED=true

# Bearer token for authentication (required for security)
NAGIOS_HEALTH_BEARER_TOKEN=your-secret-token-here

# Custom endpoint URL (optional)
NAGIOS_HEALTH_URL=/health/nagios

# Whether to always run fresh health checks
NAGIOS_ALWAYS_SEND_FRESH_RESULTS=true

# Include detailed information in output
NAGIOS_INCLUDE_DETAILS=true

# Include performance data for graphing
NAGIOS_INCLUDE_PERFORMANCE_DATA=true

Configuration File

The published configuration file (config/nagios.php) provides additional options:

return [
    'enabled' => env('NAGIOS_ENABLED', false),
    'always_send_fresh_results' => env('NAGIOS_ALWAYS_SEND_FRESH_RESULTS', true),
    'enable_authentication' => env('NAGIOS_ENABLE_AUTHENTICATION', true),
    'bearer_token' => env('NAGIOS_HEALTH_BEARER_TOKEN'),
    'url' => env('NAGIOS_HEALTH_URL', '/health/nagios'),
    'middleware' => [
        Abdullah\LaravelHealthNagios\Http\Middleware\RequiresBearerToken::class,
    ],
    'include_details' => env('NAGIOS_INCLUDE_DETAILS', true),
    'include_performance_data' => env('NAGIOS_INCLUDE_PERFORMANCE_DATA', true),
];

Usage

Basic Setup

Once installed and configured, your Nagios health check endpoint will be automatically available at the configured URL (default: /health/nagios).

Example Output

When all checks pass (HTTP 200):

OK: 3 checks executed|'database'=1 'cache'=1 'queue'=1

database: Connection successful [OK]
cache: Cache working properly [OK]
queue: Jobs processing normally [OK]

When checks fail (HTTP 503):

CRITICAL: Connection failed|'database'=0 'cache'=1 'queue'=1

database: Connection failed [FAILED]
cache: Cache working properly [OK]
queue: Jobs processing normally [OK]

Testing the Endpoint

# Test with bearer token
curl -H "Authorization: Bearer your-secret-token-here" \
     http://your-app.com/health/nagios

# Force fresh results
curl -H "Authorization: Bearer your-secret-token-here" \
     "http://your-app.com/health/nagios?fresh"

Integration with Laravel Health

This package works seamlessly with all existing Laravel Health checks. Simply configure your health checks as normal:

// In your Laravel application (e.g., AppServiceProvider)
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\UsedDiskSpaceCheck;
use Spatie\Health\Checks\Checks\DatabaseCheck;
use Spatie\Health\Checks\Checks\RedisCheck;

Health::checks([
    UsedDiskSpaceCheck::new(),
    DatabaseCheck::new(),
    RedisCheck::new(),
    // ... other health checks
]);

All configured health checks will automatically appear in the Nagios output.

Security Considerations

  • Always use bearer token authentication in production environments
  • Use HTTPS for your health check endpoints
  • Restrict network access to your monitoring systems only
  • Use strong, unique tokens for authentication

Development

Running Tests

The package includes comprehensive tests using Pest, the same testing framework used by spatie/laravel-health.

Prerequisites

Make sure you have the development dependencies installed:

composer install

Running the Test Suite

# Run all tests
./vendor/bin/pest

# Run tests with coverage
./vendor/bin/pest --coverage

# Run specific test files
./vendor/bin/pest tests/ResultFormats/NagiosResultsFormatTest.php

# Run tests with verbose output
./vendor/bin/pest --verbose

Test Structure

The test suite includes:

  • Feature Tests (tests/Feature/): Test service provider registration and configuration
  • Unit Tests (tests/Unit/): Basic sanity checks and isolated component tests
  • HTTP Tests (tests/Http/Controllers/): Test the Nagios endpoint functionality
  • Format Tests (tests/ResultFormats/): Test Nagios output formatting

Test Coverage

The tests cover:

  • ✅ Service provider registration and configuration loading
  • ✅ Bearer token authentication middleware
  • ✅ Nagios output formatting for different health check statuses
  • ✅ HTTP endpoint responses and status codes
  • ✅ Performance data generation
  • ✅ Error handling for missing results

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Add or update tests as needed
  5. Ensure all tests pass (./vendor/bin/pest)
  6. Commit your changes (git commit -m 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

Code Style

The package follows PSR-12 coding standards. You can check your code style using:

# Install PHP CS Fixer (if not already installed)
composer require --dev friendsofphp/php-cs-fixer

# Check code style
./vendor/bin/php-cs-fixer fix --dry-run --diff

Troubleshooting

Common Issues

Endpoint returns 404:

  • Check that NAGIOS_ENABLED=true in your .env file
  • Verify that NAGIOS_HEALTH_BEARER_TOKEN is set
  • Ensure the URL is correct (default: /health/nagios)

Authentication fails:

  • Verify the bearer token in your request header: Authorization: Bearer your-token
  • Check that the token matches your NAGIOS_HEALTH_BEARER_TOKEN environment variable

No health check results:

  • Ensure you have configured health checks in your Laravel application
  • Try adding ?fresh=1 to force fresh results
  • Check Laravel logs for any health check errors

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details on how to contribute to this project.

Security Vulnerabilities

If you discover a security vulnerability, please send an e-mail to [your-email@example.com]. All security vulnerabilities will be promptly addressed.

Credits

License

The MIT License (MIT). Please see License File for more information.

Related Packages

iabdullahsk/laravel-health-nagios 适用场景与选型建议

iabdullahsk/laravel-health-nagios 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「monitoring」 「health」 「nagios」 「laravel」 「spatie」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 iabdullahsk/laravel-health-nagios 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 iabdullahsk/laravel-health-nagios 我们能提供哪些服务?
定制开发 / 二次开发

基于 iabdullahsk/laravel-health-nagios 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 16
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-10