1
- // ipmi
2
- package ipmi
1
+ package ipmi_sensor
3
2
4
3
import (
5
4
"strconv"
@@ -60,30 +59,41 @@ func (m *Ipmi) gatherServer(serv string, acc telegraf.Accumulator) error {
60
59
return err
61
60
}
62
61
62
+ // each line will look something like
63
+ // Planar VBAT | 3.05 Volts | ok
63
64
lines := strings .Split (res , "\n " )
64
-
65
65
for i := 0 ; i < len (lines ); i ++ {
66
66
vals := strings .Split (lines [i ], "|" )
67
- if len (vals ) == 3 {
68
- tags := map [string ]string {"server" : conn .Hostname , "name" : trim (vals [0 ])}
69
- fields := make (map [string ]interface {})
70
- if strings .EqualFold ("ok" , trim (vals [2 ])) {
71
- fields ["status" ] = 1
72
- } else {
73
- fields ["status" ] = 0
74
- }
67
+ if len (vals ) != 3 {
68
+ continue
69
+ }
75
70
76
- val1 := trim (vals [1 ])
71
+ tags := map [string ]string {
72
+ "server" : conn .Hostname ,
73
+ "name" : transform (vals [0 ]),
74
+ }
77
75
78
- if strings . Index ( val1 , " " ) > 0 {
79
- val := strings .Split ( val1 , " " )[ 0 ]
80
- fields ["value " ] = Atofloat ( val )
81
- } else {
82
- fields ["value " ] = 0. 0
83
- }
76
+ fields := make ( map [ string ] interface {})
77
+ if strings .EqualFold ( "ok" , trim ( vals [ 2 ])) {
78
+ fields ["status " ] = 1
79
+ } else {
80
+ fields ["status " ] = 0
81
+ }
84
82
85
- acc .AddFields ("ipmi_sensor" , fields , tags , time .Now ())
83
+ val1 := trim (vals [1 ])
84
+
85
+ if strings .Index (val1 , " " ) > 0 {
86
+ // split middle column into value and unit
87
+ valunit := strings .SplitN (val1 , " " , 2 )
88
+ fields ["value" ] = Atofloat (valunit [0 ])
89
+ if len (valunit ) > 1 {
90
+ tags ["unit" ] = transform (valunit [1 ])
91
+ }
92
+ } else {
93
+ fields ["value" ] = 0.0
86
94
}
95
+
96
+ acc .AddFields ("ipmi_sensor" , fields , tags , time .Now ())
87
97
}
88
98
89
99
return nil
@@ -96,18 +106,24 @@ type Runner interface {
96
106
func Atofloat (val string ) float64 {
97
107
f , err := strconv .ParseFloat (val , 64 )
98
108
if err != nil {
99
- return float64 ( 0 )
109
+ return 0.0
100
110
} else {
101
- return float64 ( f )
111
+ return f
102
112
}
103
113
}
104
114
105
115
func trim (s string ) string {
106
116
return strings .TrimSpace (s )
107
117
}
108
118
119
+ func transform (s string ) string {
120
+ s = trim (s )
121
+ s = strings .ToLower (s )
122
+ return strings .Replace (s , " " , "_" , - 1 )
123
+ }
124
+
109
125
func init () {
110
- inputs .Add ("ipmi " , func () telegraf.Input {
126
+ inputs .Add ("ipmi_sensor " , func () telegraf.Input {
111
127
return & Ipmi {}
112
128
})
113
129
}
0 commit comments