-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
87 lines (74 loc) · 1.93 KB
/
main.go
File metadata and controls
87 lines (74 loc) · 1.93 KB
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
package main
import (
"context"
"fmt"
"io"
"log/slog"
"os"
"os/signal"
"syscall"
"github.com/daffadon/sysy/cmd/server"
v "github.com/daffadon/sysy/internal/variable"
"github.com/joho/godotenv"
"github.com/prometheus/client_golang/prometheus"
)
func init() {
prometheus.MustRegister(v.RequestsByIP, v.RequestsByURI, v.LatencyByURI)
prometheus.MustRegister(v.NginxActive, v.NginxAccepts, v.NginxHandled, v.NginxRequests)
prometheus.MustRegister(v.NginxReading, v.NginxWriting, v.NginxWaiting)
prometheus.MustRegister(v.ResponsesByHTTPCode)
prometheus.MustRegister(v.NginxUp)
}
func main() {
_ = godotenv.Load(".env")
d := os.Getenv("CONF_LOG_ENABLE")
var logger *slog.Logger
if d != "true" {
logger = slog.New(slog.NewTextHandler(io.Discard, nil))
fmt.Println("Log Mode Disabled")
} else {
logger = slog.New(slog.NewTextHandler(os.Stdout, nil))
fmt.Println("Log Mode Enabled")
}
t := "http://nginx/nginx_status"
if u := os.Getenv("CONF_NGINX_TARGET_URL"); u != "" {
t = u
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
metricsServerReady := make(chan bool)
metricsServerDone := make(chan struct{})
metricsServer := &server.MetricServer{
ServerReady: metricsServerReady,
Address: ":2112",
ScrapeTarget: t,
Logger: logger,
}
go func() {
metricsServer.Run(ctx)
close(metricsServerDone)
}()
<-metricsServerReady
addr := ":5140"
if v := os.Getenv("CONF_SYSLOG_ADDR"); v != "" {
addr = v
}
sylogServerReady := make(chan bool)
sylogServerDone := make(chan struct{})
sylogServer := &server.SysServer{
ServerReady: sylogServerReady,
Address: addr,
Logger: logger,
}
go func() {
sylogServer.Run(ctx)
close(sylogServerDone)
}()
<-sylogServerReady
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGABRT, syscall.SIGTERM)
<-sig
cancel()
<-metricsServerDone
<-sylogServerDone
}