2015-09-26 08:36:40 -07: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 04:06:41 -07:00
// +build !nodiskstats
2014-06-04 04:12:34 -07:00
package collector
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
2015-10-30 13:20:06 -07:00
"github.com/prometheus/common/log"
2014-06-04 04:12:34 -07:00
)
const (
2015-12-26 17:59:02 -08:00
diskSubsystem = "disk"
diskSectorSize uint64 = 512
2014-06-04 04:12:34 -07:00
)
var (
2016-07-09 04:23:01 -07:00
ignoredDevices = flag . String ( "collector.diskstats.ignored-devices" , "^(ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\\d+n\\d+p)\\d+$" , "Regexp of devices to ignore for diskstats." )
2014-06-04 04:12:34 -07:00
)
type diskstatsCollector struct {
ignoredDevicesPattern * regexp . Regexp
2016-12-28 06:21:31 -08:00
descs [ ] typedDesc
2014-06-04 04:12:34 -07:00
}
func init ( ) {
Factories [ "diskstats" ] = NewDiskstatsCollector
}
2017-02-28 08:44:53 -08:00
// NewDiskstatsCollector returns a new Collector exposing disk device stats.
2015-05-20 11:04:49 -07:00
func NewDiskstatsCollector ( ) ( Collector , error ) {
2014-11-24 18:00:17 -08:00
var diskLabelNames = [ ] string { "device" }
return & diskstatsCollector {
2014-06-04 04:12:34 -07:00
ignoredDevicesPattern : regexp . MustCompile ( * ignoredDevices ) ,
2014-11-24 18:00:17 -08:00
// Docs from https://www.kernel.org/doc/Documentation/iostats.txt
2016-12-28 06:21:31 -08:00
descs : [ ] typedDesc {
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( Namespace , diskSubsystem , "reads_completed" ) ,
"The total number of reads completed successfully." ,
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
} ,
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( Namespace , diskSubsystem , "reads_merged" ) ,
"The total number of reads merged. See https://www.kernel.org/doc/Documentation/iostats.txt." ,
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
} ,
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( Namespace , diskSubsystem , "sectors_read" ) ,
"The total number of sectors read successfully." ,
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
} ,
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( Namespace , diskSubsystem , "read_time_ms" ) ,
"The total number of milliseconds spent by all reads." ,
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
} ,
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( Namespace , diskSubsystem , "writes_completed" ) ,
"The total number of writes completed successfully." ,
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
} ,
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( Namespace , diskSubsystem , "writes_merged" ) ,
"The number of writes merged. See https://www.kernel.org/doc/Documentation/iostats.txt." ,
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
} ,
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( Namespace , diskSubsystem , "sectors_written" ) ,
"The total number of sectors written successfully." ,
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
} ,
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( Namespace , diskSubsystem , "write_time_ms" ) ,
"This is the total number of milliseconds spent by all writes." ,
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
} ,
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( Namespace , diskSubsystem , "io_now" ) ,
"The number of I/Os currently in progress." ,
diskLabelNames ,
nil ,
) , valueType : prometheus . GaugeValue ,
} ,
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( Namespace , diskSubsystem , "io_time_ms" ) ,
"Total Milliseconds spent doing I/Os." ,
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
} ,
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( Namespace , diskSubsystem , "io_time_weighted" ) ,
"The weighted # of milliseconds spent doing I/Os. See https://www.kernel.org/doc/Documentation/iostats.txt." ,
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
} ,
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( Namespace , diskSubsystem , "bytes_read" ) ,
"The total number of bytes read successfully." ,
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
} ,
{
desc : prometheus . NewDesc (
prometheus . BuildFQName ( Namespace , diskSubsystem , "bytes_written" ) ,
"The total number of bytes written successfully." ,
diskLabelNames ,
nil ,
) , valueType : prometheus . CounterValue ,
} ,
2014-11-24 18:00:17 -08:00
} ,
} , nil
2014-06-04 04:12:34 -07:00
}
2016-12-28 06:21:31 -08:00
func ( c * diskstatsCollector ) Update ( ch chan <- prometheus . Metric ) error {
2015-09-26 05:53:46 -07:00
procDiskStats := procFilePath ( "diskstats" )
2014-06-04 04:12:34 -07:00
diskStats , err := getDiskStats ( )
if err != nil {
2014-11-24 18:00:17 -08:00
return fmt . Errorf ( "couldn't get diskstats: %s" , err )
2014-06-04 04:12:34 -07:00
}
2014-11-24 18:00:17 -08:00
2014-06-04 04:12:34 -07:00
for dev , stats := range diskStats {
if c . ignoredDevicesPattern . MatchString ( dev ) {
2015-05-28 12:21:44 -07:00
log . Debugf ( "Ignoring device: %s" , dev )
2014-06-04 04:12:34 -07:00
continue
}
2014-11-24 18:00:17 -08:00
2016-12-28 06:21:31 -08:00
if len ( stats ) != len ( c . descs ) {
2014-11-24 18:00:17 -08:00
return fmt . Errorf ( "invalid line for %s for %s" , procDiskStats , dev )
}
2016-12-28 06:21:31 -08:00
for i , value := range stats {
2014-06-04 04:12:34 -07:00
v , err := strconv . ParseFloat ( value , 64 )
if err != nil {
2014-11-24 18:00:17 -08:00
return fmt . Errorf ( "invalid value %s in diskstats: %s" , value , err )
2014-06-04 04:12:34 -07:00
}
2016-12-28 06:21:31 -08:00
ch <- c . descs [ i ] . mustNewConstMetric ( v , dev )
2014-06-04 04:12:34 -07:00
}
}
2016-12-28 06:21:31 -08:00
return nil
2014-06-04 04:12:34 -07:00
}
func getDiskStats ( ) ( map [ string ] map [ int ] string , error ) {
2015-09-26 05:53:46 -07:00
file , err := os . Open ( procFilePath ( "diskstats" ) )
2014-06-04 04:12:34 -07:00
if err != nil {
return nil , err
}
2014-11-24 15:30:07 -08:00
defer file . Close ( )
2014-06-04 04:12:34 -07:00
return parseDiskStats ( file )
}
2015-12-26 17:59:02 -08:00
func convertDiskSectorsToBytes ( sectorCount string ) ( string , error ) {
sectors , err := strconv . ParseUint ( sectorCount , 10 , 64 )
if err != nil {
return "" , err
}
return strconv . FormatUint ( sectors * diskSectorSize , 10 ) , nil
}
2014-11-24 15:30:07 -08:00
func parseDiskStats ( r io . Reader ) ( map [ string ] map [ int ] string , error ) {
var (
diskStats = map [ string ] map [ int ] string { }
scanner = bufio . NewScanner ( r )
)
2014-06-04 04:12:34 -07:00
for scanner . Scan ( ) {
2017-02-28 09:42:43 -08:00
parts := strings . Fields ( scanner . Text ( ) )
2014-11-24 18:00:17 -08:00
if len ( parts ) < 4 { // we strip major, minor and dev
2015-09-26 05:53:46 -07:00
return nil , fmt . Errorf ( "invalid line in %s: %s" , procFilePath ( "diskstats" ) , scanner . Text ( ) )
2014-06-04 04:12:34 -07:00
}
dev := parts [ 2 ]
diskStats [ dev ] = map [ int ] string { }
for i , v := range parts [ 3 : ] {
diskStats [ dev ] [ i ] = v
}
2015-12-26 17:59:02 -08:00
bytesRead , err := convertDiskSectorsToBytes ( diskStats [ dev ] [ 2 ] )
if err != nil {
return nil , fmt . Errorf ( "invalid value for sectors read in %s: %s" , procFilePath ( "diskstats" ) , scanner . Text ( ) )
}
diskStats [ dev ] [ 11 ] = bytesRead
bytesWritten , err := convertDiskSectorsToBytes ( diskStats [ dev ] [ 6 ] )
if err != nil {
2016-02-09 20:22:29 -08:00
return nil , fmt . Errorf ( "invalid value for sectors written in %s: %s" , procFilePath ( "diskstats" ) , scanner . Text ( ) )
2015-12-26 17:59:02 -08:00
}
diskStats [ dev ] [ 12 ] = bytesWritten
2014-06-04 04:12:34 -07:00
}
2014-11-24 15:30:07 -08:00
2017-02-28 10:31:35 -08:00
return diskStats , scanner . Err ( )
2014-06-04 04:12:34 -07:00
}