|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* BaseGraph.php |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU General Public License as published by |
|
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
* |
|
18
|
|
|
* @package LibreNMS |
|
19
|
|
|
* @link http://librenms.org |
|
20
|
|
|
* @copyright 2016 Neil Lathwood |
|
21
|
|
|
* @author Neil Lathwood <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace App\Graphs; |
|
25
|
|
|
|
|
26
|
|
|
use Carbon\Carbon; |
|
27
|
|
|
use Illuminate\Http\Request; |
|
28
|
|
|
use Symfony\Component\Process\Process; |
|
29
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
|
30
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
|
31
|
|
|
use App\Models\Device; |
|
32
|
|
|
use Settings; |
|
33
|
|
|
|
|
34
|
|
|
class BaseGraph |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
private $type; |
|
38
|
|
|
private $input; |
|
39
|
|
|
private $device; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param $value |
|
43
|
|
|
* @return $this |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setType($value) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->type = $value; |
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param $input |
|
53
|
|
|
* @return $this |
|
54
|
|
|
*/ |
|
55
|
|
|
public function setInput($input) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->input = $input; |
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param $device |
|
63
|
|
|
* @return $this |
|
64
|
|
|
*/ |
|
65
|
|
|
public function setDevice($input) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->device = Device::find($input->device_id); |
|
68
|
|
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param $device |
|
73
|
|
|
* @return $this |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getDevice() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->device; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get json output. |
|
82
|
|
|
* |
|
83
|
|
|
* @return array |
|
|
|
|
|
|
84
|
|
|
*/ |
|
85
|
|
|
public function json(Request $request) |
|
86
|
|
|
{ |
|
87
|
|
|
$input = $this->input; |
|
88
|
|
|
if ($request->{'source'} === 'rrd') |
|
89
|
|
|
{ |
|
90
|
|
|
$build = $this->buildRRDJson($input, $this->buildRRDXport($input)); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
$response = $this->runRRDXport($build['cmd']); |
|
|
|
|
|
|
93
|
|
|
return $this->parseRRDJson($response, $build['headers']); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get csv output. |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
|
|
|
|
|
100
|
|
|
*/ |
|
101
|
|
|
public function csv(Request $request) |
|
102
|
|
|
{ |
|
103
|
|
|
$input = $this->input; |
|
104
|
|
|
if ($request->{'source'} === 'rrd') { |
|
105
|
|
|
$build = $this->buildRRDJson($input, $this->buildRRDXport($input)); |
|
|
|
|
|
|
106
|
|
|
$response = $this->runRRDXport($build['cmd']); |
|
107
|
|
|
return $this->parseRRDCsv($response, $build['headers']); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get png output. |
|
113
|
|
|
* |
|
114
|
|
|
* @return array |
|
|
|
|
|
|
115
|
|
|
*/ |
|
116
|
|
|
public function png(Request $request) |
|
117
|
|
|
{ |
|
118
|
|
|
$input = $this->input; |
|
119
|
|
|
if ($request->{'source'} === 'rrd') { |
|
120
|
|
|
$build = $this->buildRRDGraph($input, $this->buildRRDGraphParams($input)); |
|
|
|
|
|
|
121
|
|
|
$response = $this->runRRDGraph($build['cmd']); |
|
122
|
|
|
return base64_encode($response); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Build the RRD Xport query |
|
128
|
|
|
* |
|
129
|
|
|
* @return string |
|
|
|
|
|
|
130
|
|
|
*/ |
|
131
|
|
|
public function buildRRDJson($input, $setup) { |
|
132
|
|
|
$rrd_defs = $setup['defs']; |
|
133
|
|
|
$headers = $setup['headers']; |
|
134
|
|
|
$cmd = build_rrdtool(' xport --json -s ' . $input->{'start'} . ' -e ' . $input->{'end'} . ' ' . $rrd_defs); |
|
135
|
|
|
|
|
136
|
|
|
return [ |
|
137
|
|
|
'headers' => $headers, |
|
138
|
|
|
'cmd' => $cmd, |
|
139
|
|
|
]; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Build the RRD Graph command |
|
144
|
|
|
* |
|
145
|
|
|
* @return string |
|
|
|
|
|
|
146
|
|
|
*/ |
|
147
|
|
|
public function buildRRDGraph($input, $setup) { |
|
148
|
|
|
$rrd_defs = $setup['defs']; |
|
149
|
|
|
$headers = []; |
|
150
|
|
|
$cmd = build_rrdtool(' graph - -s ' . $input->{'start'} . ' -e ' . $input->{'end'} . ' ' . |
|
151
|
|
|
" --width " . $input->{'width'} . " --height " . $input->{'height'} . " --alt-autoscale-max --rigid -E -c BACK#EEEEEE00 -c SHADEA#EEEEEE00 -c SHADEB#EEEEEE00 -c \ |
|
152
|
|
|
FONT#000000 -c CANVAS#FFFFFF00 -c GRID#a5a5a5 -c MGRID#FF9999 -c FRAME#5e5e5e -c ARROW#5e5e5e \ |
|
153
|
|
|
-R normal --font LEGEND:8:DejaVuSansMono --font AXIS:7:DejaVuSansMono --font-render-mode normal " . |
|
154
|
|
|
$rrd_defs); |
|
155
|
|
|
return [ |
|
156
|
|
|
'headers' => $headers, |
|
157
|
|
|
'cmd' => $cmd, |
|
158
|
|
|
]; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param $cmd |
|
163
|
|
|
* @return mixed|string |
|
164
|
|
|
*/ |
|
165
|
|
|
public function runRRDGraph($cmd) |
|
166
|
|
|
{ |
|
167
|
|
|
$process = new Process($cmd); |
|
168
|
|
|
$process->run(); |
|
169
|
|
|
if (!$process->isSuccessful()) { |
|
170
|
|
|
throw new ProcessFailedException($process); |
|
171
|
|
|
} |
|
172
|
|
|
$output = $process->getOutput(); |
|
173
|
|
|
return $output; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Run the RRD Xport query |
|
178
|
|
|
* |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
|
|
public function runRRDXport($query) |
|
182
|
|
|
{ |
|
183
|
|
|
$process = new Process($query); |
|
184
|
|
|
$process->run(); |
|
185
|
|
|
if (!$process->isSuccessful()) { |
|
186
|
|
|
throw new ProcessFailedException($process); |
|
187
|
|
|
} |
|
188
|
|
|
$output = $process->getOutput(); |
|
189
|
|
|
$output = preg_replace('/\'/', '"', $output); |
|
190
|
|
|
$output = preg_replace('/about\:/', '"meta":', $output); |
|
191
|
|
|
$output = preg_replace('/meta\:/', '"meta":', $output); |
|
192
|
|
|
$output = json_decode($output); |
|
193
|
|
|
return $output; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Run the RRD Xport query |
|
198
|
|
|
* |
|
199
|
|
|
* @return string |
|
200
|
|
|
*/ |
|
201
|
|
|
public function parseRRDJson($response) |
|
202
|
|
|
{ |
|
203
|
|
|
$step = $response->{'meta'}->{'step'}; |
|
204
|
|
|
$start = $response->{'meta'}->{'start'}; |
|
205
|
|
|
$end = $response->{'meta'}->{'end'}; |
|
|
|
|
|
|
206
|
|
|
$cur_time = $start; |
|
207
|
|
|
$z = 0; |
|
208
|
|
|
$tmp_data = []; |
|
209
|
|
|
|
|
210
|
|
|
foreach ($response->{'data'} as $data) |
|
211
|
|
|
{ |
|
212
|
|
|
$tmp_data['data'][$z][] = $cur_time + $step; |
|
213
|
|
|
foreach ($data as $key => $value) |
|
214
|
|
|
{ |
|
215
|
|
|
$tmp_data['data'][$z][] = (is_null($value)) ? 0 : (int) $value; |
|
216
|
|
|
} |
|
217
|
|
|
//$tmp_data[] = $data; |
|
|
|
|
|
|
218
|
|
|
$z++; |
|
219
|
|
|
} |
|
220
|
|
|
$tmp_data['labels'] = ['x', 'A', 'B', 'C', 'D']; |
|
221
|
|
|
return json_encode($tmp_data); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Parse RRD output to csv |
|
226
|
|
|
* |
|
227
|
|
|
* @return string |
|
228
|
|
|
*/ |
|
229
|
|
|
public function parseRRDCsv($response, $headers) |
|
230
|
|
|
{ |
|
231
|
|
|
$step = $response->{'meta'}->{'step'}; |
|
232
|
|
|
$start = $response->{'meta'}->{'start'}; |
|
233
|
|
|
$end = $response->{'meta'}->{'end'}; |
|
|
|
|
|
|
234
|
|
|
$cur_time = $start; |
|
235
|
|
|
$output = 'Date, ' . implode(',', $headers) . PHP_EOL; |
|
236
|
|
|
|
|
237
|
|
|
foreach ($response->{'data'} as $data) |
|
238
|
|
|
{ |
|
239
|
|
|
$output .= Carbon::createFromTimestamp($cur_time) . ','; |
|
240
|
|
|
$tmp_data = []; |
|
241
|
|
|
foreach ($data as $key => $value) |
|
242
|
|
|
{ |
|
243
|
|
|
$tmp_data[] = (is_null($value)) ? 0 : (int) $value; |
|
244
|
|
|
} |
|
245
|
|
|
$output .= implode(',', $tmp_data) . PHP_EOL; |
|
246
|
|
|
$cur_time = $cur_time + $step; |
|
247
|
|
|
} |
|
248
|
|
|
return $output; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.