func main() {
	optRegion := flag.String("region", "", "AWS Region")
	optAccessKeyId := flag.String("access-key-id", "", "AWS Access Key ID")
	optSecretAccessKey := flag.String("secret-access-key", "", "AWS Secret Access Key")
	optIdentifier := flag.String("identifier", "", "DB Instance Identifier")
	optTempfile := flag.String("tempfile", "", "Temp file name")
	flag.Parse()

	var rds RDSPlugin

	if *optRegion == "" {
		rds.Region = aws.InstanceRegion()
	} else {
		rds.Region = *optRegion
	}

	rds.Identifier = *optIdentifier
	rds.AccessKeyId = *optAccessKeyId
	rds.SecretAccessKey = *optSecretAccessKey

	helper := mp.NewMackerelPlugin(rds)
	if *optTempfile != "" {
		helper.Tempfile = *optTempfile
	} else {
		helper.Tempfile = "/tmp/mackerel-plugin-rds"
	}

	if os.Getenv("MACKEREL_AGENT_PLUGIN_META") != "" {
		helper.OutputDefinitions()
	} else {
		helper.OutputValues()
	}
}
func main() {
	optRegion := flag.String("region", "", "AWS Region")
	optInstanceID := flag.String("instance-id", "", "Instance ID")
	optAccessKeyID := flag.String("access-key-id", "", "AWS Access Key ID")
	optSecretAccessKey := flag.String("secret-access-key", "", "AWS Secret Access Key")
	optTempfile := flag.String("tempfile", "", "Temp file name")
	flag.Parse()

	var cpucredit CPUCreditPlugin

	if *optRegion == "" || *optInstanceID == "" {
		cpucredit.Region = aws.InstanceRegion()
		cpucredit.InstanceID = aws.InstanceId()
	} else {
		cpucredit.Region = *optRegion
		cpucredit.InstanceID = *optInstanceID
	}

	cpucredit.AccessKeyID = *optAccessKeyID
	cpucredit.SecretAccessKey = *optSecretAccessKey

	helper := mp.NewMackerelPlugin(cpucredit)
	if *optTempfile != "" {
		helper.Tempfile = *optTempfile
	} else {
		helper.Tempfile = "/tmp/mackerel-plugin-cpucredit"
	}

	if os.Getenv("MACKEREL_AGENT_PLUGIN_META") != "" {
		helper.OutputDefinitions()
	} else {
		helper.OutputValues()
	}
}
Exemple #3
0
func s3Setup(bucketName string, path string, opts sequinsOptions) *sequins {
	auth, err := aws.GetAuth(*s3AccessKey, *s3SecretKey, "", time.Time{})
	if err != nil {
		log.Fatal(err)
	}

	regionName := *s3Region
	if regionName == "" {
		regionName = aws.InstanceRegion()
		if regionName == "" {
			log.Fatal("Unspecified --s3-region, and no instance region found.")
		}
	}

	region, exists := aws.Regions[regionName]
	if !exists {
		log.Fatalf("Invalid AWS region: %s", regionName)
	}

	bucket := s3.New(auth, region).Bucket(bucketName)
	backend := backend.NewS3Backend(bucket, path)
	if opts.LocalPath == "" {
		tmpDir, err := ioutil.TempDir("", "sequins-")
		if err != nil {
			log.Fatal(err)
		}

		opts.LocalPath = tmpDir
	}

	return newSequins(backend, opts)
}
Exemple #4
0
// InitializeAWS sets up access to the AWS Simple Token Service
func InitializeAWS() error {
	if *awsRegion == "" {
		*awsRegion = aws.InstanceRegion()
		if *awsRegion == "unknown" {
			*awsRegion = "us-east-1"
		}
	}

	if *awsAccessKey == "" || *awsSecretKey == "" {
		return fmt.Errorf("you must specify aws-access-key-id and " +
			"aws-secret-access-key in the config file or " +
			"AWSAUTHD_AWS_ACCESS_KEY_ID and AWSAUTHD_AWS_SECRET_ACCESS_KEY in " +
			"the environment. These must be regular permanent credentials, not " +
			"temporary or instance credentials.")
	}

	maybeAWSAuth := aws.Auth{
		AccessKey: *awsAccessKey,
		SecretKey: *awsSecretKey,
	}
	stsConnection := sts.New(maybeAWSAuth, aws.GetRegion(*awsRegion))
	_, err := stsConnection.GetFederationToken("snakeoil", "", 900)
	if err != nil {
		return fmt.Errorf("Your credentials don't work to call "+
			"GetFederationToken(). You must specify aws-access-key-id and "+
			"aws-secret-access-key in the config file or "+
			"AWSAUTHD_AWS_ACCESS_KEY_ID and AWSAUTHD_AWS_SECRET_ACCESS_KEY in "+
			"the environment. These must be regular permanent credentials, not "+
			"temporary or instance credentials. (err=%s)", err)
	}

	// If GetFederationToken worked then we are good to go.
	awsAuth = maybeAWSAuth
	return nil
}
// Do the plugin
func Do() {
	optRegion := flag.String("region", "", "AWS Region")
	optAccessKeyID := flag.String("access-key-id", "", "AWS Access Key ID")
	optSecretAccessKey := flag.String("secret-access-key", "", "AWS Secret Access Key")
	optIdentifier := flag.String("identifier", "", "DB Instance Identifier")
	optEngine := flag.String("engine", "", "RDS Engine")
	optPrefix := flag.String("metric-key-prefix", "rds", "Metric key prefix")
	optLabelPrefix := flag.String("metric-label-prefix", "", "Metric Label prefix")
	optTempfile := flag.String("tempfile", "", "Temp file name")
	flag.Parse()

	rds := RDSPlugin{
		Prefix: *optPrefix,
	}
	if *optLabelPrefix == "" {
		if *optPrefix == "rds" {
			rds.LabelPrefix = "RDS"
		} else {
			rds.LabelPrefix = strings.Title(*optPrefix)
		}
	} else {
		rds.LabelPrefix = *optLabelPrefix
	}

	if *optRegion == "" {
		rds.Region = aws.InstanceRegion()
	} else {
		rds.Region = *optRegion
	}

	rds.Identifier = *optIdentifier
	rds.AccessKeyID = *optAccessKeyID
	rds.SecretAccessKey = *optSecretAccessKey
	rds.Engine = *optEngine

	helper := mp.NewMackerelPlugin(rds)
	if *optTempfile != "" {
		helper.Tempfile = *optTempfile
	} else {
		helper.Tempfile = "/tmp/mackerel-plugin-rds"
	}

	if os.Getenv("MACKEREL_AGENT_PLUGIN_META") != "" {
		helper.OutputDefinitions()
	} else {
		helper.OutputValues()
	}
}
func main() {
	optRegion := flag.String("region", "", "AWS Region")
	optAccessKeyID := flag.String("access-key-id", "", "AWS Access Key ID")
	optSecretAccessKey := flag.String("secret-access-key", "", "AWS Secret Access Key")
	optCacheClusterID := flag.String("cache-cluster-id", "", "Cache Cluster Id")
	optCacheNodeID := flag.String("cache-node-id", "0001", "Cache Node Id")
	optElastiCacheType := flag.String("elasticache-type", "", "ElastiCache type")
	optTempfile := flag.String("tempfile", "", "Temp file name")
	flag.Parse()

	var ecache ECachePlugin

	if *optRegion == "" {
		ecache.Region = aws.InstanceRegion()
	} else {
		ecache.Region = *optRegion
	}

	ecache.AccessKeyID = *optAccessKeyID
	ecache.SecretAccessKey = *optSecretAccessKey
	ecache.CacheClusterID = *optCacheClusterID
	ecache.CacheNodeID = *optCacheNodeID
	ecache.ElastiCacheType = *optElastiCacheType
	switch ecache.ElastiCacheType {
	case "memcached":
		ecache.CacheMetrics = metricsdefMemcached
	case "redis":
		ecache.CacheMetrics = metricsdefRedis
	default:
		log.Printf("elasticache-type is 'memcached' or 'redis'.")
		os.Exit(1)
	}

	helper := mp.NewMackerelPlugin(ecache)
	if *optTempfile != "" {
		helper.Tempfile = *optTempfile
	} else {
		helper.Tempfile = fmt.Sprintf("/tmp/mackerel-plugin-aws-elasticache-%s-%s", *optCacheClusterID, *optCacheNodeID)
	}

	if os.Getenv("MACKEREL_AGENT_PLUGIN_META") != "" {
		helper.OutputDefinitions()
	} else {
		helper.OutputValues()
	}
}
func main() {
	optRegion := flag.String("region", "", "AWS Region")
	optAccessKeyID := flag.String("access-key-id", "", "AWS Access Key ID")
	optSecretAccessKey := flag.String("secret-access-key", "", "AWS Secret Access Key")
	optClientID := flag.String("client-id", "", "AWS Client ID")
	optDomain := flag.String("domain", "", "ES domain name")
	optTempfile := flag.String("tempfile", "", "Temp file name")
	flag.Parse()

	var es ESPlugin

	if *optRegion == "" {
		es.Region = aws.InstanceRegion()
	} else {
		es.Region = *optRegion
	}

	es.Domain = *optDomain
	es.ClientID = *optClientID
	es.AccessKeyID = *optAccessKeyID
	es.SecretAccessKey = *optSecretAccessKey

	err := es.prepare()
	if err != nil {
		log.Fatalln(err)
	}

	helper := mp.NewMackerelPlugin(es)
	if *optTempfile != "" {
		helper.Tempfile = *optTempfile
	} else {
		helper.Tempfile = "/tmp/mackerel-plugin-aws-elasticsearch"
	}

	if os.Getenv("MACKEREL_AGENT_PLUGIN_META") != "" {
		helper.OutputDefinitions()
	} else {
		helper.OutputValues()
	}
}
// Do the plugin
func Do() {
	optRegion := flag.String("region", "", "AWS Region")
	optLbname := flag.String("lbname", "", "ELB Name")
	optAccessKeyID := flag.String("access-key-id", "", "AWS Access Key ID")
	optSecretAccessKey := flag.String("secret-access-key", "", "AWS Secret Access Key")
	optTempfile := flag.String("tempfile", "", "Temp file name")
	flag.Parse()

	var elb ELBPlugin

	if *optRegion == "" {
		elb.Region = aws.InstanceRegion()
	} else {
		elb.Region = *optRegion
	}

	elb.AccessKeyID = *optAccessKeyID
	elb.SecretAccessKey = *optSecretAccessKey
	elb.Lbname = *optLbname

	err := elb.prepare()
	if err != nil {
		log.Fatalln(err)
	}

	helper := mp.NewMackerelPlugin(elb)
	if *optTempfile != "" {
		helper.Tempfile = *optTempfile
	} else {
		helper.Tempfile = "/tmp/mackerel-plugin-elb"
	}

	if os.Getenv("MACKEREL_AGENT_PLUGIN_META") != "" {
		helper.OutputDefinitions()
	} else {
		helper.OutputValues()
	}
}
func init() {
	regionname := aws.InstanceRegion()
	region = aws.Regions[regionname]
}
Exemple #10
0
func Main() error {
	log = os.Stderr
	if !*verbose {
		log = ioutil.Discard
	}

	if *mountPoint == "" {
		*mountPoint = "/ebs/" + *volumeID
	}

	instanceID = oldaws.InstanceId()
	if instanceID == "unknown" {
		return fmt.Errorf("cannot determine AWS instance ID. not running in EC2?")
	}
	region := oldaws.InstanceRegion()
	ec2Conn := ec2.New(&aws.Config{Region: region})

	if *unmountOnly {
		err1 := Unmount()
		err2 := Detach(ec2Conn)
		if err1 != nil {
			return err1
		}
		return err2
	}

	var err error

	linuxDeviceName := ""
	awsDeviceName := ""
	for i := 'a'; true; i++ {
		awsDeviceName = fmt.Sprintf("/dev/sd%c", i)
		linuxDeviceName = fmt.Sprintf("/dev/xvd%c", i)
		_, err = os.Stat(linuxDeviceName)
		if err != nil && os.IsNotExist(err) {
			fmt.Fprintf(log, "found device %s\n", linuxDeviceName)
			break
		}
		if err != nil {
			fmt.Fprintf(log, "%s: %s\n", linuxDeviceName, err)
		}
		if i == 'z' {
			return fmt.Errorf("Cannot locate an available device to mount")
		}
	}

	fmt.Fprintf(log, "attaching %s to %s\n", *volumeID, awsDeviceName)
	_, err = ec2Conn.AttachVolume(&ec2.AttachVolumeInput{
		Device:     &awsDeviceName,
		InstanceID: &instanceID,
		VolumeID:   volumeID,
	})
	if err != nil {
		return fmt.Errorf("failed to attach %s at %s: %s",
			*volumeID, awsDeviceName, err)
	}
	defer func() {
		fmt.Fprintf(log, "detaching %s from %s\n", *volumeID, awsDeviceName)
		if !*mountOnly || err != nil {
			if cleanupErr := Detach(ec2Conn); cleanupErr != nil {
				fmt.Fprintf(os.Stderr, "failed to detach %s: %s\n", *volumeID, cleanupErr)
			}
		}
	}()

	// Wait for the volume to attach
	for i := time.Duration(0); i < *attachTimeout; i += time.Second {
		_, err = os.Stat(linuxDeviceName)
		if err == nil {
			break
		}
		if !os.IsNotExist(err) {
			break
		}
		time.Sleep(time.Second)
	}
	if err != nil {
		return fmt.Errorf("failed to attach %s: %s\n", linuxDeviceName, err)
	}

	// Use blkid to tell if we need to create a filesystem
	_, err = exec.Command("blkid", linuxDeviceName).Output()
	if err != nil && err.Error() == "exit status 2" {
		// blkid told us we have no filesystem, so create one
		fmt.Fprintf(log, "Creating filesystem on %s\n", *volumeID)
		err = exec.Command(fmt.Sprintf("mkfs.%s", *fsType), linuxDeviceName).Run()
		if err != nil {
			return err
		}
	}

	// Mount the file system
	fmt.Fprintf(log, "mounting %s on %s\n", linuxDeviceName, *mountPoint)
	os.MkdirAll(*mountPoint, 0777)
	err = exec.Command("mount", linuxDeviceName, *mountPoint).Run()
	if err != nil {
		return fmt.Errorf("cannot mount %s: %s", *volumeID, err)
	}
	defer func() {
		if !*mountOnly || err != nil {
			fmt.Fprintf(log, "unmounting %s from %s\n", linuxDeviceName, *mountPoint)
			if cleanupErr := Unmount(); cleanupErr != nil {
				fmt.Fprintf(os.Stderr, "failed to unmount %s: %s\n", *volumeID, cleanupErr)
			}
		}
	}()

	if *mountOnly {
		return nil
	}

	// Invoke the command
	fmt.Fprintf(log, "invoking %s %#v\n", flag.Arg(0), flag.Args()[1:])
	cmd := exec.Command(flag.Arg(0), flag.Args()[1:]...)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	// Catch shutdown signals and make sure we cleanup
	signalCh := make(chan os.Signal, 1)
	signal.Notify(signalCh, os.Interrupt, os.Kill)

	// Run the command. When finished, close the signalCh to wake up the main
	// "thread".
	var cmdErr error
	go func() {
		cmdErr = cmd.Run()
		close(signalCh)
	}()

	// Wait for the command to stop or a signal to arrive
	_ = <-signalCh

	return cmdErr
}
Exemple #11
0
func main() {
	flag.Parse()

	dockerClient, err := docker.NewClient(*dockerEndpoint)
	if err != nil {
		fmt.Printf("cannot connect to docker: %s\n", err)
		os.Exit(1)
	}

	// Wait for the container to start
	// TODO(ross): this could be done with the events interface perhaps?
	for {
		_, err := dockerClient.InspectContainer(*containerID)
		if err == nil {
			break
		}
		if _, ok := err.(*docker.NoSuchContainer); ok {
			time.Sleep(time.Second)
			continue
		}
		fmt.Printf("docker: %s: %s\n", *containerID, err)
		os.Exit(1)
	}

	instanceID := aws.InstanceId()
	if instanceID == "unknown" {
		fmt.Printf("cannot determine AWS instance ID. not running in EC2?\n")
		os.Exit(1)
	}

	awsAuth, err := aws.GetAuth("", "", "", time.Time{})
	if err != nil {
		fmt.Printf("cannot get AWS auth: %s\n", err)
		os.Exit(1)
	}

	elbConn := elb.New(awsAuth, aws.GetRegion(aws.InstanceRegion()))
	_, err = elbConn.RegisterInstancesWithLoadBalancer(
		[]string{instanceID},
		*loadBalancerName)
	if err != nil {
		fmt.Printf("cannot register instance: %s\n", err)
		os.Exit(1)
	}
	fmt.Printf("registered instance %s from ELB %s\n", instanceID,
		*loadBalancerName)

	// Wait for the container to exit
	_, err = dockerClient.WaitContainer(*containerID)
	if err != nil {
		fmt.Printf("docker: %s: waiting: %s\n", *containerID, err)
	} else {
		fmt.Printf("docker: %s exited\n", *containerID)
	}

	_, err = elbConn.DeregisterInstancesFromLoadBalancer(
		[]string{instanceID}, *loadBalancerName)
	if err != nil {
		fmt.Printf("cannot unregister instance: %s\n", err)
		os.Exit(1)
	}
	fmt.Printf("unregisterd instance %s from ELB %s\n",
		instanceID, *loadBalancerName)
}