2020-08-11 03:48:40 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-08-15 17:16:52 -08:00
|
|
|
"encoding/json"
|
2020-08-11 03:48:40 -08:00
|
|
|
"fmt"
|
2020-12-26 11:44:53 -09:00
|
|
|
"flag"
|
2020-08-11 03:48:40 -08:00
|
|
|
"os"
|
2020-12-26 11:44:53 -09:00
|
|
|
"strings"
|
2021-01-01 09:26:49 -09:00
|
|
|
"alrm/config"
|
2020-08-11 03:48:40 -08:00
|
|
|
)
|
|
|
|
|
2020-08-12 18:16:15 -08:00
|
|
|
func main() {
|
2021-01-01 09:26:49 -09:00
|
|
|
cfgPath := flag.String("c", "", "path to configuration file")
|
|
|
|
debuglvl := flag.Int("d", 0, "debug level")
|
2020-12-26 11:44:53 -09:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2021-01-01 09:26:49 -09:00
|
|
|
if *cfgPath == "" {
|
2020-12-26 11:44:53 -09:00
|
|
|
if _, err := os.Stat("./alrmrc"); err == nil {
|
2021-01-01 09:26:49 -09:00
|
|
|
*cfgPath = "./alrmrc"
|
2020-12-26 11:44:53 -09:00
|
|
|
}
|
|
|
|
if _, err := os.Stat("/etc/alrmrc"); err == nil {
|
2021-01-01 09:26:49 -09:00
|
|
|
*cfgPath = "/etc/alrmrc"
|
2020-12-26 11:44:53 -09:00
|
|
|
}
|
2021-01-01 09:26:49 -09:00
|
|
|
if *cfgPath == "" {
|
2020-12-26 11:44:53 -09:00
|
|
|
fmt.Fprintf(os.Stderr, "Cannot find configuration\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-08-11 03:48:40 -08:00
|
|
|
}
|
|
|
|
|
2020-12-26 11:44:53 -09:00
|
|
|
command := strings.ToLower(flag.Arg(0))
|
|
|
|
switch command {
|
|
|
|
case "json":
|
2021-01-01 09:26:49 -09:00
|
|
|
cfg, err := config.ReadConfig(*cfgPath, *debuglvl)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
o, err := json.MarshalIndent(cfg, "", " ")
|
2020-12-26 11:44:53 -09:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "JSON error: %s\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-01-01 09:26:49 -09:00
|
|
|
fmt.Fprintf(os.Stdout, "%s\n", string(o))
|
2020-08-11 03:48:40 -08:00
|
|
|
|
2021-01-01 15:43:28 -09:00
|
|
|
case "check", "config", "":
|
2021-01-01 09:26:49 -09:00
|
|
|
_, err := config.ReadConfig(*cfgPath, *debuglvl)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-12-26 11:44:53 -09:00
|
|
|
fmt.Fprintf(os.Stdout, "Config is OK.\n")
|
2021-01-01 15:43:28 -09:00
|
|
|
|
|
|
|
case "test":
|
|
|
|
cfg, err := config.ReadConfig(*cfgPath, *debuglvl)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
tn := flag.Arg(1)
|
|
|
|
if tn == "" {
|
|
|
|
fmt.Fprintf(os.Stderr, "test requires a host or group\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
group, exists := cfg.Groups[tn]
|
|
|
|
if !exists {
|
|
|
|
fmt.Fprintf(os.Stderr, "group or host is not defined\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = group.Check()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Check failed: %s\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(os.Stdout, "Check successful\n")
|
2020-12-26 11:44:53 -09:00
|
|
|
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", command)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-08-11 03:48:40 -08:00
|
|
|
}
|