Герб Укріїни
Believe in Ukraine and the Armed Forces. Glory to Ukraine!

A script for monitoring website performance with email notifications

30.05.2023

In the dynamic world of the Internet, monitoring the performance of websites is an important task for businesses and web developers. It allows for timely detection of issues such as website unavailability and taking necessary measures to address them. In this article, we will discuss creating a simple website monitoring script using PHP.

Our monitoring script will check the availability of specified websites and send email notifications in case of unavailability. Let’s take a look at the code.

// Список сайтов для мониторинга
$websites = array(
"https://airmasterskaya.com",
"https://lider-transfer.kiev.ua", 
"https://medservice.in.ua",
);

// Настройки отправки электронной почты
$to = '[email protected]';
$subject = 'Непрацює сайт';
$headers = 'From: [email protected]';

// Проверка доступности сайтов
$unavailable_websites = array();
foreach ($websites as $website) {
    $context = stream_context_create(array(
        "ssl" => array(
            "verify_peer" => false,
            "verify_peer_name" => false
        )
    ));
    $response = @file_get_contents($website, false, $context);
    if ($response === false) {
        $unavailable_websites[] = $website;
    }
}

// Отправка уведомления о недоступности сайтов
if (count($unavailable_websites) > 0) {
    // Формирование текста сообщения
    $message = "Непрацює сайт ці сайти:\n\n";
    foreach ($unavailable_websites as $website) {
        $message .= $website . "\n";
    }

    // Отправка письма
    if (mail($to, $subject, $message, $headers)) {
        echo "Лист надіслано";
    } else {
        echo "Не вдалося надіслати електронний лист";
    }
} else {
    echo "Всі сайти доступні";
}

Creating a list of websites for monitoring

$websites = array(
"https://airmasterskaya.com",
"https://lider-transfer.kiev.ua",
"https://medservice.in.ua"
);

In this array, you can specify any number of websites that you want to monitor.

Читайте також:  What is internet marketing and who needs it

Setting up email notifications:

$to = '[email protected]';
$subject = 'Непрацює сайт';
$headers = 'From: [email protected]';

 

Checking website availability

$unavailable_websites = array();
foreach ($websites as $website) {
$context = stream_context_create(array(
"ssl" => array(
"verify_peer" => false,
"verify_peer_name" => false
)
));
$response = @file_get_contents($website, false, $context);
if ($response === false) {
$unavailable_websites[] = $website;
}
}

This code checks the availability of each website from the list. If a website is unavailable, its URL is added to the $unavailable_websites array.

Sending notifications about unavailable websites:

if (count($unavailable_websites) > 0) {
$message = "Непрацює сайт ці сайти:\n\n";
foreach ($unavailable_websites as $website) {
$message .= $website . "\n";
}

if (mail($to, $subject, $message, $headers)) {
echo "Лист надіслано";
} else {
echo "Не вдалося надіслати електронний лист";
}
} else {
echo "Всі сайти доступні";
}

If any unavailable websites are found, the script generates a notification with the list of those websites and sends it using the mail() function. If the email is successfully sent, it displays the message “Email sent.” Otherwise, it displays the message “Failed to send email.” If no unavailable websites are found, it displays the message “All websites are accessible.”

Читайте також:  TOP 11 professions of the future

You can use this script to monitor the performance of websites and receive notifications if any of them become unavailable. Make sure to configure the email settings on your server to ensure the proper functioning of the mail() function.

Automatic start of checking through CRON tasks:

  1. Instructions for adding a monitoring script to CRON tasks on hosting using the CPanel control panel:
  2. Log in to your CPanel account and find the “CRON Schedules” or “Cron Jobs” section.
  3. In the “CRON Schedules” section, find the “Add New CRON Schedule” or “Add New Cron Job” option and click on it.
  4. A page for adding a new CRON task will open. In the “Command” field, enter the command to call your PHP script specifying the full path to the script file.
  5. Replace “/home/username/public_html/myscript.php” with the path to your monitoring script file.
  6. Select the frequency of executing the CRON task in the “Common Settings” field.
  7. Click on the “Add CRON Schedule” or “Add Cron Job” button to save the settings.
Читайте також:  Streamlining Logistics. The Role of Logistics Software Development Companies

It is important to make sure that the path to the executable PHP file (/usr/bin/php in the above example) is correct for your hosting. If you are unsure of the PHP path, contact your hosting provider support for more information.

Leave a Reply

Your email address will not be published. Required fields are marked *