Added ability to use JSON directly in Msteams Transport (#11129)

* Added ability to use JSON directly in Msteams Transport

* Update Transports.md

* Update Msteams.php

* Update Msteams.php

Added boolean to fix test transport button

* Update Templates.md

Added JSON example and modified titles slightly.

* Update Templates.md

Moved MS Teams examples under "Examples" header since they are no longer formatted in HTML

* Update Templates.md

* Update Templates.md

* Update Msteams.php

changed condition to use $obj['id'] rather than $obj['msg']

* Update Msteams.php
This commit is contained in:
pepperoni-pi
2020-06-04 20:18:49 -07:00
committed by GitHub
parent cd362b7983
commit 28ae37c406
3 changed files with 145 additions and 19 deletions

View File

@@ -28,7 +28,6 @@ class Msteams extends Transport
public function contactMsteams($obj, $opts)
{
$url = $opts['url'];
$data = array(
'title' => $obj['title'],
'themeColor' => self::getColorForState($obj['state']),
@@ -43,9 +42,11 @@ class Msteams extends Transport
'Expect:'
));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
if ($this->config['use-json'] === 'on' && $obj['uid'] !== '000') {
curl_setopt($curl, CURLOPT_POSTFIELDS, $obj['msg']);
}
$ret = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($code != 200) {
var_dump("Microsoft Teams returned Error, retry later");
return false;
@@ -62,6 +63,13 @@ class Msteams extends Transport
'name' => 'msteam-url',
'descr' => 'Microsoft Teams Webhook URL',
'type' => 'text',
],
[
'title' => 'Use JSON?',
'name' => 'use-json',
'descr' => 'Compose MessageCard with JSON rather than Markdown',
'type' => 'checkbox',
'default' => false,
]
],
'validation' => [

View File

@@ -361,21 +361,6 @@ Template: CPU alert <br>
@endif
```
#### MS Teams formatted default template
```
<a href="https://your.librenms.url/device/device={{ $alert->device_id }}/">{{ $alert->title }}</a>
<pre><strong>Device name:</strong> {{ $alert->sysName }}
<strong>Severity:</strong> {{ $alert->severity }}
@if ($alert->state == 0)<strong>Time elapsed:</strong>{{ $alert->elapsed }}
@endif<strong>Timestamp:</strong> {{ $alert->timestamp }}
<strong>Unique-ID:</strong> {{ $alert->uid }}
<strong>Rule:</strong>@if ($alert->name) {{ $alert->name }} @else {{ $alert->rule }} @endif</pre>
<pre style="white-space:normal;">@if ($alert->faults) <strong>Faults:</strong>
@foreach ($alert->faults as $key => $value) #{{ $key }}: {{ $value['string'] }}
@endforeach </pre> @endif
```
## Included
We include a few templates for you to use, these are specific to the
@@ -388,3 +373,131 @@ The included templates apart from the default template are:
- BGP Sessions
- Ports
- Temperature
## Other Examples
#### Microsoft Teams - Markdown
```
[{{ $alert->title }}](https://your.librenms.url/device/device={{ $alert->device_id }}/)
**Device name:** {{ $alert->sysName }}
**Severity:** {{ $alert->severity }}
@if ($alert->state == 0)
**Time elapsed:** {{ $alert->elapsed }}
@endif
**Timestamp:** {{ $alert->timestamp }}
**Unique-ID:** {{ $alert->uid }}
@if ($alert->name)
**Rule:** {{ $alert->name }}
@else
**Rule:** {{ $alert->rule }}
@endif
@if ($alert->faults)
**Faults:**@foreach ($alert->faults as $key => $value) {{ $key }}: {{ $value['string'] }}
@endforeach
@endif
```
#### Microsoft Teams - JSON
```
{
"@context": "https://schema.org/extensions",
"@type": "MessageCard",
"title": "{{ $alert->title }}",
@if ($alert->state === 0)
"themeColor": "00FF00",
@elseif ($alert->state === 1)
"themeColor": "FF0000",
@elseif ($alert->state === 2)
"themeColor": "337AB7",
@elseif ($alert->state === 3)
"themeColor": "FF0000",
@elseif ($alert->state === 4)
"themeColor": "F0AD4E",
@else
"themeColor": "337AB7",
@endif
"summary": "LibreNMS",
"sections": [
{
@if ($alert->name)
"facts": [
{
"name": "Rule:",
"value": "[{{ $alert->name }}](https://your.librenms.url/device/device={{ $alert->device_id }}/tab=alert/)"
},
@else
{
"name": "Rule:",
"value": "[{{ $alert->rule }}](https://your.librenms.url/device/device={{ $alert->device_id }}/tab=alert/)"
},
@endif
{
"name": "Severity:",
"value": "{{ $alert->severity }}"
},
{
"name": "Unique-ID:",
"value": "{{ $alert->uid }}"
},
{
"name": "Timestamp:",
"value": "{{ $alert->timestamp }}"
},
@if ($alert->state == 0)
{
"name": "Time elapsed:",
"value": "{{ $alert->elapsed }}"
},
@endif
{
"name": "Hostname:",
"value": "[{{ $alert->hostname }}](https://your.librenms.url/device/device={{ $alert->device_id }}/)"
},
{
"name": "Hardware:",
"value": "{{ $alert->hardware }}"
},
{
"name": "IP:",
"value": "{{ $alert->ip }}"
},
{
"name": "Faults:",
"value": " "
}
]
@if ($alert->faults)
@foreach ($alert->faults as $key => $value)
},
{
"facts": [
{
"name": "Port:",
"value": "[{{ $value['ifName'] }}](https://your.librenms.url/device/device={{ $alert->device_id }}/tab=port/port={{ $value['port_id'] }}/)"
},
{
"name": "Description:",
"value": "{{ $value['ifAlias'] }}"
},
@if ($alert->state != 0)
{
"name": "Status:",
"value": "down"
}
]
@else
{
"name": "Status:",
"value": "up"
}
]
@endif
@endforeach
@endif
}
]
}
```

View File

@@ -356,14 +356,19 @@ As a small reminder, here is it's configuration directives including defaults:
## Microsoft Teams
Microsoft Teams. LibreNMS can send alerts to Microsoft Teams Connector
API which are then posted to a specific channel.
LibreNMS can send alerts to Microsoft Teams [Incoming Webhooks](https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook) which are
then posted to a specific channel. Microsoft recommends using
[markdown](https://docs.microsoft.com/en-us/microsoftteams/platform/task-modules-and-cards/cards/cards-format#markdown-formatting-for-connector-cards) formatting for connector cards.
Administrators can opt to [compose](https://messagecardplayground.azurewebsites.net/)
the [MessageCard](https://docs.microsoft.com/en-us/outlook/actionable-messages/message-card-reference)
themselves using JSON to get the full functionality.
**Example:**
| Config | Example |
| ------ | ------- |
| WebHook URL | <https://outlook.office365.com/webhook/123456789> |
| Use JSON? | x |
## Nagios Compatible