-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (73 loc) · 2.63 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
76
77
78
79
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"power-monitor/client"
"power-monitor/config"
"power-monitor/monitor"
"power-monitor/server"
"time"
)
func runServer(alertTime string) {
// Create shared ClientStatus data structure
clientStatus := server.ClientStatus{Status: make(map[string]time.Time)}
// Load the config
config := config.ReadConfig("config.yml")
// Start Goroutine to monitor client status
alertThreshold, err := time.ParseDuration(alertTime)
if err != nil {
log.Fatal("Failed to parse Alert Threshold duration.\n")
return
}
go monitor.MonitorClientStatus(&clientStatus, &config, alertThreshold)
// Start HTTP Server
http.HandleFunc("/", server.Index)
http.HandleFunc("/checkin", server.ClientCheckIn(&clientStatus))
listenPort := 8000
log.Printf("Server is listening on port %d.", listenPort)
err = http.ListenAndServe(fmt.Sprintf(":%d", listenPort), nil)
log.Printf("%s", err)
}
func runClient(serverHostname string, serverPort int, clientName string, requestDelay int) {
requestURL := fmt.Sprintf("http://%s:%d/checkin", serverHostname, serverPort)
log.Printf("Using check-in URL %s", requestURL)
for {
// Perform checkin request
_, err := client.CheckIn(requestURL, clientName)
if err != nil {
log.Printf("Error making http request: %s\n", err)
} else {
log.Println("Successfully checked in with server.")
}
// Sleep for requestDelay seconds
time.Sleep(time.Duration(requestDelay) * time.Second)
}
}
func main() {
// Parse command line arguments
serverPtr := flag.Bool("server", false, "Run the server.")
clientPtr := flag.Bool("client", false, "Run the client.")
// Args only for client
alertTime := flag.String("alert-time", "5m", "A time.Duration string representing the duration at which an email alert should be sent after a client goes offline.")
// Args only for client
serverHostnamePtr := flag.String("hostname", "localhost", "Hostname for the server to check in with.")
serverPortPtr := flag.Int("port", 8000, "Port the server is listening on.")
clientNamePtr := flag.String("name", "test", "Name for this client. It should be unique.")
requestDelayPtr := flag.Int("delay", 60, "Delay between checking requests, in seconds.")
flag.Parse()
if *serverPtr && *clientPtr {
log.Println("Cannot simultaneously run the server and the client.")
os.Exit(1)
} else if *serverPtr {
log.Println("Starting server...")
runServer(*alertTime)
} else if *clientPtr {
log.Println("Starting client...")
runClient(*serverHostnamePtr, *serverPortPtr, *clientNamePtr, *requestDelayPtr)
} else {
log.Println("Please specify if the client (-client) or server (-server) should be run.")
}
}