-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b550c47
Showing
7 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
onewire_exporter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Martin | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# onewire_exporter | ||
|
||
Prometheus exporter for 1-wire temperature sensors connected to a Raspberry PI | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"github.com/prometheus/common/log" | ||
yaml "gopkg.in/yaml.v2" | ||
) | ||
|
||
const version string = "0.1" | ||
|
||
type List struct { | ||
Names map[string]string | ||
} | ||
|
||
var ( | ||
showVersion = flag.Bool("version", false, "Print version information.") | ||
listenAddress = flag.String("listen-address", ":9330", "Address on which to expose metrics.") | ||
metricsPath = flag.String("path", "/metrics", "Path under which to expose metrics.") | ||
ignoreUnknown = flag.Bool("ignore", true, "Ignores sensors without a name") | ||
nameFile = flag.String("names", "names.yaml", "File maping IDs to names") | ||
|
||
list List | ||
) | ||
|
||
func init() { | ||
flag.Usage = func() { | ||
fmt.Println("Usage: onewire_exporter [ ... ]\n\nParameters:") | ||
fmt.Println() | ||
flag.PrintDefaults() | ||
} | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if *showVersion { | ||
printVersion() | ||
os.Exit(0) | ||
} | ||
|
||
filename, _ := filepath.Abs(*nameFile) | ||
yamlFile, err := ioutil.ReadFile(filename) | ||
|
||
if err != nil { | ||
log.Fatal("Can't read names file") | ||
} | ||
|
||
err = yaml.Unmarshal(yamlFile, &list) | ||
if err != nil { | ||
log.Fatal("Can't read names file") | ||
} | ||
|
||
startServer() | ||
} | ||
|
||
func printVersion() { | ||
fmt.Println("onewire_exporter") | ||
fmt.Printf("Version: %s\n", version) | ||
} | ||
|
||
func startServer() { | ||
log.Infof("Starting onewire exporter (Version: %s)\n", version) | ||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte(`<html> | ||
<head><title>onewire Exporter (Version ` + version + `)</title></head> | ||
<body> | ||
<h1>onewire Exporter</h1> | ||
<p><a href="` + *metricsPath + `">Metrics</a></p> | ||
<h2>More information:</h2> | ||
<p><a href="https://github.com/l3akage/onewire_exporter">github.com/l3akage/onewire_exporter</a></p> | ||
</body> | ||
</html>`)) | ||
}) | ||
http.HandleFunc(*metricsPath, handleMetricsRequest) | ||
|
||
log.Infof("Listening for %s on %s\n", *metricsPath, *listenAddress) | ||
log.Fatal(http.ListenAndServe(*listenAddress, nil)) | ||
} | ||
|
||
func handleMetricsRequest(w http.ResponseWriter, r *http.Request) { | ||
reg := prometheus.NewRegistry() | ||
reg.MustRegister(&onewireCollector{}) | ||
|
||
promhttp.HandlerFor(reg, promhttp.HandlerOpts{ | ||
ErrorLog: log.NewErrorLogger(), | ||
ErrorHandling: promhttp.ContinueOnError}).ServeHTTP(w, r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
names: | ||
id: test device |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/common/log" | ||
) | ||
|
||
const prefix = "onewire_" | ||
|
||
var ( | ||
upDesc *prometheus.Desc | ||
tempDesc *prometheus.Desc | ||
) | ||
|
||
func init() { | ||
upDesc = prometheus.NewDesc(prefix+"up", "Scrape was successful", nil, nil) | ||
tempDesc = prometheus.NewDesc(prefix+"temp", "Air temperature (in degrees C)", []string{"id", "name"}, nil) | ||
} | ||
|
||
type Temp struct { | ||
ID string | ||
Value float64 | ||
} | ||
|
||
type onewireCollector struct { | ||
} | ||
|
||
func getTemperatures() ([]Temp, error) { | ||
reg, err := regexp.Compile("[^0-9]+") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
devices, err := ioutil.ReadDir("/sys/bus/w1/devices/") | ||
if err != nil { | ||
return nil, err | ||
} | ||
var values []Temp | ||
for _, device := range devices { | ||
if device.Name() == "w1_bus_master1" { | ||
continue | ||
} | ||
content, err := ioutil.ReadFile("/sys/bus/w1/devices/" + device.Name() + "/w1_slave") | ||
if err != nil { | ||
log.Infof("Error reading device %s\n", device.Name()) | ||
continue | ||
} | ||
lines := strings.Split(string(content), "\n") | ||
if len(lines) != 3 { | ||
log.Infof("Unknown format for device %s\n", device.Name()) | ||
continue | ||
} | ||
if !strings.Contains(lines[0], "YES") { | ||
log.Infof("CRC invalid for device %s\n", device.Name()) | ||
continue | ||
} | ||
data := strings.SplitAfter(lines[1], "t=") | ||
if len(data) != 2 { | ||
log.Infof("Temp value not found for device %s\n", device.Name()) | ||
continue | ||
} | ||
strValue := reg.ReplaceAllString(data[1], "") | ||
|
||
tempInt, err := strconv.ParseFloat(strValue, 64) | ||
if err != nil { | ||
continue | ||
} | ||
values = append(values, Temp{ | ||
ID: device.Name(), | ||
Value: tempInt / 1000.0, | ||
}) | ||
} | ||
return values, nil | ||
} | ||
|
||
func (c onewireCollector) Describe(ch chan<- *prometheus.Desc) { | ||
ch <- upDesc | ||
ch <- tempDesc | ||
} | ||
|
||
func (c onewireCollector) Collect(ch chan<- prometheus.Metric) { | ||
values, err := getTemperatures() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, "error getting sensor data", err) | ||
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 0) | ||
} else { | ||
for _, sensor := range values { | ||
n := list.Names[sensor.ID] | ||
if n == "" { | ||
if *ignoreUnknown == true { | ||
log.Infof("Ingoring unknown device %s\n", sensor.ID) | ||
continue | ||
} else { | ||
n = sensor.ID | ||
} | ||
} | ||
l := []string{sensor.ID, n} | ||
ch <- prometheus.MustNewConstMetric(tempDesc, prometheus.GaugeValue, float64(sensor.Value), l...) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[Unit] | ||
Description=Prometheus onewire_exporter | ||
Wants=basic.target | ||
After=basic.target network.target | ||
|
||
[Service] | ||
User=pi | ||
Group=pi | ||
ExecStart=/home/pi/onewire_exporter -names=/home/pi/names.yaml | ||
|
||
ExecReload=/bin/kill -HUP $MAINPID | ||
KillMode=process | ||
Restart=always | ||
|
||
[Install] | ||
WantedBy=multi-user.target |