gh-3 config file entry and cli flag to enable the Prometheus exporter. Disabled by default.

This commit is contained in:
Jarno Rankinen 2023-03-14 23:27:49 +02:00
parent df9e6d5471
commit 4062e62071
2 changed files with 12 additions and 40 deletions

View File

@ -44,6 +44,7 @@ type Conf struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
Interval int `yaml:"interval"`
EnableMetrics bool `yaml:"enable_metrics"`
LogAccess bool `yaml:"log_access"`
Debug bool `yaml:"debug"`
}
@ -198,7 +199,9 @@ func serve(cert, key *string) {
http.HandleFunc("/api/v1/status", authHandlerFunc(status))
http.HandleFunc("/api/v1/registers/", authHandlerFunc(registers))
http.HandleFunc("/api/v1/temperature/", authHandlerFunc(temperature))
if config.EnableMetrics {
http.Handle("/metrics", promhttp.Handler())
}
html, err := fs.Sub(static, "static/html")
if err != nil {
log.Fatal(err)
@ -280,6 +283,7 @@ func initDefaultConfig(confpath string) {
4,
false,
false,
false,
}
conffile := confpath + "/configuration.yaml"
confbytes, err := yaml.Marshal(&config)
@ -303,6 +307,7 @@ func configure() {
keyflag := flag.String("key", config.SslPrivatekey, "Path to SSL private key to use for HTTPS")
usernflag := flag.String("username", config.Username, "Username for HTTP Basic Authentication")
passwflag := flag.String("password", config.Password, "Password for HTTP Basic Authentication")
promflag := flag.Bool("enable-metrics", config.EnableMetrics, "Enable the built-in Prometheus exporter")
// TODO: log file flag
flag.Parse()
config.Debug = *debugflag
@ -312,6 +317,7 @@ func configure() {
config.SslPrivatekey = *keyflag
config.Username = *usernflag
config.Password = *passwflag
config.EnableMetrics = *promflag
usernamehash = sha256.Sum256([]byte(config.Username))
passwordhash = sha256.Sum256([]byte(config.Password))
// Check that certificate file exists, generate if needed
@ -327,8 +333,10 @@ func configure() {
log.Println("HTTP Access logging enabled")
}
log.Println("Update interval set to", config.Interval, "seconds")
if config.EnableMetrics {
prometheus.MustRegister(&pingvin)
}
}
func main() {
log.Println("enervent-ctrl version", version)

View File

@ -573,27 +573,7 @@ func (p *PingvinKL) Monitor(interval int) {
}
}
// func (p *PingvinKL) Describe(ch chan<- *prometheus.Desc) {
// }
func (p *PingvinKL) prometheusRegister() {
for _, hreg := range p.Registers {
hreg.PromDesc = prometheus.NewDesc(
prometheus.BuildFQName("", "pingvin", strings.ToLower(hreg.Symbol)),
hreg.Description,
nil,
nil,
)
}
// prometheus.MustRegister(
// // collectors.NewGoCollector(),
// // collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
// p,
// )
}
// Implements prometheus.Describe()
func (p *PingvinKL) Describe(ch chan<- *prometheus.Desc) {
for _, hreg := range p.Registers {
if !hreg.Reserved {
@ -607,6 +587,7 @@ func (p *PingvinKL) Describe(ch chan<- *prometheus.Desc) {
}
}
// Implements prometheus.Collect()
func (p *PingvinKL) Collect(ch chan<- prometheus.Metric) {
for _, hreg := range p.Registers {
if !hreg.Reserved {
@ -632,22 +613,6 @@ func (p *PingvinKL) Collect(ch chan<- prometheus.Metric) {
}
}
// for _, coil := range p.Coils {
// if coil.Reserved {
// continue
// }
// if err := p.Promreg.Register(prometheus.NewGauge(
// prometheus.GaugeOpts{
// Subsystem: "pingvin",
// Name: strings.ToLower(coil.Symbol),
// Help: coil.Description,
// },
// )); err == nil {
// p.Debug.Println("GaugeFunc pingvin_"+coil.Symbol, "registered.")
// }
// }
// }
// create a PingvinKL struct, read coils and registers from CSVs
func New(debug bool) PingvinKL {
pingvin := PingvinKL{}
@ -667,6 +632,5 @@ func New(debug bool) PingvinKL {
newRegister(registerData[i][0], registerData[i][1], registerData[i][2], registerData[i][3], registerData[i][6]))
}
log.Println("Parsed", len(pingvin.Registers), "registers")
// pingvin.prometheusRegister()
return pingvin
}