|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + "time" |
| 11 | + |
| 12 | + internal_flag "github.com/ohmydevops/arvancloud-radar-notif/internal/flag" |
| 13 | + "github.com/ohmydevops/arvancloud-radar-notif/internal/notification" |
| 14 | + "github.com/ohmydevops/arvancloud-radar-notif/radar" |
| 15 | +) |
| 16 | + |
| 17 | +const ProgramName = "📡 Arvan Cloud Radar Monitor" |
| 18 | + |
| 19 | +// Max consecutive errors to consider outage |
| 20 | +const maxConsecutiveErrorsForOutage = 3 |
| 21 | +const notificationIconPath = "./icon.png" |
| 22 | + |
| 23 | +var ( |
| 24 | + DatacenterErrorCounts = make(map[radar.Datacenter]int) |
| 25 | + erroredDatacenters = make(map[radar.Datacenter]bool) |
| 26 | + mu sync.Mutex // protects DatacenterErrorCounts & erroredDatacenters |
| 27 | +) |
| 28 | + |
| 29 | +func main() { |
| 30 | + cfg, err := internal_flag.ParseFlags() |
| 31 | + if err != nil { |
| 32 | + fmt.Println("❌", err) |
| 33 | + flag.Usage() |
| 34 | + os.Exit(1) |
| 35 | + } |
| 36 | + |
| 37 | + if cfg.ShowServices { |
| 38 | + printServices() |
| 39 | + os.Exit(0) |
| 40 | + } |
| 41 | + |
| 42 | + // Create notification manager |
| 43 | + notifiers := []notification.Notifier{ |
| 44 | + notification.NewConsoleNotifier(), |
| 45 | + } |
| 46 | + if cfg.DesktopNotification { |
| 47 | + notifiers = append(notifiers, notification.NewDesktopNotofier(ProgramName, notificationIconPath)) |
| 48 | + } |
| 49 | + notifiersManager := notification.NewNotofiersManager(notifiers) |
| 50 | + |
| 51 | + fmt.Println(ProgramName) |
| 52 | + |
| 53 | + fmt.Printf("✅ Monitoring service: %s\n\n", capitalizeFirst(cfg.Service)) |
| 54 | + |
| 55 | + // performDelay(cfg.CheckDelay) |
| 56 | + |
| 57 | + for { |
| 58 | + fmt.Printf("⏰ %s\n", time.Now().Format("15:04:05")) |
| 59 | + |
| 60 | + var wg sync.WaitGroup |
| 61 | + |
| 62 | + for _, datacenter := range radar.AllDatacenters { |
| 63 | + wg.Add(1) |
| 64 | + go func(dc radar.Datacenter) { |
| 65 | + defer wg.Done() |
| 66 | + checkDatacenter(dc, radar.Service(cfg.Service), notifiersManager) |
| 67 | + }(datacenter) |
| 68 | + } |
| 69 | + |
| 70 | + wg.Wait() |
| 71 | + |
| 72 | + performDelay(cfg.CheckDelay) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// checkDatacenter handles checking & notification for a single datacenter |
| 77 | +func checkDatacenter(datacenter radar.Datacenter, service radar.Service, notifiersManager *notification.NotifiersManager) { |
| 78 | + |
| 79 | + // Retrieve connectivity statistics between the datacenter and the service |
| 80 | + stats, err := radar.CheckDatacenterServiceStatistics(datacenter, service) |
| 81 | + if err != nil { |
| 82 | + fmt.Printf("⚠️ Statistics: %v from [%s]\n", err, datacenter) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + // Prevents race condition |
| 87 | + mu.Lock() |
| 88 | + defer mu.Unlock() |
| 89 | + |
| 90 | + if stats.IsAccessibleNow() { |
| 91 | + if erroredDatacenters[datacenter] { |
| 92 | + title := "🟢 Internet Restored" |
| 93 | + msg := fmt.Sprintf("%s is reachable again from %s", capitalizeFirst(string(service)), datacenter) |
| 94 | + |
| 95 | + // Notify through notification mechanisms |
| 96 | + if err := notifiersManager.Notify(title, msg); err != nil { |
| 97 | + log.Printf("❌ Notification: %v from [%s]", err, datacenter) |
| 98 | + } |
| 99 | + } |
| 100 | + erroredDatacenters[datacenter] = false |
| 101 | + DatacenterErrorCounts[datacenter] = 0 |
| 102 | + } else { |
| 103 | + DatacenterErrorCounts[datacenter]++ |
| 104 | + if DatacenterErrorCounts[datacenter] >= maxConsecutiveErrorsForOutage && !erroredDatacenters[datacenter] { |
| 105 | + title := "🔴 Internet Outage" |
| 106 | + msg := fmt.Sprintf("%s is unreachable from %s", capitalizeFirst(string(service)), datacenter) |
| 107 | + |
| 108 | + // Notify through notification mechanisms |
| 109 | + if err := notifiersManager.Notify(title, msg); err != nil { |
| 110 | + fmt.Printf("❌ Notification: %v from [%s]", err, datacenter) |
| 111 | + } |
| 112 | + erroredDatacenters[datacenter] = true |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// printServices prints available services |
| 118 | +func printServices() { |
| 119 | + fmt.Println("Available services:") |
| 120 | + for _, s := range radar.AllServices { |
| 121 | + fmt.Printf(" - %s\n", s) |
| 122 | + } |
| 123 | + |
| 124 | +} |
| 125 | + |
| 126 | +// performDelay sleeps for the specified number of minutes |
| 127 | +func performDelay(minutes int) { |
| 128 | + time.Sleep(time.Duration(minutes) * time.Minute) |
| 129 | + // time.Sleep(5 * time.Second) |
| 130 | +} |
| 131 | + |
| 132 | +// capitalizeFirst makes the first letter uppercase |
| 133 | +func capitalizeFirst(s string) string { |
| 134 | + if len(s) == 0 { |
| 135 | + return s |
| 136 | + } |
| 137 | + return strings.ToUpper(s[:1]) + s[1:] |
| 138 | +} |
0 commit comments