A simple monitoring solution written in Go (work in progress)
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.

144 lines
3.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package main
  2. import (
  3. "encoding/json"
  4. "flag"
  5. "fmt"
  6. "git.binarythought.com/cdramey/alrm/config"
  7. "os"
  8. "strings"
  9. )
  10. func main() {
  11. cfgPath := flag.String("c", "", "path to configuration file")
  12. debuglvl := flag.Int("d", 0, "debug level")
  13. flag.Usage = printUsage
  14. flag.Parse()
  15. if *cfgPath == "" {
  16. searchpaths := []string{"/etc/alrmrc", "./alrmrc"}
  17. for _, sp := range searchpaths {
  18. if _, err := os.Stat(sp); err == nil {
  19. *cfgPath = sp
  20. break
  21. }
  22. }
  23. if *cfgPath == "" {
  24. fmt.Fprintf(os.Stderr, "cannot find configuration\n")
  25. os.Exit(1)
  26. }
  27. }
  28. command := strings.ToLower(flag.Arg(0))
  29. switch command {
  30. case "config":
  31. if *debuglvl > 0 {
  32. fmt.Printf("checking config %s .. \n", *cfgPath)
  33. }
  34. cfg, err := config.ReadConfig(*cfgPath, *debuglvl)
  35. if err != nil {
  36. fmt.Fprintf(os.Stderr, "%s\n", err.Error())
  37. os.Exit(1)
  38. }
  39. if *debuglvl > 0 {
  40. o, err := json.MarshalIndent(cfg, "", " ")
  41. if err != nil {
  42. fmt.Fprintf(os.Stderr, "JSON error: %s\n", err.Error())
  43. os.Exit(1)
  44. }
  45. fmt.Printf("%s\n", string(o))
  46. }
  47. fmt.Printf("config is OK\n")
  48. case "alarm":
  49. an := flag.Arg(1)
  50. if an == "" {
  51. fmt.Fprintf(os.Stderr, "alarm name required\n")
  52. os.Exit(1)
  53. }
  54. cfg, err := config.ReadConfig(*cfgPath, 0)
  55. if err != nil {
  56. fmt.Fprintf(os.Stderr, "%s\n", err.Error())
  57. os.Exit(1)
  58. }
  59. al, exists := cfg.Alarms[an]
  60. if !exists {
  61. fmt.Fprintf(os.Stderr, "group or host is not defined\n")
  62. os.Exit(1)
  63. }
  64. err = al.Alarm(
  65. "test group", "test host", "test check",
  66. fmt.Errorf("test alarm message"),
  67. )
  68. if err != nil {
  69. fmt.Fprintf(os.Stderr, "alarm failed: %s\n", err.Error())
  70. os.Exit(1)
  71. }
  72. fmt.Printf("alarm sounded successfully\n")
  73. case "check":
  74. cn := flag.Arg(1)
  75. if cn == "" {
  76. fmt.Fprintf(os.Stderr, "check host or group name required\n")
  77. os.Exit(1)
  78. }
  79. cfg, err := config.ReadConfig(*cfgPath, 0)
  80. if err != nil {
  81. fmt.Fprintf(os.Stderr, "%s\n", err.Error())
  82. os.Exit(1)
  83. }
  84. gr, exists := cfg.Groups[cn]
  85. if !exists {
  86. fmt.Fprintf(os.Stderr, "group or host is not defined\n")
  87. os.Exit(1)
  88. }
  89. err = gr.Check(*debuglvl)
  90. if err != nil {
  91. fmt.Fprintf(os.Stderr, "check failed: %s\n", err.Error())
  92. os.Exit(1)
  93. }
  94. fmt.Printf("check successful\n")
  95. case "server":
  96. cfg, err := config.ReadConfig(*cfgPath, *debuglvl)
  97. if err != nil {
  98. fmt.Fprintf(os.Stderr, "%s\n", err.Error())
  99. os.Exit(1)
  100. }
  101. err = startServer(cfg, *debuglvl)
  102. if err != nil {
  103. fmt.Fprintf(os.Stderr, "%s\n", err.Error())
  104. os.Exit(1)
  105. }
  106. case "help", "":
  107. printUsage()
  108. default:
  109. fmt.Fprintf(os.Stderr, "unknown command: %s\n", command)
  110. os.Exit(1)
  111. }
  112. }
  113. func printUsage() {
  114. fmt.Printf("Usage: %s [args] <action> ...\n", os.Args[0])
  115. fmt.Printf("Arguments:\n")
  116. fmt.Printf(" -c <path> : path to configuration file\n")
  117. fmt.Printf(" -d <level> : debug level (0-9, higher for more debugging)\n")
  118. fmt.Printf("Actions:\n")
  119. fmt.Printf(" verify configuration: %s [args] config\n", os.Args[0])
  120. fmt.Printf(" run a check manually: %s [args] check <host/group>\n", os.Args[0])
  121. fmt.Printf(" test an alarm: %s [args] alarm <name>\n", os.Args[0])
  122. fmt.Printf(" start server (forground): %s [args] server\n", os.Args[0])
  123. }