示例#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":   "sensupluginses",
			"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 $HOME/.sensupluginses.yaml)")
	RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
		}

		client := &http.Client{Transport: tr}

		req, _ := http.NewRequest("GET", "https://"+apiHost+":4567/stashes", nil)
		req.SetBasicAuth(apiUser, apiPassword)
		resp, _ := client.Do(req)
		if resp.StatusCode == 200 {
			bodyBytes, _ = ioutil.ReadAll(resp.Body)
			bodyString = string(bodyBytes)
		}
		defer resp.Body.Close()

		if strings.Contains(bodyString, "\"expire\":-1") {

			sensuutil.Exit("critical", bodyString)
		} else {
			sensuutil.Exit("ok")
		}

	},
}

func init() {
	RootCmd.AddCommand(checkSensuSilencesCmd)

	checkSensuSilencesCmd.Flags().StringVar(&apiUser, "user", "", "Username for the sensu api")
	checkSensuSilencesCmd.Flags().StringVar(&apiPassword, "password", "", "Password for the sensu api")
	checkSensuSilencesCmd.Flags().StringVar(&apiHost, "host", "", "Sensu Api host")
	checkSensuSilencesCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
	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),

			Fields: []slack.AttachmentField{
				slack.AttachmentField{
					Title: "Monitored Instance",