1 // Copyright 2019 The Prometheus Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package iscsi 16 17 import ( 18 "path/filepath" 19 "strings" 20 21 "github.com/prometheus/procfs/internal/fs" 22 ) 23 24 // iscsi target started with /sys/kernel/config/target/iscsi/iqn* 25 // configfs + target/iscsi/iqn* 26 // iqnGlob is representing all the possible IQN. 27 const iqnGlob = "target/iscsi/iqn*" 28 29 // targetCore static path /sys/kernel/config/target/core for node_exporter 30 // reading runtime status. 31 const targetCore = "target/core" 32 33 // devicePath static path /sys/devices/rbd/[0-9]* for rbd devices to 34 // read at runtime status. 35 const devicePath = "devices/rbd" 36 37 // FS represents the pseudo-filesystem configfs, which provides an interface to 38 // iscsi kernel data structures in sysfs as /sys and configfs as /sys/kernel/config. 39 type FS struct { 40 sysfs *fs.FS 41 configfs *fs.FS 42 } 43 44 // NewFS returns a new configfs mounted under the given mount point. It will 45 // error and return empty FS if the mount point can't be read. For the ease of 46 // use, an empty string parameter configfsMountPoint will call internal fs for 47 // the default sys path as /sys/kernel/config. 48 func NewFS(sysfsPath string, configfsMountPoint string) (FS, error) { 49 if strings.TrimSpace(sysfsPath) == "" { 50 sysfsPath = fs.DefaultSysMountPoint 51 } 52 sysfs, err := fs.NewFS(sysfsPath) 53 if err != nil { 54 return FS{}, err 55 } 56 if strings.TrimSpace(configfsMountPoint) == "" { 57 configfsMountPoint = fs.DefaultConfigfsMountPoint 58 } 59 configfs, err := fs.NewFS(configfsMountPoint) 60 if err != nil { 61 return FS{}, err 62 } 63 return FS{&sysfs, &configfs}, nil 64 } 65 66 // Path is a helper function to get configfs path. 67 func (fs FS) Path(p ...string) string { 68 return fs.configfs.Path(p...) 69 } 70 71 // ISCSIStats getting iscsi runtime information. 72 func (fs FS) ISCSIStats() ([]*Stats, error) { 73 matches, err := filepath.Glob(fs.configfs.Path(iqnGlob)) 74 if err != nil { 75 return nil, err 76 } 77 78 stats := make([]*Stats, 0, len(matches)) 79 for _, iqnPath := range matches { 80 // stats 81 s, err := GetStats(iqnPath) 82 if err != nil { 83 return nil, err 84 } 85 stats = append(stats, s) 86 } 87 88 return stats, nil 89 } 90 91 // TPGT struct for sys target portal group tag info. 92 type TPGT struct { 93 Name string // name of the tpgt group 94 TpgtPath string // file path of tpgt 95 IsEnable bool // is the tpgt enable 96 Luns []LUN // the Luns that tpgt has 97 } 98 99 // LUN struct for sys logical unit number info. 100 type LUN struct { 101 Name string // name of the lun 102 LunPath string // file path of the lun 103 Backstore string // backstore of the lun 104 ObjectName string // place holder for object 105 TypeNumber string // place holder for number of the device 106 } 107 108 // FILEIO struct for backstore info. 109 type FILEIO struct { 110 Name string // name of the fileio 111 Fnumber string // number related to the backstore 112 ObjectName string // place holder for object in iscsi object 113 Filename string // link to the actual file being export 114 } 115 116 // IBLOCK struct for backstore info. 117 type IBLOCK struct { 118 Name string // name of the iblock 119 Bnumber string // number related to the backstore 120 ObjectName string // place holder for object in iscsi object 121 Iblock string // link to the actual block being export 122 } 123 124 // RBD struct for backstore info. 125 type RBD struct { 126 Name string // name of the rbd 127 Rnumber string // number related to the backstore 128 Pool string // place holder for the rbd pool 129 Image string // place holder for the rbd image 130 } 131 132 // RDMCP struct for backstore info. 133 type RDMCP struct { 134 Name string // name of the rdm_cp 135 ObjectName string // place holder for object name 136 } 137 138 // Stats struct for all targets info. 139 type Stats struct { 140 Name string 141 Tpgt []TPGT 142 RootPath string 143 } 144