📜 ⬆️ ⬇️

Mattermost. Integration with external services (part 2)

We continue to deal with the mattermost in terms of integration with external services.


Part two. Integration with Zabbix


In the second part of the integration story mattermost, it will be about sending accident reports from zabbix to mattermost. As a result of searches in the network was taken as a basis here
this script . The code is written in Perl, so you may need to install the pearl-barley packages. Before proceeding to the description of the code (it is somewhat modified from the original), first, as usual, we will make some adjustments.


Mattermost


To use the mechanism for receiving messages from third-party services, you need to add a record of incoming "hooks". Go to the settings "Integrations" -> "Incoming webhooks" and add the entry:


image


After that, copy the underlined link to paste into the script.


image


Zabbix


In the zabbix management interface through the menu "Administration" -> "Alert methods" we will add a new alert method:


image


Where in the script parameters you should specify:


  1. {ALERT.SENDTO}
  2. Link to the materialmost webbook (copied earlier)
  3. The username to the mattermost from which the messages will come
  4. Link to avatar (file must be accessible via http)
  5. {ALERT.MESSAGE}

Then, in the user settings, add a new notification method:


image


Where in the field "Send to" you need to specify the name of the channel in mattermost, where messages will pour, in our case it is "it" (it is worth noting that the system name of the channel is used). This value will be substituted for the {ALERT.SENDTO} macro, which appears in the parameters of the newly created notification method. {ALERT.MESSAGE} - this is actually the message itself.


The original script has undergone some changes, this is due to the fact that the used version of zabbix does not give the message to json (maybe it is configured somewhere). Because the code had to change. The code is supplied with comments and should not cause difficulties in understanding.
On the server where Zabbix is ​​installed, create the file /usr/lib/zabbix/alertscripts/zabbixMatterBot.pl (I have Debian and zabbix installed from the official repository) with the following content:


#!/usr/bin/perl # https://github.com/drewbeer/zabbix-mattermost-alertscript DrewBeer # passes data in and curls it out via json to mattermost webhooks as attachments. # you can use this as you wish, free as in beer, life is that way. # minify your json before you set it in zabbix, it will make your life easier use warnings; use strict; use JSON; use Data::Dumper; #   , /tmp/zabbix-mattermost.log my $debug = 0; my $logFH; my $zabbixData = (); # debug log the incoming data if ($debug) { open($logFH, '>>', '/var/log/zabbix/zabbix-mattermost.log'); my $dump = Dumper(@ARGV); print $logFH "args:\n$dump\n"; } #    zabbix #  -  ,   mattermost,  , #   ,   my ($channel, $hook, $botName, $iconUrl, $body) = @ARGV; $zabbixData = $body; #   my $payload; $payload = processInternal($zabbixData); #  if ($debug) { print $logFH "final payload: $payload\n"; } #   if ($payload) { sendPayload($payload); } exit; #    #  curl sub sendPayload { my($payload) = @_; my $cmd = qq( curl -s -i -X POST --data-urlencode '$payload' $hook > /dev/null); if ($debug) { $cmd = qq( curl -i -X POST --data-urlencode '$payload' $hook ); } my $cmdOutput = `$cmd`; # final debug if ($debug) { print $logFH "curl:\n$cmd \n$cmdOutput\n"; } } #    zabbix #   json- sub processInternal { my $data = shift; my $attach = (); #    $attach->{'channel'} = $channel; $attach->{'username'} = $botName; $attach->{'icon_url'} = $iconUrl; $attach->{'response_type'} = 'comment'; $attach->{'text'} = $data; if ($debug) { my $body = Dumper $attach; print $logFH "object:\n$body\n"; } my $jsonBody = encode_json $attach; #    my $jsonPayload = qq(payload=$jsonBody); #    return $jsonPayload; } 

Now either we wait for the trigger to trigger, or we simulate an accident in zabbix and get the message


image


That's the whole integration.


')

Source: https://habr.com/ru/post/442404/


All Articles