/** * @param int $farmId * @param string $serverId * @param string $eventId * @param int $scriptId * @param string $eventServerId * @param int $schedulerId * @param string $byDate * @param string $fromTime * @param string $toTime * @param string $status * @param string $event * @param JsonData $sort * @param int $start * @param int $limit * @param string $query * @throws Scalr_Exception_Core * @throws Scalr_Exception_InsufficientPermissions */ public function xListOrchestrationLogsAction($farmId = 0, $serverId = '', $eventId = '', $scriptId = 0, $eventServerId = '', $schedulerId = 0, $byDate = '', $fromTime = '', $toTime = '', $status = '', $event = '', JsonData $sort, $start = 0, $limit = 20, $query = '') { $this->request->restrictAccess(Acl::RESOURCE_LOGS_ORCHESTRATION_LOGS); $o = new Entity\OrchestrationLog(); $f = new Entity\Farm(); $criteria = [Entity\Farm::STMT_FROM => "{$o->table()} JOIN {$f->table('f')} ON {$f->columnId('f')} = {$o->columnFarmId}", Entity\Farm::STMT_WHERE => $this->request->getFarmSqlQuery() . " AND {$f->columnEnvId('f')} = " . $this->db->qstr($this->getEnvironmentId())]; if ($farmId) { $criteria[] = ['farmId' => $farmId]; } if ($serverId) { $criteria[] = ['serverId' => $serverId]; } if ($eventId) { $criteria[] = ['eventId' => $eventId]; } if ($eventServerId) { $criteria[] = ['eventServerId' => $eventServerId]; } if ($scriptId) { /* @var $script Script */ $script = Script::findPk($scriptId); if ($script && $this->request->hasPermissions($script)) { $scriptName = preg_replace("/[^A-Za-z0-9]+/", "_", $script->name); $criteria[] = ['scriptName' => $scriptName]; } } if ($query || $event) { $logEntity = new OrchestrationLog(); $eventEntity = new Event(); $criteria[AbstractEntity::STMT_FROM] = $criteria[AbstractEntity::STMT_FROM] . "\n LEFT JOIN {$eventEntity->table('e')}\n ON {$logEntity->columnEventId} = {$eventEntity->columnEventId('e')}\n "; if ($event && $query) { $query = $this->db->qstr('%' . $query . '%'); $criteria[AbstractEntity::STMT_WHERE] = $criteria[AbstractEntity::STMT_WHERE] . " AND (\n {$eventEntity->columnType('e')} = {$this->db->qstr($event)}\n OR ({$logEntity->columnType} LIKE {$query}\n AND {$logEntity->columnScriptName} LIKE {$query})\n )"; } else { if ($event) { $criteria[AbstractEntity::STMT_WHERE] = $criteria[AbstractEntity::STMT_WHERE] . " AND (\n {$eventEntity->columnType('e')} = {$this->db->qstr($event)}\n )"; } else { $query = $this->db->qstr('%' . $query . '%'); $criteria[AbstractEntity::STMT_WHERE] = $criteria[AbstractEntity::STMT_WHERE] . " AND (\n ({$eventEntity->columnType('e')} LIKE {$query}\n OR {$logEntity->columnType} LIKE {$query}\n OR {$logEntity->columnScriptName} LIKE {$query})\n )"; } } } if ($schedulerId) { $criteria[] = ['taskId' => $schedulerId]; } if ($byDate) { try { $tz = $this->user->getSetting(Scalr_Account_User::SETTING_UI_TIMEZONE) ?: 'UTC'; $tz = new DateTimeZone($tz); $dtS = new DateTime($byDate, $tz); $dtE = new DateTime($byDate, $tz); if ($fromTime) { $dtS = DateTime::createFromFormat('Y-m-d H:i', "{$byDate} {$fromTime}", $tz); } if ($toTime) { $dtE = DateTime::createFromFormat('Y-m-d H:i', "{$byDate} {$toTime}", $tz); } else { $dtE = $dtE->add(new DateInterval('P1D')); } if ($dtS && $dtE) { Scalr_Util_DateTime::convertTimeZone($dtS); Scalr_Util_DateTime::convertTimeZone($dtE); $criteria[] = ['added' => ['$gt' => $dtS]]; $criteria[] = ['added' => ['$lt' => $dtE]]; } } catch (Exception $e) { } } if ($status === 'success') { $criteria[] = ['execExitCode' => 0]; } else { if ($status === 'failure') { $criteria[] = ['execExitCode' => ['$ne' => 0]]; } } $logs = OrchestrationLog::find($criteria, null, Scalr\UI\Utils::convertOrder($sort, ['id' => false], ['id', 'added']), $limit, $start, true); $data = $this->prepareOrchestrationLogData($logs); $this->response->data(['data' => $data, 'total' => $logs->totalNumber]); }
function handle($queue, Scalr_Messaging_Msg $message, $rawMessage) { $dbserver = DBServer::LoadByID($message->getServerId()); $msg = ''; if ($message instanceof Scalr_Messaging_Msg_ExecScriptResult) { try { $storage = \Scalr::config('scalr.system.scripting.logs_storage'); if (!$message->executionId || $storage == 'scalr') { $msg = sprintf("STDERR: %s \n\n STDOUT: %s", base64_decode($message->stderr), base64_decode($message->stdout)); } if ($message->scriptPath) { $name = stristr($message->scriptPath, 'C:\\Windows\\TEMP\\scalr-scripting') || stristr($message->scriptPath, '/usr/local/bin/scalr-scripting') || preg_match('/fatmouse-agent\\/tasks\\/[^\\/]+\\/[^\\/]+\\/bin/', $message->scriptPath) ? $message->scriptName : $message->scriptPath; } else { $name = $message->scriptName; } $log = new OrchestrationLog(); if (strpos($message->eventName, 'Scheduler (TaskID: ') === 0) { $type = OrchestrationLog::TYPE_SCHEDULER; $log->taskId = filter_var($message->eventName, FILTER_SANITIZE_NUMBER_INT); } else { if ($message->eventName == 'Manual') { $type = OrchestrationLog::TYPE_MANUAL; } else { $type = OrchestrationLog::TYPE_EVENT; } } $log->farmId = $dbserver->farmId; $log->serverId = $message->getServerId(); $log->type = $type; $log->message = $msg; $log->added = new DateTime('now'); $log->scriptName = $name; $log->execTime = round($message->timeElapsed, 2); $log->execExitCode = $message->returnCode; $log->eventId = $message->eventId; $log->eventServerId = $message->eventServerId; $log->executionId = $message->executionId; $log->runAs = $message->runAs; $log->save(); if ($type === OrchestrationLog::TYPE_MANUAL) { $logManual = OrchestrationLogManualScript::findOne([['executionId' => $message->executionId], ['serverId' => $message->getServerId()]]); /* @var $logManual OrchestrationLogManualScript */ if ($logManual && empty($logManual->orchestrationLogId)) { $logManual->orchestrationLogId = $log->id; $logManual->save(); } } if ($message->meta[Scalr_Messaging_MsgMeta::SZR_VERSION]) { DBServer::LoadByID($message->getServerId())->setScalarizrVersion($message->meta[Scalr_Messaging_MsgMeta::SZR_VERSION]); } if ($message->eventId) { $updateTotal = ''; if ($message->returnCode == 130) { $field = 'scripts_timedout'; } elseif ($message->returnCode != 0) { $field = 'scripts_failed'; } else { $field = 'scripts_completed'; } if (stristr($name, '[Scalr built-in]')) { $updateTotal = ', `scripts_total` = `scripts_total`+1'; } $this->db->Execute("UPDATE events SET `{$field}` = `{$field}`+1 {$updateTotal} WHERE event_id = ?", [$message->eventId]); } } catch (Exception $e) { $this->logger->fatal($e->getMessage()); } } elseif ($message instanceof Scalr_Messaging_Msg_Log) { try { if ($message->meta[Scalr_Messaging_MsgMeta::SZR_VERSION]) { DBServer::LoadByID($message->getServerId())->setScalarizrVersion($message->meta[Scalr_Messaging_MsgMeta::SZR_VERSION]); } } catch (Exception $e) { } foreach ($message->entries as $entry) { if (self::$severityCodes[$entry->level] < 3) { continue; } $level = $entry->level === "WARNING" ? "warn" : strtolower($entry->level); \Scalr::getContainer()->logger($entry->name)->{$level}(new FarmLogMessage($dbserver, !empty($entry->msg) ? $entry->msg : null)); } } elseif ($message instanceof Scalr_Messaging_Msg_RebundleLog) { try { $this->db->Execute("INSERT INTO bundle_task_log SET\n bundle_task_id = ?,\n dtadded = NOW(),\n message = ?\n ", [$message->bundleTaskId, $message->message]); } catch (Exception $e) { $this->logger->error($e->getMessage()); } } }