func _monitor() { client := nhttpclient.GetHttpClient("monitor.get", 5*time.Second, 10*time.Second) for _, host := range g.Config().Monitor.Cluster { hostInfo := strings.Split(host, ",") // "module,hostname:port/health/monitor/url" if len(hostInfo) != 2 { continue } //hostType := hostInfo[0] hostUrl := hostInfo[1] if !strings.Contains(hostUrl, "http://") { hostUrl = "http://" + hostUrl } req, _ := http.NewRequest("GET", hostUrl, nil) req.Header.Set("Connection", "close") getResp, err := client.Do(req) if err != nil { log.Printf(host+", monitor error,", err) onMonitorErr(host) continue } defer getResp.Body.Close() body, err := ioutil.ReadAll(getResp.Body) // body=['o','k',...] if !(err == nil && len(body) >= 2 && string(body[:2]) == "ok") { // err log.Println(host, ", error,", err) onMonitorErr(host) } else { // get "ok" onMonitorOk(host) } } }
func startHttpServer() { if !g.Config().Http.Enable { return } addr := g.Config().Http.Listen if addr == "" { return } // init url mapping configRoutes() s := &http.Server{ Addr: addr, MaxHeaderBytes: 1 << 30, } log.Println("http.startHttpServer ok, listening ", addr) log.Fatalln(s.ListenAndServe()) }
func configCommonRoutes() { http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("ok\n")) }) http.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(fmt.Sprintf("%s\n", g.VERSION))) }) http.HandleFunc("/workdir", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(fmt.Sprintf("%s\n", file.SelfDir()))) }) http.HandleFunc("/config", func(w http.ResponseWriter, r *http.Request) { RenderDataJson(w, g.Config()) }) }
// alarm judge func alarmJudge() { interval := time.Duration(10) * time.Second for { time.Sleep(interval) var content bytes.Buffer keys := alarmCache.Keys() if len(keys) == 0 { continue } for _, key := range keys { aitem, found := alarmCache.GetAndRemove(key) if !found { continue } content.WriteString(aitem.(*Alarm).String() + "\n") } if content.Len() < 6 { return } cfg := g.Config() // mail if cfg.Mail.Enable { hn, _ := os.Hostname() mailContent := formAlarmMailContent(cfg.Mail.Receivers, "AntEye.Alarm.From.["+hn+"]", content.String(), "AntEye") err := sendMail(cfg.Mail.Url, mailContent) if err != nil { log.Println("alarm send mail error, mail:", mailContent, "", err) } else { // statistics pfc.Meter("MonitorAlarmMail", 1) } } // sms if cfg.Sms.Enable { smsContent := formAlarmSmsContent(cfg.Sms.Receivers, content.String(), "AntEye") err := sendSms(cfg.Sms.Url, smsContent) if err != nil { log.Println("alarm send sms error, sms:", smsContent, "", err) } else { // statistics pfc.Meter("MonitorAlarmSms", 1) } } // callback if cfg.Callback.Enable { cbc := content.String() err := alarmCallback(cfg.Callback.Url, cbc) if err != nil { log.Println("alarm callback error, callback:", cfg.Callback, ", content:", cbc, "", err) } else { // statistics pfc.Meter("MonitorAlarmCallback", 1) } } } }