Skip to content

Instantly share code, notes, and snippets.

@drushadrusha
Created August 14, 2019 15:53
Show Gist options
  • Save drushadrusha/fcb487371fdea36598fc9f0ab7189ed6 to your computer and use it in GitHub Desktop.
Save drushadrusha/fcb487371fdea36598fc9f0ab7189ed6 to your computer and use it in GitHub Desktop.
Big Brother Telegram Notifications
package main
import (
"log"
"net/http"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
// Host - сsdfjfsdnkjsdfn
type Host struct {
Address string
Status string
}
var hosts []Host
func bbParser() {
res, err := http.Get("<BB LINK>")
if err != nil {
log.Println("Couldn't get the bb page")
return
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Println("Server returned error: ", res.StatusCode, res.Status)
return
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Println(err)
return
}
doc.Find("tr").Each(func(i int, s *goquery.Selection) {
host := s.Find("td[nowrap]").Text()
if host == "" {
return
}
host = strings.Replace(host, "\n", "", -1)
//log.Println(host)
indicator := false
for _, element := range hosts {
if element.Address == host && element.Status == "Down" {
indicator = true
}
}
if indicator == false {
log.Println("Помечаем хост как упавший ", host)
hosts = append(hosts, Host{
Address: host,
Status: "Down"})
res, err := http.Get("https://api.telegram.org/bot<BOT_TOKEN>/sendMessage?chat_id=<CHAT_ID>&text=Down " + host)
if err != nil {
log.Println("Ошибка запроса к телеграму ", err)
return
}
if res != nil {
log.Println("Оповещение в телеграм отправлено")
}
// Сюда можно впихнуть оповещение
}
})
doc.Find("tr").Each(func(i int, s *goquery.Selection) { // Теперь найдем Up хосты
//log.Println("Нашли TR")
host := s.Find("td[bgcolor=green]").Text()
if host == "" {
//log.Println("TD Пустой")
return
}
//log.Println("Найдена строчка", host)
for index, element := range hosts {
//log.Println("Цикл сравнения начат")
//log.Println("Сравниваем ", host)
//log.Println("С ", element.Address)
if element.Address == host && element.Status == "Down" {
log.Println("Помечаем host как поднявшийся ", host)
hosts[index].Status = "Up"
res, err := http.Get("https://api.telegram.org/bot<BOT_TOKEN>/sendMessage?chat_id=<USER_ID>&text=Up " + host)
if err != nil {
log.Println("Ошибка запроса к телеграму ", err)
return
}
if res != nil {
log.Println("Оповещение в телеграм отправлено")
}
}
}
})
log.Println(hosts)
}
func main() {
hosts = append(hosts, Host{ // по пустому слайсу цикл не хочет крутить
Address: "None",
Status: "None"})
for true {
bbParser()
time.Sleep(120 * time.Second)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment