You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.8 KiB

  1. #!/usr/bin/perl -w
  2. # check_ro_mounts.pl Copyright (c) 2008 Valentin Vidic <vvidic@carnet.hr>
  3. #
  4. # Checks the mount table for read-only mounts - these are usually a sign of
  5. # trouble (broken filesystem etc.)
  6. #
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty
  15. # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # you should have received a copy of the GNU General Public License
  19. # along with this program (or with Nagios); if not, write to the
  20. # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. # Boston, MA 02111-1307, USA
  22. use strict;
  23. use Getopt::Long;
  24. use lib "/usr/lib/nagios/plugins";
  25. use utils qw (%ERRORS &support);
  26. my $name = 'RO_MOUNTS';
  27. my $mtab = '/proc/mounts';
  28. my @includes = ();
  29. my @excludes = ();
  30. my @excluded_types = ();
  31. my @ro_mounts = ();
  32. my $want_help = 0;
  33. Getopt::Long::Configure(qw(no_ignore_case));
  34. my $res = GetOptions(
  35. "help|h" => \$want_help,
  36. "mtab|m=s" => \$mtab,
  37. "path|p=s" => \@includes,
  38. "partition=s" => \@includes,
  39. "exclude|x=s" => \@excludes,
  40. "exclude-type|X=s" => \@excluded_types,
  41. );
  42. if ($want_help or !$res) {
  43. print_help();
  44. exit $ERRORS{$res ? 'OK' : 'UNKNOWN'};
  45. }
  46. my $includes_re = globs2re(@includes);
  47. my $excludes_re = globs2re(@excludes);
  48. my $excluded_types_re = globs2re(@excluded_types);
  49. open(MTAB, $mtab) or nagios_exit(UNKNOWN => "Can't open $mtab: $!");
  50. MOUNT: while (<MTAB>) {
  51. # parse mtab lines
  52. my ($dev, $dir, $fs, $opt) = split;
  53. my @opts = split(',', $opt);
  54. # check includes/excludes
  55. if ($includes_re) {
  56. next MOUNT unless $dev =~ qr/$includes_re/
  57. or $dir =~ qr/$includes_re/;
  58. }
  59. if ($excludes_re) {
  60. next MOUNT if $dev =~ qr/$excludes_re/
  61. or $dir =~ qr/$excludes_re/;
  62. }
  63. if ($excluded_types_re) {
  64. next MOUNT if $fs =~ qr/$excluded_types_re/;
  65. }
  66. # check for ro option
  67. if (grep /^ro$/, @opts) {
  68. push @ro_mounts, $dir;
  69. }
  70. }
  71. nagios_exit(UNKNOWN => "Read failed on $mtab: $!") if $!;
  72. close(MTAB) or nagios_exit(UNKNOWN => "Can't close $mtab: $!");
  73. # report findings
  74. if (@ro_mounts) {
  75. nagios_exit(CRITICAL => "Found ro mounts: @ro_mounts");
  76. } else {
  77. nagios_exit(OK => "No ro mounts found");
  78. }
  79. # convert glob patterns to a RE (undef if no patterns)
  80. sub globs2re {
  81. my(@patterns) = @_;
  82. @patterns or return undef;
  83. foreach (@patterns) {
  84. s/ \\(.) / sprintf('\x%02X', ord($1)) /egx;
  85. s/ ([^\\*?\w]) / sprintf('\x%02X', ord($1)) /egx;
  86. s/\*/.*/g;
  87. s/\?/./g;
  88. }
  89. return '\A(?:' . join('|', @patterns) . ')\z';
  90. }
  91. # output the result and exit plugin style
  92. sub nagios_exit {
  93. my ($result, $msg) = @_;
  94. print "$name $result: $msg\n";
  95. exit $ERRORS{$result};
  96. }
  97. sub print_help {
  98. print <<EOH;
  99. check_ro_mounts 0.1
  100. Copyright (c) 2008 Valentin Vidic <vvidic\@carnet.hr>
  101. This plugin checks the mount table for read-only mounts.
  102. Usage:
  103. check_ro_mounts [-m mtab] [-p path] [-x path] [-X type]
  104. Options:
  105. -h, --help
  106. Print detailed help screen
  107. -m, --mtab=FILE
  108. Use this mtab instead (default is /proc/mounts)
  109. -p, --path=PATH, --partition=PARTITION
  110. Glob pattern of path or partition to check (may be repeated)
  111. -x, --exclude=PATH <STRING>
  112. Glob pattern of path or partition to ignore (only works if -p unspecified)
  113. -X, --exclude-type=TYPE <STRING>
  114. Ignore all filesystems of indicated type (may be repeated)
  115. EOH
  116. support();
  117. }
  118. # vim:sw=4:ts=4:et