howardjones/network-weathermap

View on GitHub
lib/Weathermap/Plugins/Datasources/CactiHostStatus.php

Summary

Maintainability
A
3 hrs
Test Coverage
<?php

//require_once dirname(__FILE__) . "/../database.php";

namespace Weathermap\Plugins\Datasources;

use Weathermap\Core\MapUtility;
use Weathermap\Core\Map;
use Weathermap\Core\MapDataItem;
use PDO;

/**
 * Get data from Cacti's host table
 *
 * @package Weathermap\Plugins\Datasources
 */
class CactiHostStatus extends Base
{
    public function __construct()
    {
        parent::__construct();

        $this->regexpsHandled = array(
            '/^cactihost:(\d+)$/'
        );

        $this->name = "CactiHost";
    }

    public function init(&$map)
    {
        if ($map->context == 'cacti') {
            if (function_exists('db_fetch_row')) {
                return true;
            } else {
                MapUtility::debug('ReadData CactiHost: Cacti database library not found.\n');
            }
        } else {
            MapUtility::debug("ReadData CactiHost: Can only run from Cacti environment.\n");
        }

        return false;
    }

    /**
     * @param string $targetString
     * @param Map $map
     * @param MapDataItem $mapItem
     * @return array
     */
    public function readData($targetString, &$map, &$mapItem)
    {
        $this->data[IN] = null;
        $this->data[OUT] = null;

        if (preg_match($this->regexpsHandled[0], $targetString, $matches)) {
            $cactiHostId = intval($matches[1]);

            $pdo = weathermap_get_pdo();

            $statement = $pdo->prepare("select * from host where id=?");

            // 0=disabled
            // 1=down
            // 2=recovering
            // 3=up

            $statement->execute(array($cactiHostId));

            $state = -1;
            $result = $statement->fetch(PDO::FETCH_ASSOC);
            if (isset($result)) {
                // create a note, which can be used in icon filenames or labels more nicely
                if ($result['status'] == 1) {
                    $state = 1;
                    $statename = 'down';
                }
                if ($result['status'] == 2) {
                    $state = 2;
                    $statename = 'recovering';
                }
                if ($result['status'] == 3) {
                    $state = 3;
                    $statename = 'up';
                }
                if ($result['disabled']) {
                    $state = 0;
                    $statename = 'disabled';
                }

                $this->data[IN] = $state;
                $this->data[OUT] = $state;
                $mapItem->addNote("state", $statename);
                $mapItem->addNote("cacti_description", $result['description']);

                $mapItem->addNote("cacti_hostname", $result['hostname']);
                $mapItem->addNote("cacti_curtime", $result['cur_time']);
                $mapItem->addNote("cacti_avgtime", $result['avg_time']);
                $mapItem->addNote("cacti_mintime", $result['min_time']);
                $mapItem->addNote("cacti_maxtime", $result['max_time']);
                $mapItem->addNote("cacti_availability", $result['availability']);

                $mapItem->addNote("cacti_faildate", $result['status_fail_date']);
                $mapItem->addNote("cacti_recdate", $result['status_rec_date']);
            }
        }
        return $this->returnData();
    }
}


// vim:ts=4:sw=4: