コード例 #1
0
ファイル: awslogtail.go プロジェクト: ninchat/awslogtail
func findEC2Instances(sess *session.Session, filter []string, output chan<- stream) (err error) {
	instances, err := ec2.New(sess).DescribeInstances(&ec2.DescribeInstancesInput{
		MaxResults: pointer.Int64(1000),
	})
	if err != nil {
		return
	}

	go func() {
		defer close(output)

		for _, r := range instances.Reservations {
			for _, i := range r.Instances {
				if len(filter) > 0 {
					var name string

					for _, t := range i.Tags {
						if *t.Key == "Name" {
							name = *t.Value
							break
						}
					}

					var match bool

					for _, s := range filter {
						if strings.HasPrefix(name, s) {
							match = true
							break
						}
					}

					if !match {
						continue
					}
				}

				output <- stream{
					name: *i.InstanceId,
					live: *i.State.Code != 48,
				}
			}
		}
	}()

	return
}
コード例 #2
0
ファイル: awslogtail.go プロジェクト: ninchat/awslogtail
func load(logService *cloudwatchlogs.CloudWatchLogs, initial chan<- string, follow chan<- string, limit int, startTime time.Time, endTime time.Time, instanceId string, terminated bool) {
	initialParams := &cloudwatchlogs.GetLogEventsInput{
		LogGroupName:  pointer.String(logGroupName),
		LogStreamName: &instanceId,
	}

	if endTime.IsZero() {
		if !startTime.IsZero() {
			initialParams.StartFromHead = pointer.Bool(true)
			initialParams.StartTime = pointer.Int64(startTime.UnixNano() / int64(time.Millisecond))
		} else {
			initialParams.EndTime = pointer.Int64(time.Now().UnixNano() / int64(time.Millisecond))
		}

		initialParams.Limit = pointer.Int64(int64(limit))
	} else {
		initialParams.StartFromHead = pointer.Bool(true)
		initialParams.StartTime = pointer.Int64(startTime.UnixNano() / int64(time.Millisecond))
		initialParams.EndTime = pointer.Int64(endTime.UnixNano() / int64(time.Millisecond))
	}

	logEvents, err := logService.GetLogEvents(initialParams)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", instanceId, err)
		initial <- ""
		return
	}

	for _, e := range logEvents.Events {
		if *e.Message != "" {
			initial <- formatMessage(e)
		}
	}

	initial <- ""

	if follow == nil || terminated {
		return
	}

	token := logEvents.NextForwardToken

	for {
		logEvents, err := logService.GetLogEvents(&cloudwatchlogs.GetLogEventsInput{
			LogGroupName:  pointer.String(logGroupName),
			LogStreamName: &instanceId,
			NextToken:     token,
			StartFromHead: pointer.Bool(true),
		})
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", instanceId, err)
			continue
		}

		for _, e := range logEvents.Events {
			follow <- formatMessage(e)
		}

		token = logEvents.NextForwardToken

		time.Sleep(pollInterval)
	}
}