Skip to content

Commit

Permalink
Merge pull request #8 from nixwiz/feature/ignore-read-only
Browse files Browse the repository at this point in the history
Add --include-read-only option
  • Loading branch information
Todd Campbell authored Feb 15, 2021
2 parents 9347e99 + 9040d49 commit 56664e3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

### Added
- Added --include-read-only option to support ignoring read-only file systems

## [0.3.1] - 2020-12-30

### Changed
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Flags:
-w, --warning float Warning threshold for file system usage (default 85)
-c, --critical float Critical threshold for file system usage (default 95)
-p, --include-pseudo-fs Include pseudo-filesystems (e.g. tmpfs) (default false)
-r, --include-read-only Include read-only filesystems (default false)
-f, --fail-on-error Fail and exit on errors getting file system usage (e.g. permission denied) (default false)
-h, --help help for check-disk-usage
Expand All @@ -63,6 +64,8 @@ specified as such (e.g. NTFS, C:)
* The `--include-pseudo-fs` option is false by default meaning that on Linux
systems file system with types such as tmpfs (e.g. /dev, /run, etc.) will
be ignored. This takes precedence over any explicit includes or excludes.
* The `--include-read-only` checks for the `ro` mount option on Linux and the
`read-only` mount option on macOS. Support for Windows has not been implemented.
* The `--fail-on-error` option determines what occurs if the check encounters an
error, such as `permission denied` for a file system. If true, the check will
exit with as a critical failure and provide the error message. If false (the
Expand Down
42 changes: 34 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"strings"

human "github.com/dustin/go-humanize"
"github.com/sensu-community/sensu-plugin-sdk/sensu"
Expand All @@ -12,14 +13,15 @@ import (
// Config represents the check plugin config.
type Config struct {
sensu.PluginConfig
IncludeFSType []string
ExcludeFSType []string
IncludeFSPath []string
ExcludeFSPath []string
Warning float64
Critical float64
IncludePseudo bool
FailOnError bool
IncludeFSType []string
ExcludeFSType []string
IncludeFSPath []string
ExcludeFSPath []string
Warning float64
Critical float64
IncludePseudo bool
IncludeReadOnly bool
FailOnError bool
}

var (
Expand Down Expand Up @@ -104,6 +106,15 @@ var (
Usage: "Fail and exit on errors getting file system usage (e.g. permission denied) (default false)",
Value: &plugin.FailOnError,
},
{
Path: "include-read-only",
Env: "",
Argument: "include-read-only",
Shorthand: "r",
Default: false,
Usage: "Include read-only filesystems (default false)",
Value: &plugin.IncludeReadOnly,
},
}
)

Expand Down Expand Up @@ -147,6 +158,11 @@ func executeCheck(event *types.Event) (int, error) {
continue
}

// Ignore read-only file systems?
if !plugin.IncludeReadOnly && isReadOnly(p.Opts) {
continue
}

device := p.Mountpoint
s, err := disk.Usage(device)
if err != nil {
Expand Down Expand Up @@ -213,6 +229,16 @@ func isValidFSPath(fsPath string) bool {
return true
}

func isReadOnly(mountOpts string) bool {
mOpts := strings.Split(mountOpts, ",")
// "ro" should cover Linux, macOS, and Windows, "read-only" is reportd by mount(8)
// on macOS so check for it, just in case
if contains(mOpts, "ro") || contains(mOpts, "read-only") {
return true
}
return false
}

func contains(a []string, s string) bool {
for _, v := range a {
if v == s {
Expand Down

0 comments on commit 56664e3

Please sign in to comment.