You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
gridscale-check/main.go

108 lines
2.0 KiB

// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
package main
import (
"fmt"
"github.com/jessevdk/go-flags"
"github.com/spf13/viper"
"log"
"os"
)
type CheckCommand struct {
ConfigFile string `short:"f" long:"config-file" description:"Name of config file" value-name:"FILE"`
Servers ServersCommand `command:"servers" description:"checks on servers"`
Snapshots SnapshotsCommand `command:"snapshots" description:"checks on snapshots"`
Storages StoragesCommand `command:"storages" description:"checks on storages"`
}
var Checker CheckCommand
var Config CheckConfig
type CheckConfig struct {
UserId string
Token string
}
func config(configFile string) {
if configFile == "" {
return
}
viper.SetConfigFile(configFile)
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Config file not found or error parsing: %v", err)
}
Config.UserId = viper.GetString("gridscale.userid")
Config.Token = viper.GetString("gridscale.token")
}
func main() {
var parser = flags.NewParser(&Checker, flags.Default)
if (os.Getenv("GRIDSCALE_USER") != "") && (os.Getenv("GRIDSCALE_TOKEN") != "") {
Config.UserId = os.Getenv("GRIDSCALE_USER")
Config.Token = os.Getenv("GRIDSCALE_TOKEN")
} else {
if checkFileExistance("config/check.toml") {
config("config/check.toml")
}
}
if _, err := parser.Parse(); err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
os.Exit(1)
}
}
Unknown("no subcommands specificed.")
}
func PrintMsg(msg string) {
if msg != "" {
fmt.Println(" - " + msg)
}
}
func Ok(msg string) {
fmt.Print("OK")
PrintMsg(msg)
os.Exit(0)
}
func Warning(msg string) {
fmt.Print("WARNING")
PrintMsg(msg)
os.Exit(1)
}
func Critical(msg string) {
fmt.Print("CRITICAL")
PrintMsg(msg)
os.Exit(2)
}
func Unknown(msg string) {
fmt.Print("UNKNOWN")
PrintMsg(msg)
os.Exit(3)
}
func checkFileExistance(fileName string) bool {
if _, err := os.Stat(fileName); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}