1 | <?php |
||
2 | /* Copyright (C) 2018 Drew Hynes <[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 | * GitLab API Transport |
||
18 | * |
||
19 | * @author Drew Hynes <[email protected]> |
||
20 | * @copyright 2018 Drew Hynes, LibreNMS |
||
21 | * @license GPL |
||
22 | */ |
||
23 | |||
24 | namespace LibreNMS\Alert\Transport; |
||
25 | |||
26 | use LibreNMS\Alert\Transport; |
||
27 | use LibreNMS\Enum\AlertState; |
||
28 | |||
29 | class Gitlab extends Transport |
||
30 | { |
||
31 | public function deliverAlert($obj, $opts) |
||
32 | { |
||
33 | if (! empty($this->config)) { |
||
34 | $opts['project-id'] = $this->config['gitlab-id']; |
||
35 | $opts['key'] = $this->config['gitlab-key']; |
||
36 | $opts['host'] = $this->config['gitlab-host']; |
||
37 | } |
||
38 | |||
39 | return $this->contactGitlab($obj, $opts); |
||
40 | } |
||
41 | |||
42 | public function contactGitlab($obj, $opts) |
||
43 | { |
||
44 | // Don't create tickets for resolutions |
||
45 | if ($obj['state'] != AlertState::CLEAR) { |
||
46 | $device = device_by_id_cache($obj['device_id']); // for event logging |
||
47 | |||
48 | $project_id = $opts['project-id']; |
||
49 | $project_key = $opts['key']; |
||
50 | $details = 'Librenms alert for: ' . $obj['hostname']; |
||
51 | $description = $obj['msg']; |
||
52 | $title = urlencode($details); |
||
53 | $desc = urlencode($description); |
||
54 | $url = $opts['host'] . "/api/v4/projects/$project_id/issues?title=$title&description=$desc"; |
||
55 | $curl = curl_init(); |
||
56 | |||
57 | $data = ['title' => $details, |
||
58 | 'description' => $description, |
||
59 | ]; |
||
60 | $postdata = ['fields' => $data]; |
||
61 | $datastring = json_encode($postdata); |
||
62 | |||
63 | set_curl_proxy($curl); |
||
64 | |||
65 | $headers = ['Accept: application/json', 'Content-Type: application/json', 'PRIVATE-TOKEN: ' . $project_key]; |
||
66 | |||
67 | curl_setopt($curl, CURLOPT_URL, $url); |
||
68 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); |
||
69 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); |
||
70 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
71 | curl_setopt($curl, CURLOPT_VERBOSE, 1); |
||
72 | curl_setopt($curl, CURLOPT_POSTFIELDS, $datastring); |
||
73 | |||
74 | $ret = curl_exec($curl); |
||
75 | $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
76 | if ($code == 200) { |
||
77 | $gitlabout = json_decode($ret, true); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
78 | d_echo('Created GitLab issue ' . $gitlabout['key'] . ' for ' . $device); |
||
79 | |||
80 | return true; |
||
81 | } else { |
||
82 | d_echo('GitLab connection error: ' . serialize($ret)); |
||
83 | |||
84 | return false; |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | |||
89 | public static function configTemplate() |
||
90 | { |
||
91 | return [ |
||
92 | 'config' => [ |
||
93 | [ |
||
94 | 'title' => 'Host', |
||
95 | 'name' => 'gitlab-host', |
||
96 | 'descr' => 'GitLab Host', |
||
97 | 'type' => 'text', |
||
98 | ], |
||
99 | [ |
||
100 | 'title' => 'Project ID', |
||
101 | 'name' => 'gitlab-id', |
||
102 | 'descr' => 'GitLab Project ID', |
||
103 | 'type'=> 'text', |
||
104 | ], |
||
105 | [ |
||
106 | 'title' => 'Personal Access Token', |
||
107 | 'name' => 'gitlab-key', |
||
108 | 'descr' => 'Personal Access Token', |
||
109 | 'type' => 'text', |
||
110 | ], |
||
111 | ], |
||
112 | 'validation' => [ |
||
113 | 'gitlab-host' => 'required|string', |
||
114 | 'gitlab-id' => 'required|string', |
||
115 | 'gitlab-key' => 'required|string', |
||
116 | ], |
||
117 | ]; |
||
118 | } |
||
119 | } |
||
120 |