-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (61 loc) · 2.73 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"flag"
"fmt"
"time"
"github.com/tejastn10/argus/logs"
"github.com/tejastn10/argus/monitor"
)
func main() {
// Define the flag for logging behavior
logToFile := flag.Bool("logToFile", false, "Set to true to log to file, false to log to console")
logTimestamp := flag.Bool("logTimeStamp", true, "Set to true to log timestamps. Timestamps are logged by default in the file")
url := flag.String("url", "https://example.com", "The URL to monitor. Default is https://example.com")
interval := flag.Int("interval", 30, "The monitoring interval in seconds. Default is 30 seconds")
retryCount := flag.Int("retryCount", 3, "The number of retries for monitoring requests. Must be >= 3. Default is 3 retries")
backoffDuration := flag.Int("backoffDuration", 3, "The backoff duration (in seconds) between retries. Must be >= 3. Default is 3 seconds")
// Parse the flags
flag.Parse()
// Validate and enforce minimum values for retryCount and backoffDuration
if *retryCount < 3 {
logs.Warning("Invalid retryCount provided. Enforcing minimum value of 3.")
*retryCount = 3
}
if *backoffDuration < 3 {
logs.Warning("Invalid backoffDuration provided. Enforcing minimum value of 3 seconds.")
*backoffDuration = 3
}
// Initialize logger with a timestamp
logs.Init(*logToFile, *logTimestamp)
// Log the start of the monitoring process
logs.Info(fmt.Sprintf("Starting uptime monitoring for %s every %v seconds with %d retries and %d seconds backoff duration",
*url, *interval, *retryCount, *backoffDuration))
// Variable to track consecutive failures
failureCount := 0
// Monitoring loop
for {
// Convert backoffDuration to time.Duration
backoff := time.Duration(*backoffDuration) * time.Second
// Call MonitorURL with retryCount and backoffDuration
status, elapsed, err := monitor.MonitorURL(*url, *retryCount, backoff)
if err != nil {
// Increment failure count
failureCount++
// Log error with improved structure
logs.Error(fmt.Errorf("URL: %s | Elapsed Time: %v | Error: %v | Failure Count: %d", *url, elapsed, err, failureCount))
// If 5 consecutive failures occur, print the message and break the loop
if failureCount >= 5 {
logs.Warning(fmt.Sprintf("Service is down: 5 consecutive failures detected for URL %s. Please check your server logs and monitoring systems for further details.", *url))
logs.Fatal("Stopping monitoring due to 5 consecutive failures.")
break
}
} else {
// Log status and response time using the logs package
logs.Success(fmt.Sprintf("URL: %s | Response Time: %v | Status: %d", *url, elapsed, status))
// Reset failure count on success
failureCount = 0
}
// Wait for the next monitoring interval
time.Sleep(time.Duration(*interval) * time.Second)
}
}