forked from docker-archive/infra-container_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevicemap.go
More file actions
54 lines (48 loc) · 1022 Bytes
/
devicemap.go
File metadata and controls
54 lines (48 loc) · 1022 Bytes
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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
const (
procDiskStats = "/proc/diskstats"
unknownFormatStr = "unknown(%d,%d)"
)
type deviceMap map[int]map[int]string
func newDeviceMap(filename string) (dm deviceMap, err error) {
dm = make(deviceMap)
file, err := os.Open(filename)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := string(scanner.Text())
parts := strings.Fields(line)
if len(parts) <= 3 {
return nil, fmt.Errorf("Invalid line in %s: %s", filename, line)
}
major, err := strconv.Atoi(parts[0])
if err != nil {
return nil, err
}
minor, err := strconv.Atoi(parts[1])
if err != nil {
return nil, err
}
if _, ok := dm[major]; !ok {
dm[major] = map[int]string{}
}
dm[major][minor] = parts[2]
}
return dm, nil
}
func (dm deviceMap) name(major, minor uint64) string {
name, ok := dm[int(major)][int(minor)]
if ok {
return name
}
return fmt.Sprintf(unknownFormatStr, major, minor)
}