[Ruby Newbie] Reading in a file generated by df

Hello all, (yes its me again :slight_smile: )

I’m here at work and was hit with a very interesting problem. My boss
would like for me to build a script that monitors disk usage on our
database systems (OS X).

I figured what better way to learn ruby then go right into a live
situation. Granted, I’m not sure I’m ready for it but hell I will give
it a shot anyway.

So I don’t have a specific way to implement this so I figure I try
something that I believe to be basic.

  1. Generate a file (df -k > filename.txt)
  2. Read the file using Ruby code
    A. compare bites or percentage size (which ever is easier) with a set
    size that I determine. If the size read in exceeds the size that I have
    determined then it shoots a warning to me via e mail letting me know I
    need to deal with it.

So, now that I have my idea out of my head an on paper, I started
writing my code.

require ‘yaml’ # Just in case I need it
doc_array = [] # Array to have each line read from the file put into a

file = File.new(‘diskcheck.txt’,‘r’) # Read the file
while(line = file.gets) # Condition to put the files into the array
doc_array[doc_array.length] = line

puts doc_array # Test to see if file was read into array correctly

So on to my problem (thanks for reading this far),
When I try to get an ouput of the array that is supposed to have the
several lines of the file read, it actually doesn’t come up with

I run my program and it just exits without any errors but there is also
no output that comes to the screen… Do I need to convert this file or
find a better way to read in this file?

When I read in this file, It doesn’t spit out anything

I’d say your code is correct. I tried it myself, and it works. I use
linux, though, but I don’t think this makes a difference. The only
reason I can think of for what happens to you is that the file you’re
trying to read is empty, but you’ll have already checked that. At any
rate, there’s a far simpler way to read the contents of the file:


I hope this can help

  1. Generate a file (df -k > filename.txt)

Your logic is correct, but if a human being is monitoring that disk
it would be better to use df -kh so you get ``easily interpretable"
the `h’ for human readable form. This is a well know fact, but i just
thought i’d mention it anyway. :slight_smile:


  • vihan

Why create the file at all?

disk_check = df -k # shell out for the info directly
disk_check_lines = disk_check.split("/\n") # break it up
disk_check_lines.each { test } #do your real test


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs