Beispiel #1
0
func (o *HttpOutput) Init(config interface{}) (err error) {
	o.HttpOutputConfig = config.(*HttpOutputConfig)
	if o.url, err = url.Parse(o.Address); err != nil {
		return fmt.Errorf("Can't parse URL '%s': %s", o.Address, err.Error())
	}
	if o.url.Scheme != "http" && o.url.Scheme != "https" {
		return errors.New("`address` must contain an absolute http or https URL.")
	}
	o.Method = strings.ToUpper(o.Method)
	if o.Method != "POST" && o.Method != "GET" && o.Method != "PUT" {
		return errors.New("HTTP Method must be POST, GET, or PUT.")
	}
	if o.Method != "GET" {
		o.sendBody = true
	}
	o.client = new(http.Client)
	if o.HttpTimeout > 0 {
		o.client.Timeout = time.Duration(o.HttpTimeout) * time.Millisecond
	}
	if o.Username != "" || o.Password != "" {
		o.useBasicAuth = true
	}
	if o.url.Scheme == "https" {
		transport := &http.Transport{}
		if transport.TLSClientConfig, err = tcp.CreateGoTlsConfig(&o.Tls); err != nil {
			return fmt.Errorf("TLS init error: %s", err.Error())
		}
		o.client.Transport = transport
	}
	return
}
Beispiel #2
0
func (ao *AMQPOutput) Init(config interface{}) (err error) {
	conf := config.(*AMQPOutputConfig)
	ao.config = conf
	var tlsConf *tls.Config = nil
	if strings.HasPrefix(conf.URL, "amqps://") && &ao.config.Tls != nil {
		if tlsConf, err = tcp.CreateGoTlsConfig(&ao.config.Tls); err != nil {
			return fmt.Errorf("TLS init error: %s", err)
		}
	}

	var dialer = NewAMQPDialer(tlsConf)
	if ao.amqpHub == nil {
		ao.amqpHub = GetAmqpHub()
	}
	ch, usageWg, connectionWg, err := ao.amqpHub.GetChannel(conf.URL, dialer)
	if err != nil {
		return
	}
	ao.connWg = connectionWg
	ao.usageWg = usageWg
	closeChan := make(chan *amqp.Error)
	ao.closeChan = ch.NotifyClose(closeChan)
	err = ch.ExchangeDeclare(conf.Exchange, conf.ExchangeType,
		conf.ExchangeDurability, conf.ExchangeAutoDelete, false, false,
		nil)
	if err != nil {
		usageWg.Done()
		return
	}
	ao.ch = ch
	return
}
Beispiel #3
0
func (output *NsqOutput) SetNsqConfig(conf *NsqOutputConfig) (err error) {
	nsqConfig := nsq.NewConfig()
	if conf.ReadTimeout != nil {
		nsqConfig.ReadTimeout = time.Duration(*conf.ReadTimeout) * time.Millisecond
	}
	if conf.WriteTimeout != nil {
		nsqConfig.WriteTimeout = time.Duration(*conf.WriteTimeout) * time.Millisecond
	}
	if conf.ClientID != nil {
		nsqConfig.ClientID = *conf.ClientID
	}
	if conf.Hostname != nil {
		nsqConfig.Hostname = *conf.Hostname
	}
	if conf.UserAgent != nil {
		nsqConfig.UserAgent = *conf.UserAgent
	}
	if conf.HeartbeatInterval != nil {
		nsqConfig.HeartbeatInterval = time.Duration(*conf.HeartbeatInterval) * time.Millisecond
	}
	if conf.Deflate != nil {
		nsqConfig.Deflate = *conf.Deflate
	}
	if conf.DeflateLevel != nil {
		nsqConfig.DeflateLevel = *conf.DeflateLevel
	}
	if conf.Snappy != nil {
		nsqConfig.Snappy = *conf.Snappy
	}
	if conf.OutputBufferSize != nil {
		nsqConfig.OutputBufferSize = *conf.OutputBufferSize
	}
	if conf.OutputBufferTimeout != nil {
		nsqConfig.OutputBufferTimeout = time.Duration(*conf.OutputBufferTimeout) * time.Millisecond
	}
	if conf.MaxInFlight != nil {
		nsqConfig.MaxInFlight = *conf.MaxInFlight
	}
	if conf.MaxBackoffDuration != nil {
		nsqConfig.MaxBackoffDuration = time.Duration(*conf.MaxBackoffDuration) * time.Millisecond
	}
	if conf.MsgTimeout != nil {
		nsqConfig.MsgTimeout = time.Duration(*conf.MsgTimeout) * time.Millisecond
	}
	if conf.AuthSecret != nil {
		nsqConfig.AuthSecret = *conf.AuthSecret
	}

	// Tls
	if conf.UseTls {
		tls, err := tcp.CreateGoTlsConfig(&conf.Tls)
		if err != nil {
			return err
		}
		output.config.TlsConfig = tls
	}
	output.config = nsqConfig

	return
}
Beispiel #4
0
func (n *NagiosOutput) Init(config interface{}) (err error) {
	n.conf = config.(*NagiosOutputConfig)

	n.submitter = n.submitSendNsca
	if n.conf.SendNscaBin == "" {
		// HTTP is implied.
		n.submitter = n.submitHttp

		rht := time.Duration(n.conf.ResponseHeaderTimeout) * time.Second
		transport := &http.Transport{
			Proxy: http.ProxyFromEnvironment,
			ResponseHeaderTimeout: rht,
		}
		if n.conf.UseTls {
			var tlsConf *tls.Config
			if tlsConf, err = tcp.CreateGoTlsConfig(&n.conf.Tls); err != nil {
				return fmt.Errorf("TLS init error: %s", err)
			}
			transport.TLSClientConfig = tlsConf
		}
		n.client = &http.Client{
			Transport: transport,
		}
	}
	return
}
Beispiel #5
0
func (o *ElasticSearchOutput) Init(config interface{}) (err error) {
	o.conf = config.(*ElasticSearchOutputConfig)

	o.batchChan = make(chan ESBatch)
	o.backChan = make(chan []byte, 2)
	o.recvChan = make(chan MsgPack, 100)

	var serverUrl *url.URL
	if serverUrl, err = url.Parse(o.conf.Server); err == nil {
		var scheme string = strings.ToLower(serverUrl.Scheme)
		switch scheme {
		case "http", "https":
			var tlsConf *tls.Config = nil
			if scheme == "https" && &o.conf.Tls != nil {
				if tlsConf, err = tcp.CreateGoTlsConfig(&o.conf.Tls); err != nil {
					return fmt.Errorf("TLS init error: %s", err)
				}
			}

			o.bulkIndexer = NewHttpBulkIndexer(scheme, serverUrl.Host, serverUrl.Path,
				o.conf.FlushCount, o.conf.Username, o.conf.Password, o.conf.HTTPTimeout,
				o.conf.HTTPDisableKeepalives, o.conf.ConnectTimeout, tlsConf)
		case "udp":
			o.bulkIndexer = NewUDPBulkIndexer(serverUrl.Host, o.conf.FlushCount)
		default:
			err = errors.New("Server URL must specify one of `udp`, `http`, or `https`.")
		}
	} else {
		err = fmt.Errorf("Unable to parse ElasticSearch server URL [%s]: %s", o.conf.Server, err)
	}
	return
}
Beispiel #6
0
func (ai *AMQPInput) Init(config interface{}) (err error) {
	conf := config.(*AMQPInputConfig)
	ai.config = conf
	var tlsConf *tls.Config = nil
	if strings.HasPrefix(conf.URL, "amqps://") && &ai.config.Tls != nil {
		if tlsConf, err = tcp.CreateGoTlsConfig(&ai.config.Tls); err != nil {
			return fmt.Errorf("TLS init error: %s", err)
		}
	}

	if ai.amqpHub == nil {
		ai.amqpHub = getAmqpHub()
	}
	var dialer = AMQPDialer{tlsConf}
	ch, usageWg, connWg, err := ai.amqpHub.GetChannel(conf.URL, dialer)
	if err != nil {
		return
	}

	var args amqp.Table

	ttl := conf.QueueTTL

	if ttl != -1 {
		args = amqp.Table{"x-message-ttl": int32(ttl)}
	}

	defer func() {
		if err != nil {
			usageWg.Done()
		}
	}()
	ai.connWg = connWg
	ai.usageWg = usageWg
	err = ch.ExchangeDeclare(conf.Exchange, conf.ExchangeType,
		conf.ExchangeDurability, conf.ExchangeAutoDelete, false, false,
		nil)
	if err != nil {
		return
	}
	ai.ch = ch
	_, err = ch.QueueDeclare(conf.Queue, conf.QueueDurability,
		conf.QueueAutoDelete, conf.QueueExclusive, false, args)
	if err != nil {
		return
	}
	err = ch.QueueBind(conf.Queue, conf.RoutingKey, conf.Exchange, false, nil)
	if err != nil {
		return
	}
	err = ch.Qos(conf.PrefetchCount, 0, false)
	if err != nil {
		return
	}
	return
}
func (t *TcpInput) setupTls(tomlConf *tcp.TlsConfig) (err error) {
	if tomlConf.CertFile == "" || tomlConf.KeyFile == "" {
		return errors.New("TLS config requires both cert_file and key_file value.")
	}
	var goConf *tls.Config
	if goConf, err = tcp.CreateGoTlsConfig(tomlConf); err == nil {
		t.listener = tls.NewListener(t.listener, goConf)
	}
	return
}
Beispiel #8
0
func createSender(test FloodTest) (sender *client.NetworkSender, err error) {
	if test.UseTls {
		var goTlsConfig *tls.Config
		goTlsConfig, err = tcp.CreateGoTlsConfig(&test.Tls)
		if err != nil {
			return
		}
		sender, err = client.NewTlsSender(test.Sender, test.IpAddress, goTlsConfig)
	} else {
		sender, err = client.NewNetworkSender(test.Sender, test.IpAddress)
	}
	return
}
Beispiel #9
0
func (o *ElasticSearchOutput) Init(config interface{}) (err error) {
	o.conf = config.(*ElasticSearchOutputConfig)

	if !o.conf.UseBuffering {
		o.batchChan = make(chan ESBatch)
		o.backChan = make(chan []byte, 2)
	}

	o.outputExit = make(chan error)

	var serverUrl *url.URL
	if serverUrl, err = url.Parse(o.conf.Server); err == nil {
		var scheme string = strings.ToLower(serverUrl.Scheme)
		switch scheme {
		case "http", "https":
			var tlsConf *tls.Config = nil
			if scheme == "https" && &o.conf.Tls != nil {
				if tlsConf, err = tcp.CreateGoTlsConfig(&o.conf.Tls); err != nil {
					return fmt.Errorf("TLS init error: %s", err)
				}
			}

			o.bulkIndexer = NewHttpBulkIndexer(scheme, serverUrl.Host,
				o.conf.FlushCount, o.conf.Username, o.conf.Password, o.conf.HTTPTimeout,
				o.conf.HTTPDisableKeepalives, o.conf.ConnectTimeout, tlsConf)
		case "udp":
			o.bulkIndexer = NewUDPBulkIndexer(serverUrl.Host, o.conf.FlushCount)
		default:
			err = errors.New("Server URL must specify one of `udp`, `http`, or `https`.")
		}
	} else {
		err = fmt.Errorf("Unable to parse ElasticSearch server URL [%s]: %s", o.conf.Server, err)
	}
	switch o.conf.QueueFullAction {
	case "shutdown", "drop", "block":
	default:
		return fmt.Errorf("`queue_full_action` must be 'shutdown', 'drop', or 'block', got %s",
			o.conf.QueueFullAction)
	}
	return
}
Beispiel #10
0
// NewIrcConn creates an *irc.Connection. It handles using Heka's tcp plugin to
// create a cryto/tls config
func NewIrcConn(config *IrcOutputConfig) (IrcConnection, error) {
	conn := irc.IRC(config.Nick, config.Ident)
	if conn == nil {
		return nil, errors.New("Nick or Ident cannot be blank")
	}
	if config.Server == "" {
		return nil, errors.New("Irc server cannot be blank.")
	}
	if len(config.Channels) < 1 {
		return nil, errors.New("Need at least 1 channel to join.")
	}
	var tlsConf *tls.Config = nil
	var err error = nil
	if tlsConf, err = tcp.CreateGoTlsConfig(&config.Tls); err != nil {
		return nil, fmt.Errorf("TLS init error: %s", err)
	}
	conn.UseTLS = config.UseTLS
	conn.TLSConfig = tlsConf
	conn.Password = config.Password
	conn.Timeout = time.Duration(config.Timeout) * time.Second
	conn.VerboseCallbackHandler = config.VerboseIRCLogging
	return conn, nil
}
Beispiel #11
0
func main() {
	configFile := flag.String("config", "flood.toml", "Heka Flood configuration file")
	configTest := flag.String("test", "default", "Test section to load")

	flag.Parse()

	if flag.NFlag() == 0 {
		flag.PrintDefaults()
		os.Exit(0)
	}

	var config FloodConfig
	if _, err := toml.DecodeFile(*configFile, &config); err != nil {
		client.LogError.Printf("Error decoding config file: %s", err)
		return
	}
	var test FloodTest
	var ok bool
	if test, ok = config[*configTest]; !ok {
		client.LogError.Printf("Configuration test: '%s' was not found", *configTest)
		return
	}

	if test.MsgInterval != "" {
		var err error
		if test.msgInterval, err = time.ParseDuration(test.MsgInterval); err != nil {
			client.LogError.Printf("Invalid message_interval duration %s: %s", test.MsgInterval,
				err.Error())
			return
		}
	}

	if test.PprofFile != "" {
		profFile, err := os.Create(test.PprofFile)
		if err != nil {
			client.LogError.Fatalln(err)
		}
		pprof.StartCPUProfile(profFile)
		defer pprof.StopCPUProfile()
	}

	if test.MaxMessageSize > 0 {
		message.SetMaxMessageSize(test.MaxMessageSize)
	}

	var sender *client.NetworkSender
	var err error
	if test.UseTls {
		var goTlsConfig *tls.Config
		goTlsConfig, err = tcp.CreateGoTlsConfig(&test.Tls)
		if err != nil {
			client.LogError.Fatalf("Error creating TLS config: %s\n", err)
		}
		sender, err = client.NewTlsSender(test.Sender, test.IpAddress, goTlsConfig)
	} else {
		sender, err = client.NewNetworkSender(test.Sender, test.IpAddress)
	}
	if err != nil {
		client.LogError.Fatalf("Error creating sender: %s\n", err)
	}

	unsignedEncoder := client.NewProtobufEncoder(nil)
	signedEncoder := client.NewProtobufEncoder(&test.Signer)
	oversizedEncoder := &OversizedEncoder{}

	var numTestMessages = 1
	var unsignedMessages [][]byte
	var signedMessages [][]byte
	var oversizedMessages [][]byte

	rdm := &randomDataMaker{
		src:       rand.NewSource(time.Now().UnixNano()),
		asciiOnly: test.AsciiOnly,
	}

	if test.VariableSizeMessages {
		numTestMessages = 64
		unsignedMessages = makeVariableMessage(unsignedEncoder, numTestMessages, rdm, false)
		signedMessages = makeVariableMessage(signedEncoder, numTestMessages, rdm, false)
		oversizedMessages = makeVariableMessage(oversizedEncoder, 1, rdm, true)
	} else {
		if test.StaticMessageSize == 0 {
			test.StaticMessageSize = 1000
		}
		unsignedMessages = makeFixedMessage(unsignedEncoder, test.StaticMessageSize,
			rdm)
		signedMessages = makeFixedMessage(signedEncoder, test.StaticMessageSize,
			rdm)
	}
	// wait for sigint
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGINT)
	var msgsSent, bytesSent, msgsDelivered uint64
	var corruptPercentage, lastCorruptPercentage, signedPercentage, lastSignedPercentage, oversizedPercentage, lastOversizedPercentage float64
	var corrupt bool

	// set up counter loop
	ticker := time.NewTicker(time.Duration(time.Second))
	go timerLoop(&msgsSent, &bytesSent, ticker)

	test.CorruptPercentage /= 100.0
	test.SignedPercentage /= 100.0
	test.OversizedPercentage /= 100.0

	var buf []byte
	for gotsigint := false; !gotsigint; {
		runtime.Gosched()
		select {
		case <-sigChan:
			gotsigint = true
			continue
		default:
		}
		msgId := rand.Int() % numTestMessages
		corruptPercentage = math.Floor(float64(msgsSent) * test.CorruptPercentage)
		if corruptPercentage != lastCorruptPercentage {
			lastCorruptPercentage = corruptPercentage
			corrupt = true
		} else {
			corrupt = false
		}
		signedPercentage = math.Floor(float64(msgsSent) * test.SignedPercentage)
		if signedPercentage != lastSignedPercentage {
			lastSignedPercentage = signedPercentage
			buf = signedMessages[msgId]
		} else {
			oversizedPercentage = math.Floor(float64(msgsSent) * test.OversizedPercentage)
			if oversizedPercentage != lastOversizedPercentage {
				lastOversizedPercentage = oversizedPercentage
				buf = oversizedMessages[0]
			} else {
				buf = unsignedMessages[msgId]
			}
		}
		bytesSent += uint64(len(buf))
		if err = sendMessage(sender, buf, corrupt); err != nil {
			client.LogError.Printf("Error sending message: %s\n", err.Error())
		} else {
			msgsDelivered++
		}
		msgsSent++
		if test.NumMessages != 0 && msgsSent >= test.NumMessages {
			break
		}
		if test.msgInterval != 0 {
			time.Sleep(test.msgInterval)
		}
	}
	sender.Close()
	client.LogInfo.Println("Clean shutdown: ", msgsSent, " messages sent; ",
		msgsDelivered, " messages delivered.")
}
Beispiel #12
0
func main() {
	configFile := flag.String("config", "sbmgr.toml", "Sandbox manager configuration file")
	scriptFile := flag.String("script", "xyz.lua", "Sandbox script file")
	scriptConfig := flag.String("scriptconfig", "xyz.toml", "Sandbox script configuration file")
	filterName := flag.String("filtername", "filter", "Sandbox filter name (used on unload)")
	action := flag.String("action", "load", "Sandbox manager action")
	flag.Parse()

	var config SbmgrConfig
	if _, err := toml.DecodeFile(*configFile, &config); err != nil {
		client.LogError.Printf("Error decoding config file: %s", err)
		return
	}
	var sender *client.NetworkSender
	var err error
	if config.UseTls {
		var goTlsConfig *tls.Config
		goTlsConfig, err = tcp.CreateGoTlsConfig(&config.Tls)
		if err != nil {
			client.LogError.Fatalf("Error creating TLS config: %s\n", err)
		}
		sender, err = client.NewTlsSender("tcp", config.IpAddress, goTlsConfig)
	} else {
		sender, err = client.NewNetworkSender("tcp", config.IpAddress)
	}
	if err != nil {
		client.LogError.Fatalf("Error creating sender: %s\n", err.Error())
	}
	encoder := client.NewProtobufEncoder(&config.Signer)
	manager := client.NewClient(sender, encoder)

	hostname, _ := os.Hostname()
	msg := &message.Message{}
	msg.SetLogger(pipeline.HEKA_DAEMON) // identify the message as 'internal' for filtering purposes
	msg.SetType("heka.control.sandbox")
	msg.SetTimestamp(time.Now().UnixNano())
	msg.SetUuid(uuid.NewRandom())
	msg.SetHostname(hostname)

	switch *action {
	case "load":
		code, err := ioutil.ReadFile(*scriptFile)
		if err != nil {
			client.LogError.Printf("Error reading scriptFile: %s\n", err.Error())
			return
		}
		msg.SetPayload(string(code))
		conf, err := ioutil.ReadFile(*scriptConfig)
		if err != nil {
			client.LogError.Printf("Error reading scriptConfig: %s\n", err.Error())
			return
		}
		f, _ := message.NewField("config", string(conf), "toml")
		msg.AddField(f)
	case "unload":
		f, _ := message.NewField("name", *filterName, "")
		msg.AddField(f)
	default:
		client.LogError.Printf("Invalid action: %s", *action)
	}

	f1, _ := message.NewField("action", *action, "")
	msg.AddField(f1)
	err = manager.SendMessage(msg)
	if err != nil {
		client.LogError.Printf("Error sending message: %s\n", err.Error())
	}
}
Beispiel #13
0
func (input *NsqInput) SetNsqConfig(conf *NsqInputConfig) (err error) {
	nsqConfig := nsq.NewConfig()

	if conf.LookupdPollInterval != nil {
		nsqConfig.LookupdPollInterval = time.Duration(*conf.LookupdPollInterval) * time.Millisecond
	}
	if conf.LookupdPollJitter != nil {
		nsqConfig.LookupdPollJitter = *conf.LookupdPollJitter
	}
	if conf.MaxRequeueDelay != nil {
		nsqConfig.MaxRequeueDelay = time.Duration(*conf.MaxRequeueDelay) * time.Millisecond
	}
	if conf.DefaultRequeueDelay != nil {
		nsqConfig.DefaultRequeueDelay = time.Duration(*conf.DefaultRequeueDelay) * time.Millisecond
	}
	if conf.BackoffMultiplier != nil {
		nsqConfig.BackoffMultiplier = time.Duration(*conf.BackoffMultiplier) * time.Millisecond
	}
	if conf.MaxAttempts != nil {
		nsqConfig.MaxAttempts = *conf.MaxAttempts
	}
	if conf.LowRdyIdleTimeout != nil {
		nsqConfig.LowRdyIdleTimeout = time.Duration(*conf.LowRdyIdleTimeout) * time.Millisecond
	}
	if conf.SampleRate != nil {
		nsqConfig.SampleRate = *conf.SampleRate
	}

	if conf.ReadTimeout != nil {
		nsqConfig.ReadTimeout = time.Duration(*conf.ReadTimeout) * time.Millisecond
	}
	if conf.WriteTimeout != nil {
		nsqConfig.WriteTimeout = time.Duration(*conf.WriteTimeout) * time.Millisecond
	}
	if conf.ClientID != nil {
		nsqConfig.ClientID = *conf.ClientID
	}
	if conf.Hostname != nil {
		nsqConfig.Hostname = *conf.Hostname
	}
	if conf.UserAgent != nil {
		nsqConfig.UserAgent = *conf.UserAgent
	}
	if conf.HeartbeatInterval != nil {
		nsqConfig.HeartbeatInterval = time.Duration(*conf.HeartbeatInterval) * time.Millisecond
	}
	if conf.Deflate != nil {
		nsqConfig.Deflate = *conf.Deflate
	}
	if conf.DeflateLevel != nil {
		nsqConfig.DeflateLevel = *conf.DeflateLevel
	}
	if conf.Snappy != nil {
		nsqConfig.Snappy = *conf.Snappy
	}
	if conf.OutputBufferSize != nil {
		nsqConfig.OutputBufferSize = *conf.OutputBufferSize
	}
	if conf.OutputBufferTimeout != nil {
		nsqConfig.OutputBufferTimeout = time.Duration(*conf.OutputBufferTimeout) * time.Millisecond
	}
	if conf.MaxInFlight != nil {
		nsqConfig.MaxInFlight = *conf.MaxInFlight
	}
	if conf.MaxBackoffDuration != nil {
		nsqConfig.MaxBackoffDuration = time.Duration(*conf.MaxBackoffDuration) * time.Millisecond
	}
	if conf.MsgTimeout != nil {
		nsqConfig.MsgTimeout = time.Duration(*conf.MsgTimeout) * time.Millisecond
	}
	if conf.AuthSecret != nil {
		nsqConfig.AuthSecret = *conf.AuthSecret
	}

	// Tls
	if conf.UseTls {
		tls, err := tcp.CreateGoTlsConfig(&conf.Tls)
		if err != nil {
			return err
		}
		input.config.TlsConfig = tls
	}
	input.config = nsqConfig

	return
}
Beispiel #14
0
func main() {
	configFile := flag.String("config", "sbmgrload.toml", "Sandbox manager load configuration file")
	action := flag.String("action", "load", "load/unload")
	numItems := flag.Int("num", 1, "Number of sandboxes to load/unload")
	flag.Parse()

	code := `
require "string"
require "table"
require "os"

lastTime = os.time() * 1e9
lastCount = 0
count = 0
rate = 0.0
rates = {}

function process_message ()
    count = count + 1
    return 0
end

function timer_event(ns)
    local msgsSent = count - lastCount
    if msgsSent == 0 then return end

    local elapsedTime = ns - lastTime
    if elapsedTime == 0 then return end

    lastCount = count
    lastTime = ns
    rate = msgsSent / (elapsedTime / 1e9)
    rates[#rates+1] = rate
    output(string.format("Got %d messages. %0.2f msg/sec", count, rate))
    inject_message()

    local samples = #rates
    if samples == 10 then -- generate a summary every 10 samples
        table.sort(rates)
	     local min = rates[1]
	     local max = rates[samples]
	     local sum = 0
        for i, val in ipairs(rates) do
            sum = sum + val
	     end
        output(string.format("AGG Sum. Min: %0.2f Max: %0.2f Mean: %0.2f", min, max, sum/samples))
        inject_message()
	     rates = {}
    end
end
`
	confFmt := `
[CounterSandbox%d]
type = "SandboxFilter"
message_matcher = "Type == 'hekabench'"
ticker_interval = 1
script_type = "lua"
filename = ""
preserve_data = true
`

	var config SbmgrConfig
	if _, err := toml.DecodeFile(*configFile, &config); err != nil {
		log.Printf("Error decoding config file: %s", err)
		return
	}
	var sender *client.NetworkSender
	var err error
	if config.UseTls {
		var goTlsConfig *tls.Config
		goTlsConfig, err = tcp.CreateGoTlsConfig(&config.Tls)
		if err != nil {
			log.Fatalf("Error creating TLS config: %s\n", err)
		}
		sender, err = client.NewTlsSender("tcp", config.IpAddress, goTlsConfig)
	} else {
		sender, err = client.NewNetworkSender("tcp", config.IpAddress)
	}
	if err != nil {
		log.Fatalf("Error creating sender: %s\n", err.Error())
	}
	encoder := client.NewProtobufEncoder(&config.Signer)
	manager := client.NewClient(sender, encoder)
	hostname, _ := os.Hostname()

	switch *action {
	case "load":
		for i := 0; i < *numItems; i++ {
			conf := fmt.Sprintf(confFmt, i)
			msg := &message.Message{}
			msg.SetType("heka.control.sandbox")
			msg.SetTimestamp(time.Now().UnixNano())
			msg.SetUuid(uuid.NewRandom())
			msg.SetHostname(hostname)
			msg.SetPayload(code)
			f, _ := message.NewField("config", conf, "toml")
			msg.AddField(f)
			f1, _ := message.NewField("action", *action, "")
			msg.AddField(f1)
			err = manager.SendMessage(msg)
			if err != nil {
				log.Printf("Error sending message: %s\n", err.Error())
			}
		}
	case "unload":
		for i := 0; i < *numItems; i++ {
			msg := &message.Message{}
			msg.SetType("heka.control.sandbox")
			msg.SetTimestamp(time.Now().UnixNano())
			msg.SetUuid(uuid.NewRandom())
			msg.SetHostname(hostname)
			f, _ := message.NewField("name", fmt.Sprintf("CounterSandbox%d", i), "")
			msg.AddField(f)
			f1, _ := message.NewField("action", *action, "")
			msg.AddField(f1)
			err = manager.SendMessage(msg)
			if err != nil {
				log.Printf("Error sending message: %s\n", err.Error())
			}
		}

	default:
		log.Printf("Invalid action: %s\n", *action)
	}

}
func (o *InfluxDBOutput) Init(config interface{}) error {
	conf := config.(*InfluxDBOutputConfig)

	serverUrl, err := url.Parse(conf.Server)
	if err != nil {
		return fmt.Errorf("Unable to parse InfluxDB server URL (%s): %s", conf.Server, err)
	}

	switch strings.ToLower(serverUrl.Scheme) {
	case "http", "https":
		serverUrl.Path = path.Join(serverUrl.Path, "db", url.QueryEscape(conf.Database), "series")

		vals := serverUrl.Query()
		vals.Set("time_precision", "u") // microseconds
		if conf.Username != "" {
			vals.Set("u", conf.Username)
		}
		if conf.Password != "" {
			vals.Set("p", conf.Password)
		}
		serverUrl.RawQuery = vals.Encode()
	default:
		return fmt.Errorf("Unable to handle server URL scheme %r", serverUrl.Scheme)
	}
	o.seriesUrl = serverUrl.String()

	transport := &http.Transport{
		Proxy: http.ProxyFromEnvironment,
		ResponseHeaderTimeout: time.Duration(conf.ResponseHeaderTimeout) * time.Second,
	}
	if serverUrl.Scheme == "https" {
		tlsConf, err := hekaTcp.CreateGoTlsConfig(&conf.Tls)
		if err != nil {
			return fmt.Errorf("Error settings TLS configuration: %s", err)
		}
		transport.TLSClientConfig = tlsConf
	}
	o.httpClient = &http.Client{Transport: transport}

	if conf.Series == "" && conf.FieldMap["series"] == "" {
		return fmt.Errorf("Must set series or field_map.series")
	}
	o.series = conf.Series

	if conf.UseHekaTimestamp {
		_, hasTime := conf.FieldMap["time"]
		if !hasTime {
			o.columns = []string{"time"}
			o.columnFields = []string{"_hekaTimestampMicro"}
		}
	}

	for column, field := range conf.FieldMap {
		if column == "series" {
			o.seriesField = field
			continue
		}
		o.columns = append(o.columns, column)
		o.columnFields = append(o.columnFields, field)
	}

	o.flushInterval = time.Duration(conf.FlushInterval) * time.Millisecond
	o.flushCount = conf.FlushCount

	return nil
}