forked from integr8ly/workload-web-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
176 lines (162 loc) · 4.71 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/integr8ly/workload-web-app/pkg/checks"
"github.com/integr8ly/workload-web-app/pkg/counters"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
const (
evnVarPort = "PORT"
envVarAMQAddress = "AMQ_ADDRESS"
envVarAMQQueue = "AMQ_QUEUE"
envVarEnvironment = "ENVIRONMENT"
envVarRequestInterval = "REQUEST_INTERVAL"
envVarURL = "RHSSO_SERVER_URL"
envVarUser = "RHSSO_USER"
envVarPassword = "RHSSO_PWD"
envVarThreeScaleURL = "THREE_SCALE_URL"
envVarAMQCRUDNamespace = "AMQ_CRUD_NAMESPACE"
envVarAMQConsoleURL = "AMQ_CONSOLE_URL"
envVarAMQConsoleUsername = "AMQ_CONSOLE_USER"
envVarAMQConsolePassword = "AMQ_CONSOLE_PWD"
productionEnv = "production"
)
func init() {
log.SetOutput(os.Stdout)
if strings.ToLower(os.Getenv(envVarEnvironment)) == productionEnv {
log.SetLevel(log.InfoLevel)
} else {
log.SetLevel(log.DebugLevel)
}
if os.Getenv(envVarRequestInterval) != "" {
if val, err := strconv.Atoi(os.Getenv(envVarRequestInterval)); err == nil {
counters.RequestInterval = time.Duration(val) * time.Second
}
}
prometheus.MustRegister(counters.ServiceUpGauge)
prometheus.MustRegister(counters.ServiceTotalRequestsCounter)
prometheus.MustRegister(counters.ServiceTotalErrorsCounter)
prometheus.MustRegister(counters.ServiceTotalDowntimeCounter)
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World!")
}
func startAMQChecks() {
addr := os.Getenv(envVarAMQAddress)
q := os.Getenv(envVarAMQQueue)
if addr != "" && q != "" {
log.WithFields(log.Fields{
"address": addr,
"queue": q,
"interval": counters.RequestInterval,
}).Info("Start AMQ checks")
c := &checks.AMQChecks{
Address: addr,
QueueName: q,
SendTimeout: 2 * time.Second,
Interval: counters.RequestInterval,
}
c.RunForever()
} else {
log.Warnf("AMQ Checks are not started as env vars %s, %s are not set", envVarAMQAddress, envVarAMQQueue)
}
}
func startAMQConsoleChecks() {
console := os.Getenv(envVarAMQConsoleURL)
username := os.Getenv(envVarAMQConsoleUsername)
password := os.Getenv(envVarAMQConsolePassword)
if console != "" && username != "" && password != "" {
log.WithFields(log.Fields{
"consoleURL": console,
"interval": counters.RequestInterval,
}).Info("Start AMQ Console checks")
c := &checks.AMQConsoleChecks{
ConsoleURL: console,
Username: username,
Password: password,
Interval: counters.RequestInterval,
}
c.RunForever()
} else {
log.Warnf("AMQ Console checks are not started as env vars %s, %s, %s are not set correctly!",
envVarAMQConsoleURL, envVarAMQConsoleUsername, envVarAMQConsolePassword)
}
}
func startAMQCRUDChecks() {
namespace := os.Getenv(envVarAMQCRUDNamespace)
if namespace != "" {
log.Info("Start AMQ CRUD checks")
c, err := checks.NewAMQCRUDChecks(namespace)
if err != nil {
log.Warnf("Failed to start AMQ CRUD Checks with error: %s", err)
return
}
c.RunForever()
} else {
log.Warnf("AMQ CRUD Checks are not started as env var %s is not set", envVarAMQCRUDNamespace)
}
}
func startSSOChecks() {
url := os.Getenv(envVarURL)
user := os.Getenv(envVarUser)
pwd := os.Getenv(envVarPassword)
realm := "master"
if url != "" && user != "" && pwd != "" && realm != "" {
log.WithFields(log.Fields{
"serverURL": url,
"realmName": realm,
"interval": counters.RequestInterval,
}).Info("Start SSO Checks")
c := &checks.SSOChecks{
ServerURL: url,
User: user,
Password: pwd,
RealmName: realm,
Interval: counters.RequestInterval,
}
c.RunForever()
} else {
log.Warnf("SSO checks are not started as env vars are not set correctly!")
}
}
func startThreeScaleChecks() {
url := os.Getenv(envVarThreeScaleURL)
if url != "" {
log.WithFields(log.Fields{
"url": url,
"interval": counters.RequestInterval,
}).Info("Start 3scale checks")
c := &checks.ThreeScaleChecks{
URL: url,
Interval: counters.RequestInterval,
}
c.RunForever()
} else {
log.Warnf("3sclae: 3scale checks are not started because the environment variables %s is not set", envVarThreeScaleURL)
}
}
func main() {
go startAMQChecks()
go startAMQConsoleChecks()
go startSSOChecks()
go startThreeScaleChecks()
go startAMQCRUDChecks()
startHttpServer()
}
func startHttpServer() {
p := os.Getenv(evnVarPort)
if p == "" {
p = "8080"
}
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", handler)
log.WithField("port", p).Infof("Starting HTTP server")
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", p), nil))
}