Example #1
0
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)
		}
	}
}
Example #2
0
func alarmCallback(caUrl string, content string) error {
	client := nhttpclient.GetHttpClient("monitor.callback", 5*time.Second, 10*time.Second)
	// send by http-post
	req, err := http.NewRequest("POST", caUrl, bytes.NewBufferString(content))
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
	req.Header.Set("Connection", "close")
	postResp, err := client.Do(req)
	if err != nil {
		return err
	}
	defer postResp.Body.Close()

	if postResp.StatusCode/100 != 2 {
		return fmt.Errorf("Http-Post Error, Code %d", postResp.StatusCode)
	}
	return nil
}