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
mendelgusmao committed Jun 27, 2016
1 parent f62c493 commit 8e8c194
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ should now look like:
- [#1402](https://github.com/influxdata/telegraf/pull/1402): docker-machine/boot2docker no longer required for unit tests.
- [#1350](https://github.com/influxdata/telegraf/pull/1350): cgroup input plugin.
- [#1369](https://github.com/influxdata/telegraf/pull/1369): Add input plugin for consuming metrics from NSQD.
- [#1411](https://github.com/influxdata/telegraf/pull/1411): Implement support for fetching hddtemp data

### Bugfixes

Expand Down
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
21 changes: 21 additions & 0 deletions plugins/inputs/hddtemp/go-hddtemp/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Mendelson Gusmão

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.
61 changes: 61 additions & 0 deletions plugins/inputs/hddtemp/go-hddtemp/hddtemp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package hddtemp

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

type disk struct {
DeviceName string
Model string
Temperature int32
Unit string
Status string
}

func Fetch(address string) ([]disk, error) {
var (
err error
conn net.Conn
buffer bytes.Buffer
disks []disk
)

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

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

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

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

temperatureField := fields[offset+3]
temperature, err := strconv.ParseInt(temperatureField, 10, 32)

if err != nil {
temperature = 0
status = temperatureField
}

disks = append(disks, disk{
DeviceName: device,
Model: fields[offset+2],
Temperature: int32(temperature),
Unit: fields[offset+4],
Status: status,
})
}

return disks, nil
}
116 changes: 116 additions & 0 deletions plugins/inputs/hddtemp/go-hddtemp/hddtemp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package hddtemp

import (
"net"
"reflect"
"testing"
)

func TestFetch(t *testing.T) {
l := serve(t, []byte("|/dev/sda|foobar|36|C|"))
defer l.Close()

disks, err := Fetch(l.Addr().String())

if err != nil {
t.Error("expecting err to be nil")
}

expected := []disk{
{
DeviceName: "sda",
Model: "foobar",
Temperature: 36,
Unit: "C",
},
}

if !reflect.DeepEqual(expected, disks) {
t.Error("disks' slice is different from expected")
}
}

func TestFetchWrongAddress(t *testing.T) {
_, err := Fetch("127.0.0.1:1")

if err == nil {
t.Error("expecting err to be non-nil")
}
}

func TestFetchStatus(t *testing.T) {
l := serve(t, []byte("|/dev/sda|foobar|SLP|C|"))
defer l.Close()

disks, err := Fetch(l.Addr().String())

if err != nil {
t.Error("expecting err to be nil")
}

expected := []disk{
{
DeviceName: "sda",
Model: "foobar",
Temperature: 0,
Unit: "C",
Status: "SLP",
},
}

if !reflect.DeepEqual(expected, disks) {
t.Error("disks' slice is different from expected")
}
}

func TestFetchTwoDisks(t *testing.T) {
l := serve(t, []byte("|/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|"))
defer l.Close()

disks, err := Fetch(l.Addr().String())

if err != nil {
t.Error("expecting err to be nil")
}

expected := []disk{
{
DeviceName: "hda",
Model: "ST380011A",
Temperature: 46,
Unit: "C",
},
{
DeviceName: "hdd",
Model: "ST340016A",
Temperature: 0,
Unit: "*",
Status: "SLP",
},
}

if !reflect.DeepEqual(expected, disks) {
t.Error("disks' slice is different from expected")
}
}

func serve(t *testing.T, data []byte) net.Listener {
l, err := net.Listen("tcp", "127.0.0.1:0")

if err != nil {
t.Fatal(err)
}

go func(t *testing.T) {
conn, err := l.Accept()

if err != nil {
t.Fatal(err)
}

conn.Write(data)
conn.Close()
}(t)

return l
}
74 changes: 74 additions & 0 deletions plugins/inputs/hddtemp/hddtemp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// +build linux

package hddtemp

import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/inputs/hddtemp/go-hddtemp"
)

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 {
disks, err := hddtemp.Fetch(h.Address)

if err != nil {
return err
}

for _, disk := range disks {
for _, chosenDevice := range h.Devices {
if chosenDevice == "*" || chosenDevice == disk.DeviceName {
tags := map[string]string{
"device": disk.DeviceName,
"model": disk.Model,
"unit": disk.Unit,
"status": disk.Status,
}

fields := map[string]interface{}{
disk.DeviceName: disk.Temperature,
}

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

package hddtemp

0 comments on commit 8e8c194

Please sign in to comment.