[Ruby Newbie] Reading in a file generated by df

  • Thread starter Lovell Mcilwain
  • Start date
L

Lovell Mcilwain

Hello all, (yes its me again :) )

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
list

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
end

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
anything.

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
 
S

Stefano Crocco

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:

doc_array=File.readlines('diskcheck.txt')

I hope this can help
 
R

Reprisal

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

William
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top