Kudos
Collect
Twiiter
Facebook
Share
Develop somethings, meditation, reading and thinking...

Discord Notification for checking MySQL alive with PHP

Last updated 5 days ago
0 0 0 0

Monitor Database Status and Send to Discord via Cronjob

You can register a simple script as a Cronjob to regularly check the database status and send the result to Discord.

<?php
// Create connection

$con = mysqli_connect("localhost", "database_user_name", "database_user_password", "database_name");

// Check connection
if ( !mysqli_connect_errno() ) {
    $message = "Database works";
} else {
    $message = "Database is not working";
}

// Discord Webhook

$webhook_url = 'https://discordapp.com/api/webhooks/xxxyyyzzz...';
$payload     = json_encode( ['content' => $message] );

$ch = curl_init($webhook_url);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
curl_close($ch);

Register a Cron Job to Send Discord Alerts Every 5 Minutes

*/5 * * * * /usr/bin/php /path/to/a-server-db-monitor.php

With this setup, you can receive regular updates on your server status via Discord.

Hi, my name is Richard. I’m a developer wants to make the world better with logic power. Mainly I use Linux, Nginx, MySQL, PHP and JavaScript . I want to share my knowledge with someone that it was also based from some great persons via LYNMP. 👨‍💻

Essedrop - Make your file online instantly
 

Responses

Leave a response to @richard

Please sign in to comment.
Markdown is also available in comment.