sannsann / ruptime

Ruby wrapper for the Bash command `uptime`.
0 stars 0 forks source link

Convoluted operation on line 40 #2

Open zpallin opened 8 years ago

zpallin commented 8 years ago

I ran the following:

$ ruby ruptime.rb
ruptime.rb:40:in `uptime_json': undefined method `split' for nil:NilClass (NoMethodError)
        from ruptime.rb:91:in `<main>'

It seems that this operation: load_avgs = uptimeArray[-1].split(": ")[1].split(" ").map{ |i| i.to_f.to_s }

Has an issue with the second split operation. Please fix.

sannsann commented 8 years ago

Hi @zpallin. This error my result from an non-standard print from an uptime invoke. Would you please run the updated version to see whether it catches this issue? Thanks!

zpallin commented 8 years ago

Issue actually still exists... I have identified a problem in the code.

Here is a typical uptime output from my ubuntu instance:

16:29:43 up 23 days, 7:28, 1 user, load average: 0.13, 0.13, 0.14

The problem here is that you are trying to access the load averages and store them in a variable, load_avgs with a convoluted pattern on (now) line 52.

The variable uptimeArray by this line looks like this: [" 16:34:08 up 23 days", " 7:33", " 1 user", " load average: 0.09", "0.09", "0.12"]

And uptimeArray[-1] looks like this: 0.12

And uptimeArray[-1].split(": ") looks like this: ["0.12"]

So... there is no value at uptimeArray[-1].split(": ")[1]. Perhaps there is a difference in the way these values are printing out in your environment and my environment that I am not aware of.

Either way, if that's the case, let me try this on my mac tonight and see if the problem persists. If so, then perhaps you should find an easier way to tease out the details you are looking for than this. If it works on my mac, then it's no big deal. :P

sannsann commented 8 years ago

Ahh... that makes sense! The Ubuntu uptime is slightly different from the OS X version: 16:45 up 1 day, 3:31, 4 users, load averages: 2.69 7.92 5.87

This output lacks the commas from your Ubuntu machine's output. I'll figure out another way to pinpoint and extrapolate those values.

Thank you for sharing, Zach!

zpallin commented 8 years ago

:+1:

sannsann commented 8 years ago

Updated the script to properly parse loads from both OS X and Ubuntu. I added unit tests for both environments. Thanks!