-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
46 lines (37 loc) · 878 Bytes
/
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
package main
import (
"fmt"
"log"
"net/http"
"os"
)
var failFlag bool = false
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: url_checker <url1> <url2> <url3> ...")
os.Exit(1)
}
logFile, err := os.OpenFile("url_check.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("Error opening log file: %v", err)
}
defer logFile.Close()
logger := log.New(logFile, "", log.LstdFlags)
for _, url := range os.Args[1:] {
resp, err := http.Get(url)
if err != nil {
logger.Printf("Error checking %s: %s\n", url, err)
failFlag = true
continue
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
logger.Printf("%s is up and running (Status Code: %d)\n", url, resp.StatusCode)
} else {
logger.Printf("%s is not up (Status Code: %d)\n", url, resp.StatusCode)
}
}
if failFlag {
os.Exit(1)
}
}