1 | <?php |
||
2 | /* Copyright (C) 2015 Daniel Preussker <[email protected]> |
||
3 | * This program is free software: you can redistribute it and/or modify |
||
4 | * it under the terms of the GNU General Public License as published by |
||
5 | * the Free Software Foundation, either version 3 of the License, or |
||
6 | * (at your option) any later version. |
||
7 | * |
||
8 | * This program is distributed in the hope that it will be useful, |
||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
||
11 | * GNU General Public License for more details. |
||
12 | * |
||
13 | * You should have received a copy of the GNU General Public License |
||
14 | * along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
||
15 | |||
16 | /** |
||
17 | * PagerDuty Generic-API Transport |
||
18 | * |
||
19 | * @author f0o <[email protected]> |
||
20 | * @copyright 2015 f0o, LibreNMS |
||
21 | * @license GPL |
||
22 | */ |
||
23 | |||
24 | namespace LibreNMS\Alert\Transport; |
||
25 | |||
26 | use GuzzleHttp\Client; |
||
27 | use GuzzleHttp\Exception\GuzzleException; |
||
28 | use Illuminate\Http\Request; |
||
29 | use LibreNMS\Alert\Transport; |
||
30 | use LibreNMS\Enum\AlertState; |
||
31 | use Log; |
||
32 | use Validator; |
||
33 | |||
34 | class Pagerduty extends Transport |
||
35 | { |
||
36 | public static $integrationKey = '2fc7c9f3c8030e74aae6'; |
||
37 | |||
38 | public function deliverAlert($obj, $opts) |
||
39 | { |
||
40 | if ($obj['state'] == AlertState::RECOVERED) { |
||
41 | $obj['event_type'] = 'resolve'; |
||
42 | } elseif ($obj['state'] == AlertState::ACKNOWLEDGED) { |
||
43 | $obj['event_type'] = 'acknowledge'; |
||
44 | } else { |
||
45 | $obj['event_type'] = 'trigger'; |
||
46 | } |
||
47 | |||
48 | return $this->contactPagerduty($obj, $this->config); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param array $obj |
||
53 | * @param array $config |
||
54 | * @return bool|string |
||
55 | */ |
||
56 | public function contactPagerduty($obj, $config) |
||
57 | { |
||
58 | $data = [ |
||
59 | 'routing_key' => $config['service_key'], |
||
60 | 'event_action' => $obj['event_type'], |
||
61 | 'dedup_key' => (string) $obj['alert_id'], |
||
62 | 'payload' => [ |
||
63 | 'custom_details' => strip_tags($obj['msg']) ?: 'Test', |
||
64 | 'device_groups' => \DeviceCache::get($obj['device_id'])->groups->pluck('name'), |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
65 | 'source' => $obj['hostname'], |
||
66 | 'severity' => $obj['severity'], |
||
67 | 'summary' => ($obj['name'] ? $obj['name'] . ' on ' . $obj['hostname'] : $obj['title']), |
||
68 | ], |
||
69 | ]; |
||
70 | |||
71 | // EU service region |
||
72 | if ($config['region'] == 'EU') { |
||
73 | $url = 'https://events.eu.pagerduty.com/v2/enqueue'; |
||
74 | } |
||
75 | |||
76 | // US service region |
||
77 | else { |
||
78 | $url = 'https://events.pagerduty.com/v2/enqueue'; |
||
79 | } |
||
80 | |||
81 | $client = new Client(); |
||
82 | |||
83 | $request_opts = ['json' => $data]; |
||
84 | $request_opts['proxy'] = get_proxy(); |
||
85 | |||
86 | try { |
||
87 | $result = $client->request('POST', $url, $request_opts); |
||
88 | |||
89 | if ($result->getStatusCode() == 202) { |
||
90 | return true; |
||
91 | } |
||
92 | |||
93 | return $result->getReasonPhrase(); |
||
94 | } catch (GuzzleException $e) { |
||
95 | return 'Request to PagerDuty API failed. ' . $e->getMessage(); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | public static function configTemplate() |
||
100 | { |
||
101 | return [ |
||
102 | 'config' => [ |
||
103 | [ |
||
104 | 'title' => 'Authorize (EU)', |
||
105 | 'descr' => 'Alert with PagerDuty', |
||
106 | 'type' => 'oauth', |
||
107 | 'icon' => 'pagerduty-white.svg', |
||
108 | 'class' => 'btn-success', |
||
109 | 'url' => 'https://connect.eu.pagerduty.com/connect?vendor=' . self::$integrationKey . '&callback=', |
||
110 | ], |
||
111 | [ |
||
112 | 'title' => 'Authorize (US)', |
||
113 | 'descr' => 'Alert with PagerDuty', |
||
114 | 'type' => 'oauth', |
||
115 | 'icon' => 'pagerduty-white.svg', |
||
116 | 'class' => 'btn-success', |
||
117 | 'url' => 'https://connect.pagerduty.com/connect?vendor=' . self::$integrationKey . '&callback=', |
||
118 | ], |
||
119 | [ |
||
120 | 'title' => 'Service Region', |
||
121 | 'name' => 'region', |
||
122 | 'descr' => 'Service Region of the PagerDuty account', |
||
123 | 'type' => 'select', |
||
124 | 'options' => [ |
||
125 | 'EU' => 'EU', |
||
126 | 'US' => 'US', |
||
127 | ], |
||
128 | ], |
||
129 | [ |
||
130 | 'title' => 'Account', |
||
131 | 'type' => 'hidden', |
||
132 | 'name' => 'account', |
||
133 | ], |
||
134 | [ |
||
135 | 'title' => 'Service', |
||
136 | 'type' => 'hidden', |
||
137 | 'name' => 'service_name', |
||
138 | ], |
||
139 | [ |
||
140 | 'title' => 'Integration Key', |
||
141 | 'type' => 'text', |
||
142 | 'name' => 'service_key', |
||
143 | ], |
||
144 | ], |
||
145 | 'validation' => [ |
||
146 | 'region' => 'in:EU,US', |
||
147 | ], |
||
148 | ]; |
||
149 | } |
||
150 | |||
151 | public function handleOauth(Request $request) |
||
152 | { |
||
153 | $validator = Validator::make($request->all(), [ |
||
154 | 'account' => 'alpha_dash', |
||
155 | 'service_key' => 'regex:/^[a-fA-F0-9]+$/', |
||
156 | 'service_name' => 'string', |
||
157 | ]); |
||
158 | |||
159 | if ($validator->fails()) { |
||
160 | Log::error('Pagerduty oauth failed validation.', ['request' => $request->all()]); |
||
161 | |||
162 | return false; |
||
163 | } |
||
164 | |||
165 | $config = json_encode($request->only('account', 'service_key', 'service_name')); |
||
166 | |||
167 | if ($id = $request->get('id')) { |
||
168 | return (bool) dbUpdate(['transport_config' => $config], 'alert_transports', 'transport_id=?', [$id]); |
||
169 | } else { |
||
170 | return (bool) dbInsert([ |
||
171 | 'transport_name' => $request->get('service_name', 'PagerDuty'), |
||
172 | 'transport_type' => 'pagerduty', |
||
173 | 'is_default' => 0, |
||
174 | 'transport_config' => $config, |
||
175 | ], 'alert_transports'); |
||
176 | } |
||
177 | } |
||
178 | } |
||
179 |