Skip to content

Instantly share code, notes, and snippets.

@zaz600
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaz600/1432a2c3e518797fa933 to your computer and use it in GitHub Desktop.
Save zaz600/1432a2c3e518797fa933 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"os"
"time"
)
const url = "https://golang.org/"
func main() {
check_loop()
}
func check_loop() {
for {
tm := time.Now().Format("2006-01-02 15:04:05")
// статус, который возвращает check, пока не используем, поэтому ставим _
_, msg := check(url)
log_to_file(tm, msg)
fmt.Println(tm, msg)
time.Sleep(1 * time.Minute)
}
}
func check(url string) (bool, string) {
// возвращает true - если сервис доступен, false, если нет и текст сообщения
fmt.Println("Проверяем адрес ", url)
resp, err := http.Get(url)
if err != nil {
return false, fmt.Sprintf("Ошибка соединения. %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return false, fmt.Sprintf("Ошибка. http-статус: %s", resp.StatusCode)
}
return true, fmt.Sprintf("Онлайн. http-статус: %d", resp.StatusCode)
}
func log_to_file(tm, s string) {
// Сохраняет сообщения в файл
f, err := os.OpenFile("web_check.log", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
fmt.Println(tm, err)
return
}
defer f.Close()
if _, err = f.WriteString(fmt.Sprintln(tm, s)); err != nil {
fmt.Println(tm, err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment