_processObservation(); } /** * Adds additional editor fields to the Contentserv interface * * @param CSGuiEditor $editor Editor object that can add fields * @param CSMonitorObserver $observer */ public function prepareEditor(CSGuiEditor $editor, CSMonitorObserver $observer) { $this->_observer = $observer; $editor->addField("RecordType", CS::translate('CSALIVE_MONITOR_RECORD_TYPE', 'csalive'), $this->getRecordTypes(), 'Pdmarticle', FALSE, array( 'PaneTitle' => CS::translate('REST_GUI_EDIT_SETTINGS'), 'noEmptyOption' => true, 'onlyFolder' => true )); $editor->addField("InspectRequests", CS::translate('CSALIVE_MONITOR_REQUEST_VALUES', 'csalive'), $this->getInspectProcesses(), '', FALSE, array( 'PaneTitle' => CS::translate('REST_GUI_EDIT_SETTINGS'), 'noEmptyOption' => true, 'multiple' => true )); } ///// PROTECTED FUNCTIONS ////////////////////////////////////////////////////////////////////////////////////////////// /** * Function is automatically called when the observation runs. */ protected function _processObservation() { $intObserverID = $this->_observer->getID(); $this->strRecordType = $this->_observer->getValue('RecordType'); $this->arrChosenRequests = explode(',', $this->_observer->getValue('InspectRequests')); $objConfig = $this->getDatabaseConfig(); $strServiceName = str_ireplace('CS18', '', $this->getClassName()); try { $this->objRBConnection = new DatabaseRBConnection(CS::getTablePrefix() . 'idb_' . $this->strRecordType, $objConfig); if (!empty($this->arrChosenRequests)) { foreach ((array) $this->arrChosenRequests as $strFunctionName) { if (!method_exists($this, $strFunctionName)) { continue; } $arrReturnValues = $this->$strFunctionName(); if (!empty($arrReturnValues)) { $this->storeInDatabase($intObserverID, $arrReturnValues['Value'], $arrReturnValues['Unit'], $strServiceName . '.' . substr($strFunctionName, 7)); } else { $this->storeInDatabase($intObserverID, 99999, 'ERROR', $strServiceName . '.Error'); } } } else { $this->storeInDatabase($intObserverID, 99999, 'ERROR', $strServiceName . '.Error'); } } catch (Exception $ex) { $this->storeInDatabase($intObserverID, 99999, $ex->getMessage(), $strServiceName . '.Error'); } } ///// PRIVATE FUNCTIONS //////////////////////////////////////////////////////////////////////////////////////////////// /** * Compares the number of objects with the main database and returns the difference. * * @return array Returns an array for the database export */ private function requestObjectCount() { //Export database $intExportRowCount = $this->objRBConnection->getRowCount(); return ['Value' => $intExportRowCount, 'Unit' => 'Items']; } /** * Compares the the change date of the newest object with the main database and returns the difference. * * @return array Returns an array for the database export */ private function requestChangeDate() { //Export database $strNewestDate = $this->objRBConnection->getNewestEntryDate('LastChange'); if (empty($strNewestDate)) { return ['Value' => 0, 'Unit' => 's']; } $intExportChangeDate = strtotime($strNewestDate); //MySQL standard database $intSQLChangeDate = strtotime(Database::queryValue('SELECT LastChange FROM ' . CS::getTablePrefix() . $this->strRecordType . ' ORDER BY LastChange DESC LIMIT 0,1')); return ['Value' => $intExportChangeDate - $intSQLChangeDate, 'Unit' => 's']; } /** * Stores a value with the given parameter in the monitor observation database * * @param integer $intObserverID ID which the observation ID shall have * @param mixed $mixedMeasuredValue Result that shall be stored in the value column * @param string $strUnit Unit of the value (ca. 150 character text field) * @param string $strServiceName Name of the result */ private function storeInDatabase(int $intObserverID, $mixedMeasuredValue, string $strUnit, string $strServiceName) { $observation = CSMonitor::createObservation($intObserverID, $strServiceName); $observation->setMeasuredValue($mixedMeasuredValue); $observation->setTimestamp(CS::getDate()); $observation->setUnit($strUnit); $state = CSMonitor::MONITOR_GUI_NOTIFICATION_NONE_ID; $observation->setState($state); $observation->setWarningValue(1); $observation->setCriticalValue(2); $observation->store(); } /** * Returns an array of Strings of all processes the plugin can monitor. * * @return array Returns all processes possible to be inspect */ private function getInspectProcesses() { $arrClassMethods = get_class_methods($this); $strSearchWord = 'request'; $arrMatches = array_filter((array) $arrClassMethods, function($var) use ($strSearchWord) { return preg_match("/$strSearchWord/i", $var); }); foreach ((array) $arrMatches as $value) { $arrTranslatedMatches[$value] = CS::translate($value, 'csalive'); } return (array) $arrTranslatedMatches; } /** * Quality of life method for faster debugging. * * @param mixed $mixMessage */ private function quickAlert($mixMessage) { alert($mixMessage, 1); die(); } /** * Returns an array of Strings of all available filetypes * * @return array Possible datatypes that can be chosen to be monitored */ private function getRecordTypes() { return (array) array( 'Pdmarticle' => CS::translate('Pdmarticle'), 'Mamfile' => CS::translate('Mamfile'), 'Pdmarticlestructure' => CS::translate('Pdmarticlestructure'), 'User' => CS::translate('User') ); } /** * Creates the class name but deletes the word "Observer" * * @return string Returns the changed class name */ private function getClassName() { $strClassName = get_class($this); return substr($strClassName, 0, -8); } /** * Creates a custom database configuration for another connection * * @return \CSDatabaseConfiguration Returns a database configuration object */ private function getDatabaseConfig() { $objConfig = new CSDatabaseConfiguration(); $objConfig->setHost(CS::getOption('jdbcAddress', 'exportstaging')); $objConfig->setPort(CS::getOption('jdbcPort', 'exportstaging')); $objConfig->setName(CS::getOption('intermediateDatabase', 'exportstaging')); $objConfig->setUser(CS::getOption('jdbcUsername', 'exportstaging')); $objConfig->setPassword(CS::getOption('jdbcPassword', 'exportstaging')); return $objConfig; } }