StackFormation\Helper\Finder::findCloudWatchLogGroupByStream PHP Метод

findCloudWatchLogGroupByStream() публичный статический Метод

public static findCloudWatchLogGroupByStream ( $stream, $logGroupNamePrefix = null )
    public static function findCloudWatchLogGroupByStream($stream, $logGroupNamePrefix = null)
    {
        return null;
        // TODO: FIx this!
        // TODO: refactor this to use \AwsInspector\Model\CloudWatchLogs\Repository
        $cloudWatchLogClient = \AwsInspector\SdkFactory::getClient('CloudWatchLogs');
        /* @var $cloudWatchLogClient \Aws\CloudWatchLogs\CloudWatchLogsClient */
        $groupsNextToken = null;
        do {
            $params = [];
            if ($logGroupNamePrefix) {
                $params['logGroupNamePrefix'] = $logGroupNamePrefix;
            }
            if ($groupsNextToken) {
                $params['nextToken'] = $groupsNextToken;
            }
            $resGroups = $cloudWatchLogClient->describeLogGroups($params);
            foreach ($resGroups->search('logGroups[].logGroupName') as $logGroupName) {
                $streamsNextToken = null;
                do {
                    $streamsParams = ['logGroupName' => $logGroupName, 'orderBy' => 'LastEventTime'];
                    if ($streamsNextToken) {
                        $streamsParams['nextToken'] = $streamsNextToken;
                    }
                    $resStreams = $cloudWatchLogClient->describeLogStreams($streamsParams);
                    foreach ($resStreams->search('logStreams[].logStreamName') as $logStreamName) {
                        if ($stream == $logStreamName) {
                            return $logGroupName;
                        }
                    }
                    $streamsNextToken = $resStreams->get("nextToken");
                } while ($streamsNextToken);
            }
            $groupsNextToken = $resGroups->get("nextToken");
        } while ($groupsNextToken);
        return null;
    }

Usage Example

Пример #1
0
 /**
  * @param $resourceStatusReason
  * @return array
  * @throws \Exception
  */
 public function getDetailedLogFromResourceStatusReason($resourceStatusReason)
 {
     $logMessages = [];
     if (preg_match('/See the details in CloudWatch Log Stream: (.*)/', $resourceStatusReason, $matches)) {
         $logStream = $matches[1];
         $logGroupName = Finder::findCloudWatchLogGroupByStream($logStream);
         if (empty($logGroupName)) {
             throw new \Exception('Could not find logGroupName for logStream: ' . $logStream);
         }
         $params = ['limit' => 20, 'logGroupName' => $logGroupName, 'logStreamName' => $logStream];
         $cloudWatchLogClient = \AwsInspector\SdkFactory::getClient('CloudWatchLogs');
         /* @var $cloudWatchLogClient \Aws\CloudWatchLogs\CloudWatchLogsClient */
         $res = $cloudWatchLogClient->getLogEvents($params);
         $logMessages = array_merge(["==> Showing last 20 messages from {$logGroupName} -> {$logStream}"], $res->search('events[].message'));
     } elseif (preg_match('/WaitCondition received failed message:.*for uniqueId: (i-[0-9a-f]+)/', $resourceStatusReason, $matches)) {
         $instanceId = $matches[1];
         $ec2Repo = new \AwsInspector\Model\Ec2\Repository();
         $instance = $ec2Repo->findEc2InstanceBy('instance-id', $instanceId);
         if ($instance) {
             try {
                 $res = $instance->exec('tail -50 /var/log/cloud-init-output.log');
                 $logMessages = array_merge(["==> Showing last 50 lines in /var/log/cloud-init-output.log"], $res['output']);
             } catch (FileNotFoundException $e) {
                 $logMessages = ["Could not log in to instance '{$instanceId}' because the pem file could not be found"];
             }
         } else {
             $logMessages = ["Could not find instance '{$instanceId}'"];
         }
     }
     return $logMessages;
 }