-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmodbus_exporter.go
176 lines (149 loc) · 6.35 KB
/
modbus_exporter.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
// Copyright 2019 Richard Hartmann
// 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 (
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
"github.com/RichiH/modbus_exporter/config"
"github.com/RichiH/modbus_exporter/modbus"
)
func main() {
var (
configFile = kingpin.Flag(
"config.file",
"Sets the configuration file.",
).Default("modbus.yml").Strings()
toolkitFlags = webflag.AddFlags(kingpin.CommandLine, ":9602")
)
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.Version(version.Print("modbus_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := promlog.New(promlogConfig)
level.Info(logger).Log("msg", "Starting modbus_exporter", "version", version.Info())
level.Info(logger).Log("build_context", version.BuildContext())
telemetryRegistry := prometheus.NewRegistry()
telemetryRegistry.MustRegister(collectors.NewGoCollector())
telemetryRegistry.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
level.Info(logger).Log("msg", "Loading configuration file(s)", "config_file", strings.Join(*configFile, ", "))
config, err := config.LoadConfig(*configFile)
if err != nil {
level.Error(logger).Log("msg", "Error loading config", "err", err)
os.Exit(1)
}
http.Handle("/metrics", promhttp.HandlerFor(telemetryRegistry, promhttp.HandlerOpts{}))
exporter := modbus.NewExporter(config)
http.Handle("/modbus",
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
scrapeHandler(exporter, w, r, logger)
}),
)
srv := &http.Server{}
if err := web.ListenAndServe(srv, toolkitFlags, logger); err != nil {
level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
os.Exit(1)
}
}
func scrapeHandler(e *modbus.Exporter, w http.ResponseWriter, r *http.Request, logger log.Logger) {
moduleName := r.URL.Query().Get("module")
if moduleName == "" {
http.Error(w, "'module' parameter must be specified", http.StatusBadRequest)
return
}
if !e.GetConfig().HasModule(moduleName) {
http.Error(w, fmt.Sprintf("module '%v' not defined in configuration file", moduleName), http.StatusBadRequest)
return
}
target := r.URL.Query().Get("target")
if target == "" {
http.Error(w, "'target' parameter must be specified", http.StatusBadRequest)
return
}
sT := r.URL.Query().Get("sub_target")
if sT == "" {
http.Error(w, "'sub_target' parameter must be specified", http.StatusBadRequest)
return
}
subTarget, err := strconv.ParseUint(sT, 10, 32)
if err != nil {
http.Error(w, fmt.Sprintf("'sub_target' parameter must be a valid integer: %v", err), http.StatusBadRequest)
return
}
if subTarget > 255 {
http.Error(w, fmt.Sprintf("'sub_target' parameter must be from 0 to 255. Invalid value: %d", subTarget), http.StatusBadRequest)
return
}
level.Info(logger).Log("msg", "got scrape request", "module", moduleName, "target", target, "sub_target", subTarget)
gatherer, err := e.Scrape(target, byte(subTarget), moduleName) // Scrape
// No errors, export data to Prometheus
if err == nil {
promhttp.HandlerFor(gatherer, promhttp.HandlerOpts{}).ServeHTTP(w, r)
return
}
// In case of scraping error: sleep ScrapeErrorWait time and try again for ScrapeErrorRetryCount times.
// Try again if a race condition if happens where the same target is queried on different sub-targets,
// before a previous query has gotten a response.
ScrapeErrorRetryCount := e.Config.GetModule(moduleName).Workarounds.ScrapeErrorRetryCount // int cannot be nil, can arise issue if user wants to set it to 0
ScrapeErrorWait := e.Config.GetModule(moduleName).Workarounds.ScrapeErrorWait
if ScrapeErrorRetryCount == 0 { // if unset or 0, set to 3 retries
ScrapeErrorRetryCount = 3
level.Error(logger).Log("msg", "ScrapeErrorRetryCount: Scrape retry count is unset, using default value 3", "target", target, "module", moduleName, "err", err)
}
if ScrapeErrorWait == 0 { // If unset or 0, wait 100 milliseconds
ScrapeErrorWait = 100
level.Error(logger).Log("msg", "ScrapeErrorWait: Scrape retry waiting time is unset, using default value 100", "target", target, "module", moduleName, "err", err)
}
for i := 1; i <= ScrapeErrorRetryCount; i++ { // Retry x times until giving up and returning error
time.Sleep(time.Duration(ScrapeErrorWait) * time.Millisecond) // sleep for y milliseconds
// Another attempt at scraping
gatherer, err := e.Scrape(target, byte(subTarget), moduleName)
if err == nil {
promhttp.HandlerFor(gatherer, promhttp.HandlerOpts{}).ServeHTTP(w, r)
return
}
}
// Return error to Prometheus and log if it still persists.
if err != nil {
httpStatus := http.StatusInternalServerError
if strings.Contains(fmt.Sprintf("%v", err), "unable to connect with target") {
httpStatus = http.StatusServiceUnavailable
// Throw HTTP 504 StatusGatewayTimeout error in case of module returning modbus exception 11
} else if strings.Contains(fmt.Sprintf("%v", err), "i/o timeout") || strings.Contains(fmt.Sprintf("%v", err), "exception '11' (gateway target device failed to respond)") {
httpStatus = http.StatusGatewayTimeout
}
http.Error(
w,
fmt.Sprintf("failed to scrape target '%v' with module '%v': %v", target, moduleName, err),
httpStatus,
)
level.Error(logger).Log("msg", "failed to scrape", "target", target, "module", moduleName, "err", err)
return
}
promhttp.HandlerFor(gatherer, promhttp.HandlerOpts{}).ServeHTTP(w, r)
}