PHP code example of mohsenabrishami / stethoscope

1. Go to this page and download the library: Download mohsenabrishami/stethoscope library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mohsenabrishami / stethoscope example snippets


    /*
    |--------------------------------------------------------------------------
    | Dashboard Configuration
    |--------------------------------------------------------------------------
    | Here, you can specify whether the monitoring panel is enabled and the key 

    /*
    |--------------------------------------------------------------------------
    | Monitorable Resources
    |--------------------------------------------------------------------------
    | Here you can Define which resources should be monitored.
    | Set true if you want a resource to be monitored, otherwise false.
    |
    */

    'monitorable_resources' => [
        'cpu' => true,
        'memory' => true,
        'storage' => true,
        'network' => true,
        'web_server' => true,
    ],

    /*
    |--------------------------------------------------------------------------
    | Web Server Name
    |--------------------------------------------------------------------------
    | Here you can define what web server installed on your server.
    | Set `nginx` or `apache`
    |
    */

    'web_server_name' => 'nginx',

    /*
    |--------------------------------------------------------------------------
    | Log File Storage
    |--------------------------------------------------------------------------
    | Define storage driver and path for save log file.
    |
    */

    'log_file_storage' => [
        'driver' => 'local',
        'path' => 'stethoscope/',
    ],

    /*
    |--------------------------------------------------------------------------
    | Thresholds
    |--------------------------------------------------------------------------
    | If resource consumption exceeds these thresholds, a log will be created.
    | You may define maximum CPU and memory usage by percent.
    | You may define minimum Storage space by GB.
    |
    */

    'thresholds' => [

        'cpu' => env('CPU_MONITOR_THRESHOLD', 90),

        'memory' => env('MEMORY_MONITOR_THRESHOLD', 80),

        'storage' => env('storage_MONITOR_THRESHOLD', 10),

    ],

    /*
    |--------------------------------------------------------------------------
    | Network Monitor URL
    |--------------------------------------------------------------------------
    | Here you can define the multiple desired URL for network monitoring. 
    | If an address cannot be reached, it checks other addresses to make sure that the problem is with the server network
    |
    */

    'network_monitor_url' => env('NETWORK_MONITOR_URL', ['https://1.1.1.1', 'https://www.google.com']),

    /*
    |--------------------------------------------------------------------------
    | Log Record Driver
    |--------------------------------------------------------------------------
    | Set `database` for save logs in database and `file` for record logs in file
    |
    */

    'drivers' => [
        'log_record' => env('STETHOSCOPE_LOG_DRIVER', 'file'),
    ],

    /*
    |--------------------------------------------------------------------------
    | Clean up resource logs
    |--------------------------------------------------------------------------
    | Here you define the number of days for which resource logs must be kept.
    | Older resource logs will be removed.
    |
    */
    'cleanup_resource_logs' => 7,

'mail' => [
    'to' => null,
],


namespace App\Notifications;

use Illuminate\Bus\Queueable;
use MohsenAbrishami\Stethoscope\Notifications\LogReportNotification;
use NotificationChannels\Telegram\TelegramMessage;

class StethoscopeNotification extends LogReportNotification
{
    use Queueable;

    public function toTelegram()
    {
        $formattedMessage = "
        *Message from stethoscope:*

        *Be careful!! 💀*
        
        Your server has the following problems:
        " . (isset($this->logs['cpu']) ? '- Cpu usage: ' . $this->logs['cpu'] . ' %' : '') . "
        " . (isset($this->logs['memory']) ? '- Memory usage: ' . $this->logs['memory'] . ' %' : '') . "
        " . (isset($this->logs['network']) ? '- Network connection status: ' . $this->logs['network'] : '') . "
        " . (isset($this->logs['storage']) ? '- Remaining free space on the Storage:  ' . $this->logs['storage'] . ' GB' : '') . "
        " . (isset($this->logs['webServer']) ? '- Web server status:  ' . $this->logs['webServer'] : '') . "
    ";

        return TelegramMessage::create()->content($formattedMessage);
    }
}

namespace App\Notifications;

use MohsenAbrishami\Stethoscope\Notifications\Notifiable;

class StethoscopeNotifiable extends Notifiable
{
    public function routeNotificationForTelegram()
    {
        return config('stethoscope.notifications.telegram.channel_id');
    }
}

    /*
    |--------------------------------------------------------------------------
    | Notifications
    |--------------------------------------------------------------------------
    | You can get notified when specific events occur. you should set an email to get notifications here.
    | If you don't need to send an email notification, set null.
    |
    */
    'notifications' => [

        'notifications' => [
            App\Notifications\StethoscopeNotification::class => ['telegram'],
        ],

        'notifiable' => App\Notifications\StethoscopeNotifiable::class,

        'telegram' => [
            'channel_id' => env('TELEGRAM_CHAT_ID')
        ]

    ],

composer test
 bash
php artisan vendor:publish --tag=stethoscope-publish-config
 bash
php artisan migrate
 bash
php artisan stethoscope:listen
 bash
php artisan stethoscope:monitor
 bash
php artisan stethoscope:clean
 bash
php artisan vendor:publish --tag=stethoscope-publish-view