2015-09-26 15:36:40 +00:00
// Copyright 2015 The Prometheus Authors
// 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.
2015-05-12 11:06:41 +00:00
// +build !nodiskstats
2014-06-04 11:12:34 +00:00
package collector
import (
"bufio"
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
2019-12-31 16:19:37 +00:00
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
2014-06-04 11:12:34 +00:00
"github.com/prometheus/client_golang/prometheus"
2017-08-12 13:07:24 +00:00
"gopkg.in/alecthomas/kingpin.v2"
2014-06-04 11:12:34 +00:00
)
const (
2018-10-15 15:24:28 +00:00
diskSectorSize = 512
diskstatsFilename = "diskstats"
2014-06-04 11:12:34 +00:00
)
var (
2017-08-12 13:07:24 +00:00
ignoredDevices = kingpin . Flag ( "collector.diskstats.ignored-devices" , "Regexp of devices to ignore for diskstats." ) . Default ( "^(ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\\d+n\\d+p)\\d+$" ) . String ( )
2014-06-04 11:12:34 +00:00
)
2018-01-17 16:55:55 +00:00
type typedFactorDesc struct {
desc * prometheus . Desc
valueType prometheus . ValueType
factor float64
}
func ( d * typedFactorDesc ) mustNewConstMetric ( value float64 , labels ... string ) prometheus . Metric {
if d . factor != 0 {
value *= d . factor
}
return prometheus . MustNewConstMetric ( d . desc , d . valueType , value , labels ... )
}
2014-06-04 11:12:34 +00:00
type diskstatsCollector struct {
ignoredDevicesPattern * regexp . Regexp
2018-01-17 16:55:55 +00:00
descs [ ] typedFactorDesc
2019-12-31 16:19:37 +00:00
logger log . Logger
2014-06-04 11:12:34 +00:00
}
func init ( ) {
2017-09-28 13:06:26 +00:00
registerCollector ( "diskstats" , defaultEnabled , NewDiskstatsCollector )
2014-06-04 11:12:34 +00:00
}
2017-02-28 16:44:53 +00:00
// NewDiskstatsCollector returns a new Collector exposing disk device stats.
2018-10-15 15:24:28 +00:00
// Docs from https://www.kernel.org/doc/Documentation/iostats.txt
2019-12-31 16:19:37 +00:00
func NewDiskstatsCollector ( logger log . Logger ) ( Collector , error ) {
2014-11-25 02:00:17 +00:00
var diskLabelNames = [ ] string { "device" }
return & diskstatsCollector {
2014-06-04 11:12:34 +00:00
ignoredDevicesPattern : regexp . MustCompile ( * ignoredDevices ) ,
2018-01-17 16:55:55 +00:00
descs : [ ] typedFactorDesc {
2016-12-28 14:21:31 +00:00
{
2019-02-06 10:36:22 +00:00
desc : readsCompletedDesc , valueType : prometheus . CounterValue ,
2016-12-28 14:21:31 +00:00
} ,
{
desc : prometheus . NewDesc (
2018-01-17 16:55:55 +00:00
prometheus . BuildFQName ( namespace , diskSubsystem , "reads_merged_total" ) ,
2018-10-15 15:24:28 +00:00
"The total number of reads merged." ,
2016-12-28 14:21:31 +00:00
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
} ,
{
2019-02-06 10:36:22 +00:00
desc : readBytesDesc , valueType : prometheus . CounterValue ,
2018-01-17 16:55:55 +00:00
factor : diskSectorSize ,
2016-12-28 14:21:31 +00:00
} ,
{
2019-02-06 10:36:22 +00:00
desc : readTimeSecondsDesc , valueType : prometheus . CounterValue ,
2018-01-17 16:55:55 +00:00
factor : .001 ,
2016-12-28 14:21:31 +00:00
} ,
{
2019-02-06 10:36:22 +00:00
desc : writesCompletedDesc , valueType : prometheus . CounterValue ,
2016-12-28 14:21:31 +00:00
} ,
{
desc : prometheus . NewDesc (
2018-01-17 16:55:55 +00:00
prometheus . BuildFQName ( namespace , diskSubsystem , "writes_merged_total" ) ,
2018-10-15 15:24:28 +00:00
"The number of writes merged." ,
2016-12-28 14:21:31 +00:00
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
} ,
{
2019-02-06 10:36:22 +00:00
desc : writtenBytesDesc , valueType : prometheus . CounterValue ,
2018-01-17 16:55:55 +00:00
factor : diskSectorSize ,
2016-12-28 14:21:31 +00:00
} ,
{
2019-02-06 10:36:22 +00:00
desc : writeTimeSecondsDesc , valueType : prometheus . CounterValue ,
2018-01-17 16:55:55 +00:00
factor : .001 ,
2016-12-28 14:21:31 +00:00
} ,
{
desc : prometheus . NewDesc (
2017-09-28 13:06:26 +00:00
prometheus . BuildFQName ( namespace , diskSubsystem , "io_now" ) ,
2016-12-28 14:21:31 +00:00
"The number of I/Os currently in progress." ,
diskLabelNames ,
nil ,
) , valueType : prometheus . GaugeValue ,
} ,
{
2019-02-06 10:36:22 +00:00
desc : ioTimeSecondsDesc , valueType : prometheus . CounterValue ,
2018-01-17 16:55:55 +00:00
factor : .001 ,
2016-12-28 14:21:31 +00:00
} ,
{
desc : prometheus . NewDesc (
2018-01-17 16:55:55 +00:00
prometheus . BuildFQName ( namespace , diskSubsystem , "io_time_weighted_seconds_total" ) ,
2018-10-15 15:24:28 +00:00
"The weighted # of seconds spent doing I/Os." ,
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
factor : .001 ,
} ,
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( namespace , diskSubsystem , "discards_completed_total" ) ,
"The total number of discards completed successfully." ,
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
} ,
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( namespace , diskSubsystem , "discards_merged_total" ) ,
"The total number of discards merged." ,
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
} ,
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( namespace , diskSubsystem , "discarded_sectors_total" ) ,
2018-10-30 17:45:00 +00:00
"The total number of sectors discarded successfully." ,
2018-10-15 15:24:28 +00:00
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
} ,
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( namespace , diskSubsystem , "discard_time_seconds_total" ) ,
"This is the total number of seconds spent by all discards." ,
2016-12-28 14:21:31 +00:00
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
2018-01-17 16:55:55 +00:00
factor : .001 ,
2016-12-28 14:21:31 +00:00
} ,
2019-11-25 19:16:15 +00:00
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( namespace , diskSubsystem , "flush_requests_total" ) ,
"The total number of flush requests completed successfully" ,
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
} ,
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( namespace , diskSubsystem , "flush_requests_time_seconds_total" ) ,
"This is the total number of seconds spent by all flush requests." ,
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
factor : .001 ,
} ,
2014-11-25 02:00:17 +00:00
} ,
2019-12-31 16:19:37 +00:00
logger : logger ,
2014-11-25 02:00:17 +00:00
} , nil
2014-06-04 11:12:34 +00:00
}
2016-12-28 14:21:31 +00:00
func ( c * diskstatsCollector ) Update ( ch chan <- prometheus . Metric ) error {
2014-06-04 11:12:34 +00:00
diskStats , err := getDiskStats ( )
if err != nil {
2020-06-15 20:27:14 +00:00
return fmt . Errorf ( "couldn't get diskstats: %w" , err )
2014-06-04 11:12:34 +00:00
}
2014-11-25 02:00:17 +00:00
2014-06-04 11:12:34 +00:00
for dev , stats := range diskStats {
if c . ignoredDevicesPattern . MatchString ( dev ) {
2019-12-31 16:19:37 +00:00
level . Debug ( c . logger ) . Log ( "msg" , "Ignoring device" , "device" , dev )
2014-06-04 11:12:34 +00:00
continue
}
2014-11-25 02:00:17 +00:00
2016-12-28 14:21:31 +00:00
for i , value := range stats {
2018-10-30 17:45:00 +00:00
// ignore unrecognized additional stats
if i >= len ( c . descs ) {
break
}
2014-06-04 11:12:34 +00:00
v , err := strconv . ParseFloat ( value , 64 )
if err != nil {
2020-06-15 20:27:14 +00:00
return fmt . Errorf ( "invalid value %s in diskstats: %w" , value , err )
2014-06-04 11:12:34 +00:00
}
2016-12-28 14:21:31 +00:00
ch <- c . descs [ i ] . mustNewConstMetric ( v , dev )
2014-06-04 11:12:34 +00:00
}
}
2016-12-28 14:21:31 +00:00
return nil
2014-06-04 11:12:34 +00:00
}
2018-10-15 15:24:28 +00:00
func getDiskStats ( ) ( map [ string ] [ ] string , error ) {
file , err := os . Open ( procFilePath ( diskstatsFilename ) )
2014-06-04 11:12:34 +00:00
if err != nil {
return nil , err
}
2014-11-24 23:30:07 +00:00
defer file . Close ( )
2014-06-04 11:12:34 +00:00
return parseDiskStats ( file )
}
2018-10-15 15:24:28 +00:00
func parseDiskStats ( r io . Reader ) ( map [ string ] [ ] string , error ) {
2014-11-24 23:30:07 +00:00
var (
2018-10-15 15:24:28 +00:00
diskStats = map [ string ] [ ] string { }
2014-11-24 23:30:07 +00:00
scanner = bufio . NewScanner ( r )
)
2014-06-04 11:12:34 +00:00
for scanner . Scan ( ) {
2017-02-28 17:42:43 +00:00
parts := strings . Fields ( scanner . Text ( ) )
2014-11-25 02:00:17 +00:00
if len ( parts ) < 4 { // we strip major, minor and dev
2018-10-15 15:24:28 +00:00
return nil , fmt . Errorf ( "invalid line in %s: %s" , procFilePath ( diskstatsFilename ) , scanner . Text ( ) )
2014-06-04 11:12:34 +00:00
}
dev := parts [ 2 ]
2018-10-15 15:24:28 +00:00
diskStats [ dev ] = parts [ 3 : ]
2014-06-04 11:12:34 +00:00
}
2014-11-24 23:30:07 +00:00
2017-02-28 18:31:35 +00:00
return diskStats , scanner . Err ( )
2014-06-04 11:12:34 +00:00
}