Skip to content

Commit abd3a26

Browse files
authored
Merge pull request #1 from ohmydevops/add-availableServices
Add site selection
1 parent 1b7391a commit abd3a26

File tree

1 file changed

+70
-10
lines changed

1 file changed

+70
-10
lines changed

main.go

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package main
22

33
import (
44
"encoding/json"
5+
"flag"
56
"fmt"
67
"io"
78
"net/http"
89
"net/http/cookiejar"
10+
"os"
911
"time"
1012

1113
"github.com/gen2brain/beeep"
@@ -14,11 +16,21 @@ import (
1416
const baseURL = "https://radar.arvancloud.ir/api/v1/internet-monitoring?isp="
1517

1618
var (
17-
errorCounts = make(map[string]int)
18-
erroredISPs = make(map[string]bool)
19+
errorCounts = make(map[string]int)
20+
erroredISPs = make(map[string]bool)
21+
serviceIndicator string
1922
)
2023

21-
var serviceIndicator string = "google"
24+
var availableServices = []string{
25+
"google",
26+
"wikipedia",
27+
"playstation",
28+
"bing",
29+
"github",
30+
"digikala",
31+
"divar",
32+
"aparat",
33+
}
2234

2335
var isps = []string{
2436
"sindad-buf",
@@ -36,6 +48,34 @@ var isps = []string{
3648
"irancell",
3749
}
3850

51+
func printServices() {
52+
fmt.Println("Usage:")
53+
fmt.Println(" --service=N Run directly without prompt, where N is the service number (see below)")
54+
fmt.Println(" --help Show this help message and available services")
55+
fmt.Println()
56+
fmt.Println("Available services:")
57+
for i, s := range availableServices {
58+
fmt.Printf(" %2d) %s\n", i+1, s)
59+
}
60+
fmt.Println()
61+
fmt.Println("Examples:")
62+
fmt.Println(" ./radar-linux # Interactive mode (asks for service)")
63+
fmt.Println(" ./radar-linux --service=3 # Monitor playstation directly")
64+
fmt.Println(" ./radar-linux --help # Show this help message")
65+
}
66+
67+
func chooseServiceInteractive() string {
68+
printServices()
69+
var choice int
70+
fmt.Print("Enter number: ")
71+
_, err := fmt.Scanln(&choice)
72+
if err != nil || choice < 1 || choice > len(availableServices) {
73+
fmt.Println("❌ Invalid choice. Defaulting to 'google'")
74+
return "google"
75+
}
76+
return availableServices[choice-1]
77+
}
78+
3979
func fetchData(client *http.Client, isp string) (float64, error) {
4080
url := baseURL + isp
4181

@@ -60,12 +100,12 @@ func fetchData(client *http.Client, isp string) (float64, error) {
60100
return 0, fmt.Errorf("❌ JSON parse error: %v", err)
61101
}
62102

63-
googleValues, ok := data[serviceIndicator]
64-
if !ok || googleValues == nil || len(googleValues) == 0 {
103+
values, ok := data[serviceIndicator]
104+
if !ok || values == nil || len(values) == 0 {
65105
return 0, fmt.Errorf("-")
66106
}
67107

68-
return googleValues[len(googleValues)-1], nil
108+
return values[len(values)-1], nil
69109
}
70110

71111
func checkStatus(isp string, value float64) {
@@ -74,19 +114,19 @@ func checkStatus(isp string, value float64) {
74114
if value != 0 {
75115
errorCounts[isp]++
76116
if errorCounts[isp] >= 3 && !erroredISPs[isp] {
77-
err := beeep.Notify("🔴 Internet Outage", fmt.Sprintf("Google unreachable from %s", isp), "./icon.png")
117+
err := beeep.Notify("🔴 Internet Outage", fmt.Sprintf("%s unreachable from %s", serviceIndicator, isp), "./icon.png")
78118
if err != nil {
79119
fmt.Printf("[%s] ❌ Notification error: %v\n", isp, err)
80120
}
81121
erroredISPs[isp] = true
82122
}
83123
} else {
84124
if erroredISPs[isp] {
85-
err := beeep.Notify("🟢 Internet Outage fixed", fmt.Sprintf("Google reachable from %s", isp), "./icon.png")
125+
err := beeep.Notify("🟢 Internet Restored", fmt.Sprintf("%s is reachable again from %s", serviceIndicator, isp), "./icon.png")
86126
if err != nil {
87127
fmt.Printf("[%s] ❌ Notification error: %v\n", isp, err)
88128
}
89-
fmt.Printf("[%s] 🟢 Google is reachable again\n", isp)
129+
fmt.Printf("[%s] 🟢 %s is reachable again\n", isp, serviceIndicator)
90130
}
91131
errorCounts[isp] = 0
92132
erroredISPs[isp] = false
@@ -100,7 +140,27 @@ func waitUntilNextMinute() {
100140
}
101141

102142
func main() {
103-
fmt.Println("Arvan Cloud Outage Notification started ...")
143+
fmt.Println("📡 Arvan Cloud Radar Monitor")
144+
serviceFlag := flag.Int("service", 0, "Service number to monitor (e.g. 1 for google, 2 for wikipedia...)")
145+
helpFlag := flag.Bool("help", false, "Show available services")
146+
flag.Parse()
147+
148+
if *helpFlag {
149+
printServices()
150+
return
151+
}
152+
153+
if *serviceFlag > 0 && *serviceFlag <= len(availableServices) {
154+
serviceIndicator = availableServices[*serviceFlag-1]
155+
} else if *serviceFlag != 0 {
156+
fmt.Println("❌ Invalid service number. Use --help to see available options.")
157+
os.Exit(1)
158+
} else {
159+
serviceIndicator = chooseServiceInteractive()
160+
}
161+
162+
fmt.Printf("✅ Monitoring service: %s\n", serviceIndicator)
163+
104164
jar, _ := cookiejar.New(nil)
105165
client := &http.Client{Jar: jar}
106166

0 commit comments

Comments
 (0)