BradHodges wrote:I fixed the problem with the puts "Error...
And I changed the format of the output, easier to read I think.
I also added the YorN on the last time element, it was missing
crons = []
counts = []
cparts = []
basedir = "/allcrontabs"
def YorN(part)
if part == "*"
"N"
else
"Y"
end
end
Dir.new(basedir).entries.each do |logfile|
unless File.directory? logfile
if logfile.split('.')[1] == 'log'
file = File.new(logfile, "r")
while (line = file.gets)
parts = line.split(' ')
if parts[5,parts.length-5]
cmd = parts[5,parts.length-5].join(' ')
idx = crons.index(cmd)
if idx
counts[idx] += 1
else
crons << cmd
idx = crons.index(cmd)
counts[idx] = 1
cparts[idx] = parts[0,5] # an Array containing another Array !
end
else
puts "Error on: #{line} in file #{logfile}"
end
end
file.close
end
end
end
# OUTPUT results
puts "# Servers Min Hour DOM Month DOW Cronjob"
crons.each do |c|
idx = crons.index(c)
puts "#{counts[idx]} #{YorN(cparts[idx][0])} #{YorN(cparts[idx][1])} #{YorN(cparts[idx][2])} #{YorN(cparts[idx][3])} #{YorN(cparts[idx][4])} #{crons[ids]}"
end
Ahh, that's much easier to read. It looks like the count is correct, by what I can see as well (there's 189 log files in total). One shows 334 for the count, but that's because that particular cron job is duplicated in a lot of servers somehow so there will be 2 entries in the cron tab.
Output looks like this:
$ ruby cron_parse.rb
# Servers Min Hour DOM Month DOW Cronjob
4 N N N N N /root/staydown.sh &>/dev/null
180 Y Y N N N /usr/local/cpanel/scripts/upcp --cron
180 Y Y N N N /usr/local/cpanel/scripts/cpbackup
180 Y N N N N /usr/bin/test -x /usr/local/cpanel/bin/tail-check && /usr/local/cpanel/bin/tail-check
Also, there was a small typo in the 2nd to last line.
Should have been:
So, hey, at least I can find typos 
This looks awesome, though. I've really learned a lot doing this - A LOT more than I would have by just reading a book about ruby.
My goal is to code something entirely by myself now (not as elaborate as this, though, to start) and go from there! So far I'm really liking ruby and how, for the most part, easy it is to understand.
For example, the "def YorN(part)" line is pretty straight forward. Even if I hadn't the slightest knowledge of programming, I could ascertain what that meant.
And then "Dir.new" and ""unless File.directory? logfile" are both very straight forward.
I really can't thank you enough for all your help in this. I feel like I should be paying you for the ruby lesson 