I want to graph free disk space for all /dev/ volumes on a server in a single nagiosgraph.I have been following a tutorial here
This tutorial uses a Ruby script to check the entire disk structure using this code:
used_space=`df -h / | grep -v "Filesystem" | awk '{print $5}'`
I have two questions, how to best dynamically determine which volumes a server has and then how to output the free space for each volume to nagios perfdata in a way that I can get a line for each volume on the server.
Here is my complete script - thanks for the answer below:
#!/usr/bin/env ruby
def largest_hash_key(hash)
hash.max_by{|k,v| v}
end
filesystem = %x(df -h)
perfdata = filesystem.split("\n")
.grep(/\A\/dev/)
.map(&:split)
.map{ |e| "'%s'=%s" % [ e[-1], e[-2] ] }
.join(" ")
volumes = Hash[perfdata.split(" ").map {|str| str.split("=")}]
volumes = volumes.map{ |k, v| [k, v.to_i] }
full_disk = largest_hash_key(volumes)
pc_full = full_disk[1]
message = "#{perfdata} | #{perfdata}"
if pc_full > 94
puts "DISK CRITICAL - #{message}"
exit 2
elsif pc_full > 89
puts "DISK WARNING - #{message}"
exit 1
else
puts "DISK OK - #{message}"
exit 0
end
UPDATE: joining with spaces instead of new lines, see markhorrocks comment below.
Assuming your script is running on a Linux machine and this is the format you are referring to by nagios perfdata, you could write:
%x(df -h)
.split("\n")
.grep(/\A\/dev/)
.map(&:split)
.map{ |e| "'%s'=%s" % [ e[0], e[-2] ] }
.join(" ")
which will output
'/dev/sda3'=50%
Related
I wrote a script to read IP addresses from a file and print the amount in the file. I wasn't fully satisfied so I attempted to modify it to allow reading multiple files, and I would specify the files via cmd arguments. The problem that I'm having is that it seems to read multiple files as one argument.
def host_count(*files)
begin
files.each do
files = files.join(' ')
read = IO.read(files)
reg = read.scan(/(?:\d{1,3}\.){3}\d{1,3}/).size
puts "There are " << reg.to_s << " IP addresses in #{files}."
end
rescue Errno::ENOENT
puts "File #{files} does not exist!"
rescue TypeError
puts "Usage: #{$0} [file]"
puts "Example: #{$0} /home/user/ipfile.txt"
end
end
host_count(ARGV)
Running this script with multiple files gives me this error:
File file1 file2 does not exist!
They aren't separated by commas or anything, so it's not reading my arguments as: ["file1","file2"], which was my original problem. What am I not understanding?
You wrote
files.each do
files = files.join(' ')
Why would you do that?
you're changing the array..
the "files" array is already an array, you don't have to join it with strings.
edit1:
to get the specific file for each run, you should write:
files.each do |file|
puts file # will print "file 1", and in the next iteration will print "file 2".
end
contents of a.rb is :
if system("mount | grep /boot") != ""
print "true" # or do something
else
print "false" # or do something else
end
Running this code doesn't print "true" or "false", but prints the output of system call.
]$ ruby a.rb
/dev/sda1 on /boot type ext4 (rw)
What's the right syntax to check the if condition ?
if `mount | grep /boot` != ""
print "true" # or do something
else
print "false" # or do something else
end
In ruby, backticks cause the process to fork, which is expensive and slow. Instead, you could read /etc/mtab and look at the content which prevents forking.
if File.read('/etc/mtab').lines.grep(/boot/)[0]
# not nil, its mounted
else
# its nil, not mounted
end
Don't use system calls, instead you can try this (fromFile path depends of
your operating system, in RedHat is /proc/mounts):
fromFile='/proc/mounts'
toFind='/boot'
if File.readlines(fromFile).grep(toFind).size > 0
print "true" # or do something
else
print "false" # or do something else
end
I'm looking for something equivalent of the backticks operator (``) with the capability to display output during shell command execution.
I saw a solution in another post:
(Running a command from Ruby displaying and capturing the output)
output = []
IO.popen("ruby -e '3.times{|i| p i; sleep 1}'").each do |line|
p line.chomp
output << line.chomp
end
p output
This solution doesn't fit my needs since $? remains nil after the shell command execution. The solution I'm looking for should also set $? (returning the value of $?.exitstatus in another way is also sufficient)
Thanks!
First, I'd recommend using one of the methods in Open3.
I use capture3 for one of my systems where we need to grab the output of STDOUT and STDERR of a lot of command-line applications.
If you need a piped sub-process, try popen3 or one of the other "pipeline" commands.
Here's some code to illustrate how to use popen2, which ignores the STDERR channel. If you want to track that also use popen3:
require 'open3'
output = []
exit_status = Open3.popen2(ENV, "ruby -e '3.times{|i| p i; sleep 1}'") { |stdin, stdout, thr|
stdin.close
stdout.each_line do |o|
o.chomp!
output << o
puts %Q(Read from pipe: "#{ o }")
end
thr.value
}
puts "Output array: #{ output.join(', ') }"
puts "Exit status: #{ exit_status }"
Running that outputs:
Read from pipe: "0"
Read from pipe: "1"
Read from pipe: "2"
Output array: 0, 1, 2
Exit status: pid 43413 exit 0
The example code shows one way to do it.
It's not necessary to use each_line, but that demonstrates how you can read line-by-line until the sub-process closes its STDOUT.
capture3 doesn't accept a block; It waits until the child has closed its output and exits, then it returns the content, which is great when you want a blocking process. popen2 and popen3 have blocking and non-blocking versions, but I show only the non-blocking version here to demonstrate how to read and output the content as it comes in from the sub-process.
Try following:
output = []
IO.popen("ruby -e '3.times{|i| p i; sleep 1 }'") do |f|
f.each do |line|
p line.chomp
output << line.chomp
end
end
p $?
prints
"0"
"1"
"2"
#<Process::Status: pid 2501 exit 0>
Using open3
require 'open3'
output = []
Open3.popen2("ruby -e '3.times{|i| p i; sleep 1}'") do |stdin,stdout,wait_thr|
stdout.each do |line|
p line.chomp
output << line.chomp
end
p wait_thr.value
end
f = File.open("/test/serverlist.txt", "r")
list = f.readlines
list.each do|servers|
File.open('/test/results.txt','w') do |b|
servers.each do |p|
r = `ping -n 1 #{p}`
b.puts r
end
end
It reads the serverlist file, and returns a string. The serverlist file contains the following IP addresses:
192.168.150.254
192.168.120.2
Are you looking to read each line from the file and then do something with like this.
fout = File.open('/test/results.txt','w')
File.open("/test/serverlist.txt", "r").each_line do |server|
server.chomp!
r = `ping -n 1 #{server}`
fout.puts r
end
I don't think you will need to iterate over the server line itself, and with a few style mods added and ping(1) arguments changed, I would suggest...
open 'serverlist.txt', 'r' do |f|
open '/tmp/results.txt', 'w' do |b|
f.readlines.each do |server|
b.puts `ping -c 1 -t 1 #{server}`
end
end
end
Just use b.write in place of b.puts
if you're using linux you could just go for
File.open("serverlist.txt").each { |addy| `echo "#{`ping -c 1 #{addy}`}" >> result.txt` }
and be done with it
well .. maybe add
`echo "# server-availability" > result.txt`
before the above line so the file gets reset every time you call this
This is my code for getting all system users:
def get_all_system_users
user_paths = Dir["#{ENV['HOME']}/../*"]
users = user_paths.map { |path| path.split("..")[-1].gsub(/\W/, '') }
users.select { |user| %x{id #{user}}.include?("uid") }
end
The problem is the id #{user} command which returns an output for inexistent users that bubbles all the way, exactly like a puts or pp.
How can I mute it but still evaluate the output of the command?
I'd prefer a more direct way: (EDIT: Updated to work with OSX, I think)
def get_all_system_users
`dscacheutil -q user|grep name:|cut -d: -f2`.split
end
Or, to handle multiple OSes:
require 'rbconfig'
def get_all_system_users
case RbConfig::CONFIG['host_os']
when /mac|darwin/i
`dscacheutil -q user|grep name:|cut -d: -f2`.split
when /linux/i
`cat /etc/passwd|grep "/home"|cut -d: -f1`.split
else
raise "InferiorOS Error" #or whatever
end
end
You may try to redirect the stderr to stdout (or dev/null), but it depends on your shell:
%x{id #{user} 2>&1}
..and you will need to detect when the utility returned a failure code:
if $?.success?
It's easier to just parse the /etc/passwd file:
Hash[File.readlines('/etc/passwd').map { |line|
line.split ':'
}.select { |field|
field[5].index('/home/') == 0 && File.directory?(field[5])
}.map { |field|
[field[0], field[2].to_i]
}]
This will return a hash with username as key and uid as value for
all users with an existing home directory under /home/.