_processObservation(); } /** * Adds additional editor fields to the Contentserv interface * * @param CSGuiEditor $editor * @param CSMonitorObserver $observer */ public function prepareEditor(CSGuiEditor $editor, CSMonitorObserver $observer) { $this->_observer = $observer; $editor->addField('RangeInDays', CS::translate('CSALIVE_MONITOR_RANGE_IN_DAYS', 'csalive'), '', '1 - 5', FALSE, array( 'PaneTitle' => CS::translate('REST_GUI_EDIT_SETTINGS') )); $editor->addField('RangeInHours', CS::translate('CSALIVE_MONITOR_RANGE_IN_HOURS', 'csalive'), '', '6 - 22', FALSE, array( 'PaneTitle' => CS::translate('REST_GUI_EDIT_SETTINGS') )); $editor->addField('MaxOldInMinutes', CS::translate('CSALIVE_MONITOR_MAX_OLD_IN_MINUTES', 'csalive'), 'number', 60, FALSE, array( 'PaneTitle' => CS::translate('REST_GUI_EDIT_SETTINGS') )); $editor->addField("MAMFile", CS::translate('CSALIVE_MONITOR_MAMFILE', 'csalive'), CS::getRecord('Mamfile'), '', FALSE, array( 'PaneTitle' => CS::translate('REST_GUI_EDIT_SETTINGS'), )); $editor->addField("SelectedStatus", CS::translate('CSALIVE_MONITOR_SERVICE_STATUS', 'csalive'), $this->getStatusNames(), 'MAMChange.Status1', FALSE, array( 'PaneTitle' => CS::translate('REST_GUI_EDIT_SETTINGS'), 'noEmptyOption' => true )); } ///// PROTECTED METHODS ////////////////////////////////////////////////////// /** * Measures the time needed for a specific process and stores it in the database */ protected function _processObservation() { $boolRangeDays = $this->checkRangeValid($this->getRange($this->_observer->getValue('RangeInDays')), 'N'); $boolRangeHours = $this->checkRangeValid($this->getRange($this->_observer->getValue('RangeInHours')), 'H'); if ($boolRangeDays && $boolRangeHours) { if ($this->checkChangeWithMax($this->_observer->getValue('MAMFile'), $this->_observer->getValue('MaxOldInMinutes'))) { $this->saveReturnValue(2, $this->getSelectedStatus(), 'Status'); } else { $this->saveReturnValue(1, $this->getSelectedStatus(), 'Status'); } } } ///// PRIVATE METHODS ////////////////////////////////////////////////////// /** * Is called when a function has a return value. * Saves the return value in the database with the Unit "Count" * * @param mixed $mixedReturnValue * @param String $strService */ private function saveReturnValue($mixedReturnValue, $strService, $strUnit = 's', $intWarn = 0, $intCrit = 1) { $observation = CSMonitor::createObservation($this->_observer->getID(), $strService); $observation->setMeasuredValue($mixedReturnValue); $observation->setTimestamp(CS::getDate()); $observation->setUnit($strUnit); $state = CSMonitor::MONITOR_GUI_NOTIFICATION_NONE_ID; $observation->setState($state); $observation->setWarningValue($intWarn); $observation->setCriticalValue($intCrit); $observation->store(); } /** * return the given value as number or range * @param String $strRange * @return mixed */ private function getRange($strRange) { $arrRange = explode('-', trim($strRange)); if ($arrRange[1]) { return (array) $arrRange; } else { return (int) $arrRange[0]; } } /** * check the given range or number with the current time * @param mixed $mixRange must be integer for specified day or hour, or array for range * @param String $strDateTyp 'N' to check days and 'H' to check hours * @return boolean */ private function checkRangeValid($mixRange, $strDateTyp) { $boolStatus = false; if (is_array($mixRange) && date($strDateTyp) >= (int) $mixRange[0] && date($strDateTyp) <= (int) $mixRange[1]) { $boolStatus = true; } elseif (is_int($mixRange) && (int) $mixRange == date($strDateTyp)) { $boolStatus = true; } return (bool) $boolStatus; } /** * check if the given ID file or folder and not older than intMaxOldInMinutes * @param int $intMamID File or Folder ID * @param int $intMaxOldInMinutes Max old in minutes * @param String $strType file type like Mamfile.... * @return boolean */ private function checkChangeWithMax($intMamID, $intMaxOldInMinutes, $strType = 'Mamfile') { $strSubDate = $this->subMinutesFromDate($intMaxOldInMinutes); if (CS::getRecord($strType, $intMamID)->IsFolder() == true) { if (!empty($this->getDirectionChildrens($intMamID))) { foreach ($this->getDirectionChildrens($intMamID) as $intID) { $strMamLastChange = $this->getLastChange($intID); if ($strMamLastChange >= $strSubDate) { return true; } } } else { //when the Folder empty return true; } } else { $strMamLastChange = $this->getLastChange($intMamID); if ($strMamLastChange >= $strSubDate) { return true; } } return false; } private function subMinutesFromDate($intMin) { $strDateNow = date("Y-m-d H:i:s"); $intTime = strtotime($strDateNow); $intSubTime = $intTime - ($intMin * 60); $strDate = date("Y-m-d H:i:s", $intSubTime); return $strDate; } private function getLastChange($intID, $stype = 'Mamfile') { return CS::getRecord($stype, $intID)->getValue('LastChange'); } private function getDirectionChildrens($intID, $stype = 'Mamfile') { return (array) CS::getRecord($stype, $intID)->getChildrenIds($intID, false, false); } private function getStatusNames() { $arrSelectableContent = array(); for ($intI = 1; $intI <= 10; $intI++) { $arrSelectableContent['MAMChange.Status' . $intI] = 'MAMChange.Status' . $intI; } return $arrSelectableContent; } private function getSelectedStatus() { $strStatus = $this->_observer->getValue('SelectedStatus'); return $strStatus; } }