SystemhealthController.php 26.3 KB
Newer Older
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
<?php
/**
 *    Copyright (C) 2015 Deciso B.V. - J. Schellevis
 *
 *    All rights reserved.
 *
 *    Redistribution and use in source and binary forms, with or without
 *    modification, are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 *    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 *    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 *    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 *    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *    POSSIBILITY OF SUCH DAMAGE.
 *
 */


31
namespace OPNsense\Diagnostics\Api;
32 33 34

use \OPNsense\Base\ApiControllerBase;
use \OPNsense\Core\Backend;
35
use \OPNsense\Core\Config;
36

37 38 39 40
/**
 * Class ServiceController
 * @package OPNsense\SystemHealth
 */
41
class SystemhealthController extends ApiControllerBase
42 43 44 45
{

    /**
     * Return full archive information
46 47
     * @param \SimpleXMLElement $xml rrd data xml
     * @return array info set, metadata
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
     */
    private function getDataSetInfo($xml)
    {
        $info = array();
        if (isset($xml)) {
            $step = intval($xml->step);
            $lastUpdate = intval($xml->lastupdate);
            foreach ($xml->rra as $key => $value) {
                $step_size = (int)$value->pdp_per_row * $step;
                $first = floor(($lastUpdate / $step_size)) * $step_size -
                    ($step_size * (count($value->database->children()) - 1));
                $last = floor(($lastUpdate / $step_size)) * $step_size;
                $firstValue_rowNumber = (int)$this->findFirstValue($value);
                $firstValue_timestamp = (int)$first + ((int)$firstValue_rowNumber * $step_size);
                array_push($info, [
                    "step" => $step,
                    "pdp_per_row" => (int)$value->pdp_per_row,
                    "rowCount" => $this->countRows($value),
                    "first_timestamp" => (int)$first,
                    "last_timestamp" => (int)$last,
                    "firstValue_rowNumber" => $firstValue_rowNumber,
                    "firstValue_timestamp" => $firstValue_timestamp,
                    "available_rows" => ($this->countRows($value) - $firstValue_rowNumber),
                    "full_step" => ($step * (int)$value->pdp_per_row),
                    "recorded_time" => ($step * (int)$value->pdp_per_row) *
                        ($this->countRows($value) - $firstValue_rowNumber)
                ]);
            }
        }
        return ($info);
    }

    /**
     * Returns row number of first row with values other than 'NaN'
82 83
     * @param \SimpleXMLElement $data rrd data xml
     * @return int rownumber
84
     */
85
    private function findFirstValue($data)
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    {

        $rowNumber = 0;
        $containsValues = false; // used to break foreach on first row with collected data

        foreach ($data->database->row as $item => $row) {
            foreach ($row as $rowKey => $rowVal) {
                if (trim($rowVal) != "NaN") {
                    $containsValues = true;
                }
            }
            if ($containsValues == true) {
                break;
            }
            $rowNumber++;
        }

        return $rowNumber;
    }

    /**
     * Return total number of rows in rra
108 109
     * @param \SimpleXMLElement $data rrd data xml
     * @return int total number of rows
110
     */
111
    private function countRows($data)
112 113 114 115 116 117 118 119 120 121 122
    {
        $rowCount = 0;
        foreach ($data->database->row as $item => $row) {
            $rowCount++;
        }

        return $rowCount;
    }

    /**
     * internal: retrieve selections within range (0-0=full range) and limit number of datapoints (max_values)
123 124 125 126
     * @param array $rra_info dataset information
     * @param int $from_timestamp from
     * @param int $to_timestamp to
     * @param int $max_values approx. max number of values
127 128
     * @return array
     */
129
    private function getSelection($rra_info, $from_timestamp, $to_timestamp, $max_values)
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
    {
        $full_range = false;
        if ($from_timestamp == 0 && $to_timestamp == 0) {
            $full_range = true;
            $from_timestamp = $this->getMaxRange($rra_info)["oldest_timestamp"];
            $to_timestamp = $this->getMaxRange($rra_info)["newest_timestamp"];
        }

        $archives = array();
        // find archive match
        foreach ($rra_info as $key => $value) {
            if ($from_timestamp >= $value['firstValue_timestamp'] && $to_timestamp <= ($value['last_timestamp'] +
                    $value['full_step'])) {
                // calculate number of rows in set
                $rowCount = ($to_timestamp - $from_timestamp) / $value['full_step'] + 1;

                // factor to be used to compress the data.
                // example if 2 then 2 values will be used to calculate one data point.
                $condense_factor = round($rowCount / $max_values);

                if ($condense_factor == 0) { // if rounded to 0 we will not condense the data
                    $condense_factor = 1; // and thus return the full set of data points
                }
                // actual number of rows after compressing/condensing the dataSet
                $condensed_rowCount = (int)($rowCount / $condense_factor);

                // count the number if rra's (sets), deduct 1 as we need the counter to start at 0
                $last_rra_key = count($rra_info) - 1;

                // dynamic (condensed) values for full overview to detail level
                $overview = round($rra_info[$last_rra_key]["available_rows"] / (int)$max_values);

                if ($full_range == false) { // JSC WIP removed: && count($rra_info)==1  // add detail when selected
                    array_push($archives, [
                        "key" => $key,
                        "condensed_rowCount" => $condensed_rowCount,
                        "condense_by" => (int)$condense_factor,
                        "type" => "detail"
                    ]);
                } else { // add condensed detail
                    array_push($archives, [
                        "key" => $key,
                        "condensed_rowCount" => (int)($condensed_rowCount / ($rra_info[$last_rra_key]["pdp_per_row"] /
                                $value["pdp_per_row"])),
                        "condense_by" => (int)$condense_factor * ($rra_info[$last_rra_key]["pdp_per_row"] /
                                $value["pdp_per_row"]),
                        "type" => "detail"
                    ]);
                }
                // search for last dataSet with actual values, used to exclude sets that do not contain data
                for ($count = $last_rra_key; $count > 0; $count--) {
                    if ($rra_info[$count]["available_rows"] > 0) {
                        // Found last rra set with values
                        $last_rra_key = $count;
                        break;
                    }
                }
187
                if ($overview != 0) {
188 189 190 191 192
                    $condensed_rowCount = (int)($rra_info[$last_rra_key]["available_rows"] / $overview);
                } else {
                    $condensed_rowCount = 0;
                }

193 194
                array_push($archives, [
                    "key" => $last_rra_key,
195
                    "condensed_rowCount" => $condensed_rowCount,
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
                    "condense_by" => (int)$overview,
                    "type" => "overview"
                ]);
                break;
            }
        }

        return (["from" => $from_timestamp, "to" => $to_timestamp, "full_range" => $full_range, "data" => $archives]);
    }

    /**
     * internal: get full available range
     * @param array $rra_info
     * @return array
     */
211
    private function getMaxRange($rra_info)
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
    {
        // count the number if rra's (sets), deduct 1 as we need the counter to start at 0
        $last_rra_key = count($rra_info) - 1;
        for ($count = $last_rra_key; $count > 0; $count--) {
            if ($rra_info[$count]["available_rows"] > 0) {
                // Found last rra set with values
                $last_rra_key = $count;
                break;
            }
        }
        if (isset($rra_info[0])) {
            $last = $rra_info[0]["firstValue_timestamp"];
            $first = $rra_info[$last_rra_key]["firstValue_timestamp"] + $rra_info[$last_rra_key]["recorded_time"] -
                $rra_info[$last_rra_key]["full_step"];
        } else {
            $first = 0;
            $last = 0;
        }

        return ["newest_timestamp" => $first, "oldest_timestamp" => $last];
    }

    /**
     * translate rrd data to usable format for d3 charts
     * @param array $data
237 238
     * @param boolean $applyInverse inverse selection (multiply -1)
     * @param array $field_units mapping for descriptive field names
239 240
     * @return array
     */
241
    private function translateD3($data, $applyInverse, $field_units)
242 243 244 245 246 247 248 249 250
    {
        $d3_data = array();
        $from_timestamp = 0;
        $to_timestamp = 0;

        foreach ($data['archive'] as $row => $rowValues) {
            $timestamp = $rowValues['timestamp'] * 1000; // javascript works with milliseconds
            foreach ($data['columns'] as $key => $value) {
                $name = $value['name'];
251
                $value = $rowValues['condensed_values'][$key];
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
                if (!isset($d3_data[$key])) {
                    $d3_data[$key] = [];
                    $d3_data[$key]["area"] = true;
                    if (isset($field_units[$name])) {
                        $d3_data[$key]["key"] = $name . " " . $field_units[$name];
                    } else {
                        $d3_data[$key]["key"] = $name;
                    }
                    $d3_data[$key]["values"] = [];
                }

                if ($value == "NaN") {
                    // If first or the last NaN value in series then add a value of 0 for presentation purposes
                    $nan = false;
                    if (isset($data['archive'][$row - 1]['condensed_values'][$key]) &&
                        (string)$data['archive'][$row - 1]['condensed_values'][$key] != "NaN") {
                        // Translate NaN to 0 as d3chart can't render NaN - (first NaN item before value)
                        $value = 0;
                    } elseif (isset($data['archive'][$row + 1]['condensed_values'][$key]) &&
                        (string)$data['archive'][$row + 1]['condensed_values'][$key] != "NaN") {
                        $value = 0; // Translate NaN to 0 as d3chart can't render NaN - (last NaN item before value)
                    } else {
                        $nan = true; // suppress NaN item as we already drawn a line to 0
                    }
                } else {
                    $nan = false; // Not a NaN value, so add to list
                }
                if ($applyInverse == true) {
                    $check_value = $key / 2; // every odd row gets data inversed (* -1)
                    if ($check_value != (int)$check_value) {
                        $value = $value * -1;
                    }
                }
                if ($nan == false) {
                    if ($from_timestamp == 0 || $timestamp < $from_timestamp) {
                        $from_timestamp = $timestamp; // Actual from_timestamp after condensing and cleaning data
                    }
                    if ($to_timestamp == 0 || $timestamp > $to_timestamp) {
                        $to_timestamp = $timestamp; // Actual to_timestamp after condensing and cleaning data
                    }
                    array_push($d3_data[$key]["values"], [$timestamp, $value]);
                }
            }
        }


        // Sort value sets based on timestamp
        foreach ($d3_data as $key => $value) {
            usort($value["values"], array($this, "orderByTimestampASC"));
            $d3_data[$key]["values"] = $value["values"];
        }

        return [
            "stepSize" => $data['condensed_step'],
            "from_timestamp" => $from_timestamp,
            "to_timestamp" => $to_timestamp,
308
            "count" => isset($d3_data[0]) ? count($d3_data[0]['values']) : 0,
309 310 311 312
            "data" => $d3_data
        ];
    }

313 314 315 316 317 318 319
    /**
     * retrieve rrd data
     * @param array $xml
     * @param array $selection
     * @return array
     */
    private function getCondensedArchive($xml, $selection)
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
    {
        $key_counter = 0;
        $info = $this->getDataSetInfo($xml);
        $count_values = 0;
        $condensed_row_values = array();
        $condensed_archive = array();
        $condensed_step = 0;
        $skip_nan = false;
        $selected_archives = $selection["data"];

        foreach ($xml->rra as $key => $value) {
            $calculation_type = trim($value->cf);
            foreach ($value->database as $db_key => $db_value) {
                foreach ($selected_archives as $archKey => $archValue) {
                    if ($archValue['key'] == $key_counter) {
                        $rowCount = 0;
                        $condense_counter = 0;
                        $condense = $archValue['condense_by'];

                        foreach ($db_value as $rowKey => $rowValues) {
                            if ($rowCount >= $info[$key_counter]['firstValue_rowNumber']) {
                                $timestamp = $info[$key_counter]['first_timestamp'] +
                                    ($rowCount * $info[$key_counter]['step'] * $info[$key_counter]['pdp_per_row']);
                                if (($timestamp >= $selection["from"] && $timestamp <= $selection["to"] &&
                                        $archValue["type"] == "detail") || ($archValue["type"] == "overview" &&
                                        $timestamp <= $selection["from"]) || ($archValue["type"] == "overview" &&
                                        $timestamp >= $selection["to"])) {
                                    $condense_counter++;
                                    // Find smallest step in focus area = detail
                                    if ($archValue['type'] == "detail" && $selection["full_range"] == false) {
                                        // Set new calculated step size
                                        $condensed_step = ($info[$key_counter]['full_step'] * $condense);
                                    } else {
                                        if ($selection["full_range"] == true && $archValue['type'] == "overview") {
                                            $condensed_step = ($info[$key_counter]['full_step'] * $condense);
                                        }
                                    }
                                    $column_counter = 0;
                                    if (!isset($condensed_row_values[$count_values])) {
                                        $condensed_row_values[$count_values] = [];
                                    }

                                    foreach ($rowValues->v as $columnKey => $columnValue) {
                                        if (!isset($condensed_row_values[$count_values][$column_counter])) {
                                            $condensed_row_values[$count_values][$column_counter] = 0;
                                        }
                                        if (trim($columnValue) == "NaN") {
                                            // skip processing the rest of the values as this set has a NaN value
                                            $skip_nan = true;

                                            $condensed_row_values[$count_values][$column_counter] = "NaN";
                                        } elseif ($skip_nan == false) {
                                            if ($archValue["type"] == "overview") {
                                                // overwrite this values and skip averaging, looks better for overview
                                                $condensed_row_values[$count_values][$column_counter] =
                                                    ((float)$columnValue);
                                            } elseif ($calculation_type == "AVERAGE") {
                                                // For AVERAGE always add the values
                                                $condensed_row_values[$count_values][$column_counter] +=
                                                    (float)$columnValue;
                                            } elseif ($calculation_type == "MINIMUM" || $condense_counter == 1) {
                                                // For MINIMUM update value if smaller one found or first
                                                if ($condensed_row_values[$count_values][$column_counter] >
                                                    (float)$columnValue) {
                                                    $condensed_row_values[$count_values][$column_counter] =
                                                        (float)$columnValue;
                                                }
                                            } elseif ($calculation_type == "MAXIMUM" || $condense_counter == 1) {
                                                // For MAXIMUM update value if higher one found or first
                                                if ($condensed_row_values[$count_values][$column_counter] <
                                                    (float)$columnValue) {
                                                    $condensed_row_values[$count_values][$column_counter] =
                                                        (float)$columnValue;
                                                }
                                            }
                                        }

                                        $column_counter++;
                                    }

                                    if ($condense_counter == $condense) {
                                        foreach ($condensed_row_values[$count_values] as $crvKey => $crValue) {
                                            if ($condensed_row_values[$count_values][$crvKey] != "NaN" &&
                                                $calculation_type == "AVERAGE" && $archValue["type"] != "overview") {
                                                // For AVERAGE we need to calculate it,
                                                // dividing by the total number of values collected
                                                $condensed_row_values[$count_values][$crvKey] =
                                                    (float)$condensed_row_values[$count_values][$crvKey] / $condense;
                                            }
                                        }
                                        $skip_nan = false;
                                        if ($info[$key_counter]['available_rows'] > 0) {
                                            array_push($condensed_archive, [
                                                "timestamp" => $timestamp - ($info[$key_counter]['step'] *
                                                        $info[$key_counter]['pdp_per_row']),
                                                "condensed_values" => $condensed_row_values[$count_values]
                                            ]);
                                        }
                                        $count_values++;
                                        $condense_counter = 0;
                                    }
                                }
                            }
                            $rowCount++;
                        }
                    }
                }
            }

            $key_counter++;
        }

        // get value information to include in set
        $column_data = array();
        foreach ($xml->ds as $key => $value) {
            array_push($column_data, ["name" => trim($value->name), "type" => trim($value->type)]);
        }

        return ["condensed_step" => $condensed_step, "columns" => $column_data, "archive" => $condensed_archive];
    }

    /**
     * Custom Compare for usort
     * @param $a
     * @param $b
     * @return mixed
     */
    private function orderByTimestampASC($a, $b)
    {
        return $a[0] - $b[0];
    }
451 452 453

    /**
     * retrieve descriptive details of rrd
454
     * @param string $rrd rrd category - item
455 456 457 458 459 460 461 462
     * @return array result status and data
     */
    private function getRRDdetails($rrd)
    {
        # Source of data: xml fields of corresponding .xml metadata
        $result = array();
        $backend = new Backend();
        $response = $backend->configdpRun("systemhealth list");
463 464 465 466 467 468 469 470 471 472 473
        $healthList = json_decode($response, true);
        // search by topic and name, return array with filename
        if (is_array($healthList)) {
            foreach ($healthList as $filename => $healthItem) {
                if ($healthItem['itemName'] .'-' . $healthItem['topic'] == $rrd) {
                    $result["result"] = "ok";
                    $healthItem['filename'] = $filename;
                    $result["data"] = $healthItem;
                    return $result;
                }
            }
474
        }
475 476 477 478

        // always return a valid (empty) data set
        $result["result"] = "not found";
        $result["data"] = ["title"=>"","y-axis_label"=>"","field_units"=>[], "itemName" => "", "filename" => ""];
479 480 481 482 483 484 485 486 487 488
        return $result;
    }


    /**
     * retrieve Available RRD data
     * @return array
     */
    public function getRRDlistAction()
    {
489
        # Source of data: filelisting of /var/db/rrd/*.rrd
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
        $result = array();
        $backend = new Backend();
        $response = $backend->configdpRun("systemhealth list");
        $healthList = json_decode($response, true);

        $result['data'] = array();
        if (is_array($healthList)) {
            foreach ($healthList as $healthItem => $details) {
                if (!array_key_exists($details['topic'], $result['data'])) {
                    $result['data'][$details['topic']] = array();
                }
                $result['data'][$details['topic']][] = $details['itemName'];
            }
        }
        ksort($result['data']);
        $result["result"] = "ok";

        // Category => Items
        return $result;
    }

    /**
     * retrieve SystemHealth Data (previously called RRD Graphs)
     * @param string $rrd
     * @param int $from
     * @param int $to
     * @param int $max_values
     * @param bool $inverse
     * @param int $detail
     * @return array
     */
    public function getSystemHealthAction(
        $rrd = "",
        $from = 0,
        $to = 0,
        $max_values = 120,
        $inverse = false,
        $detail = -1
    ) {
        /**
         * $rrd = rrd filename without extension
         * $from = from timestamp (0=min)
         * $to = to timestamp (0=max)
         * $max_values = limit datapoint as close as possible to this number (or twice if detail (zoom) + overview )
         * $inverse = Inverse every odd row (multiply by -1)
         * $detail = limits processing of dataSets to max given (-1 = all ; 1 = 0,1 ; 2 = 0,1,2 ; etc)
         */

        $rrd_details=$this->getRRDdetails($rrd)["data"];
539
        $xml = false;
540 541 542
        if ($rrd_details['filename'] != "") {
            $backend = new Backend();
            $response = $backend->configdpRun("systemhealth fetch ", array($rrd_details['filename']));
543
            $xml = @simplexml_load_string($response);
544
        }
545 546 547 548

        if ($xml !== false) {
            // we only use the average databases in any RRD, remove the rest to avoid strange behaviour.
            for ($count = count($xml->rra) -1; $count >= 0; $count--) {
549
                if (trim((string)$xml->rra[$count]->cf) != "AVERAGE") {
550 551 552 553 554 555 556 557 558 559 560
                    unset($xml->rra[$count]);
                }
            }
            $data_sets_full = $this->getDataSetInfo($xml); // get dataSet information to include in answer

            if ($inverse == 'true') {
                $inverse = true;
            } else {
                $inverse = false;
            }

561
            // The zoom (timespan) level determines the number of datasets to
562
            // use in the equation. All the irrelevant sets are removed here.
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
            if ((int)$detail >= 0) {
                for ($count = count($xml->rra) - 1; $count > $detail; $count--) {
                    unset($xml->rra[$count]);
                }
            }

            // determine available dataSets within range and how to handle them
            $selected_archives = $this->getSelection($this->getDataSetInfo($xml), $from, $to, $max_values);
            // get condensed dataSets and translate them to d3 usable data
            $result = $this->translateD3(
                $this->getCondensedArchive($xml, $selected_archives),
                $inverse,
                $rrd_details["field_units"]
            );

            return ["sets" => $data_sets_full,
                "d3" => $result,
580 581 582
                "title"=>$rrd_details["title"] != "" ?
                         $rrd_details["title"] . " | " . ucfirst($rrd_details['itemName']) :
                         ucfirst($rrd_details['itemName']),
583 584 585 586 587 588
                "y-axis_label"=>$rrd_details["y-axis_label"]
            ]; // return details and d3 data
        } else {
            return ["sets" => [], "d3" => [], "title" => "error", "y-axis_label" => ""];
        }
    }
589 590 591 592 593 594 595 596 597

    /**
     * Retrieve network interfaces by key (lan, wan, opt1,..)
     * @return array
     */
    public function getInterfacesAction()
    {
        // collect interface names
        $intfmap = array();
598
        $config = Config::getInstance()->object();
599 600
        if ($config->interfaces != null) {
            foreach ($config->interfaces->children() as $key => $node) {
601
                $intfmap[(string)$key] = array("descr" => !empty((string)$node->descr) ? (string)$node->descr : $key);
602 603 604 605
            }
        }
        return $intfmap;
    }
606
}