From 4f6d95f55fdd965ea0c7baa73b96d58cfad8f0a0 Mon Sep 17 00:00:00 2001 From: Nick Cabatoff Date: Sun, 14 Aug 2016 14:01:04 -0400 Subject: [PATCH] Add a simple Dockerfile. Add retry for hddtemp connection. Tolerate devices with a unit of '*', treat them as having a temperature of -1. --- Dockerfile | 19 +++++++++++++++++++ sensor-exporter/main.go | 15 +++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3571981 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +# Start from a Debian image with the latest version of Go installed +# and a workspace (GOPATH) configured at /go. +FROM golang + +# Copy the local package files to the container's workspace. +ADD sensor-exporter /go/src/github.com/ncabatoff/sensor-exporter + +# Build the outyet command inside the container. +# (You may fetch or manage dependencies here, +# either manually or with a tool like "godep".) +RUN apt-get update +RUN apt-get --yes install libsensors4-dev +RUN go get github.com/ncabatoff/gosensors github.com/prometheus/client_golang/prometheus && go install github.com/ncabatoff/sensor-exporter + +# Run the outyet command by default when the container starts. +ENTRYPOINT /go/bin/sensor-exporter + +# Document that the service listens on port 9255. +EXPOSE 9255 diff --git a/sensor-exporter/main.go b/sensor-exporter/main.go index 03790d4..0fb9f11 100644 --- a/sensor-exporter/main.go +++ b/sensor-exporter/main.go @@ -163,6 +163,12 @@ func (h *HddCollector) Init() error { } func (h *HddCollector) readTempsFromConn() (string, error) { + if h.conn == nil { + if err := h.Init(); err != nil { + return "", err + } + } + _, err := io.Copy(&h.buf, h.conn) if err != nil { return "", fmt.Errorf("Error reading from hddtemp socket: %v", err) @@ -198,11 +204,16 @@ func parseHddTemp(s string) (HddTemperature, error) { if len(pieces) != 4 { return HddTemperature{}, fmt.Errorf("error parsing item from hddtemp, expected 4 tokens: %s", s) } - if pieces[3] != "C" { + dev, id, temp, unit := pieces[0], pieces[1], pieces[2], pieces[3] + + if unit == "*" { + return HddTemperature{Device: dev, Id: id, TemperatureCelsius: -1}, nil + } + + if unit != "C" { return HddTemperature{}, fmt.Errorf("error parsing item from hddtemp, I only speak Celsius", s) } - dev, id, temp := pieces[0], pieces[1], pieces[2] ftemp, err := strconv.ParseFloat(temp, 64) if err != nil { return HddTemperature{}, fmt.Errorf("Error parsing temperature as float: %s", temp)