mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
alerting: Added Gitlab transport for alerting (#8246)
* gitlab alert transport added * fixed SQL file and cleaned up a bit * updated license information * added relevant information Transports documentation * Fixed spacing issues * removed unnecessary text from variable * fixed sql schema file syntax * changed sql schema order * fixed creation of issues for resolution events * addressed file name conflict * fixed spacing, again * removed unused variable * Update Transports.md
This commit is contained in:
committed by
Neil Lathwood
parent
9dac4cfa6e
commit
764abde504
76
LibreNMS/Alert/Transport/Gitlab.php
Normal file
76
LibreNMS/Alert/Transport/Gitlab.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
/* Copyright (C) 2018 Drew Hynes <drew.hynes@gmail.com>
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
/**
|
||||
* Gitlab API Transport
|
||||
* @author Drew Hynes <drew.hynes@gmail.com>
|
||||
* @copyright 2018 Drew Hynes, LibreNMS
|
||||
* @license GPL
|
||||
* @package LibreNMS
|
||||
* @subpackage Alerts
|
||||
*/
|
||||
namespace LibreNMS\Alert\Transport;
|
||||
|
||||
use LibreNMS\Interfaces\Alert\Transport;
|
||||
|
||||
class Gitlab implements Transport
|
||||
{
|
||||
public function deliverAlert($obj, $opts)
|
||||
{
|
||||
// Don't create tickets for resolutions
|
||||
if ($obj['state'] == 0) {
|
||||
return true;
|
||||
} else {
|
||||
$device = device_by_id_cache($obj['device_id']); // for event logging
|
||||
|
||||
$project_id = $opts['project_id'];
|
||||
$project_key = $opts['key'];
|
||||
$details = "Librenms alert for: " . $obj['hostname'];
|
||||
$description = $obj['msg'];
|
||||
$title = urlencode($details);
|
||||
$desc = urlencode($description);
|
||||
$url = $opts['host'] . "/api/v4/projects/$project_id/issues?title=$title&description=$desc";
|
||||
$curl = curl_init();
|
||||
|
||||
$data = array("title" => $details,
|
||||
"description" => $description
|
||||
);
|
||||
$postdata = array("fields" => $data);
|
||||
$datastring = json_encode($postdata);
|
||||
|
||||
set_curl_proxy($curl);
|
||||
|
||||
$headers = array('Accept: application/json', 'Content-Type: application/json', 'PRIVATE-TOKEN: '.$project_key);
|
||||
|
||||
curl_setopt($curl, CURLOPT_URL, $url);
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($curl, CURLOPT_VERBOSE, 1);
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $datastring);
|
||||
|
||||
$ret = curl_exec($curl);
|
||||
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||
if ($code == 200) {
|
||||
$gitlabout = json_decode($ret, true);
|
||||
d_echo("Created Gitlab issue " . $gitlabout['key'] . " for " . $device);
|
||||
return true;
|
||||
} else {
|
||||
d_echo("Gitlab connection error: " . serialize($ret));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -449,3 +449,13 @@ $config['alert']['transports']['jira']['password'] = 'myjirapass';
|
||||
$config['alert']['transports']['jira']['prjkey'][] = 'JIRAPROJECTKEY';
|
||||
$config['alert']['transports']['jira']['issuetype'][] = 'Myissuetype';
|
||||
```
|
||||
|
||||
## Gitlab
|
||||
|
||||
LibreNMS will create issues for warning and critical level alerts however only title and description are set. Uses Personal access tokens to authenticate with Gitlab and will store the token in cleartext.
|
||||
|
||||
```php
|
||||
$config['alert']['transports']['gitlab']['host'] = 'http://gitlab.host.tld';
|
||||
$config['alert']['transports']['gitlab']['project_id'] = '1';
|
||||
$config['alert']['transports']['gitlab']['key'] = 'AbCdEf12345';
|
||||
```
|
||||
|
@@ -1379,12 +1379,57 @@ echo '
|
||||
<input id="syslog_facility" class="form-control" type="text" name="global-config-input" value="'.$syslog_facility['config_value'].'" data-config_id="'.$syslog_facility['config_id'].'" onkeypress="return (event.charCode == 8 || event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57">
|
||||
<span class="form-control-feedback">
|
||||
<i class="fa" aria-hidden="true"></i>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>';
|
||||
// Gitlab Transport Section
|
||||
|
||||
$gitlab_host = get_config_by_name('alert.transports.gitlab.host');
|
||||
$gitlab_project = get_config_by_name('alert.transports.gitlab.project_id');
|
||||
$gitlab_key = get_config_by_name('alert.transports.gitlab.key');
|
||||
echo '
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">
|
||||
<h4 class="panel-title">
|
||||
<a data-toggle="collapse" data-parent="#accordion" href="#gitlab_transport_expand"><i class="fa fa-caret-down"></i> Gitlab transport</a> <button name="test-alert" id="test-alert" type="button" data-transport="gitlab" class="btn btn-primary btn-xs pull-right">Test transport</button>
|
||||
</h4>
|
||||
</div>
|
||||
<div id="gitlab_transport_expand" class="panel-collapse collapse">
|
||||
<div class="panel-body">
|
||||
<div class="form-group has-feedback">
|
||||
<label for="gitlab_host" class="col-sm-4 control-label">Gitlab Host</label>
|
||||
<div class="col-sm-4">
|
||||
<input id="gitlab_host" class="form-control validation" type="url" name="global-config-input" value="'.$gitlab_host['config_value'].'" data-config_id="'.$gitlab_host['config_id'].'" pattern="[a-zA-Z0-9]{1,5}://.*">
|
||||
<span class="form-control-feedback">
|
||||
<i class="fa" aria-hidden="true"></i>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group has-feedback">
|
||||
<label for="gitlab_project" class="col-sm-4 control-label">Gitlab Project ID</label>
|
||||
<div class="col-sm-4">
|
||||
<input id="gitlab_project" class="form-control" type="text" name="global-config-input" value="'.$gitlab_project['config_value'].'" data-config_id="'.$gitlab_project['config_id'].'">
|
||||
<span class="form-control-feedback">
|
||||
<i class="fa" aria-hidden="true"></i>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group has-feedback">
|
||||
<label for="gitlab_key" class="col-sm-4 control-label">Gitlab API Key</label>
|
||||
<div class="col-sm-4">
|
||||
<input id="gitlab_key" class="form-control" type="text" name="global-config-input" value="'.$gitlab_key['config_value'].'" data-config_id="'.$gitlab_key['config_id'].'">
|
||||
<span class="form-control-feedback">
|
||||
<i class="fa" aria-hidden="true"></i>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>';
|
||||
|
||||
// Jira Transport Section
|
||||
$jira_prj = get_config_by_name('alert.transports.jira.prjkey');
|
||||
$jira_url = get_config_by_name('alert.transports.jira.url');
|
||||
|
3
sql-schema/238.sql
Normal file
3
sql-schema/238.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
INSERT INTO `config` (`config_name`,`config_value`,`config_default`,`config_descr`,`config_group`,`config_group_order`,`config_sub_group`,`config_sub_group_order`,`config_hidden`,`config_disabled`) VALUES('alert.transports.gitlab.host','','','Gitlab URL','alerting','0','transports','0','0','0');
|
||||
INSERT INTO `config` (`config_name`,`config_value`,`config_default`,`config_descr`,`config_group`,`config_group_order`,`config_sub_group`,`config_sub_group_order`,`config_hidden`,`config_disabled`) VALUES('alert.transports.gitlab.project_id','','','Gitlab Project ID','alerting','0','transports','0','0','0');
|
||||
INSERT INTO `config` (`config_name`,`config_value`,`config_default`,`config_descr`,`config_group`,`config_group_order`,`config_sub_group`,`config_sub_group_order`,`config_hidden`,`config_disabled`) VALUES('alert.transports.gitlab.key','','','Gitlab Private Token','alerting','0','transports','0','0','0');
|
Reference in New Issue
Block a user