Example #1
0
package cmd

import (
	"github.com/marcsauter/trapforwarder/logger"
	"github.com/marcsauter/trapforwarder/trap"
	"github.com/spf13/cobra"
)

var prefix, severity string

// logCmd respresents the log command
var logCmd = &cobra.Command{
	Use:   "log",
	Short: "write the trap syslog",
	Run: func(cmd *cobra.Command, args []string) {
		// read the trap
		t := trap.NewTrap()
		// new logger
		w, _ := logger.New(logger.Priority(severity), prefix)
		// send raw event
		t.Send(w)
	},
}

func init() {
	RootCmd.AddCommand(logCmd)
	logCmd.Flags().StringVar(&prefix, "prefix", logger.Prefix, "log prefix")
	// TODO(marc): check severity
	logCmd.Flags().StringVarP(&severity, "severity", "s", "info", "log severity")
}
Example #2
0
var host, port string

// sensuCmd respresents the sensu command
var sensuCmd = &cobra.Command{
	Use:   "sensu",
	Short: "converted and send trap to sensu",
	Run: func(cmd *cobra.Command, args []string) {
		// read the trap
		t := trap.NewTrap()
		// sensu
		s := sensu.NewSensu(t)
		// connect to sensu client
		c, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%s", host, port), time.Second*connectTimeout)
		if err != nil {
			w, _ := logger.New(logger.Severity, logger.Prefix)
			t.Send(w)
			logger.Fatal(err.Error())
		}
		c.SetWriteDeadline(time.Now().Add(time.Second * writeTimeout))
		// send event
		if err := s.Send(c); err != nil {
			w, _ := logger.New(logger.Severity, logger.Prefix)
			t.Send(w)
			logger.Fatal(err.Error())
		}
	},
}

func init() {
	RootCmd.AddCommand(sensuCmd)