1 | <?php |
||
2 | /* Copyright (C) 2015 Aldemir Akpinar <[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 | * Jira API Transport |
||
18 | * |
||
19 | * @author Aldemir Akpinar <[email protected]> |
||
20 | * @copyright 2017 Aldemir Akpinar, LibreNMS |
||
21 | * @license GPL |
||
22 | */ |
||
23 | |||
24 | namespace LibreNMS\Alert\Transport; |
||
25 | |||
26 | use LibreNMS\Alert\Transport; |
||
27 | |||
28 | class Jira extends Transport |
||
29 | { |
||
30 | public function deliverAlert($obj, $opts) |
||
31 | { |
||
32 | if (! empty($this->config)) { |
||
33 | $opts['username'] = $this->config['jira-username']; |
||
34 | $opts['password'] = $this->config['jira-password']; |
||
35 | $opts['prjkey'] = $this->config['jira-key']; |
||
36 | $opts['issuetype'] = $this->config['jira-type']; |
||
37 | $opts['url'] = $this->config['jira-url']; |
||
38 | } |
||
39 | |||
40 | return $this->contactJira($obj, $opts); |
||
41 | } |
||
42 | |||
43 | public function contactJira($obj, $opts) |
||
44 | { |
||
45 | // Don't create tickets for resolutions |
||
46 | if ($obj['severity'] == 'recovery' && $obj['msg'] != 'This is a test alert') { |
||
47 | return true; |
||
48 | } |
||
49 | |||
50 | $device = device_by_id_cache($obj['device_id']); // for event logging |
||
51 | |||
52 | $username = $opts['username']; |
||
53 | $password = $opts['password']; |
||
54 | $prjkey = $opts['prjkey']; |
||
55 | $issuetype = $opts['issuetype']; |
||
56 | $details = 'Librenms alert for: ' . $obj['hostname']; |
||
57 | $description = $obj['msg']; |
||
58 | $url = $opts['url'] . '/rest/api/latest/issue'; |
||
59 | $curl = curl_init(); |
||
60 | |||
61 | $data = ['project' => ['key' => $prjkey], |
||
62 | 'summary' => $details, |
||
63 | 'description' => $description, |
||
64 | 'issuetype' => ['name' => $issuetype], ]; |
||
65 | $postdata = ['fields' => $data]; |
||
66 | $datastring = json_encode($postdata); |
||
67 | |||
68 | set_curl_proxy($curl); |
||
69 | |||
70 | $headers = ['Accept: application/json', 'Content-Type: application/json']; |
||
71 | |||
72 | curl_setopt($curl, CURLOPT_URL, $url); |
||
73 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); |
||
74 | curl_setopt($curl, CURLOPT_USERPWD, "$username:$password"); |
||
75 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); |
||
76 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
77 | curl_setopt($curl, CURLOPT_VERBOSE, 1); |
||
78 | curl_setopt($curl, CURLOPT_POSTFIELDS, $datastring); |
||
79 | |||
80 | $ret = curl_exec($curl); |
||
81 | $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
82 | if ($code == 200) { |
||
83 | $jiraout = json_decode($ret, true); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
84 | d_echo('Created jira issue ' . $jiraout['key'] . ' for ' . $device); |
||
85 | |||
86 | return true; |
||
87 | } else { |
||
88 | d_echo('Jira connection error: ' . serialize($ret)); |
||
89 | |||
90 | return false; |
||
91 | } |
||
92 | } |
||
93 | |||
94 | public static function configTemplate() |
||
95 | { |
||
96 | return [ |
||
97 | 'config' => [ |
||
98 | [ |
||
99 | 'title' => 'URL', |
||
100 | 'name' => 'jira-url', |
||
101 | 'descr' => 'Jira URL', |
||
102 | 'type' => 'text', |
||
103 | ], |
||
104 | [ |
||
105 | 'title' => 'Project Key', |
||
106 | 'name' => 'jira-key', |
||
107 | 'descr' => 'Jira Project Key', |
||
108 | 'type' => 'text', |
||
109 | ], |
||
110 | [ |
||
111 | 'title' => 'Issue Type', |
||
112 | 'name' => 'jira-type', |
||
113 | 'descr' => 'Jira Issue Type', |
||
114 | 'type' => 'text', |
||
115 | ], |
||
116 | [ |
||
117 | 'title' => 'Jira Username', |
||
118 | 'name' => 'jira-username', |
||
119 | 'descr' => 'Jira Username', |
||
120 | 'type' => 'text', |
||
121 | ], |
||
122 | [ |
||
123 | 'title' => 'Jira Password', |
||
124 | 'name' => 'jira-password', |
||
125 | 'descr' => 'Jira Password', |
||
126 | 'type' => 'text', |
||
127 | ], |
||
128 | ], |
||
129 | 'validation' => [ |
||
130 | 'jira-key' => 'required|string', |
||
131 | 'jira-url' => 'required|string', |
||
132 | 'jira-type' => 'required|string', |
||
133 | 'jira-username' => 'required|string', |
||
134 | 'jira-password' => 'required|string', |
||
135 | ], |
||
136 | ]; |
||
137 | } |
||
138 | } |
||
139 |