github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/cell-helpers/tee2metron/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"os/exec"
     9  
    10  	"github.com/cloudfoundry/dropsonde"
    11  	"github.com/cloudfoundry/dropsonde/logs"
    12  )
    13  
    14  const latticeDebugStreamId = "lattice-debug"
    15  
    16  var dropsondeDestination, sourceInstance string
    17  
    18  func init() {
    19  	flag.StringVar(
    20  		&dropsondeDestination,
    21  		"dropsondeDestination",
    22  		"",
    23  		`where to stream logs to in the form of hostname:port
    24      eg. -dropsondeDestination=127.0.0.1:3457
    25      `)
    26  
    27  	flag.StringVar(
    28  		&sourceInstance,
    29  		"sourceInstance",
    30  		"",
    31  		"The label for the log source instance that shows up when consuming the stream",
    32  	)
    33  }
    34  
    35  func main() {
    36  	flag.Parse()
    37  	if dropsondeDestination == "" {
    38  		fmt.Println("dropsondeDestination flag is required")
    39  		os.Exit(1)
    40  	}
    41  	if sourceInstance == "" {
    42  		fmt.Println("sourceInstance flag is required")
    43  		os.Exit(1)
    44  	}
    45  
    46  	args := flag.Args()
    47  
    48  	if len(args) == 0 {
    49  		fmt.Println("Command not specified!")
    50  		fmt.Println("Usage: tee2metron -dropsondeDestionation=127.0.0.1:3457 -sourceInstance=cell-21 COMMAND")
    51  		os.Exit(3)
    52  	}
    53  
    54  	if err := dropsonde.Initialize(dropsondeDestination, sourceInstance, args[0]); err != nil {
    55  		panic("error initializing dropsonde" + err.Error())
    56  	}
    57  
    58  	dropsondeStdoutReader, dropsondeStdoutWriter := io.Pipe()
    59  	dropsondeStderrReader, dropsondeStderrWriter := io.Pipe()
    60  
    61  	stdoutTeeWriter := io.MultiWriter(dropsondeStdoutWriter, os.Stdout)
    62  	stderrTeeWriter := io.MultiWriter(dropsondeStderrWriter, os.Stderr)
    63  
    64  	defer dropsondeStdoutReader.Close()
    65  	defer dropsondeStderrReader.Close()
    66  	defer dropsondeStdoutWriter.Close()
    67  	defer dropsondeStderrWriter.Close()
    68  	cmd := exec.Command(args[0], args[1:]...)
    69  	cmd.Stdout = stdoutTeeWriter
    70  	cmd.Stderr = stderrTeeWriter
    71  	go logs.ScanLogStream(latticeDebugStreamId, args[0], sourceInstance, dropsondeStdoutReader)
    72  	go logs.ScanErrorLogStream(latticeDebugStreamId, args[0], sourceInstance, dropsondeStderrReader)
    73  
    74  	if err := cmd.Start(); err != nil {
    75  		fmt.Println(err)
    76  		os.Exit(3)
    77  	}
    78  
    79  	// if the child is killed abnormally we would know
    80  	if err := cmd.Wait(); err != nil {
    81  		fmt.Println(args[0], ":", err)
    82  		os.Exit(3)
    83  	}
    84  }