Last active
August 1, 2017 08:51
-
-
Save hollodotme/9275ddadee3a8f64f8a04498f563d110 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
/** | |
* @author hollodotme | |
* This script receives a POST request from www.serverguard24.de | |
* and transforms it to a POST request to a slack webhook URL with JSON payload | |
* 1. Configure a slack webhook URL and paste it here in line 75 | |
* 2. Put this script on a server an make it accessible via http | |
* 3. Add a contact at serverguard24.de and paste the URL to this script in HTTP Push URL field | |
* 4. Add the contact to your server guard checks | |
* NOTE: Script is written for PHP >= 7.1 | |
*/ | |
namespace YourVendor\YourProject; | |
header('Conent-Type: text/plain; charset=utf-8', true, 200); | |
if ($_SERVER['REQUEST_METHOD'] !== 'POST') die('Nope'); | |
final class PostRequest | |
{ | |
private const SERVER_NAME = 'server_name'; | |
private const SERVER_ADDRESS = 'server_address'; | |
private const SERVICE_NAME = 'service_name'; | |
private const SERVICE_SHORTNAME = 'service_shortname'; | |
private const CHECK_RESULT = 'check_result'; | |
private const CHECK_OUTPUT = 'check_output'; | |
private const NOTIFICATION_TIME = 'notification_time'; | |
/** @var array */ | |
private $data; | |
public function __construct( array $data ) | |
{ | |
$this->data = $data; | |
} | |
public function getServerName() : string | |
{ | |
return (string)$this->data[self::SERVER_NAME]; | |
} | |
public function getServerAddress() : string | |
{ | |
return (string)$this->data[self::SERVER_ADDRESS]; | |
} | |
public function getServiceName() : string | |
{ | |
return (string)$this->data[self::SERVICE_NAME]; | |
} | |
public function getServiceShortname() : string | |
{ | |
return (string)$this->data[self::SERVICE_SHORTNAME]; | |
} | |
public function getCheckResult() : string | |
{ | |
return (string)$this->data[self::CHECK_RESULT]; | |
} | |
public function getCheckOutput() : string | |
{ | |
return (string)$this->data[self::CHECK_OUTPUT]; | |
} | |
public function getNotificationTime() : string | |
{ | |
return (string)$this->data[self::NOTIFICATION_TIME]; | |
} | |
} | |
final class SlackPusher | |
{ | |
private const WEBHOOK_URL = '<YOUR SLACK WEBHOOK URL HERE>'; | |
/** @var PostRequest */ | |
private $request; | |
public function __construct(PostRequest $request) | |
{ | |
$this->request = $request; | |
} | |
public function pushMessage() : void | |
{ | |
$payload = $this->buildPayload(); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, self::WEBHOOK_URL); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, ['payload' => $payload]); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$result = curl_exec($ch); | |
$curlInfo = curl_getinfo($ch); | |
$curlError = curl_error($ch); | |
curl_close($ch); | |
} | |
private function buildPayload() : string | |
{ | |
$payload = [ | |
'attachments' => [ | |
[ | |
"fallback" => "Server-Check für {$this->request->getServerName()} - {$this->request->getServiceName()}", | |
"pretext" => "Server-Check für {$this->request->getServerName()} - {$this->request->getServiceName()}", | |
"color" => ($this->request->getCheckResult() == "OK") ? "#249c1e" : "#d60a0a", | |
"fields" => [ | |
[ | |
"title" => $this->request->getCheckResult(), | |
"value" => $this->request->getCheckOutput() . " - " . $this->request->getNotificationTime(), | |
"short" => false, | |
], | |
], | |
], | |
], | |
]; | |
return json_encode($payload, JSON_PRETTY_PRINT); | |
} | |
} | |
$request = new PostRequest($_POST); | |
$slackPusher = new SlackPusher($request); | |
$slackPusher->pushMessage(); | |
die('OK'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment