示例#1
0
// AcquireLocalChecks will retrieve the currently running configuration and
// return a list of all checks it knows about
func AcquireLocalChecks() {
	var jsonOut map[string][]string
	localChecks := exec.Command("/opt/sensu/embedded/bin/sensu-client", "-L", "error", "-d", "/etc/sensu/conf.d", "-P")

	out, err := localChecks.Output()
	if err != nil {
		syslogLog.WithFields(logrus.Fields{
			"check":   "sensupluginssensu",
			"client":  host,
			"version": version.AppVersion(),
			"error":   err,
		}).Error(`Local Checks returned invalid output`)
	}

	err = json.Unmarshal(out, &jsonOut)
	if err != nil {
		syslogLog.WithFields(logrus.Fields{
			"check":   "sensupluginssensu",
			"client":  host,
			"version": version.AppVersion(),
			"error":   err,
			"output":  out,
		}).Error(`Could not unmarshall the json.`)
	}

	fmt.Println(jsonOut)
	fmt.Println(jsonOut["transport"])
}
示例#2
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":   "sensupluginssensu",
			"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/.sensupluginssensu.yaml)")
	RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
	RootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "print debugging info (if any)")

}
示例#3
0
// initConfig reads in config file and ENV variables if set.
func initConfig() {
	if cfgFile != "" {
		viper.SetConfigFile(cfgFile)
	} else {
		viper.SetConfigName("sensupluginssensu")
		viper.AddConfigPath("/etc/sensuplugins/conf.d")
	}

	viper.AutomaticEnv()
	if err := viper.ReadInConfig(); err == nil {
	} else {
		syslogLog.WithFields(logrus.Fields{
			"check":   "sensupluginssensu",
			"client":  host,
			"version": version.AppVersion(),
			"error":   err,
			"cfgFile": cfgFile,
		}).Error(`Could not read in the configuration file.`)
	}
}
示例#4
0
// Configuration via Viper
var cfgFile string

// Hostname for logging
var host string

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

// Dump debugging info or not
var debug bool

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
	Use:   "sensupluginssensu",
	Short: fmt.Sprintf("A set of process checks for Sensu - (%s)", version.AppVersion()),
}

// 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)

	// Setup logging for the package. Doing it here is much eaiser than in each