Exemple #1
0
func setEnabledRoute(c web.C, w http.ResponseWriter, r *http.Request) {
	status := common.GetStatus()
	enabled, _ := strconv.Atoi(r.URL.Query().Get("enabled"))
	status.Disabled = enabled != 1
	log.Printf("setting disabled to %t\n", status.Disabled)
	common.SetStatus(status)
	http.Redirect(w, r, "/", http.StatusFound)
}
Exemple #2
0
func homeRoute(c web.C, w http.ResponseWriter, r *http.Request) {
	status := common.GetStatus()
	config := common.GetConfig()
	templateArgs := map[string]interface{}{
		"url":          config.URL,
		"enabled":      !status.Disabled,
		"lastPingTime": status.LastRunAt.Format("2006-01-02 15:04:05 MST"),
		"lastStatus":   status.LastStatus,
		"numContacts":  len(config.Phones),
	}
	fmt.Fprintln(w, getTemplate("home", templateArgs))
}
Exemple #3
0
func statusRoute(c web.C, w http.ResponseWriter, r *http.Request) {
	status := common.GetStatus()
	encoder := json.NewEncoder(w)
	encoder.Encode(status)
}
Exemple #4
0
func pingSite(c *cli.Context) {
	simulateDown := c.GlobalBool("down")

	config := common.GetConfig()
	status := common.GetStatus()

	if config.URL == "" {
		return
	}

	defer func() {
		if e := recover(); e != nil {
			switch x := e.(type) {
			case error:
				common.HipchatMessage(x.Error())
			default:
				common.HipchatMessage(fmt.Sprintf("%v", x))
			}
			panic(e)
		}
	}()

	client := &http.Client{
		Timeout: time.Duration(20 * time.Second),
	}

	req, err := http.NewRequest("GET", config.URL, nil)
	if err != nil {
		log.Fatalln(err)
	}

	req.Close = true
	req.Header.Set("User-Agent", "SupPinger")

	isError := false
	statusCode := 0

	resp, err := client.Do(req)
	if err != nil && err != io.EOF {
		fmt.Printf("err: %+v\n", err)
		fmt.Printf("resp: %+v\n", resp)
		isError = true
	}
	if resp != nil {
		defer resp.Body.Close()
		statusCode = resp.StatusCode
	}

	if simulateDown || isError || statusCode != http.StatusOK {
		log.Println("Site is down. Status is ", statusCode)
		status.NumErrors++
		if status.NumErrors >= 5 && !status.Disabled {
			callDevTeam()
		}
	} else {
		status.NumErrors = 0
	}

	status.LastStatus = statusCode
	status.LastRunAt = time.Now()
	common.SetStatus(status)
}