mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
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:
@@ -28,7 +28,6 @@ class Msteams extends Transport
|
|||||||
public function contactMsteams($obj, $opts)
|
public function contactMsteams($obj, $opts)
|
||||||
{
|
{
|
||||||
$url = $opts['url'];
|
$url = $opts['url'];
|
||||||
|
|
||||||
$data = array(
|
$data = array(
|
||||||
'title' => $obj['title'],
|
'title' => $obj['title'],
|
||||||
'themeColor' => self::getColorForState($obj['state']),
|
'themeColor' => self::getColorForState($obj['state']),
|
||||||
@@ -43,9 +42,11 @@ class Msteams extends Transport
|
|||||||
'Expect:'
|
'Expect:'
|
||||||
));
|
));
|
||||||
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
|
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);
|
$ret = curl_exec($curl);
|
||||||
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||||
|
|
||||||
if ($code != 200) {
|
if ($code != 200) {
|
||||||
var_dump("Microsoft Teams returned Error, retry later");
|
var_dump("Microsoft Teams returned Error, retry later");
|
||||||
return false;
|
return false;
|
||||||
@@ -62,6 +63,13 @@ class Msteams extends Transport
|
|||||||
'name' => 'msteam-url',
|
'name' => 'msteam-url',
|
||||||
'descr' => 'Microsoft Teams Webhook URL',
|
'descr' => 'Microsoft Teams Webhook URL',
|
||||||
'type' => 'text',
|
'type' => 'text',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'title' => 'Use JSON?',
|
||||||
|
'name' => 'use-json',
|
||||||
|
'descr' => 'Compose MessageCard with JSON rather than Markdown',
|
||||||
|
'type' => 'checkbox',
|
||||||
|
'default' => false,
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
'validation' => [
|
'validation' => [
|
||||||
|
@@ -361,21 +361,6 @@ Template: CPU alert <br>
|
|||||||
@endif
|
@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
|
## Included
|
||||||
|
|
||||||
We include a few templates for you to use, these are specific to the
|
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
|
- BGP Sessions
|
||||||
- Ports
|
- Ports
|
||||||
- Temperature
|
- 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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
@@ -356,14 +356,19 @@ As a small reminder, here is it's configuration directives including defaults:
|
|||||||
|
|
||||||
## Microsoft Teams
|
## Microsoft Teams
|
||||||
|
|
||||||
Microsoft Teams. LibreNMS can send alerts to Microsoft Teams Connector
|
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
|
||||||
API which are then posted to a specific channel.
|
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:**
|
**Example:**
|
||||||
|
|
||||||
| Config | Example |
|
| Config | Example |
|
||||||
| ------ | ------- |
|
| ------ | ------- |
|
||||||
| WebHook URL | <https://outlook.office365.com/webhook/123456789> |
|
| WebHook URL | <https://outlook.office365.com/webhook/123456789> |
|
||||||
|
| Use JSON? | x |
|
||||||
|
|
||||||
## Nagios Compatible
|
## Nagios Compatible
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user