|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/cookiejar" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/gen2brain/beeep" |
| 12 | +) |
| 13 | + |
| 14 | +const baseURL = "https://radar.arvancloud.ir/api/v1/internet-monitoring?isp=" |
| 15 | + |
| 16 | +var ( |
| 17 | + errorCounts = make(map[string]int) |
| 18 | + erroredISPs = make(map[string]bool) |
| 19 | +) |
| 20 | + |
| 21 | +var serviceIndicator string = "google" |
| 22 | + |
| 23 | +var isps = []string{ |
| 24 | + "sindad-buf", |
| 25 | + "sindad-thr-fanava", |
| 26 | + "sindad-thr", |
| 27 | + "bertina-xrx", |
| 28 | + "ajk-abrbaran", |
| 29 | + "tehran-3", |
| 30 | + "tehran-2", |
| 31 | + "bertina-thr", |
| 32 | + "hostiran", |
| 33 | + "parsonline", |
| 34 | + "afranet", |
| 35 | + "mci", |
| 36 | + "irancell", |
| 37 | +} |
| 38 | + |
| 39 | +func fetchData(client *http.Client, isp string) (float64, error) { |
| 40 | + url := baseURL + isp |
| 41 | + |
| 42 | + req, err := http.NewRequest("GET", url, nil) |
| 43 | + if err != nil { |
| 44 | + return 0, fmt.Errorf("❌ request creation error: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + resp, err := client.Do(req) |
| 48 | + if err != nil { |
| 49 | + return 0, fmt.Errorf("❌ request error: %v", err) |
| 50 | + } |
| 51 | + defer resp.Body.Close() |
| 52 | + |
| 53 | + body, err := io.ReadAll(resp.Body) |
| 54 | + if err != nil { |
| 55 | + return 0, fmt.Errorf("❌ error reading response: %v", err) |
| 56 | + } |
| 57 | + |
| 58 | + var data map[string][]float64 |
| 59 | + if err := json.Unmarshal(body, &data); err != nil { |
| 60 | + return 0, fmt.Errorf("❌ JSON parse error: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + googleValues, ok := data[serviceIndicator] |
| 64 | + if !ok || googleValues == nil || len(googleValues) == 0 { |
| 65 | + return 0, fmt.Errorf("-") |
| 66 | + } |
| 67 | + |
| 68 | + return googleValues[len(googleValues)-1], nil |
| 69 | +} |
| 70 | + |
| 71 | +func checkStatus(isp string, value float64) { |
| 72 | + fmt.Printf("[%s] => Value: %.2f\n", isp, value) |
| 73 | + beeep.AppName = "Arvan Cloud Radar" |
| 74 | + if value != 0 { |
| 75 | + errorCounts[isp]++ |
| 76 | + if errorCounts[isp] >= 3 && !erroredISPs[isp] { |
| 77 | + err := beeep.Notify("🔴 Internet Outage", fmt.Sprintf("Google unreachable from %s", isp), "./icon.png") |
| 78 | + if err != nil { |
| 79 | + fmt.Printf("[%s] ❌ Notification error: %v\n", isp, err) |
| 80 | + } |
| 81 | + erroredISPs[isp] = true |
| 82 | + } |
| 83 | + } else { |
| 84 | + if erroredISPs[isp] { |
| 85 | + err := beeep.Notify("🟢 Internet Outage fixed", fmt.Sprintf("Google reachable from %s", isp), "./icon.png") |
| 86 | + if err != nil { |
| 87 | + fmt.Printf("[%s] ❌ Notification error: %v\n", isp, err) |
| 88 | + } |
| 89 | + fmt.Printf("[%s] 🟢 Google is reachable again\n", isp) |
| 90 | + } |
| 91 | + errorCounts[isp] = 0 |
| 92 | + erroredISPs[isp] = false |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func waitUntilNextMinute() { |
| 97 | + now := time.Now() |
| 98 | + delay := time.Until(now.Truncate(time.Minute).Add(time.Minute)) |
| 99 | + time.Sleep(delay) |
| 100 | +} |
| 101 | + |
| 102 | +func main() { |
| 103 | + fmt.Println("Arvan Cloud Outage Notification started ...") |
| 104 | + jar, _ := cookiejar.New(nil) |
| 105 | + client := &http.Client{Jar: jar} |
| 106 | + |
| 107 | + waitUntilNextMinute() |
| 108 | + for { |
| 109 | + fmt.Printf("⏰ %s\n", time.Now().Format("15:04:05")) |
| 110 | + for _, isp := range isps { |
| 111 | + go func(isp string) { |
| 112 | + value, err := fetchData(client, isp) |
| 113 | + if err != nil { |
| 114 | + fmt.Printf("[%s] %v\n", isp, err) |
| 115 | + return |
| 116 | + } |
| 117 | + checkStatus(isp, value) |
| 118 | + }(isp) |
| 119 | + } |
| 120 | + time.Sleep(1 * time.Minute) |
| 121 | + } |
| 122 | +} |
0 commit comments