Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. <?php
  2. require_once(__DIR__ . '/../vendor/autoload.php');
  3. $logger = new SimpleLogger\File(__DIR__ . '/../log/simplelogger.log');
  4. $config = require(__DIR__ . '/../config/main.php');
  5. const EVERYTHING_IS_BROKEN = "DEGRADED";
  6.  
  7. $sendNotification = function () use ($logger, $config) {
  8. if (array_key_exists("status", $_GET) && array_key_exists("unit_status", $_GET) && array_key_exists("hostname", $_GET)) {
  9. $status = urldecode($_GET["status"]);
  10. $unit_status = str_replace("\n", "<br>", urldecode($_GET["unit_status"]));
  11. $hostname = urldecode($_GET["hostname"]);
  12.  
  13. if ($status == EVERYTHING_IS_BROKEN) {
  14. $logger->info('DEGRADED STATUS. Try to Send Mail');
  15. $mail = new \PHPMailer\PHPMailer\PHPMailer();
  16. $mail->IsSMTP();
  17.  
  18. $mail->SMTPAuth = true;
  19. $mail->Host = $config['smtp']['host'];
  20. $mail->Port = $config['smtp']['port'];
  21. $mail->Username = $config['smtp']['username'];
  22. $mail->Password = $config['smtp']['password'];
  23. $mail->Mailer = "smtp";
  24. $mail->WordWrap = $config['smtp']['wordwrap'];
  25. $mail->SMTPSecure = $config['smtp']['smtpsecure'];
  26.  
  27. $mail->From = $config['sender']['email'];
  28. $mail->FromName = $config['sender']['name'];
  29.  
  30. if ($config['smtp']['smtpsecure'] == 'tls') {
  31. $mail->SMTPOptions = [
  32. 'ssl' => [
  33. 'verify_peer' => false,
  34. 'verify_peer_name' => false,
  35. 'allow_self_signed' => true,
  36. ],
  37. ];
  38. }
  39. $mail->AddAddress($config['recipient']['email']);
  40.  
  41. $mail->Subject = "RAID Degraded on {$hostname}";
  42. $mail->Body = "RAID degradation on {$hostname} <br> Disk Status: <br> {$unit_status}";
  43.  
  44. $mail->IsHTML(true);
  45. $message = '';
  46. if (!$mail->Send()) {
  47. $message = "Mailer Error: " . $mail->ErrorInfo;
  48. $logger->error($message);
  49. } else {
  50. $message = "Email has been sent to {$config['recipient']['email']}";
  51. $logger->info($message);
  52. }
  53. echo $message;
  54. } else {
  55. $message = 'Wrong status! (Not "' . EVERYTHING_IS_BROKEN . '")';
  56. echo $message;
  57. $logger->alert($message);
  58. }
  59. } else {
  60. $message = "Wrong params! (status, unit_status or hostname does not present in GET)";
  61. echo $message;
  62. $logger->alert($message);
  63. }
  64. };
  65.  
  66. try {
  67. $logger->info('Request with params ' . json_encode($_REQUEST));
  68. $logger->info($_SERVER['QUERY_STRING']);
  69. $sendNotification();
  70. } catch (Exception $e) {
  71. $logger->error('Error: ' . $e . $message);
  72. }
  73. ~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement