1 | <?php |
||
2 | /* Copyright (C) 2014 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 | * API Transport |
||
18 | * |
||
19 | * @author f0o <[email protected]> |
||
20 | * @author PipoCanaja (github.com/PipoCanaja) |
||
21 | * @copyright 2014 f0o, LibreNMS |
||
22 | * @license GPL |
||
23 | */ |
||
24 | |||
25 | namespace LibreNMS\Alert\Transport; |
||
26 | |||
27 | use LibreNMS\Alert\Transport; |
||
28 | |||
29 | class Api extends Transport |
||
30 | { |
||
31 | public function deliverAlert($obj, $opts) |
||
32 | { |
||
33 | $url = $this->config['api-url']; |
||
34 | $options = $this->config['api-options']; |
||
35 | $headers = $this->config['api-headers']; |
||
36 | $body = $this->config['api-body']; |
||
37 | $method = $this->config['api-method']; |
||
38 | $auth = [$this->config['api-auth-username'], $this->config['api-auth-password']]; |
||
39 | |||
40 | return $this->contactAPI($obj, $url, $options, $method, $auth, $headers, $body); |
||
41 | } |
||
42 | |||
43 | private function contactAPI($obj, $api, $options, $method, $auth, $headers, $body) |
||
44 | { |
||
45 | $request_opts = []; |
||
46 | $request_heads = []; |
||
47 | $query = []; |
||
48 | |||
49 | $method = strtolower($method); |
||
50 | $host = explode('?', $api, 2)[0]; //we don't use the parameter part, cause we build it out of options. |
||
51 | |||
52 | //get each line of key-values and process the variables for Headers; |
||
53 | foreach (preg_split('/\\r\\n|\\r|\\n/', $headers, -1, PREG_SPLIT_NO_EMPTY) as $current_line) { |
||
54 | [$u_key, $u_val] = explode('=', $current_line, 2); |
||
55 | foreach ($obj as $p_key => $p_val) { |
||
56 | $u_val = str_replace('{{ $' . $p_key . ' }}', $p_val, $u_val); |
||
57 | } |
||
58 | //store the parameter in the array for HTTP headers |
||
59 | $request_heads[$u_key] = $u_val; |
||
60 | } |
||
61 | //get each line of key-values and process the variables for Options; |
||
62 | foreach (preg_split('/\\r\\n|\\r|\\n/', $options, -1, PREG_SPLIT_NO_EMPTY) as $current_line) { |
||
63 | [$u_key, $u_val] = explode('=', $current_line, 2); |
||
64 | // Replace the values |
||
65 | foreach ($obj as $p_key => $p_val) { |
||
66 | $u_val = str_replace('{{ $' . $p_key . ' }}', $p_val, $u_val); |
||
67 | } |
||
68 | //store the parameter in the array for HTTP query |
||
69 | $query[$u_key] = $u_val; |
||
70 | } |
||
71 | |||
72 | $client = new \GuzzleHttp\Client(); |
||
73 | $request_opts['proxy'] = get_guzzle_proxy(); |
||
74 | if (isset($auth) && ! empty($auth[0])) { |
||
75 | $request_opts['auth'] = $auth; |
||
76 | } |
||
77 | if (count($request_heads) > 0) { |
||
78 | $request_opts['headers'] = $request_heads; |
||
79 | } |
||
80 | if ($method == 'get') { |
||
81 | $request_opts['query'] = $query; |
||
82 | $res = $client->request('GET', $host, $request_opts); |
||
83 | } elseif ($method == 'put') { |
||
84 | $request_opts['query'] = $query; |
||
85 | $request_opts['body'] = $body; |
||
86 | $res = $client->request('PUT', $host, $request_opts); |
||
87 | } else { //Method POST |
||
88 | $request_opts['form_params'] = $query; |
||
89 | foreach ($query as $metric => $value) { // replace all variables defined in Options and found in Body for their values |
||
90 | $body = str_replace('{{ $' . $metric . ' }}', $value, $body); |
||
91 | } |
||
92 | $request_opts['body'] = $body; |
||
93 | $res = $client->request('POST', $host, $request_opts); |
||
94 | } |
||
95 | |||
96 | $code = $res->getStatusCode(); |
||
97 | if ($code != 200) { |
||
98 | var_dump("API '$host' returned Error"); |
||
0 ignored issues
–
show
Security
Debugging Code
introduced
by
Loading history...
|
|||
99 | var_dump('Params:'); |
||
100 | var_dump($query); |
||
101 | var_dump('Response headers:'); |
||
102 | var_dump($res->getHeaders()); |
||
103 | var_dump('Return: ' . $res->getReasonPhrase()); |
||
104 | |||
105 | return 'HTTP Status code ' . $code; |
||
106 | } |
||
107 | |||
108 | return true; |
||
109 | } |
||
110 | |||
111 | public static function configTemplate() |
||
112 | { |
||
113 | return [ |
||
114 | 'config' => [ |
||
115 | [ |
||
116 | 'title' => 'API Method', |
||
117 | 'name' => 'api-method', |
||
118 | 'descr' => 'API Method: GET, POST or PUT', |
||
119 | 'type' => 'select', |
||
120 | 'options' => [ |
||
121 | 'GET' => 'GET', |
||
122 | 'POST' => 'POST', |
||
123 | 'PUT' => 'PUT', |
||
124 | ], |
||
125 | ], |
||
126 | [ |
||
127 | 'title' => 'API URL', |
||
128 | 'name' => 'api-url', |
||
129 | 'descr' => 'API URL', |
||
130 | 'type' => 'text', |
||
131 | ], |
||
132 | [ |
||
133 | 'title' => 'Options', |
||
134 | 'name' => 'api-options', |
||
135 | 'descr' => 'Enter the options (format: option=value separated by new lines)', |
||
136 | 'type' => 'textarea', |
||
137 | ], |
||
138 | [ |
||
139 | 'title' => 'headers', |
||
140 | 'name' => 'api-headers', |
||
141 | 'descr' => 'Enter the headers (format: option=value separated by new lines)', |
||
142 | 'type' => 'textarea', |
||
143 | ], |
||
144 | [ |
||
145 | 'title' => 'body', |
||
146 | 'name' => 'api-body', |
||
147 | 'descr' => 'Enter the body (only used by PUT method, discarded otherwise)', |
||
148 | 'type' => 'textarea', |
||
149 | ], |
||
150 | [ |
||
151 | 'title' => 'Auth Username', |
||
152 | 'name' => 'api-auth-username', |
||
153 | 'descr' => 'Auth Username', |
||
154 | 'type' => 'text', |
||
155 | ], |
||
156 | [ |
||
157 | 'title' => 'Auth Password', |
||
158 | 'name' => 'api-auth-password', |
||
159 | 'descr' => 'Auth Password', |
||
160 | 'type' => 'password', |
||
161 | ], |
||
162 | ], |
||
163 | 'validation' => [ |
||
164 | 'api-method' => 'in:GET,POST,PUT', |
||
165 | 'api-url' => 'required|url', |
||
166 | ], |
||
167 | ]; |
||
168 | } |
||
169 | } |
||
170 |