/** * Store event in database * * @param integer $farmid * @param string $event_name */ public static function StoreEvent($farmid, Event $event, $eventTime = null) { if ($event->DBServer) { $eventServerId = $event->DBServer->serverId; } try { $DB = self::getDb(); // Generate event message $message = $event->getTextDetails(); //short_message temporary used for time tracking // Store event in database $DB->Execute("INSERT INTO events SET\n farmid\t= ?,\n type\t= ?,\n dtadded\t= NOW(),\n message\t= ?,\n event_object = '',\n event_id\t = ?,\n event_server_id = ?,\n short_message = ?,\n msg_expected = ?,\n msg_created = ?,\n scripts_total = ?,\n is_suspend = ?", array($farmid, $event->GetName(), $message, $event->GetEventID(), $eventServerId, $eventTime, $event->msgExpected, $event->msgCreated, $event->scriptsCount, $event->isSuspended ? 1 : 0)); } catch (Exception $e) { Logger::getLogger(__CLASS__)->fatal(sprintf(_("Cannot store event in database: %s"), $e->getMessage())); } try { if ($eventServerId) { $dbServer = DBServer::LoadByID($eventServerId); if (!$dbServer->farmRoleId) { return true; } $dt = new DateTime('now', new DateTimeZone("UTC")); $timestamp = $dt->format("D d M Y H:i:s e"); $payload = new stdClass(); $payload->eventName = $event->GetName(); $payload->eventId = $event->GetEventID(); $payload->timestamp = $timestamp; $globalVars = Scalr_Scripting_GlobalVariables::listServerGlobalVariables($dbServer, true, $event); $webhooks = WebhookConfig::findByEvent($event->GetName(), $farmid, $dbServer->clientId, $dbServer->envId); $count = 0; foreach ($webhooks as $webhook) { /* @var $webhook \Scalr\Model\Entity\WebhookConfig */ $payload->configurationId = $webhook->webhookId; $payload->data = array(); $variables = []; foreach ($globalVars as $gv) { $variables[$gv->name] = $gv->value; if ($gv->private && $webhook->skipPrivateGv == 1 && !$gv->system) { continue; } $payload->data[$gv->name] = $gv->value; } if ($webhook->postData) { //Parse variable $keys = array_keys($variables); $f = create_function('$item', 'return "{".$item."}";'); $keys = array_map($f, $keys); $values = array_values($variables); // Strip undefined variables & return value $payload->userData = preg_replace("/{[A-Za-z0-9_-]+}/", "", str_replace($keys, $values, $webhook->postData)); } else { $payload->userData = ''; } foreach ($webhook->getEndpoints() as $ce) { /* @var $ce \Scalr\Model\Entity\WebhookConfigEndpoint */ $endpoint = $ce->getEndpoint(); if (!$endpoint->isValid) { continue; } $payload->endpointId = $endpoint->endpointId; $encPayload = json_encode($payload); $history = new WebhookHistory(); $history->eventId = $event->GetEventID(); $history->eventType = $event->GetName(); $history->payload = $encPayload; $history->serverId = $event->DBServer ? $event->DBServer->serverId : null; $history->endpointId = $endpoint->endpointId; $history->webhookId = $webhook->webhookId; $history->farmId = $farmid; $history->save(); $count++; } } if ($count != 0) { $DB->Execute("UPDATE events SET wh_total = ? WHERE event_id = ?", array($count, $event->GetEventID())); } } } catch (Exception $e) { Logger::getLogger(__CLASS__)->fatal(sprintf(_("WebHooks: %s"), $e->getMessage())); } }
/** * Store event in database * * @param integer $farmid * @param string $event_name */ public static function StoreEvent($farmid, Event $event, $eventTime = null) { if ($event->DBServer) { $eventServerId = $event->DBServer->serverId; } try { $DB = self::getDb(); // Generate event message $message = $event->getTextDetails(); $eventStr = null; try { $eventStr = serialize($event); } catch (Exception $e) { } //short_message temporary used for time tracking // Store event in database $DB->Execute("INSERT INTO events SET\n farmid\t= ?,\n type\t= ?,\n dtadded\t= NOW(),\n message\t= ?,\n event_object = ?,\n event_id\t = ?,\n event_server_id = ?,\n short_message = ?,\n msg_expected = ?,\n msg_created = ?\n ", array($farmid, $event->GetName(), $message, $eventStr, $event->GetEventID(), $eventServerId, $eventTime, $event->msgExpected, $event->msgCreated)); } catch (Exception $e) { Logger::getLogger(__CLASS__)->fatal(sprintf(_("Cannot store event in database: %s"), $e->getMessage())); } try { if ($eventServerId) { $dbServer = DBServer::LoadByID($eventServerId); $dt = new DateTime('now', new DateTimeZone("UTC")); $timestamp = $dt->format("D d M Y H:i:s e"); $payload = new stdClass(); $payload->eventName = $event->GetName(); $payload->eventId = $event->GetEventID(); $payload->timestamp = $timestamp; $globalVars = Scalr_Scripting_GlobalVariables::listServerGlobalVariables($dbServer, true, $event); $webhooks = WebhookConfig::findByEvent($event->GetName(), $farmid, $dbServer->clientId, $dbServer->envId); foreach ($webhooks as $webhook) { /* @var $webhook \Scalr\Model\Entity\WebhookConfig */ $payload->data = array(); foreach ($globalVars as $gv) { if ($gv->private && $webhook->skipPrivateGv == 1 && !$gv->system) { continue; } $payload->data[$gv->name] = $gv->value; } if ($webhook->postData) { $payload->userData = $dbServer->applyGlobalVarsToValue($webhook->postData); } else { $payload->userData = ''; } $encPayload = json_encode($payload); foreach ($webhook->getEndpoints() as $ce) { /* @var $ce \Scalr\Model\Entity\WebhookConfigEndpoint */ $endpoint = $ce->getEndpoint(); if (!$endpoint->isValid) { continue; } $history = new WebhookHistory(); $history->eventId = $event->GetEventID(); $history->eventType = $event->GetName(); $history->payload = $encPayload; $history->endpointId = $endpoint->endpointId; $history->webhookId = $webhook->webhookId; $history->farmId = $farmid; $history->save(); } } } } catch (Exception $e) { Logger::getLogger(__CLASS__)->fatal(sprintf(_("WebHooks: %s"), $e->getMessage())); } }