-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
141 lines (119 loc) · 4.44 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
// Copyright © 2023 Axoflow
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"context"
"flag"
"fmt"
"io"
"net/http"
"os"
"time"
syslogngctl "github.com/axoflow/axosyslog-metrics-exporter/pkg/syslog-ng-ctl"
"github.com/prometheus/common/expfmt"
"golang.org/x/exp/slog"
)
const (
DEFAULT_TIMEOUT_SYSLOG time.Duration = time.Second * 5
DEFAULT_SERVICE_PORT = "9577"
DEFAULT_SOCKET_ADDR = "/var/run/syslog-ng/syslog-ng.ctl"
license = "Apache License, Version 2.0"
)
var (
Version = "dev" // should be set build-time, see Makefile
)
type RunArgs struct {
SocketAddr string
ServicePort string
ServiceAddress string
RequestTimeout string
}
func envOrDef(envName string, def string) (res string) {
res = os.Getenv(envName)
if res == "" {
res = def
}
return
}
func main() {
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
slog.SetDefault(logger)
runArgs := RunArgs{}
logger.Info("starting axosyslog-metrics-exporter", "version", Version, "license", license)
flag.StringVar(&runArgs.SocketAddr, "socket.path", envOrDef("CONTROL_SOCKET", DEFAULT_SOCKET_ADDR), "syslog-ng control socket path")
flag.StringVar(&runArgs.ServicePort, "service.port", envOrDef("SERVICE_PORT", DEFAULT_SERVICE_PORT), "service bind port")
flag.StringVar(&runArgs.ServiceAddress, "service.address", envOrDef("SERVICE_ADDRESS", ""), "service bind address in [host]:port format (overwrites service.port)")
flag.StringVar(&runArgs.RequestTimeout, "service.timeout", envOrDef("SERVICE_TIMEOUT", DEFAULT_TIMEOUT_SYSLOG.String()), "request timeout")
flag.Parse()
if runArgs.ServiceAddress == "" {
runArgs.ServiceAddress = fmt.Sprintf(":%v", runArgs.ServicePort)
}
logger.Info("listening", "bindAddress", runArgs.ServiceAddress, "requestTimeout", runArgs.RequestTimeout)
_, err := os.Stat(runArgs.SocketAddr)
logger.Info("testing syslog-ng control socket path", "socketPath", runArgs.SocketAddr, "found", err == nil, "error", err)
requestTimeout, err := time.ParseDuration(runArgs.RequestTimeout)
if err != nil {
requestTimeout = DEFAULT_TIMEOUT_SYSLOG
}
ctl := syslogngctl.NewController(syslogngctl.NewUnixDomainSocketControlChannel(runArgs.SocketAddr))
mux := http.NewServeMux()
mux.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
logger := logger.With("remote", r.RemoteAddr, "userAgent", r.UserAgent(), "path", "/metrics")
subCtx, cancel := context.WithTimeout(r.Context(), requestTimeout)
defer cancel()
mfs, err := ctl.StatsPrometheus(subCtx)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
logger.Error("socket command failed", "error", err)
return
}
var resp bytes.Buffer
for _, mf := range mfs {
_, err := expfmt.MetricFamilyToText(&resp, mf)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
logger.Error("metrics conversion failed", "error", err)
return
}
}
bodyLen, err := io.Copy(w, &resp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
logger.Error("writing response failed", "error", err)
return
}
logger.Info("writing response", "bodyLength", bodyLen)
})
mux.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
logger := logger.With("remote", r.RemoteAddr, "userAgent", r.UserAgent(), "path", "/ping")
subCtx, cancel := context.WithTimeout(r.Context(), requestTimeout)
defer cancel()
err := ctl.Ping(subCtx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
logger.Error("socket command failed", "error", err)
return
}
_, err = w.Write([]byte(`PONG`))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
logger.Error("writing response failed", "error", err)
return
}
logger.Info("pong")
})
err = http.ListenAndServe(runArgs.ServiceAddress, mux)
logger.Info("exiting", "error", err)
}