1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
// Copyright 2016 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.
// +build !nozfs
package collector
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
// constants from https://github.com/zfsonlinux/zfs/blob/master/lib/libspl/include/sys/kstat.h
// kept as strings for comparison thus avoiding conversion to int
const (
kstatDataChar = "0"
kstatDataInt32 = "1"
kstatDataUint32 = "2"
kstatDataInt64 = "3"
kstatDataUint64 = "4"
kstatDataLong = "5"
kstatDataUlong = "6"
kstatDataString = "7"
)
func (c *zfsCollector) openProcFile(path string) (*os.File, error) {
file, err := os.Open(procFilePath(path))
if err != nil {
// file not found error can occur if:
// 1. zfs module is not loaded
// 2. zfs version does not have the feature with metrics -- ok to ignore
log.Debugf("Cannot open %q for reading", procFilePath(path))
return nil, errZFSNotAvailable
}
return file, nil
}
func (c *zfsCollector) updateZfsStats(subsystem string, ch chan<- prometheus.Metric) error {
file, err := c.openProcFile(filepath.Join(c.linuxProcpathBase, c.linuxPathMap[subsystem]))
if err != nil {
return err
}
defer file.Close()
return c.parseProcfsFile(file, c.linuxPathMap[subsystem], func(s zfsSysctl, v uint64) {
ch <- c.constSysctlMetric(subsystem, s, v)
})
}
func (c *zfsCollector) updatePoolStats(ch chan<- prometheus.Metric) error {
zpoolPaths, err := filepath.Glob(procFilePath(filepath.Join(c.linuxProcpathBase, c.linuxZpoolIoPath)))
if err != nil {
return err
}
if zpoolPaths == nil {
return nil
}
for _, zpoolPath := range zpoolPaths {
file, err := os.Open(zpoolPath)
if err != nil {
// this file should exist, but there is a race where an exporting pool can remove the files -- ok to ignore
log.Debugf("Cannot open %q for reading", zpoolPath)
return errZFSNotAvailable
}
err = c.parsePoolProcfsFile(file, zpoolPath, func(poolName string, s zfsSysctl, v uint64) {
ch <- c.constPoolMetric(poolName, s, v)
})
file.Close()
if err != nil {
return err
}
}
return nil
}
func (c *zfsCollector) parseProcfsFile(reader io.Reader, fmtExt string, handler func(zfsSysctl, uint64)) error {
scanner := bufio.NewScanner(reader)
parseLine := false
for scanner.Scan() {
parts := strings.Fields(scanner.Text())
if !parseLine && len(parts) == 3 && parts[0] == "name" && parts[1] == "type" && parts[2] == "data" {
// Start parsing from here.
parseLine = true
continue
}
if !parseLine || len(parts) < 3 {
continue
}
// kstat data type (column 2) should be KSTAT_DATA_UINT64, otherwise ignore
// TODO: when other KSTAT_DATA_* types arrive, much of this will need to be restructured
if parts[1] == kstatDataUint64 {
key := fmt.Sprintf("kstat.zfs.misc.%s.%s", fmtExt, parts[0])
value, err := strconv.ParseUint(parts[2], 10, 64)
if err != nil {
return fmt.Errorf("could not parse expected integer value for %q", key)
}
handler(zfsSysctl(key), value)
}
}
if !parseLine {
return fmt.Errorf("did not parse a single %q metric", fmtExt)
}
return scanner.Err()
}
func (c *zfsCollector) parsePoolProcfsFile(reader io.Reader, zpoolPath string, handler func(string, zfsSysctl, uint64)) error {
scanner := bufio.NewScanner(reader)
parseLine := false
var fields []string
for scanner.Scan() {
line := strings.Fields(scanner.Text())
if !parseLine && len(line) >= 12 && line[0] == "nread" {
//Start parsing from here.
parseLine = true
fields = make([]string, len(line))
copy(fields, line)
continue
}
if !parseLine {
continue
}
zpoolPathElements := strings.Split(zpoolPath, "/")
pathLen := len(zpoolPathElements)
if pathLen < 2 {
return fmt.Errorf("zpool path did not return at least two elements")
}
zpoolName := zpoolPathElements[pathLen-2]
zpoolFile := zpoolPathElements[pathLen-1]
for i, field := range fields {
key := fmt.Sprintf("kstat.zfs.misc.%s.%s", zpoolFile, field)
value, err := strconv.ParseUint(line[i], 10, 64)
if err != nil {
return fmt.Errorf("could not parse expected integer value for %q: %v", key, err)
}
handler(zpoolName, zfsSysctl(key), value)
}
}
return scanner.Err()
}
|