bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/silence/main.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "flag" 6 "fmt" 7 "io/ioutil" 8 "log" 9 "net/http" 10 "os" 11 "runtime" 12 "time" 13 14 "bosun.org/host" 15 16 "bytes" 17 18 "bosun.org/util" 19 ) 20 21 var ( 22 flagHost = flag.String("h", "bosun", "Hostname of your bosun server, defaults to bosun.") 23 flagUser = flag.String("u", "", "Username, defaults to the username returned from the OS.") 24 flagDuration = flag.String("d", "30m", "A duration to silence this host for. Defaults to 30m and the format is defined at https://golang.org/pkg/time/#ParseDuration") 25 flagTags = flag.String("t", "", "OpenTSDB tags to be silenced in the format of tagKey=value,tagKey=value. Defaults to host=<hostname>, use -t= to use empty tag set.") 26 flagAlert = flag.String("a", "", "Name of the alert to silence, defaults to empty which means all alerts.") 27 flagMessage = flag.String("m", "", "Reason for the silence, defaults to an empty string.") 28 flagForget = flag.String("f", "", "Set to 'true' to forget anything that goes unknown during the silence. Used when decommissioning something.") 29 ) 30 31 func initHostManager() { 32 hm, err := host.NewManager(false) 33 if err != nil { 34 log.Fatalf("couldn't initialise host factory: %v", err) 35 } 36 37 util.SetHostManager(hm) 38 } 39 40 func main() { 41 initHostManager() 42 43 flag.Parse() 44 un := *flagUser 45 if un == "" { 46 // Use os.Getenv because os/user and user.Current() requires cgo 47 switch runtime.GOOS { 48 case "darwin", "linux": 49 un = os.Getenv("USER") 50 case "windows": 51 un = os.Getenv("USERNAME") 52 default: 53 un = "UNKNOWN" 54 } 55 sudo := os.Getenv("SUDO_USER") 56 if sudo != "" { 57 un = sudo 58 } 59 } 60 if *flagTags == "" { 61 flagTagsIsPresent := false 62 for _, arg := range os.Args { 63 if arg == "-t=" { 64 flagTagsIsPresent = true 65 } 66 } 67 if !flagTagsIsPresent { 68 *flagTags = "host=" + util.GetHostManager().GetHostName() 69 } 70 } 71 now := time.Now().UTC() 72 d, err := time.ParseDuration(*flagDuration) 73 if err != nil { 74 log.Fatal(err) 75 } 76 end := now.Add(d) 77 if *flagForget != "" { 78 *flagForget = "true" 79 } 80 s := struct { 81 User string `json:"user"` 82 Start string `json:"start"` 83 End string `json:"end"` 84 Tags string `json:"tags"` 85 Alert string `json:"alert"` 86 Message string `json:"message"` 87 Confirm string `json:"confirm"` 88 Forget string `json:"forget"` 89 }{ 90 un, 91 now.Format("2006-01-02 15:04:05 MST"), 92 end.Format("2006-01-02 15:04:05 MST"), 93 *flagTags, 94 *flagAlert, 95 *flagMessage, 96 "confirm", 97 *flagForget, 98 } 99 b, err := json.Marshal(s) 100 if err != nil { 101 log.Fatal(err, s) 102 } 103 resp, err := http.Post("https://"+*flagHost+"/api/silence/set", "application/json", bytes.NewBuffer(b)) 104 if err != nil { 105 log.Fatal(err, s) 106 } 107 c, err := ioutil.ReadAll(resp.Body) 108 defer resp.Body.Close() 109 if err != nil { 110 log.Fatal(err) 111 } 112 if resp.StatusCode != 200 { 113 log.Fatalf("%s", c) 114 } 115 if s.Alert == "" { 116 s.Alert = "None" 117 } 118 if s.Tags == "" { 119 s.Tags = "None" 120 } 121 if s.Message == "" { 122 s.Message = "None" 123 } 124 fmt.Printf("Created silence: Start: %s, End: %s, Tags: %s, Alert: %s, Message: %s\n", 125 s.Start, s.End, s.Tags, s.Alert, s.Message) 126 }