diff --git a/inmail_monitoring/config/schema/inmail_monitoring.schema.yml b/inmail_monitoring/config/schema/inmail_monitoring.schema.yml new file mode 100644 index 0000000..e7e3eac --- /dev/null +++ b/inmail_monitoring/config/schema/inmail_monitoring.schema.yml @@ -0,0 +1,8 @@ +# Schema for IMAP quota sensor. +monitoring.settings.inmail_monitoring_imap_quota: + type: monitoring.settings_base + label: 'IMAP quota sensor settings' + mapping: + imap_fetcher: + label: Inmail IMAP fetcher to track its quota + type: string diff --git a/inmail_monitoring/inmail_monitoring.info.yml b/inmail_monitoring/inmail_monitoring.info.yml new file mode 100644 index 0000000..a988931 --- /dev/null +++ b/inmail_monitoring/inmail_monitoring.info.yml @@ -0,0 +1,10 @@ +name: Inmail Monitoring +type: module +description: Monitors the state of Inmail module. +package: Mail +version: 8.x-1.x +core: 8.x + +dependencies: + - inmail + - monitoring diff --git a/inmail_monitoring/src/Plugin/monitoring/SensorPlugin/IMAPQuotaSensorPlugin.php b/inmail_monitoring/src/Plugin/monitoring/SensorPlugin/IMAPQuotaSensorPlugin.php new file mode 100644 index 0000000..1b3260e --- /dev/null +++ b/inmail_monitoring/src/Plugin/monitoring/SensorPlugin/IMAPQuotaSensorPlugin.php @@ -0,0 +1,95 @@ +condition('status', TRUE)->execute(); + /** @var \Drupal\inmail\Entity\DelivererConfig[] $deliverers */ + $deliverers = \Drupal::entityTypeManager()->getStorage('inmail_deliverer')->loadMultiple($deliverer_ids); + /** @var \Drupal\Component\Plugin\PluginManagerInterface $deliverer_manager */ + $deliverer_manager = \Drupal::service('plugin.manager.inmail.deliverer'); + + $imap_fetchers = []; + foreach ($deliverers as $deliverer) { + // @todo: Replace with getPluginInstance() after https://www.drupal.org/node/2769617. + $plugin = $deliverer_manager->createInstance($deliverer->getPluginId(), $deliverer->getConfiguration()); + + // List IMAP fetchers only. + if ($plugin instanceof ImapFetcher) { + $imap_fetchers[$deliverer->id()] = $deliverer->label(); + } + } + + $form['imap_fetcher'] = array( + '#type' => 'radios', + '#options' => $imap_fetchers, + '#title' => t('IMAP Fetchers'), + '#description' => t('Select a fetcher to track its IMAP quota.'), + '#default_value' => $this->sensorConfig->getSetting('imap_fetcher'), + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $sensor_config = $form_state->getFormObject()->getEntity(); + parent::submitConfigurationForm($form, $form_state); + + $sensor_config->settings['imap_fetcher'] = $form_state->getValue(['settings', 'imap_fetcher']); + } + + /** + * {@inheritdoc} + */ + public function runSensor(SensorResultInterface $result) { + $imap_fetcher = DelivererConfig::load($this->sensorConfig->settings['imap_fetcher']); + // @todo: Replace with getPluginInstance() after https://www.drupal.org/node/2769617. + /** @var \Drupal\Component\Plugin\PluginManagerInterface $deliverer_manager */ + $deliverer_manager = \Drupal::service('plugin.manager.inmail.deliverer'); + $plugin = $deliverer_manager->createInstance($imap_fetcher->getPluginId(), $imap_fetcher->getConfiguration()); + + if ($quota = $plugin->getQuota()) { + $usage = $quota['STORAGE']['usage']; + $limit = $quota['STORAGE']['limit']; + if ($usage >= $limit) { + $result->setStatus(SensorResultInterface::STATUS_CRITICAL); + } + else { + $result->setStatus(SensorResultInterface::STATUS_OK); + } + $result->setValue($usage); + $result->setMessage('The usage quota for @imap_fetcher is @usage KB and the limit is @limit KB', ['@imap_fetcher' => $imap_fetcher->id(), '@usage' => $usage, '@limit' => $limit]); + } + else { + $result->setStatus(SensorResultInterface::STATUS_UNKNOWN); + $result->setMessage('No quota information for @imap_fetcher fetcher.', ['@imap_fetcher' => $imap_fetcher->id()]); + } + } +} diff --git a/src/Plugin/inmail/Deliverer/FetcherInterface.php b/src/Plugin/inmail/Deliverer/FetcherInterface.php index 26b0bcd..4e28228 100644 --- a/src/Plugin/inmail/Deliverer/FetcherInterface.php +++ b/src/Plugin/inmail/Deliverer/FetcherInterface.php @@ -38,6 +38,14 @@ interface FetcherInterface extends DelivererInterface, ConfigurablePluginInterfa public function update(); /** + * Retrieves the quota settings for "INBOX". + * + * @return array|null + * An array of usage and limit or null if quota is not available. + */ + public function getQuota(); + + /** * Update the timestamp of the last status check made. * * @param int|null $timestamp diff --git a/src/Plugin/inmail/Deliverer/ImapFetcher.php b/src/Plugin/inmail/Deliverer/ImapFetcher.php index 71dc8c3..7bcdcbd 100644 --- a/src/Plugin/inmail/Deliverer/ImapFetcher.php +++ b/src/Plugin/inmail/Deliverer/ImapFetcher.php @@ -92,6 +92,19 @@ class ImapFetcher extends FetcherBase implements ContainerFactoryPluginInterface } /** + * {@inheritdoc} + */ + public function getQuota() { + return $this->doImap(function($imap_stream) { + $quota = imap_get_quotaroot($imap_stream, 'INBOX'); + if (!empty($quota) && is_array($quota)) { + return $quota; + } + return NULL; + }); + } + + /** * Connect to IMAP server and perform arbitrary operations. * * If connection fails, an exception is thrown and the callback is never