Merge pull request #669 from supertylerc/feat/hipchat-alerts

add new hipchat transport for alerts
This commit is contained in:
Daniel Preussker
2015-03-27 05:45:31 +00:00
2 changed files with 101 additions and 0 deletions

View File

@ -13,6 +13,7 @@ Table of Content:
- [Nagios-Compatible](#transports-nagios)
- [IRC](#transports-irc)
- [Slack](#transports-slack)
- [HipChat](#transports-hipchat)
- [Entities](#entities)
- [Devices](#entity-devices)
- [BGP Peers](#entity-bgppeers)
@ -191,6 +192,51 @@ $config['alert']['transports']['slack'][] = array('url' => "https://hooks.slack.
```
## <a name="transports-hipchat">HipChat</a>
The HipChat transport requires the following:
__room_id__ = HipChat Room ID
__url__ = HipChat API URL+API Key
__from__ = The name that will be displayed
The HipChat transport makes the following optional:
__color__ = Any of HipChat's supported message colors
__message_format__ = Any of HipChat's supported message formats
__notify__ = 0 or 1
See the HipChat API Documentation for
[rooms/message](https://www.hipchat.com/docs/api/method/rooms/message)
for details on acceptable values.
> You may notice that the link points at the "deprecated" v1 API. This is
> because the v2 API is still in beta.
Below are two examples of sending messages to a HipChat room.
```php
$config['alert']['transports']['hipchat'][] = array("url" => "https://api.hipchat.com/v1/rooms/message?auth_token=9109jawregoaih",
"room_id" => "1234567",
"from" => "LibreNMS");
$config['alert']['transports']['hipchat'][] = array("url" => "https://api.hipchat.com/v1/rooms/message?auth_token=109jawregoaihj",
"room_id" => "7654321",
"from" => "LibreNMS",
"color" => "red",
"notify" => 1,
"message_format" => "text");
```
> Note: The default message format for HipChat messages is HTML. It is
> recommended that you specify the `text` message format to prevent unexpected
> results, such as HipChat attempting to interpret angled brackets (`<` and
> `>`).
# <a name="entities">Entities
Entities as described earlier are based on the table and column names within the database, if you are ensure of what the entity is you want then have a browse around inside MySQL using `show tables` and `desc <tablename>`.

View File

@ -0,0 +1,55 @@
/* Copyright (C) 2014 Daniel Preussker <f0o@devilcode.org>, Tyler Christiansen <code@tylerc.me>
* 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/>. */
/*
* API Transport
* @author Tyler Christiansen <code@tylerc.me>
* @copyright 2014 Daniel Preussker, Tyler Christiansen, LibreNMS
* @license GPL
* @package LibreNMS
* @subpackage Alerts
*/
foreach($opts as $option) {
$url = $option['url'];
foreach($obj as $key=>$value) {
$api = str_replace("%".$key, $method == "get" ? urlencode($value) : $value, $api);
}
$curl = curl_init();
$data = array(
"message" => $obj["msg"],
"room_id" => $option["room_id"],
"from" => $option["from"],
"color" => $option["color"],
"notify" => $option["notify"],
"message_format" => $option["message_format"]
);
// Sane default of making the message color green if the message indicates
// that the alert recovered.
if(strpos($data["message"], "recovered")) { $data["color"] = "green"; }
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$ret = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if($code != 200) {
var_dump("API '$url' returnd Error");
var_dump("Params: " . $message);
var_dump("Return: " . $ret);
return false;
}
}
return true;