Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once(__DIR__ . '/../vendor/autoload.php');
- $logger = new SimpleLogger\File(__DIR__ . '/../log/simplelogger.log');
- $config = require(__DIR__ . '/../config/main.php');
- const EVERYTHING_IS_BROKEN = "DEGRADED";
- $sendNotification = function () use ($logger, $config) {
- if (array_key_exists("status", $_GET) && array_key_exists("unit_status", $_GET) && array_key_exists("hostname", $_GET)) {
- $status = urldecode($_GET["status"]);
- $unit_status = str_replace("\n", "<br>", urldecode($_GET["unit_status"]));
- $hostname = urldecode($_GET["hostname"]);
- if ($status == EVERYTHING_IS_BROKEN) {
- $logger->info('DEGRADED STATUS. Try to Send Mail');
- $mail = new \PHPMailer\PHPMailer\PHPMailer();
- $mail->IsSMTP();
- $mail->SMTPAuth = true;
- $mail->Host = $config['smtp']['host'];
- $mail->Port = $config['smtp']['port'];
- $mail->Username = $config['smtp']['username'];
- $mail->Password = $config['smtp']['password'];
- $mail->Mailer = "smtp";
- $mail->WordWrap = $config['smtp']['wordwrap'];
- $mail->SMTPSecure = $config['smtp']['smtpsecure'];
- $mail->From = $config['sender']['email'];
- $mail->FromName = $config['sender']['name'];
- if ($config['smtp']['smtpsecure'] == 'tls') {
- $mail->SMTPOptions = [
- 'ssl' => [
- 'verify_peer' => false,
- 'verify_peer_name' => false,
- 'allow_self_signed' => true,
- ],
- ];
- }
- $mail->AddAddress($config['recipient']['email']);
- $mail->Subject = "RAID Degraded on {$hostname}";
- $mail->Body = "RAID degradation on {$hostname} <br> Disk Status: <br> {$unit_status}";
- $mail->IsHTML(true);
- $message = '';
- if (!$mail->Send()) {
- $message = "Mailer Error: " . $mail->ErrorInfo;
- $logger->error($message);
- } else {
- $message = "Email has been sent to {$config['recipient']['email']}";
- $logger->info($message);
- }
- echo $message;
- } else {
- $message = 'Wrong status! (Not "' . EVERYTHING_IS_BROKEN . '")';
- echo $message;
- $logger->alert($message);
- }
- } else {
- $message = "Wrong params! (status, unit_status or hostname does not present in GET)";
- echo $message;
- $logger->alert($message);
- }
- };
- try {
- $logger->info('Request with params ' . json_encode($_REQUEST));
- $logger->info($_SERVER['QUERY_STRING']);
- $sendNotification();
- } catch (Exception $e) {
- $logger->error('Error: ' . $e . $message);
- }
- ~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement