Skip to content

Commit

Permalink
Implement support for fetching hddtemp data
Browse files Browse the repository at this point in the history
  • Loading branch information
Mendelson Gusmão committed Jun 23, 2016
1 parent f62c493 commit a2efcd3
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ Currently implemented sources:
* [exec](https://github.com/influxdata/telegraf/tree/master/plugins/inputs/exec) (generic executable plugin, support JSON, influx, graphite and nagios)
* [filestat](https://github.com/influxdata/telegraf/tree/master/plugins/inputs/filestat)
* [haproxy](https://github.com/influxdata/telegraf/tree/master/plugins/inputs/haproxy)
* [hddtemp](https://github.com/influxdata/telegraf/tree/master/plugins/inputs/hddtemp)
* [http_response](https://github.com/influxdata/telegraf/tree/master/plugins/inputs/http_response)
* [httpjson](https://github.com/influxdata/telegraf/tree/master/plugins/inputs/httpjson) (generic JSON-emitting http service plugin)
* [influxdb](https://github.com/influxdata/telegraf/tree/master/plugins/inputs/influxdb)
Expand Down
1 change: 1 addition & 0 deletions plugins/inputs/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
_ "github.com/influxdata/telegraf/plugins/inputs/filestat"
_ "github.com/influxdata/telegraf/plugins/inputs/graylog"
_ "github.com/influxdata/telegraf/plugins/inputs/haproxy"
_ "github.com/influxdata/telegraf/plugins/inputs/hddtemp"
_ "github.com/influxdata/telegraf/plugins/inputs/http_response"
_ "github.com/influxdata/telegraf/plugins/inputs/httpjson"
_ "github.com/influxdata/telegraf/plugins/inputs/influxdb"
Expand Down
101 changes: 101 additions & 0 deletions plugins/inputs/hddtemp/hddtemp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// +build linux,hddtemp

package hddtemp

import (
"bytes"
"io"
"net"
"strconv"
"strings"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)

const defaultAddress = "127.0.0.1:7634"

type HDDTemp struct {
Address string
Devices []string
}

func (_ *HDDTemp) Description() string {
return "Monitor disks' temperatures using hddtemp"
}

var hddtempSampleConfig = `
## By default, telegraf gathers temps data from all disks detected by the
## hddtemp.
##
## Only collect temps from the selected disks.
##
## A * as the device name will return the temperature values of all disks.
##
# address = "127.0.0.1:7634"
# devices = ["sda", "*"]
`

func (_ *HDDTemp) SampleConfig() string {
return hddtempSampleConfig
}

func (h *HDDTemp) Gather(acc telegraf.Accumulator) error {
var (
err error
conn net.Conn
buffer bytes.Buffer
)

if conn, err = net.Dial("tcp", h.Address); err != nil {
return err
}

if _, err = io.Copy(&buffer, conn); err != nil {
return err
}

fields := strings.Split(buffer.String(), "|")

for index := 0; index < len(fields)/5; index++ {
offset := index * 5
device := fields[offset+1]
device = device[strings.LastIndex(device, "/")+1:]

for _, chosenDevice := range h.Devices {
if chosenDevice == "*" || chosenDevice == device {
tags := map[string]string{
"device": device,
"model": fields[offset+2],
"unit": fields[offset+4],
"status": "",
}

temperatureField := fields[offset+3]
value, err := strconv.ParseFloat(temperatureField, 64)

if err != nil {
value = 0.0
tags["status"] = temperatureField
}

fields := map[string]interface{}{
device: value,
}

acc.AddFields("hddtemp", fields, tags)
}
}
}

return nil
}

func init() {
inputs.Add("hddtemp", func() telegraf.Input {
return &HDDTemp{
Address: defaultAddress,
Devices: []string{"*"},
}
})
}
3 changes: 3 additions & 0 deletions plugins/inputs/hddtemp/hddtemp_nocompile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// +build !linux !hddtemp

package hddtemp

0 comments on commit a2efcd3

Please sign in to comment.