-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for fetching hddtemp data
- Loading branch information
1 parent
f62c493
commit 8e8c194
Showing
8 changed files
with
278 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
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
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
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 @@ | ||
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. |
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,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 | ||
} |
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,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 | ||
} |
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,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{"*"}, | ||
} | ||
}) | ||
} |
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,3 @@ | ||
// +build !linux | ||
|
||
package hddtemp |