darkmist icon

Target and CI Legacy Code

darkmist | PRO | 06/16/16 06:37:59 PM UTC | 0 ⭐ | 298 👁️ | Never ⏰ | []
PHP |

5.62 KB

|

None

|

0 👍

/

0 👎

    /**
     * Function eventHasAValidCi
     * @param array $event Event to check for a valid CI.
     * @return bool|null
     */
    private function _eventHasAValidCi($event)
    {
        $target = $this->_targetModel->getTarget($event['targetId']);
 
        if ($target === false) {
            $this->_log->error(__METHOD__ . ' there was NO target in EMS.');
 
            return false;
        }
 
        $sql = 'SELECT name FROM monitors WHERE monitor_id=' . $event['monitorId'];
 
        $this->_log->info(__METHOD__ . ' ' . $sql);
 
        $monitor = $this->_db->fetchOne($sql);
 
        /** @var mixed $CI */
        $CI = false;
 
        /** @var string $targetUsed */
        $targetUsed = '';
 
        try {
            if (!empty($event['targetCi'])) {
                $CI = $this->_servicenow->getCiFromTarget($event['targetCi']);
                $targetUsed = $event['targetCi'];
            }
            if ($CI === false && !empty($event['targetName'])) {
                $CI = $this->_servicenow->getCiFromMonitorName($event['targetName']);
                $targetUsed = $event['targetName'];
            }
            if ($CI === false && !empty($event['targetName'])) {
                $CI = $this->_servicenow->getCiFromTarget($event['targetName']);
                $targetUsed = $event['targetName'];
            }
            if ($CI === false && !empty($event['targetHost'])) {
                $CI = $this->_servicenow->getCiFromTarget($event['targetHost']);
                $targetUsed = $event['targetHost'];
            }
            if ($CI === false && !empty($event['targetHint'])) {
                $CI = $this->_servicenow->getCiFromTarget($event['targetHint']);
                $targetUsed = $event['targetHint'];
            }
            if ($CI === false && !empty($monitor['name'])) {
                $CI = $this->_servicenow->getCiFromMonitorName($monitor['name']);
                $targetUsed = $monitor['name'];
            }
        } catch(\Exception $e) {
            $this->_log->error($e->getCode().':'.$e->getMessage());
        }
 
        if (empty($CI)) {
            $this->_log->error(__METHOD__ . ' there was NO CI found in ServiceNow.');
 
            return false;
        }
 
        if (count($CI->result) === 0) {
            $this->_log->error(__METHOD__ . ' there was NO CI found in ServiceNow.');
 
            return false;
        }
 
        $this->_processConfigurationItems(
            $CI->result,
            $targetUsed,
            $target
        );
 
        return $target;
    }
 
    /**
     * Function processConfigurationItems
     * @param array                   $ciList
     * @param string                  $targetUsed
     * @param \objects\targets\Target $target
     * @return bool
     * @access private
     */
    private function _processConfigurationItems($ciList, $targetUsed, $target)
    {
        $numInCiList = count($ciList);
        $ciHit = [];
        $ciTwoPartials = false;
        $ciPartialMatch = false;
        $ciExactMatch = false;
 
        for ($i = 0; $i < $numInCiList; $i++) {
            if ($ciList[$i]->u_phase === 'Commissioned') {
                if (strcasecmp($targetUsed, $ciList[$i]->name) === 0) {
                    if ($ciExactMatch === true) {
                        $this->_log->error(
                            __METHOD__ . ' two exact CI matches found.'
                        );
 
                        return false; // Two exact matches, bad news
                    }
                    $ciHit = $ciList[$i];
                    $ciExactMatch = true;
                    $this->_log->info(__METHOD__ . ' an exact CI match found.');
                } elseif (stripos($ciList[$i]->name, $targetUsed) === 0
                    && $ciExactMatch === false
                ) {
                    if ($ciPartialMatch === true) {
                        // Two partial prefix matches, bad news
                        $this->_log->error(
                            __METHOD__ .
                            ' two partial prefix CI matches found, continuing'
                        );
                        $ciTwoPartials = true;
                        continue;
                    }
                    $ciHit = $ciList[$i];
                    $ciPartialMatch = true;
                    $this->_log->info(__METHOD__ . ' a partial prefix CI match found.');
                }
            }
        }
 
        if ($ciTwoPartials === true && $ciExactMatch !== true) {
            $this->_log->error(
                __METHOD__ . ' two partial prefix CI matches found, returning false'
            );
 
            return false;
        }
 
        if (isset($event['targetName'])) {
            $target->lookupName = $event['targetName'];
        }
        if (isset($event['targetHost'])) {
            $target->lookupHost = $event['targetHost'];
        }
        if (isset($event['targetHint'])) {
            $target->lookupHint = $event['targetHint'];
        }
        if (isset($ciHit->{'support_group.name'})) {
            $target->ownerQueue = $ciHit->{'support_group.name'};
        }
        if (isset($ciHit->name)) {
            $target->ciName = $ciHit->name;
            $target->ciLogicalName = $ciHit->{'sys_id'};
        }
        if (isset($ciHit->u_monitoring_notification_mode)) {
            $target->notifyModel = $ciHit->u_monitoring_notification_mode;
        }
        if (isset($ciHit->used_for)) {
            $target->environment = $ciHit->used_for;
        }
 
        $target->kb = '';
        $this->_targetModel->updateTarget($target);
    }

Comments