Example #1
0
// 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)
	sensuCmd.Flags().StringVar(&host, "host", "localhost", "host where sensu client is running")
	sensuCmd.Flags().StringVar(&port, "port", "3030", "port of the sensu client")
Example #2
0
)

var pipe string

// icingaCmd respresents the icinga command
var icingaCmd = &cobra.Command{
	Use:   "icinga",
	Short: "convert and send trap to icinga",
	Run: func(cmd *cobra.Command, args []string) {
		// read the trap
		t := trap.NewTrap()
		// icinga
		i := icinga.NewIcinga(t)
		// connect to icinga client
		if stat, err := os.Stat(pipe); os.IsNotExist(err) || stat.Mode()&os.ModeNamedPipe == 0 {
			logger.Fatal(fmt.Sprintf("%s does not exist or is not a named pipe\n", pipe))
		}
		f, err := os.OpenFile(pipe, os.O_RDWR, 0666)
		if err != nil {
			logger.Fatal(err.Error())
		}
		defer f.Close()
		// send event
		if err := i.Send(f); err != nil {
			w, _ := logger.New(logger.Severity, logger.Prefix)
			t.Send(w)
			logger.Fatal(err.Error())
		}
	},
}