2020-08-11 03:48:40 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-08-15 17:16:52 -08:00
|
|
|
"encoding/json"
|
2020-12-26 11:44:53 -09:00
|
|
|
"flag"
|
2021-01-11 06:28:14 -09:00
|
|
|
"fmt"
|
2021-02-03 05:59:31 -09:00
|
|
|
"git.binarythought.com/cdramey/alrm/config"
|
2021-02-14 09:26:56 -09:00
|
|
|
"git.binarythought.com/cdramey/alrm/server"
|
2020-08-11 03:48:40 -08:00
|
|
|
"os"
|
2020-12-26 11:44:53 -09:00
|
|
|
"strings"
|
2020-08-11 03:48:40 -08:00
|
|
|
)
|
|
|
|
|
2020-08-12 18:16:15 -08:00
|
|
|
func main() {
|
2021-02-14 09:26:56 -09:00
|
|
|
cfgpath := flag.String("c", "", "path to configuration file")
|
2021-01-01 09:26:49 -09:00
|
|
|
debuglvl := flag.Int("d", 0, "debug level")
|
2020-12-26 11:44:53 -09:00
|
|
|
|
2021-01-11 06:28:14 -09:00
|
|
|
flag.Usage = printUsage
|
2020-12-26 11:44:53 -09:00
|
|
|
flag.Parse()
|
|
|
|
|
2021-02-14 09:26:56 -09:00
|
|
|
if *cfgpath == "" {
|
2021-01-20 18:49:32 -09:00
|
|
|
searchpaths := []string{"/etc/alrmrc", "./alrmrc"}
|
|
|
|
for _, sp := range searchpaths {
|
|
|
|
if _, err := os.Stat(sp); err == nil {
|
2021-02-14 09:26:56 -09:00
|
|
|
*cfgpath = sp
|
2021-01-20 18:49:32 -09:00
|
|
|
break
|
|
|
|
}
|
2020-12-26 11:44:53 -09:00
|
|
|
}
|
2021-02-14 09:26:56 -09:00
|
|
|
if *cfgpath == "" {
|
2021-01-11 06:28:14 -09:00
|
|
|
fmt.Fprintf(os.Stderr, "cannot find configuration\n")
|
2020-12-26 11:44:53 -09:00
|
|
|
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 {
|
2021-01-11 06:28:14 -09:00
|
|
|
case "config":
|
2021-01-20 18:49:32 -09:00
|
|
|
if *debuglvl > 0 {
|
2021-02-14 09:26:56 -09:00
|
|
|
fmt.Printf("checking config %s .. \n", *cfgpath)
|
2021-01-20 18:49:32 -09:00
|
|
|
}
|
|
|
|
|
2021-02-14 09:26:56 -09:00
|
|
|
cfg, err := config.ReadConfig(*cfgpath, *debuglvl)
|
2021-01-11 06:28:14 -09:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-01-01 09:26:49 -09:00
|
|
|
|
2021-01-11 06:28:14 -09:00
|
|
|
if *debuglvl > 0 {
|
2021-01-01 09:26:49 -09:00
|
|
|
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-11 06:28:14 -09:00
|
|
|
fmt.Printf("%s\n", string(o))
|
|
|
|
}
|
2020-08-11 03:48:40 -08:00
|
|
|
|
2021-01-11 06:28:14 -09:00
|
|
|
fmt.Printf("config is OK\n")
|
2021-01-01 15:43:28 -09:00
|
|
|
|
2021-01-18 12:47:03 -09:00
|
|
|
case "alarm":
|
|
|
|
an := flag.Arg(1)
|
|
|
|
if an == "" {
|
|
|
|
fmt.Fprintf(os.Stderr, "alarm name required\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-02-14 09:26:56 -09:00
|
|
|
cfg, err := config.ReadConfig(*cfgpath, 0)
|
2021-01-18 12:47:03 -09:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
al, exists := cfg.Alarms[an]
|
|
|
|
if !exists {
|
|
|
|
fmt.Fprintf(os.Stderr, "group or host is not defined\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-01-20 18:49:32 -09:00
|
|
|
err = al.Alarm(
|
|
|
|
"test group", "test host", "test check",
|
|
|
|
fmt.Errorf("test alarm message"),
|
|
|
|
)
|
2021-01-18 12:47:03 -09:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "alarm failed: %s\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Printf("alarm sounded successfully\n")
|
|
|
|
|
2021-01-11 06:28:14 -09:00
|
|
|
case "check":
|
2021-01-18 12:47:03 -09:00
|
|
|
cn := flag.Arg(1)
|
|
|
|
if cn == "" {
|
|
|
|
fmt.Fprintf(os.Stderr, "check host or group name required\n")
|
2021-01-11 06:28:14 -09:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-01-02 06:29:12 -09:00
|
|
|
|
2021-02-14 09:26:56 -09:00
|
|
|
cfg, err := config.ReadConfig(*cfgpath, 0)
|
2021-01-11 06:28:14 -09:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-01-01 15:43:28 -09:00
|
|
|
|
2021-01-18 12:47:03 -09:00
|
|
|
gr, exists := cfg.Groups[cn]
|
2021-01-11 06:28:14 -09:00
|
|
|
if !exists {
|
|
|
|
fmt.Fprintf(os.Stderr, "group or host is not defined\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-12-26 11:44:53 -09:00
|
|
|
|
2021-01-18 12:47:03 -09:00
|
|
|
err = gr.Check(*debuglvl)
|
2021-01-11 06:28:14 -09:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "check failed: %s\n", err.Error())
|
2020-12-26 11:44:53 -09:00
|
|
|
os.Exit(1)
|
2021-01-11 06:28:14 -09:00
|
|
|
}
|
|
|
|
fmt.Printf("check successful\n")
|
|
|
|
|
2021-01-28 20:14:16 -09:00
|
|
|
case "server":
|
2021-02-14 09:26:56 -09:00
|
|
|
cfg, err := config.ReadConfig(*cfgpath, *debuglvl)
|
2021-01-28 20:14:16 -09:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-02-14 09:26:56 -09:00
|
|
|
srv := server.NewServer(cfg, *debuglvl)
|
|
|
|
srv.Start()
|
2021-01-28 20:14:16 -09:00
|
|
|
|
2021-01-18 12:47:03 -09:00
|
|
|
case "help", "":
|
2021-01-11 06:28:14 -09:00
|
|
|
printUsage()
|
|
|
|
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(os.Stderr, "unknown command: %s\n", command)
|
|
|
|
os.Exit(1)
|
2020-12-26 11:44:53 -09:00
|
|
|
}
|
2020-08-11 03:48:40 -08:00
|
|
|
}
|
2021-01-11 06:28:14 -09:00
|
|
|
|
|
|
|
func printUsage() {
|
|
|
|
fmt.Printf("Usage: %s [args] <action> ...\n", os.Args[0])
|
|
|
|
fmt.Printf("Arguments:\n")
|
|
|
|
fmt.Printf(" -c <path> : path to configuration file\n")
|
|
|
|
fmt.Printf(" -d <level> : debug level (0-9, higher for more debugging)\n")
|
|
|
|
fmt.Printf("Actions:\n")
|
|
|
|
fmt.Printf(" verify configuration: %s [args] config\n", os.Args[0])
|
|
|
|
fmt.Printf(" run a check manually: %s [args] check <host/group>\n", os.Args[0])
|
2021-01-18 12:47:03 -09:00
|
|
|
fmt.Printf(" test an alarm: %s [args] alarm <name>\n", os.Args[0])
|
2021-01-28 20:14:16 -09:00
|
|
|
fmt.Printf(" start server (forground): %s [args] server\n", os.Args[0])
|
2021-01-11 06:28:14 -09:00
|
|
|
}
|