Esempio n. 1
0
func init() {
	cobra.OnInitialize(initConfig)

	// Setup logging for the package. Doing it here is much eaiser than in each
	// binary. If you want to overwrite it in a specific binary then feel free.
	hook, err := logrus_syslog.NewSyslogHook("", "", syslog.LOG_INFO, "")
	if err != nil {
		panic(err)
	}
	syslogLog.Hooks.Add(hook)
	syslogLog.Formatter = new(logrus.JSONFormatter)

	// Set the hostname for use in logging within the package. Doing it here is
	// cleaner than in each binary but if you want to use some other method just
	// override the variable in the specific binary.
	host, err = os.Hostname()
	if err != nil {
		syslogLog.WithFields(logrus.Fields{
			"check":   "sensupluginsslack",
			"client":  "unknown",
			"version": version.AppVersion(),
			"error":   err,
		}).Error(`Could not determine the hostname of this machine as reported by the kernel.`)
		sensuutil.Exit("GENERALGOLANGERROR")
	}

	RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is /etc/sensuplugins/conf.d/.sensupluginschrony.yaml)")
	RootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "print debugging info")
	RootCmd.PersistentFlags().StringVarP(&slackToken, "token", "", "", "the slack api token")
	RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

}
Esempio n. 2
0
func initConfig() {
	if cfgFile != "" {
		viper.SetConfigFile(cfgFile)
	} else {
		viper.SetConfigName("sensupluginsslack")
		viper.AddConfigPath("/etc/sensuplugins/conf.d")
	}

	if slackToken == "" {
		viper.AutomaticEnv()
		if err := viper.ReadInConfig(); err == nil {
		} else {
			syslogLog.WithFields(logrus.Fields{
				"check":   "sensupluginsslack",
				"client":  host,
				"version": version.AppVersion(),
				"error":   err,
				"cfgFile": cfgFile,
			}).Error(`Could not read in the configuration file.`)
		}
	}
}
Esempio n. 3
0
var handlerSlackCmd = &cobra.Command{
	Use:   "handlerSlack --token <token> --channel <slack channel>",
	Short: "Post Sensu check results to a slack channel",
	Long: `Read in the Sensu check result and condense the output and post it
	 as a Slack attachment to a given channel`,
	Run: func(sensupluginsslack *cobra.Command, args []string) {

		// Bring in the environmant details
		sensuEnv := new(sensuhandler.EnvDetails)
		sensuEnv = sensuEnv.SetSensuEnv()

		if slackToken == "" {
			syslogLog.WithFields(logrus.Fields{
				"check":      "sensupluginsslack",
				"client":     host,
				"version":    version.AppVersion(),
				"slackToken": slackToken,
			}).Error(`Please enter a valid slack token`)
			sensuutil.Exit("RUNTIMEERROR")
		}
		// read in the event data from the sensu server
		sensuEvent := new(sensuhandler.SensuEvent)
		sensuEvent = sensuEvent.AcquireSensuEvent()

		// This is done with an api token not an incoming webhook to a specific channel
		api := slack.New(slackToken)
		params := slack.PostMessageParameters{}
		// Build an attachment message for sending to the specified slack channel
		attachment := slack.Attachment{
			Color: sensuhandler.SetColor(sensuEvent.Check.Status),
Esempio n. 4
0
// Configuration via Viper
var cfgFile string

// Hostname for logging
var host string

// Create a logging instance.
var syslogLog = logrus.New()

// print debug info
var debug bool

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
	Use:   "sensupluginsslack",
	Short: fmt.Sprintf("A slack handler for Sensu - (%s)", version.AppVersion()),
	Long:  `Generate meaningful slack attachments from Senu events`,
}

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
	if err := RootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(-1)
	}
}

func init() {
	cobra.OnInitialize(initConfig)