Skip to content
Snippets Groups Projects
mysqlconnect.php 5.56 KiB
<?php
/**
 * ____________________________________________________________________________
 * 
 *  _____ _____ __                   _____         _ _           
 * |     |     |  |      ___ ___ ___|     |___ ___|_| |_ ___ ___ 
 * |-   -| | | |  |__   | .'| . | . | | | | . |   | |  _| . |  _|
 * |_____|_|_|_|_____|  |__,|  _|  _|_|_|_|___|_|_|_|_| |___|_|  
 *                          |_| |_|                              
 *                           _ _         _                                            
 *                       ___| |_|___ ___| |_                                          
 *                      |  _| | | -_|   |  _|                                         
 *                      |___|_|_|___|_|_|_|   
 *                                                               
 * ____________________________________________________________________________
 * 
 * CHECK DATABASE CONNECTION WITH MYSQLI
 * ____________________________________________________________________________
 * 
 * 2021-10-27  <axel.hahn@iml.unibe.ch>
 * 2024-07-23  <axel.hahn@unibe.ch>      php 8 only: use typed variables
 * 2024-11-22  <axel.hahn@unibe.ch>      detect installed mysqli function
 * 2025-03-01  <axel.hahn@unibe.ch>      add try catch 
 * 2025-03-19  <axel.hahn@unibe.ch>      add validation rules and parameter description
 */
class checkMysqlConnect extends appmonitorcheck
{
    /**
     * Self documentation and validation rules
     * @var array
     */
    protected array $_aDoc = [
        'name' => 'Plugin Mysqlconnect',
        'description' => 'Verify a database connection with mysqli real connect function.',
        'parameters' => [
            'server' => [
                'type' => 'string',
                'required' => true,
                'description' => 'Hostname/ ip of mysql server',
                'validate' => 'hostname',

                // doc
                'default' => null,
                'example' => 'localhost',
            ],
            'port' => [
                'type' => 'int',
                'required' => false,
                'description' => 'port of mysql server',
                'validate' => 'portnumber',

                'default' => 5,
                'example' => '3',
            ],
            'user' => [
                'type' => 'string',
                'required' => false,
                'description' => 'Database user to connect with',
                'default' => null,
                'example' => 'dbuser',
            ],
            'password' => [
                'type' => 'string',
                'required' => false,
                'description' => 'Password of the database user to authenticate',
                'default' => null,
                'example' => 'mySecretDatabasePassword',
            ],
            'db' => [
                'type' => 'string',