-
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
Mendelson Gusmão
committed
Jun 23, 2016
1 parent
f62c493
commit a2efcd3
Showing
4 changed files
with
106 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
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{"*"}, | ||
} | ||
}) | ||
} |
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 !hddtemp | ||
|
||
package hddtemp |