Skip to content

Instantly share code, notes, and snippets.

@nicdoye
Created August 30, 2013 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicdoye/6390618 to your computer and use it in GitHub Desktop.
Save nicdoye/6390618 to your computer and use it in GitHub Desktop.
Create basic monit config files from /proc/mounts
#!/usr/bin/env ruby
# Linux only, sorry.
#
# Look for ext[34], xfs and vxfs. Alter the regexp FILESYSTEMS for others
#
# If you have /root as a separate partition, alter the value of ROOT_USABLE
# Config
FILESYSTEMS = /\b(ext\d|v?xfs)\b/
ROOT_USABLE = 'root'
#TARGET_DIR = '/etc/monit.d/filesystem'
TARGET_DIR = '/tmp/filesystem'
SUFFIX = '.conf'
TARGET_DIR_PERM = 0700
FILE_PERM = 0600
SPACE_PCT = 80
SPACE_FAILS = 5
SPACE_FAIL_PERIODS = 15
SPACE_SUCCESSES = 5
INODE_PCT = 80
INODE_FAILS = 5
INODE_FAIL_PERIODS = 15
INODE_SUCCESSES = 5
GROUP = "disk-resource"
# Not really config - just as it is on UNIX
ROOT_PATH = '/'
# Return true iff at then end name exists, is a directory
# and we own it, with the right perms
# Yes, race conditions exist.
def safe_mkdir( name, perm )
if Dir.exists? name
begin
File.chmod( perm, name )
rescue
$stderr.puts "#{name} can not be set to perms #{perm}"
return false
end
else
if File.exists? name
$stderr.puts "#{name} exists and is a file. Bailing"
return false
end
begin
Dir.mkdir( name, perm )
rescue
$stderr.puts "#{name} could not be created. Bailing"
return false
end
end
return File.owned? name
end
exit unless safe_mkdir( TARGET_DIR, TARGET_DIR_PERM )
File.open('/proc/mounts').each do |line|
if line =~ FILESYSTEMS
components = line.split
device = components[0]
path = components[1]
usable_path = ( path == ROOT_PATH ) ? ROOT_USABLE : path[1..-1].gsub(/\//,'_')
puts usable_path
File.open( File.join( TARGET_DIR , "#{usable_path}#{SUFFIX}" ), File::WRONLY|File::TRUNC|File::CREAT, FILE_PERM ) do |file|
file.print <<-eos
check filesystem #{usable_path} path #{device}
group #{GROUP}
if space > #{SPACE_PCT}% #{SPACE_FAILS} times #{SPACE_FAIL_PERIODS} cycles then alert
else if succeeded #{SPACE_SUCCESSES} cycles then alert
if inode > #{INODE_PCT}% #{INODE_FAILS} times #{INODE_FAIL_PERIODS} cycles then alert
else if succeeded #{INODE_SUCCESSES} cycles then alert
eos
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment