This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
modbus_exporter.go
66 lines (56 loc) · 1.94 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
// Copyright 2017 Alejandro Sirgo Rica
//
// This file is part of Modbus_exporter.
//
// Modbus_exporter is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Modbus_exporter is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Modbus_exporter. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"flag"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/lupoDharkael/modbus_exporter/modbus"
"github.com/lupoDharkael/modbus_exporter/parser"
"github.com/lupoDharkael/modbus_exporter/config"
)
var (
listenAddress = flag.String("listen-address", ":9010",
"The address to listen on for HTTP requests.")
configFile = flag.String("config.file", "slaves.yml",
"Sets the configuration file.")
scrapeInterval = flag.Duration("scrape-interval", 8,
"Sets scrape interval in seconds.")
)
func main() {
flag.Parse()
config.ScrapeInterval = time.Second * (*scrapeInterval)
slavesFile, err := config.LoadSlaves(*configFile)
if err != nil {
log.Fatalln(err)
}
log.Infoln("Loading configuration file", *configFile)
parsedSlaves, err := parser.ParseSlaves(slavesFile)
if err != nil {
log.Fatalln(err)
}
err = modbus.RegisterData(parsedSlaves, slavesFile)
if err != nil {
log.Fatalln(err)
}
// Expose the registered metrics via HTTP.
http.Handle("/metrics", promhttp.Handler())
log.Infoln("Listening on ", *listenAddress)
log.Fatalln(http.ListenAndServe(*listenAddress, nil))
}