-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
576 lines (525 loc) · 15.8 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
<?php
##
## Program: PNP Grafana API , alternative version of https://github.com/lingej/pnp-metrics-api/raw/master/application/controller/api.php to work
## with older versions of Nagios and PNP. This file needs to be inside the folder /usr/local/nagios/share/pnp.
## To create a PNP Datasource in Grafana, check https://github.com/sni/grafana-pnp-datasource/
##
## License: GPL
## Copyright (c) 2018-2018 Dudssource ([email protected])
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
##
error_reporting(E_ALL ^E_NOTICE);
require ('include/function.inc.php');
require ('include/tpl_function.inc.php');
require ('include/debug.php');
if(getenv('PNP_CONFIG_FILE') != ""){
$config = getenv('PNP_CONFIG_FILE');
}else{
$config = "/usr/local/nagios/etc/pnp/config";
}
# Version info
$version=array();
if (is_readable("/usr/local/nagios/etc/pnp/pnp4nagios_release")) {
$version_file=file("/usr/local/nagios/etc/pnp/pnp4nagios_release");
foreach($version_file as $line) {
$info=explode("=", $line);
$version[$info[0]]=preg_replace("/\n|\r|\"/", "",$info[1]);
}
}
if (is_readable($config . ".php")) {
include ($config . ".php");
} else {
die("<b>$config.php</b> not found");
}
if (is_readable($config . "_local.php")) {
include ($config . "_local.php");
}
if(!isset($conf['template_dir'])){
$conf['template_dir'] = dirname(__file__);
}
if (is_readable('./lang/lang_' . $conf['lang'] . '.php')) {
include ('./lang/lang_' . $conf['lang'] . '.php');
} else {
include ('./lang/lang_en.php');
}
# timezone configuration
# check https://secure.php.net/manual/pt_BR/timezones.php
$timezone="America/Sao_Paulo";
# Debugger init
$debug = new check;
$debug->doCheck("rrdtool");
$debug->doCheck("p_open");
$debug->doCheck("fpassthru");
$debug->doCheck("xml_parser_create");
$debug->doCheck("zlib");
$debug->doCheck("gd");
$debug->doCheck("rrdbase");
// extracting the PATH
$method = explode("api.php", $_SERVER[PHP_SELF]);
// forwading the request to the right method
if(preg_match( "/\/api\/services/", $method[1] )){
services();
} else if(preg_match( "/\/api\/hosts/", $method[1] )){
hosts($_GET['query']);
} else if(preg_match( "/\/api\/labels/", $method[1] )){
labels();
} else if(preg_match( "/\/api\/metrics/", $method[1] )){
metrics();
} else {
index();
}
/*
* HTTP Methods
*/
/**
* Prints the version, it's used for testing purposes.
*
* GET EXAMPLE: http://localhost/nagios/pnp/api.php/api/index
*/
function index() {
global $version;
if (!empty($version)) {
$data['pnp_version'] = $version['PKG_VERSION'];
$data['pnp_rel_date'] = $version['PKG_REL_DATE'];
} else {
$data['pnp_version'] = PNP_VERSION;
$data['pnp_rel_date'] = PNP_REL_DATE;
}
$data['error'] = "";
return_json($data, 200);
}
/*
* Returns the list of hosts.
*
* GET EXAMPLE: http://localhost/nagios/pnp/api.php/api/hosts?query=/KAFKA01/
*/
function hosts($query = false) {
$data = array();
$hosts = _getHosts($query);
foreach ( $hosts as $host ){
$data['hosts'][] = array(
'name' => $host
);
}
return_json($data, 200);
}
/*
* Returns the list of services per host.
*
* POST EXAMPLE: http://localhost/nagios/pnp/api.php/api/services/
*
* {
* "host" : "GVTSVPX-KAFKA01"
* }
*/
function services() {
$data = array();
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
// Only Post Reuests
$data['error'] = "Only POST Requests allowed";
return_json($data, 901);
return;
}
$pdata = json_decode(file_get_contents('php://input'), TRUE);
$host = arr_get($pdata, "host");
if ( $host === false ){
$data['error'] = "No hostname specified";
return_json($data, 901);
return;
}
$services = array();
$hosts = _getHosts($host);
$services = _getServices($hosts);
$duplicates = array();
foreach($services as $service){
// skip duplicates
if(isset($duplicates[$service])) {
continue;
}
$duplicates[$service] = true;
$data['services'][] = array(
'name' => $service,
'servicedesc' => $service,
'hostname' => $host
);
}
return_json($data, 200);
}
/*
* Returns the RRD labels related to the service.
*
* POST EXAMPLE: http://localhost/nagios/pnp/api.php/api/labels/
*
* {
* "host" : "NAGIOSXI",
* "service" : "AMQ_Principal_-_mdb.central_-_entregues"
* }
*/
function labels() {
$data = array();
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
// Only Post Reuests
$data['error'] = "Only POST Requests allowed";
return_json($data, 901);
return;
}
$pdata = json_decode(file_get_contents('php://input'), TRUE);
$host = arr_get($pdata, "host");
$service = arr_get($pdata, "service");
if ( $host === false ){
$data['error'] = "No hostname specified";
return_json($data, 901);
return;
}
if ( $service === false ){
$data['error'] = "No service specified";
return_json($data, 901);
return;
}
$hosts = _getHosts($host);
$services = _getServices($hosts, $service);
foreach($services as $service){
try {
// read XML file
$xml=parse_xml($host, $service == "pnp-internal" ? ".pnp-internal" : $service, $service);
} catch (Exception $e) {
$data['error'] = "$e";
return_json($data, 901);
return;
}
foreach( $xml['PNP']['DS'] as $KEY) {
$data['labels'][] = array(
'name' => "ds" . $KEY,
'label' => $service . ":" . $xml['PNP']['NAME'][$KEY],
'service' => $service,
'hostname' => $host
);
}
}
return_json($data, 200);
}
/*
* Returns the list of metrics related to the host/service during the specified timerange.
*
* POST EXAMPLE: http://localhost/nagios/pnp/api.php/api/metrics/
*
* {
* "targets": [
* {
* "host": "GVTSVPX-KAFKA01",
* "service": "NONCORE_-_Kafka_Lag_Topicos",
* "perflabel": "NONCORE_-_Kafka_Lag_Topicos:topicSummaryDeviceGroupPipeline",
* "alias": "topicSummaryDeviceGroupPipeline",
* "type": "AVERAGE",
* "fill": "fill",
* "factor": "",
* "refId": "A"
* },
* {
* "host": "GVTSVPX-KAFKA01",
* "service": "NONCORE_-_Kafka_Lag_Topicos",
* "perflabel": "NONCORE_-_Kafka_Lag_Topicos:motor_regras",
* "type": "AVERAGE",
* "fill": "fill",
* "factor": "",
* "refId": "B"
* }
* ],
* "start": "1531623600",
* "end": "1531788056"
* }
*
*/
function metrics(){
global $timerange;
global $conf;
global $timezone;
// extract metrics for a given datasource
// TODO Multiple sources via regex
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
// Only Post Reuests
$data['error'] = "Only POST Requests allowed";
return_json($data, 901);
return;
}
$hosts = array(); // List of all Hosts
$services = array(); // List of services for a given host
$pdata = json_decode(file_get_contents('php://input'), TRUE);
$data = array();
if ( !isset($pdata['targets']) ){
$data['error'] = "No targets specified";
return_json($data, 901);
return;
}
$start = arr_get($pdata, 'start');
$end = arr_get($pdata, 'end');
$timerange = getTimeRange($start,$end,$view);
$interval = arr_get($pdata, 'intervalMs') / 1000;
foreach( $pdata['targets'] as $key => $target){
$host = arr_get($target, 'host');
$service = arr_get($target, 'service');
$perflabel = arr_get($target, 'perflabel');
$type = arr_get($target, 'type');
$refid = arr_get($target, 'refid');
$fill = arr_get($target, 'fill');
if ( $host === false ){
$data['error'] = "No hostname specified";
return_json($data, 901);
return;
}
if ( $service === false ){
$data['error'] = "No service specified";
return_json($data, 901);
return;
}
if ( $perflabel === false ){
$data['error'] = "No perfdata label specified";
return_json($data, 901);
return;
}
if ( $type === false ){
$data['error'] = "No perfdata type specified";
return_json($data, 901);
return;
}
$hosts = _getHosts($host);
$services = _getServices($hosts, $service);
$hk = 0; // Host Key
foreach ( $services as $service) {
$host = $host;
$service = $service;
try {
// read XML file
$xml=parse_xml($host, $service == "pnp-internal" ? ".pnp-internal" : $service, $service);
} catch (Kohana_Exception $e) {
$data['error'] = "$e";
return_json($data, 901);
return;
}
// create a Perflabel List
$perflabels = array();
foreach( $xml['PNP']['DS'] as $value){
$label = $service . ":" . $xml['PNP']['NAME'][$value];
if (isRegex($perflabel)) {
if(!preg_match( $perflabel, $label ) ){
continue;
}
} elseif ( $perflabel != $label ) {
continue;
}
$perflabels[] = array(
"label" => $label,
"name" => "ds" . $value,
"warn" => $xml['PNP']['WARN'][$value],
"crit" => $xml['PNP']['CRIT'][$value]
);
}
foreach ( $perflabels as $tmp_perflabel){
try {
$rrdfile = $conf['rrdbase'] . "$host/$service.rrd";
$rrddef = $conf['rrdbase'] . $host . "/" . $service . ".xml";
$xml = "<pnp>" . _rrdtool_fetch($rrdfile, $type, $interval) . "</pnp>";
} catch (Kohana_Exception $e) {
$data['error'] = "$e";
return_json($data, 901);
return;
}
$xpd = simplexml_load_string($xml);
$i = 0;
$index = -1;
$step = (string) 60;
$data['targets'][$key][$hk]['start'] = $start * 1000;
$data['targets'][$key][$hk]['end'] = $end * 1000;
$data['targets'][$key][$hk]['host'] = $host;
$data['targets'][$key][$hk]['service'] = $service;
$data['targets'][$key][$hk]['perflabel'] = $tmp_perflabel['label'];
$data['targets'][$key][$hk]['type'] = $type;
$data['targets'][$key][$hk]['fill'] = $fill;
$i = 0;
foreach ( $xpd->row as $row){
$datetime = DateTime::createFromFormat("d.m.y H:i", $row->time, new DateTimeZone( $timezone ));
$timestamp = $datetime->getTimestamp() * 1000;
$d = (string) $row->$tmp_perflabel['name'];
if ($d == "NaN"){
$d = null;
}else{
$d = floatval($d);
}
$data['targets'][$key][$hk]['datapoints'][] = array( $d, $timestamp );
$i++;
}
$hk++;
}
}
}
return_json($data, 200);
}
/*
* Internal functions.
*/
function _rrdtool_fetch($rrdfile, $type, $interval) {
global $debug;
global $conf;
global $timerange;
$descriptorspec = array (
0 => array ("pipe","r"), // stdin is a pipe that the child will read from
1 => array ("pipe","w") // stdout is a pipe that the child will write to
);
$process = proc_open($conf['rrdtool']." - ", $descriptorspec, $pipes);
if (is_resource($process)) {
$command = " fetch ".$rrdfile." ".$type." -r ".$interval." -s ".$timerange['start']." -e ".$timerange['end'];
fwrite($pipes[0], $command);
fclose($pipes[0]);
$buffer = fgets($pipes[1]);
if (preg_match('/^ERROR/', $buffer)) {
$deb['data'] = $buffer;
$deb['command'] = format_rrd_debug($command);
$deb['opt'] = $opt;
$debug->doCheck("rrdgraph",$deb);
}
ob_start();
$buffer = "";
while (!feof($pipes[1])) {
$array = split(" ", (fgets($pipes[1], 4096)));
$temp_buffer = "";
$count = 0;
if(preg_match("/^\d+:/",$array[0])){
foreach($array as $key){
if($count == 0){
$temp_buffer .= "<row><time>".date($conf['date_fmt'],$key)."</time>";
}else{
$temp_buffer .= "<ds$count>".floatval($key)."</ds$count>";
}
$count++;
}
$buffer .= $temp_buffer."</row>\n";
}
}
ob_end_clean();
fclose($pipes[1]);
proc_close($process);
}
return $buffer;
}
/*
* return array key
*/
function arr_get($array, $key=false, $default=false){
if ( isset($array) && $key == false ){
return $array;
}
$keys = explode(".", $key);
foreach ($keys as $key_part) {
if ( isset($array[$key_part] ) === false ) {
if (! is_array($array) or ! array_key_exists($key_part, $array)) {
return $default;
}
}
$array = $array[$key_part];
}
return $array;
}
/*
* Converts the content to the JSON format and prints the result in the response.
*/
function return_json( $data, $status=200 ){
$json = json_encode($data);
header('Status: '.$status);
header('Content-type: application/json');
print $json;
}
/*
* Checks if the string looks like a regex.
*/
function isRegex($string){
// if string looks like an regex /regex/
if ( substr($string,0,1) == "/" && substr($string,-1,1) == "/" && strlen($string) >= 2 ){
return true;
}else{
return false;
}
}
/*
* Wrapper to the native api to extract the hosts and format the result.
*/
function _getHosts($query) {
global $conf;
$result = array();
$hosts = getHosts();
$isRegex = false;
if ($query !== NULL && isRegex($query) ) {
$isRegex = true;
}
foreach ( $hosts as $host ){
list($name,$state) = explode(";",$host);
if ( $state != 0 ){
continue;
}
if($isRegex) {
if(preg_match("$query", $name) ) {
$result[] = $name;
}
}
elseif ($query !== NULL) {
if("$query" == $name) {
$result[] = $name;
}
} else {
$result[] = $name;
}
}
if ($query !== NULL && $query == ".pnp-internal") {
$result[] = ".pnp-internal";
}
return($result);
}
/*
* Wrapper to the native api, returns list of service hashes
*/
function _getServices($hosts, $query) {
global $conf;
$result = array();
$isRegex = false;
if ($query !== NULL && isRegex($query) ) {
$isRegex = true;
}
foreach ( $hosts as $host){
$services = getServices($conf["rrdbase"], $host);
foreach ($services as $value) {
$value = explode(";", $value);
$servicename=$value[0];
$servicedown=$value[1];
// if the service RRD file wasn't modified in the last conf[max_age] seconds, we ignore the service
if ($servicedown == 1) {
continue;
}
if ($isRegex) {
if ( preg_match("$query", $servicename)) {
$result[] = $servicename;
}
}
elseif ($query !== NULL) {
if("$query" == $servicename || "$query" == $servicename) {
$result[] = $servicename;
}
} else {
$result[] = $servicename;
}
}
}
return($result);
}
?>