You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
2.8 KiB
Go
138 lines
2.8 KiB
Go
// Copyright 2018 Felix Kronlage <fkr@hazardous.org>
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gridscale/gsclient-go/v2"
|
|
"github.com/jessevdk/go-flags"
|
|
"github.com/spf13/viper"
|
|
"log"
|
|
"os"
|
|
"regexp"
|
|
)
|
|
|
|
type CheckCommand struct {
|
|
Debug bool `short:"d" long:"debug" description:"Turn on debugging"`
|
|
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 {
|
|
Api string
|
|
UserId string
|
|
Token string
|
|
Client *gsclient.Client
|
|
}
|
|
|
|
func config(configFile string) {
|
|
|
|
if configFile == "" {
|
|
if !checkFileExistance( "config/check.toml") {
|
|
Unknown(fmt.Sprintf("Configuration file is missing: %s", configFile))
|
|
}
|
|
|
|
configFile = "config/check.toml"
|
|
|
|
}
|
|
|
|
viper.SetConfigFile(configFile)
|
|
|
|
err := viper.ReadInConfig()
|
|
if err != nil {
|
|
log.Fatalf("Config file not found or error parsing: %v", err)
|
|
}
|
|
if (os.Getenv("GRIDSCALE_USER") != "") && (os.Getenv("GRIDSCALE_TOKEN") != "") {
|
|
Config.UserId = os.Getenv("GRIDSCALE_USER")
|
|
Config.Token = os.Getenv("GRIDSCALE_TOKEN")
|
|
} else {
|
|
Config.UserId = viper.GetString("gridscale.userid")
|
|
Config.Token = viper.GetString("gridscale.token")
|
|
}
|
|
|
|
if viper.IsSet("gridscale.api") {
|
|
Config.Api = viper.GetString("gridscale.api")
|
|
} else {
|
|
Config.Api = "https://api.gridscale.io"
|
|
}
|
|
|
|
if Checker.Debug {
|
|
viper.Debug()
|
|
}
|
|
|
|
config := gsclient.NewConfiguration(
|
|
Config.Api,
|
|
Config.UserId,
|
|
Config.Token,
|
|
Checker.Debug, true, 120, 500,100)
|
|
Config.Client = gsclient.NewClient(config)
|
|
}
|
|
|
|
func main() {
|
|
|
|
var parser = flags.NewParser(&Checker, flags.Default)
|
|
_, err := parser.Parse();
|
|
if 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 removeNewline(s string) string {
|
|
re := regexp.MustCompile(` +\r?\n +`)
|
|
r := re.ReplaceAllString(s, " ")
|
|
re = regexp.MustCompile(`\s+`)
|
|
return re.ReplaceAllString(r, " ")
|
|
}
|
|
|
|
func PrintMsg(msg string) {
|
|
if msg != "" {
|
|
fmt.Println(" - " + removeNewline(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
|
|
}
|