Публикация репозитория

This commit is contained in:
Victor Chapaev 2024-09-23 11:01:07 +03:00
commit f11121aee6
Signed by: victor
GPG Key ID: 05ACE065D1EC5CA4
6 changed files with 122 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
dht-exporter
go.sum

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024 Chapvic
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

6
Makefile Normal file
View File

@ -0,0 +1,6 @@
PROJECT = dht-exporter
LDFLAGS ?= -trimpath -ldflags="-w -s"
all:
go get
go build -v $(LDFLAGS) -o $(PROJECT)

1
README.md Normal file
View File

@ -0,0 +1 @@
# Экспортер показаний датчиков DHT-11/22 в Prometheus

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module dht-exporter
go 1.22.7

89
main.go Normal file
View File

@ -0,0 +1,89 @@
package main
import (
"gitfox.ru/victor/go-dht"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"os"
"net/http"
"fmt"
"time"
"flag"
)
var (
m_temp = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "dht_sensor_temperature_total",
Help: "DHT Sensor Temperature",
},
)
m_hum = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "dht_sensor_humidity_total",
Help: "DHT Sensor Humidity",
},
)
address string
pin int
interval int
file string
cached_temp, cached_hum float64
)
func init() {
flag.StringVar(&address, "listen-address", ":4554", "The address to listen on for HTTP requests")
flag.IntVar(&pin, "pin", 4, "Pin number for DHT sensor")
flag.IntVar(&interval, "interval", 3, "Sensor read interval in seconds")
flag.StringVar(&file, "data-file", "", "Save last DHT sensor data to a file")
}
func main() {
fmt.Printf("DHT Exporter, v1.0\n")
flag.Parse()
if interval < 1 {
fmt.Printf("WARNING: Invalid interval! Using default value.\n")
interval = 3
}
/* Registering prometheus metrics */
prometheus.MustRegister(m_temp, m_hum)
go func() {
for true {
temp, hum, err := dht.ReadDHT(pin)
if err == nil {
if temp >= 0 && temp < 125 {
cached_temp = temp
}
if hum >= 0 && temp <= 100 {
cached_hum = hum
}
}
m_temp.Set(cached_temp)
m_hum.Set(cached_hum)
if file != "" {
content := fmt.Sprintf("%.1f %.1f", cached_temp, cached_hum)
err = os.WriteFile(file, []byte(content), 0666)
}
time.Sleep(time.Duration(interval * 1000) * time.Millisecond)
}
}()
/* Starting HTTP server */
fmt.Println("Starting HTTP server...")
fmt.Printf(" - server port %s\n", address)
fmt.Printf(" - sensor pin %d\n", pin)
fmt.Printf(" - read interval %d\n", interval)
if (file != "") {
fmt.Printf(" - save to file %s\n", file)
}
http.Handle("/metrics", promhttp.Handler())
err := http.ListenAndServe(address, nil)
if err != nil {
fmt.Println("Failed:", err)
}
}