guibranco / projects-monitor

⚙️🔔 GitHub projects monitor
https://guilherme.straccini.com/projects-monitor/
MIT License
3 stars 0 forks source link

[FEATURE] Implement daemon to monitor and process `error_log` files from CPanel API #503

Open guibranco opened 2 months ago

guibranco commented 2 months ago

Description

We need a service/daemon that runs every 5 minutes to query the CPanel API for error_log files. When these files are found, the service should rename, process, and delete them. The parsed error messages should then be inserted into a MariaDB/MySQL table named errors for persistence.

Problem Statement

Proposed Solution

Implementation Steps

  1. Set Up the Daemon:

    • Implement a PHP script that runs as a daemon or scheduled task every 5 minutes.
    • Use a cron job or a similar scheduling mechanism to ensure the script executes periodically.
  2. Connect to CPanel API:

    • Develop PHP code to connect to the CPanel API and query for error_log files.
    $cpanelApiUrl = 'https://your-cpanel-server:2083/execute/Fileman/get_file_list';
    $apiToken = 'your-cpanel-api-token';
    
    $options = [
       'http' => [
           'header' => "Authorization: cpanel your-cpanel-username:$apiToken\r\n",
           'method' => 'GET',
       ],
    ];
    $context = stream_context_create($options);
    $response = file_get_contents($cpanelApiUrl, false, $context);
    $files = json_decode($response, true);
  3. Process error_log Files:

    • Rename each error_log file to a unique name.
    • Read and parse the file contents.
    • Insert parsed data into the errors table in MariaDB/MySQL.
    $db = new mysqli('hostname', 'username', 'password', 'database');
    
    foreach ($files as $file) {
       $filePath = $file['path'];
       $newFileName = $filePath . '_processed';
       rename($filePath, $newFileName);
    
       $contents = file_get_contents($newFileName);
       // Parse the contents
       $errors = parseErrors($contents);
    
       foreach ($errors as $error) {
           $stmt = $db->prepare('INSERT INTO errors (error_message) VALUES (?)');
           $stmt->bind_param('s', $error);
           $stmt->execute();
       }
    
       unlink($newFileName); // Delete the processed file
    }
    
    function parseErrors($contents) {
       // Implement error parsing logic
       return explode("\n", $contents); // Example logic
    }
  4. Database Table:

    • Create the errors table in MariaDB/MySQL with appropriate fields.
    CREATE TABLE errors (
       id INT AUTO_INCREMENT PRIMARY KEY,
       error_message TEXT NOT NULL,
       created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
  5. Testing and Validation:

    • Test the script to ensure it correctly processes and stores error logs.
    • Validate that error messages are accurately inserted into the database.
  6. Documentation and Deployment:

    • Document the setup and configuration process for the daemon.
    • Ensure sensitive information (e.g., API tokens) is handled securely.

Additional Notes

gitauto-ai[bot] commented 2 months ago

Hello @guibranco, you have reached your request limit of 5, your cycle will refresh on 2024-09-21 10:07:38+00:00. Consider subscribing if you want more requests. If you have any questions or concerns, please contact us at info@gitauto.ai.