From 7dccd079a3fdd512970a4af0722c3d8a9cd2ffd3 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 11 Oct 2019 15:48:53 +0200 Subject: [PATCH 1/5] ServiceController: Don't fetch volatile states --- application/controllers/ServiceController.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/application/controllers/ServiceController.php b/application/controllers/ServiceController.php index 4f2408e0..ff552128 100644 --- a/application/controllers/ServiceController.php +++ b/application/controllers/ServiceController.php @@ -5,7 +5,6 @@ namespace Icinga\Module\Eagle\Controllers; use Icinga\Exception\NotFoundError; use Icinga\Module\Eagle\Common\CommandActions; use Icinga\Module\Eagle\Model\Service; -use Icinga\Module\Eagle\Redis\VolatileState; use Icinga\Module\Eagle\Web\Controller; use Icinga\Module\Eagle\Widget\ServiceListItem; @@ -36,10 +35,6 @@ class ServiceController extends Controller throw new NotFoundError($this->translate('Service not found')); } - $volatileState = new VolatileState($this->getRedis()); - $volatileState->add($service); - $volatileState->fetch(); - $this->service = $service; } From 4dbb672083f3f04f1e0d9b1729ef6832df800340 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 11 Oct 2019 15:49:50 +0200 Subject: [PATCH 2/5] StateList: Don't fetch volatile states --- application/controllers/HostsController.php | 1 - .../controllers/ServicesController.php | 1 - library/Eagle/Widget/StateList.php | 42 +------------------ 3 files changed, 1 insertion(+), 43 deletions(-) diff --git a/application/controllers/HostsController.php b/application/controllers/HostsController.php index 557d30f7..244bdb84 100644 --- a/application/controllers/HostsController.php +++ b/application/controllers/HostsController.php @@ -22,7 +22,6 @@ class HostsController extends Controller $hosts->limit($limitControl->getLimit()); $hostList = (new HostList($hosts)) - ->setRedis($this->getRedis()) ->setViewMode($viewModeSwitcher->getViewMode()); $this->addControl($this->createPaginationControl($hosts)); diff --git a/application/controllers/ServicesController.php b/application/controllers/ServicesController.php index 378e3c9a..20d957ee 100644 --- a/application/controllers/ServicesController.php +++ b/application/controllers/ServicesController.php @@ -26,7 +26,6 @@ class ServicesController extends Controller $services->limit($limitControl->getLimit()); $serviceList = (new ServiceList($services)) - ->setRedis($this->getRedis()) ->setViewMode($viewModeSwitcher->getViewMode()); $this->addControl($this->createPaginationControl($services)); diff --git a/library/Eagle/Widget/StateList.php b/library/Eagle/Widget/StateList.php index 7fe35eae..4fc674a6 100644 --- a/library/Eagle/Widget/StateList.php +++ b/library/Eagle/Widget/StateList.php @@ -3,55 +3,15 @@ namespace Icinga\Module\Eagle\Widget; use Icinga\Module\Eagle\Common\ViewMode; -use Icinga\Module\Eagle\Redis\VolatileState; -use Redis; abstract class StateList extends BaseItemList { use ViewMode; - /** @var iterable Data source of the list */ - protected $data; - - /** @var Redis Redis connection to fetch volatile states from */ - protected $redis; - - /** - * Set the Redis connection to fetch volatile states from - * - * @param Redis $redis - * - * @return $this - */ - public function setRedis(Redis $redis) - { - $this->redis = $redis; - - return $this; - } - - /** - * Get the helper to fetch volatile states - * - * @return VolatileState - */ - public function getVolatileState() - { - return new VolatileState($this->redis); - } - protected function assemble() { $this->addAttributes(['class' => $this->getViewMode()]); - $itemClass = $this->getItemClass(); - $volatileState = $this->getVolatileState(); - - foreach ($this->data as $object) { - $volatileState->add($object); - $this->add(new $itemClass($object)); - } - - $volatileState->fetch(); + parent::assemble(); } } From 4182fe68c63495d09707c0a3c60f1a9d05a3777f Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 11 Oct 2019 15:52:11 +0200 Subject: [PATCH 3/5] State: Fetch volatile state with a behavior --- .../Eagle/Model/Behavior/VolatileState.php | 36 ++++++++++++++ library/Eagle/Model/State.php | 7 +++ library/Eagle/Redis/VolatileState.php | 48 ++++--------------- 3 files changed, 51 insertions(+), 40 deletions(-) create mode 100644 library/Eagle/Model/Behavior/VolatileState.php diff --git a/library/Eagle/Model/Behavior/VolatileState.php b/library/Eagle/Model/Behavior/VolatileState.php new file mode 100644 index 00000000..589259f9 --- /dev/null +++ b/library/Eagle/Model/Behavior/VolatileState.php @@ -0,0 +1,36 @@ +state === null) { + // TODO(jmeyer): Use a service provider here. (Or something similar) + $config = Config::module('eagle')->getSection('redis'); + $redis = new Redis(); + $redis->connect( + $config->get('host', 'redis'), + $config->get('port', 6379) + ); + + $this->state = new RedisState($redis); + } + + return $this->state; + } + + public function apply(Model $model) + { + $this->getVolatileState()->fetch($model); + } +} diff --git a/library/Eagle/Model/State.php b/library/Eagle/Model/State.php index ac411d3b..23e0ea3f 100644 --- a/library/Eagle/Model/State.php +++ b/library/Eagle/Model/State.php @@ -2,6 +2,8 @@ namespace Icinga\Module\Eagle\Model; +use Icinga\Module\Eagle\Model\Behavior\VolatileState; +use ipl\Orm\Behaviors; use ipl\Orm\Model; /** @@ -43,6 +45,11 @@ abstract class State extends Model ]; } + public function createBehaviors(Behaviors $behaviors) + { + $behaviors->add(new VolatileState()); + } + public function mutateIsOverdueProperty() { return $this->properties['next_update'] < time(); diff --git a/library/Eagle/Redis/VolatileState.php b/library/Eagle/Redis/VolatileState.php index fb618fd1..c295db03 100644 --- a/library/Eagle/Redis/VolatileState.php +++ b/library/Eagle/Redis/VolatileState.php @@ -2,7 +2,7 @@ namespace Icinga\Module\Eagle\Redis; -use ipl\Orm\Model; +use Icinga\Module\Eagle\Model\State; /** * Fetch volatile host or service states from redis. @@ -12,12 +12,6 @@ class VolatileState /** @var \Redis */ protected $redis; - /** @var string */ - protected $type; - - /** @var array */ - protected $objects = []; - /** @var array Set of keys to sync */ public static $keys = [ 'attempt', @@ -44,48 +38,22 @@ class VolatileState } /** - * Add an object to fetch volatile states for + * Fetch volatile state * - * @param Model $object + * @param State $state * * @return $this */ - public function add(Model $object) + public function fetch(State $state) { - $this->objects[bin2hex($object->id)] = $object; - - if ($this->type === null) { - $this->type = $object->getTableName(); - } - - return $this; - } - - /** - * Fetch volatile states - * - * @return $this - */ - public function fetch() - { - $keys = array_keys($this->objects); - - if (empty($keys)) { - return $this; - } - - $rs = array_combine($keys, $this->redis->hMGet("icinga:config:state:{$this->type}", $keys)); - - foreach ($rs as $key => $json) { - if ($json === false) { - continue; - } + $type = substr($state->getTableName(), 0, -6); + $json = $this->redis->hGet("icinga:config:state:{$type}", bin2hex($state->{$type . '_id'})); + if ($json !== false) { $data = json_decode($json, true); - $data = array_intersect_key($data, array_flip(static::$keys)); - $this->objects[$key]->state->setProperties($data); + $state->setProperties($data); } return $this; From f93dcd1d9e15582ff711be917c5e820d7874fd38 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 11 Oct 2019 15:53:34 +0200 Subject: [PATCH 4/5] State: Convert millisecond columns to timestamps --- .../Model/Behavior/PropertiesBehavior.php | 25 +++++++++++++++++++ library/Eagle/Model/Behavior/Timestamp.php | 11 ++++++++ library/Eagle/Model/State.php | 9 +++++++ 3 files changed, 45 insertions(+) create mode 100644 library/Eagle/Model/Behavior/PropertiesBehavior.php create mode 100644 library/Eagle/Model/Behavior/Timestamp.php diff --git a/library/Eagle/Model/Behavior/PropertiesBehavior.php b/library/Eagle/Model/Behavior/PropertiesBehavior.php new file mode 100644 index 00000000..04460ada --- /dev/null +++ b/library/Eagle/Model/Behavior/PropertiesBehavior.php @@ -0,0 +1,25 @@ +properties = $properties; + } + + public function apply(Model $model) + { + foreach ($this->properties as $key) { + $model[$key] = $this($model[$key], $key); + } + } + + abstract public function __invoke($value, $key); +} diff --git a/library/Eagle/Model/Behavior/Timestamp.php b/library/Eagle/Model/Behavior/Timestamp.php new file mode 100644 index 00000000..07f83691 --- /dev/null +++ b/library/Eagle/Model/Behavior/Timestamp.php @@ -0,0 +1,11 @@ +add(new VolatileState()); + $behaviors->add(new Timestamp([ + 'last_update', + 'last_state_change', + 'last_soft_state', + 'last_hard_state', + 'next_check', + 'next_update' + ])); } public function mutateIsOverdueProperty() From 88ccf149075cc338f324ca9e86fe7963b20063d9 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 11 Oct 2019 15:55:14 +0200 Subject: [PATCH 5/5] Host/Service: Transform y|n enums to true|false --- library/Eagle/Model/Behavior/BoolCast.php | 11 +++++++++++ library/Eagle/Model/Host.php | 13 +++++++++++++ library/Eagle/Model/Service.php | 13 +++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 library/Eagle/Model/Behavior/BoolCast.php diff --git a/library/Eagle/Model/Behavior/BoolCast.php b/library/Eagle/Model/Behavior/BoolCast.php new file mode 100644 index 00000000..1869300d --- /dev/null +++ b/library/Eagle/Model/Behavior/BoolCast.php @@ -0,0 +1,11 @@ +add(new BoolCast([ + 'active_checks_enabled', + 'passive_checks_enabled', + 'event_handler_enabled', + 'notifications_enabled', + 'flapping_enabled' + ])); + } + public function createRelations(Relations $relations) { $relations->belongsTo('environment', Environment::class); diff --git a/library/Eagle/Model/Service.php b/library/Eagle/Model/Service.php index 34c96140..630cf685 100644 --- a/library/Eagle/Model/Service.php +++ b/library/Eagle/Model/Service.php @@ -2,6 +2,8 @@ namespace Icinga\Module\Eagle\Model; +use Icinga\Module\Eagle\Model\Behavior\BoolCast; +use ipl\Orm\Behaviors; use ipl\Orm\Model; use ipl\Orm\Relations; @@ -60,6 +62,17 @@ class Service extends Model ]; } + public function createBehaviors(Behaviors $behaviors) + { + $behaviors->add(new BoolCast([ + 'active_checks_enabled', + 'passive_checks_enabled', + 'event_handler_enabled', + 'notifications_enabled', + 'flapping_enabled' + ])); + } + public function createRelations(Relations $relations) { $relations->belongsTo('environment', Environment::class);