fix(inputs.lvm): add options to specify path to binaries (#12725)

This commit is contained in:
Pierrick Brossin 2023-02-24 15:02:29 +01:00 committed by GitHub
parent 86eee2848f
commit e51b3810ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 11 deletions

View File

@ -19,9 +19,18 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
[[inputs.lvm]]
## Use sudo to run LVM commands
use_sudo = false
## The default location of the pvs binary can be overridden with:
#pvs_binary = "/usr/sbin/pvs"
## The default location of the vgs binary can be overridden with:
#vgs_binary = "/usr/sbin/vgs"
## The default location of the lvs binary can be overridden with:
#lvs_binary = "/usr/sbin/lvs"
```
The `lvm` command requires elevated permissions. If the user has configured sudo
The LVM commands requires elevated permissions. If the user has configured sudo
with the ability to run these commands, then set the `use_sudo` to true.
### Using sudo
@ -40,6 +49,9 @@ Cmnd_Alias LVM = /usr/sbin/pvs *, /usr/sbin/vgs *, /usr/sbin/lvs *
Defaults!LVM !logfile, !syslog, !pam_session
```
Path to binaries must match those from config file (pvs_binary, vgs_binary and
lvs_binary)
## Metrics
Metrics are broken out by physical volume (pv), volume group (vg), and logical

View File

@ -23,7 +23,10 @@ var (
)
type LVM struct {
UseSudo bool `toml:"use_sudo"`
UseSudo bool `toml:"use_sudo"`
PVSBinary string `toml:"pvs_binary"`
VGSBinary string `toml:"vgs_binary"`
LVSBinary string `toml:"lvs_binary"`
}
func (*LVM) SampleConfig() string {
@ -47,12 +50,11 @@ func (lvm *LVM) Gather(acc telegraf.Accumulator) error {
}
func (lvm *LVM) gatherPhysicalVolumes(acc telegraf.Accumulator) error {
pvsCmd := "/usr/sbin/pvs"
args := []string{
"--reportformat", "json", "--units", "b", "--nosuffix",
"-o", "pv_name,vg_name,pv_size,pv_free,pv_used",
}
out, err := lvm.runCmd(pvsCmd, args)
out, err := lvm.runCmd(lvm.PVSBinary, args)
if err != nil {
return err
}
@ -102,12 +104,11 @@ func (lvm *LVM) gatherPhysicalVolumes(acc telegraf.Accumulator) error {
}
func (lvm *LVM) gatherVolumeGroups(acc telegraf.Accumulator) error {
cmd := "/usr/sbin/vgs"
args := []string{
"--reportformat", "json", "--units", "b", "--nosuffix",
"-o", "vg_name,pv_count,lv_count,snap_count,vg_size,vg_free",
}
out, err := lvm.runCmd(cmd, args)
out, err := lvm.runCmd(lvm.VGSBinary, args)
if err != nil {
return err
}
@ -166,12 +167,11 @@ func (lvm *LVM) gatherVolumeGroups(acc telegraf.Accumulator) error {
}
func (lvm *LVM) gatherLogicalVolumes(acc telegraf.Accumulator) error {
cmd := "/usr/sbin/lvs"
args := []string{
"--reportformat", "json", "--units", "b", "--nosuffix",
"-o", "lv_name,vg_name,lv_size,data_percent,metadata_percent",
}
out, err := lvm.runCmd(cmd, args)
out, err := lvm.runCmd(lvm.LVSBinary, args)
if err != nil {
return err
}
@ -284,6 +284,10 @@ type lvsReport struct {
func init() {
inputs.Add("lvm", func() telegraf.Input {
return &LVM{}
return &LVM{
PVSBinary: "/usr/sbin/pvs",
VGSBinary: "/usr/sbin/vgs",
LVSBinary: "/usr/sbin/lvs",
}
})
}

View File

@ -13,7 +13,11 @@ import (
func TestGather(t *testing.T) {
var acc testutil.Accumulator
lvm := LVM{UseSudo: false}
lvm := LVM{
PVSBinary: "/usr/sbin/pvs",
VGSBinary: "/usr/sbin/vgs",
LVSBinary: "/usr/sbin/lvs",
}
// overwriting exec commands with mock commands
execCommand = fakeExecCommand
@ -128,7 +132,11 @@ func TestHelperProcess(_ *testing.T) {
func TestGatherNoLVM(t *testing.T) {
var acc testutil.Accumulator
noLVM := LVM{UseSudo: false}
noLVM := LVM{
PVSBinary: "/usr/sbin/pvs",
VGSBinary: "/usr/sbin/vgs",
LVSBinary: "/usr/sbin/lvs",
}
// overwriting exec commands with mock commands
execCommand = fakeExecCommandNoLVM

View File

@ -2,3 +2,12 @@
[[inputs.lvm]]
## Use sudo to run LVM commands
use_sudo = false
## The default location of the pvs binary can be overridden with:
#pvs_binary = "/usr/sbin/pvs"
## The default location of the vgs binary can be overridden with:
#vgs_binary = "/usr/sbin/vgs"
## The default location of the lvs binary can be overridden with:
#lvs_binary = "/usr/sbin/lvs"