Permalink
Browse files

Add Webhook to Warning Light

  • Loading branch information...
NEMS Linux
NEMS Linux committed Dec 19, 2018
1 parent 21c5c11 commit 0c131783dbf1c09d9880699c7e2a3d3532859e79
Showing with 147 additions and 0 deletions.
  1. +36 −0 warninglight
  2. +111 −0 webhook
@@ -188,6 +188,11 @@ function setstate($state) {
case 'ok': // Green light.
if ($currentstate != $previousstate) $currentissueshead = 'NEMS State: OK.' . PHP_EOL;
$fieldsarray[] = array(
'name' => 'Everything is okay',
'value' => 'No known issues',
'inline' => false
);
break;
case 'unknown': // Turn on orange light.
@@ -198,6 +203,11 @@ function setstate($state) {
}
if (isset($issues[3])) foreach ($issues[3] as $issue) {
$currentissues .= ' - Service "' . $issue['service'] . '" on ' . $issue['host'] . ':' . PHP_EOL . ' ' . $issue['error'] . PHP_EOL;
$fieldsarray[] = array(
'name' => $issue['service'] . ' on ' . $issue['host'],
'value' => $issue['error'],
'inline' => true
);
}
break;
@@ -209,11 +219,21 @@ function setstate($state) {
}
if (isset($issues[1])) foreach ($issues[1] as $issue) {
$currentissues .= ' - Service "' . $issue['service'] . '" on ' . $issue['host'] . ':' . PHP_EOL . ' ' . $issue['error'] . PHP_EOL;
$fieldsarray[] = array(
'name' => $issue['service'] . ' on ' . $issue['host'],
'value' => $issue['error'],
'inline' => true
);
}
if (isset($issues[3])) {
$currentissues .= 'Other issues:' . PHP_EOL;
foreach ($issues[3] as $issue) {
$currentissues .= ' - Service "' . $issue['service'] . '" on ' . $issue['host'] . ':' . PHP_EOL . ' ' . $issue['error'] . PHP_EOL;
$fieldsarray[] = array(
'name' => $issue['service'] . ' on ' . $issue['host'],
'value' => $issue['error'],
'inline' => true
);
}
}
break;
@@ -226,17 +246,32 @@ function setstate($state) {
}
if (isset($issues[2])) foreach ($issues[2] as $issue) {
$currentissues .= ' - Service "' . $issue['service'] . '" on ' . $issue['host'] . ':' . PHP_EOL . ' ' . $issue['error'] . PHP_EOL;
$fieldsarray[] = array(
'name' => $issue['service'] . ' on ' . $issue['host'],
'value' => $issue['error'],
'inline' => true
);
}
if (isset($issues[1]) || isset($issues[3])) {
$currentissues .= 'Other issues:' . PHP_EOL;
if (isset($issues[1])) { // warnings
foreach ($issues[1] as $issue) {
$currentissues .= ' - Service "' . $issue['service'] . '" on ' . $issue['host'] . ':' . PHP_EOL . ' ' . $issue['error'] . PHP_EOL;
$fieldsarray[] = array(
'name' => $issue['service'] . ' on ' . $issue['host'],
'value' => $issue['error'],
'inline' => true
);
}
}
if (isset($issues[3])) { // unknown
foreach ($issues[3] as $issue) {
$currentissues .= ' - Service "' . $issue['service'] . '" on ' . $issue['host'] . ':' . PHP_EOL . ' ' . $issue['error'] . PHP_EOL;
$fieldsarray[] = array(
'name' => $issue['service'] . ' on ' . $issue['host'],
'value' => $issue['error'],
'inline' => true
);
}
}
}
@@ -245,6 +280,7 @@ function setstate($state) {
}
if ($currentstate != $previousstate || $currentissues != $previousissues) {
echo '[' . date('Y-m-d g:i:sa') . '] ' . $currentissueshead . $currentissues;
include('/root/nems/nems-tools/webhook');
}
}
111 webhook
@@ -0,0 +1,111 @@
<?php

// this file is included from warninglight. Not run separately.

$webhook = trim(shell_exec('/usr/local/bin/nems-info webhook'));

// currentissueshead set means 1) this was called by warninglight and 2) something has changed

if (isset($currentissueshead) && strlen($webhook) > 0) {

$alias = trim(shell_exec('/usr/local/bin/nems-info alias'));
$nemsver = trim(shell_exec('/usr/local/bin/nems-info nemsver'));

$light_cdn = 'https://cdn.zecheriah.com/nems/img/lights/';
$stateup = strtoupper($state);
switch ($stateup) {
case 'OK':
$color='green';
break;

case 'WARNING':
case 'UNKNOWN':
$color='yellow';
break;

case 'CRITICAL':
$color='red';
break;

default:
$color='yellow';
}

if (!isset($fieldsarray)) {
$fieldsarray[] = array(
'name' => 'No Info',
'value' => 'Something is up.',
'inline' => false
);
}

if (!class_exists('RemoteResult')) {
class RemoteResult {
public $name;
public $value;
public $inline;

public function __construct($name, $value, $inline) {
$this->name = $name;
$this->value = $value;
$this->inline = $inline;
}

public function toJson() {
return [
'name' => $this->name,
'value' => $this->value,
'inline' => $this->inline,
];
}
}
}

$output = [];

foreach ($fieldsarray as $input) {
array_push($output, new RemoteResult($input['name'], $input['value'], $input['inline']));
}

// Create the webhook object
$hookObject = new stdClass();
$hookObject->content = strtoupper($state);
$hookObject->username = 'NEMS ' . strtoupper($state);
$hookObject->avatar_url = $light_cdn . $color . '.png';
$hookObject->tts = false;

$hookObject->embeds = array();
$hookObject->embeds[0] = array();
$hookObject->embeds[0]['title'] = $currentissueshead;
$hookObject->embeds[0]['type'] = 'rich';
$hookObject->embeds[0]['description'] = '';
$hookObject->embeds[0]['timestamp'] = date('c');
$hookObject->embeds[0]['color'] = hexdec('FFFFFF');

$hookObject->embeds[0]['author'] = new stdClass();
$hookObject->embeds[0]['author']->name = 'Reporting Server: ' . $alias;

$hookObject->embeds[0]['footer'] = new stdClass();
$hookObject->embeds[0]['footer']->text = 'Powered by NEMS Linux ' . $nemsver;

$hookObject->embeds[0]['fields'] = $output;

$ch = curl_init();

$hookObjectJSON = json_encode($hookObject, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );

curl_setopt_array( $ch, [
CURLOPT_URL => $webhook,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $hookObjectJSON,
CURLOPT_HTTPHEADER => [
"Length" => strlen( $hookObjectJSON ),
"Content-Type" => "application/json"
]
]);

$response = curl_exec( $ch );
curl_close( $ch );

}
?>

0 comments on commit 0c13178

Please sign in to comment.