2019-09-23 16:18:35 +00:00
// Copyright 2017-2019 The Prometheus Authors
2017-02-07 16:46:51 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build linux
// +build !noinfiniband
package collector
import (
2019-09-23 16:18:35 +00:00
"fmt"
"strconv"
2017-02-07 16:46:51 +00:00
2019-12-31 16:19:37 +00:00
"github.com/go-kit/kit/log"
2017-02-07 16:46:51 +00:00
"github.com/prometheus/client_golang/prometheus"
2019-09-23 16:18:35 +00:00
"github.com/prometheus/procfs/sysfs"
2017-02-07 16:46:51 +00:00
)
type infinibandCollector struct {
2019-09-23 16:18:35 +00:00
fs sysfs . FS
metricDescs map [ string ] * prometheus . Desc
2019-12-31 16:19:37 +00:00
logger log . Logger
2017-02-07 16:46:51 +00:00
}
func init ( ) {
2017-09-28 13:06:26 +00:00
registerCollector ( "infiniband" , defaultEnabled , NewInfiniBandCollector )
2017-02-07 16:46:51 +00:00
}
2017-02-28 16:44:53 +00:00
// NewInfiniBandCollector returns a new Collector exposing InfiniBand stats.
2019-12-31 16:19:37 +00:00
func NewInfiniBandCollector ( logger log . Logger ) ( Collector , error ) {
2017-02-07 16:46:51 +00:00
var i infinibandCollector
2019-09-23 16:18:35 +00:00
var err error
2017-02-07 16:46:51 +00:00
2019-09-23 16:18:35 +00:00
i . fs , err = sysfs . NewFS ( * sysPath )
if err != nil {
2019-11-29 13:51:31 +00:00
return nil , fmt . Errorf ( "failed to open sysfs: %w" , err )
2017-02-07 16:46:51 +00:00
}
2019-12-31 16:19:37 +00:00
i . logger = logger
2017-02-07 16:46:51 +00:00
2019-09-23 16:18:35 +00:00
// Detailed description for all metrics.
descriptions := map [ string ] string {
"legacy_multicast_packets_received_total" : "Number of multicast packets received" ,
"legacy_multicast_packets_transmitted_total" : "Number of multicast packets transmitted" ,
"legacy_data_received_bytes_total" : "Number of data octets received on all links" ,
"legacy_packets_received_total" : "Number of data packets received on all links" ,
"legacy_unicast_packets_received_total" : "Number of unicast packets received" ,
"legacy_unicast_packets_transmitted_total" : "Number of unicast packets transmitted" ,
"legacy_data_transmitted_bytes_total" : "Number of data octets transmitted on all links" ,
"legacy_packets_transmitted_total" : "Number of data packets received on all links" ,
"link_downed_total" : "Number of times the link failed to recover from an error state and went down" ,
"link_error_recovery_total" : "Number of times the link successfully recovered from an error state" ,
"multicast_packets_received_total" : "Number of multicast packets received (including errors)" ,
"multicast_packets_transmitted_total" : "Number of multicast packets transmitted (including errors)" ,
2019-11-22 21:52:17 +00:00
"physical_state_id" : "Physical state of the InfiniBand port (0: no change, 1: sleep, 2: polling, 3: disable, 4: shift, 5: link up, 6: link error recover, 7: phytest)" ,
2019-09-23 16:18:35 +00:00
"port_constraint_errors_received_total" : "Number of packets received on the switch physical port that are discarded" ,
"port_constraint_errors_transmitted_total" : "Number of packets not transmitted from the switch physical port" ,
"port_data_received_bytes_total" : "Number of data octets received on all links" ,
"port_data_transmitted_bytes_total" : "Number of data octets transmitted on all links" ,
"port_discards_received_total" : "Number of inbound packets discarded by the port because the port is down or congested" ,
"port_discards_transmitted_total" : "Number of outbound packets discarded by the port because the port is down or congested" ,
"port_errors_received_total" : "Number of packets containing an error that were received on this port" ,
"port_packets_received_total" : "Number of packets received on all VLs by this port (including errors)" ,
"port_packets_transmitted_total" : "Number of packets transmitted on all VLs from this port (including errors)" ,
"port_transmit_wait_total" : "Number of ticks during which the port had data to transmit but no data was sent during the entire tick" ,
2019-11-22 21:52:17 +00:00
"rate_bytes_per_second" : "Maximum signal transfer rate" ,
"state_id" : "State of the InfiniBand port (0: no change, 1: down, 2: init, 3: armed, 4: active, 5: act defer)" ,
2019-09-23 16:18:35 +00:00
"unicast_packets_received_total" : "Number of unicast packets received (including errors)" ,
"unicast_packets_transmitted_total" : "Number of unicast packets transmitted (including errors)" ,
2017-03-09 22:10:36 +00:00
}
2017-02-07 16:46:51 +00:00
i . metricDescs = make ( map [ string ] * prometheus . Desc )
2019-09-23 16:18:35 +00:00
for metricName , description := range descriptions {
2017-03-09 22:10:36 +00:00
i . metricDescs [ metricName ] = prometheus . NewDesc (
2019-09-23 16:18:35 +00:00
prometheus . BuildFQName ( namespace , "infiniband" , metricName ) ,
description ,
2017-03-09 22:10:36 +00:00
[ ] string { "device" , "port" } ,
nil ,
)
}
2017-02-07 16:46:51 +00:00
return & i , nil
}
2019-09-23 16:18:35 +00:00
func ( c * infinibandCollector ) pushMetric ( ch chan <- prometheus . Metric , name string , value uint64 , deviceName string , port string , valueType prometheus . ValueType ) {
ch <- prometheus . MustNewConstMetric ( c . metricDescs [ name ] , valueType , float64 ( value ) , deviceName , port )
2017-02-07 16:46:51 +00:00
}
2019-09-23 16:18:35 +00:00
func ( c * infinibandCollector ) pushCounter ( ch chan <- prometheus . Metric , name string , value * uint64 , deviceName string , port string ) {
if value != nil {
c . pushMetric ( ch , name , * value , deviceName , port , prometheus . CounterValue )
2017-05-12 05:28:53 +00:00
}
2017-02-07 16:46:51 +00:00
}
2017-02-28 18:47:20 +00:00
func ( c * infinibandCollector ) Update ( ch chan <- prometheus . Metric ) error {
2019-09-23 16:18:35 +00:00
devices , err := c . fs . InfiniBandClass ( )
if err != nil {
return fmt . Errorf ( "error obtaining InfiniBand class info: %s" , err )
2017-02-07 16:46:51 +00:00
}
for _ , device := range devices {
2019-09-23 16:18:35 +00:00
for _ , port := range device . Ports {
portStr := strconv . FormatUint ( uint64 ( port . Port ) , 10 )
2019-11-22 21:52:17 +00:00
c . pushMetric ( ch , "state_id" , uint64 ( port . StateID ) , port . Name , portStr , prometheus . GaugeValue )
c . pushMetric ( ch , "physical_state_id" , uint64 ( port . PhysStateID ) , port . Name , portStr , prometheus . GaugeValue )
c . pushMetric ( ch , "rate_bytes_per_second" , port . Rate , port . Name , portStr , prometheus . GaugeValue )
2019-09-23 16:18:35 +00:00
c . pushCounter ( ch , "legacy_multicast_packets_received_total" , port . Counters . LegacyPortMulticastRcvPackets , port . Name , portStr )
c . pushCounter ( ch , "legacy_multicast_packets_transmitted_total" , port . Counters . LegacyPortMulticastXmitPackets , port . Name , portStr )
c . pushCounter ( ch , "legacy_data_received_bytes_total" , port . Counters . LegacyPortRcvData64 , port . Name , portStr )
c . pushCounter ( ch , "legacy_packets_received_total" , port . Counters . LegacyPortRcvPackets64 , port . Name , portStr )
c . pushCounter ( ch , "legacy_unicast_packets_received_total" , port . Counters . LegacyPortUnicastRcvPackets , port . Name , portStr )
c . pushCounter ( ch , "legacy_unicast_packets_transmitted_total" , port . Counters . LegacyPortUnicastXmitPackets , port . Name , portStr )
c . pushCounter ( ch , "legacy_data_transmitted_bytes_total" , port . Counters . LegacyPortXmitData64 , port . Name , portStr )
c . pushCounter ( ch , "legacy_packets_transmitted_total" , port . Counters . LegacyPortXmitPackets64 , port . Name , portStr )
c . pushCounter ( ch , "link_downed_total" , port . Counters . LinkDowned , port . Name , portStr )
c . pushCounter ( ch , "link_error_recovery_total" , port . Counters . LinkErrorRecovery , port . Name , portStr )
c . pushCounter ( ch , "multicast_packets_received_total" , port . Counters . MulticastRcvPackets , port . Name , portStr )
c . pushCounter ( ch , "multicast_packets_transmitted_total" , port . Counters . MulticastXmitPackets , port . Name , portStr )
c . pushCounter ( ch , "port_constraint_errors_received_total" , port . Counters . PortRcvConstraintErrors , port . Name , portStr )
c . pushCounter ( ch , "port_constraint_errors_transmitted_total" , port . Counters . PortXmitConstraintErrors , port . Name , portStr )
c . pushCounter ( ch , "port_data_received_bytes_total" , port . Counters . PortRcvData , port . Name , portStr )
c . pushCounter ( ch , "port_data_transmitted_bytes_total" , port . Counters . PortXmitData , port . Name , portStr )
c . pushCounter ( ch , "port_discards_received_total" , port . Counters . PortRcvDiscards , port . Name , portStr )
c . pushCounter ( ch , "port_discards_transmitted_total" , port . Counters . PortXmitDiscards , port . Name , portStr )
c . pushCounter ( ch , "port_errors_received_total" , port . Counters . PortRcvErrors , port . Name , portStr )
c . pushCounter ( ch , "port_packets_received_total" , port . Counters . PortRcvPackets , port . Name , portStr )
c . pushCounter ( ch , "port_packets_transmitted_total" , port . Counters . PortXmitPackets , port . Name , portStr )
c . pushCounter ( ch , "port_transmit_wait_total" , port . Counters . PortXmitWait , port . Name , portStr )
c . pushCounter ( ch , "unicast_packets_received_total" , port . Counters . UnicastRcvPackets , port . Name , portStr )
c . pushCounter ( ch , "unicast_packets_transmitted_total" , port . Counters . UnicastXmitPackets , port . Name , portStr )
2017-02-07 16:46:51 +00:00
}
}
return nil
}