From e51b3810abeb69259b7b9966eda806e57b2b5d67 Mon Sep 17 00:00:00 2001 From: Pierrick Brossin Date: Fri, 24 Feb 2023 15:02:29 +0100 Subject: [PATCH] fix(inputs.lvm): add options to specify path to binaries (#12725) --- plugins/inputs/lvm/README.md | 14 +++++++++++++- plugins/inputs/lvm/lvm.go | 20 ++++++++++++-------- plugins/inputs/lvm/lvm_test.go | 12 ++++++++++-- plugins/inputs/lvm/sample.conf | 9 +++++++++ 4 files changed, 44 insertions(+), 11 deletions(-) diff --git a/plugins/inputs/lvm/README.md b/plugins/inputs/lvm/README.md index 15abe6895..79f808548 100644 --- a/plugins/inputs/lvm/README.md +++ b/plugins/inputs/lvm/README.md @@ -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 diff --git a/plugins/inputs/lvm/lvm.go b/plugins/inputs/lvm/lvm.go index 12e046a28..0f00fbadb 100644 --- a/plugins/inputs/lvm/lvm.go +++ b/plugins/inputs/lvm/lvm.go @@ -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", + } }) } diff --git a/plugins/inputs/lvm/lvm_test.go b/plugins/inputs/lvm/lvm_test.go index 7bc82509b..0b3caf5f7 100644 --- a/plugins/inputs/lvm/lvm_test.go +++ b/plugins/inputs/lvm/lvm_test.go @@ -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 diff --git a/plugins/inputs/lvm/sample.conf b/plugins/inputs/lvm/sample.conf index ec2b7be83..6c2ad5720 100644 --- a/plugins/inputs/lvm/sample.conf +++ b/plugins/inputs/lvm/sample.conf @@ -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"