sys-uptime 0.5.4 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,7 +1,8 @@
1
- == 0.5.4 - 26-Jul-2011
2
- * Refactored and updated Rakefile and gemspec.
3
- * Some internal file reorganization and renaming.
4
- * License changed to Artistic 2.0.
1
+ == 0.6.0 - 11-Dec-2011
2
+ * Switched Unix code to use FFI.
3
+ * Removed all of the C related tasks from the Rakefile and added the gem:build
4
+ and gem:install tasks.
5
+ * Internal directory layout changes, with appropriate changes to the gemspec.
5
6
 
6
7
  == 0.5.3 - 7-May-2009
7
8
  * Altered the Uptime.seconds implementation on Linux so that it works with
data/MANIFEST CHANGED
@@ -2,11 +2,12 @@
2
2
  * MANIFEST
3
3
  * Rakefile
4
4
  * README
5
+ * install.rb
5
6
  * sys-uptime.gemspec
6
7
  * doc/uptime.txt
7
- * examples/sys_uptime_example.rb
8
+ * examples/test.rb
8
9
  * ext/extconf.rb
9
10
  * ext/sys/uptime.c
10
- * lib/linux/sys/uptime.rb
11
- * lib/windows/sys/uptime.rb
12
- * test/test_sys_uptime.rb
11
+ * lib/sys/linux.rb
12
+ * lib/sys/windows.rb
13
+ * test/test_sys_uptime.rb
data/README CHANGED
@@ -1,14 +1,67 @@
1
1
  = Description
2
- A Ruby interface for getting system uptime information.
2
+ A Ruby interface for getting system uptime information.
3
3
 
4
4
  = Prerequisites
5
- Ruby 1.8.0 or later.
6
- Ruby 1.8.2 or later is recommended on MS Windows.
7
- A C compiler, except for MS Windows.
5
+ ffi 0.5.0 or later.
8
6
 
9
7
  = Installation
10
- rake test (optional)
11
- rake install (non-gem) or rake install_gem (gem).
8
+ gem install sys-uptime # Unix platforms
9
+ gem install sys-uptime --platform mswin32 # MS Windows
12
10
 
13
- == Notes
14
- For additional documentation see doc/uptime.txt.
11
+ = Synopsis
12
+ require 'sys/uptime'
13
+ include Sys
14
+
15
+ # Get everything
16
+ p Uptime.uptime
17
+ p Uptime.dhms.join(', ')
18
+
19
+ # Get individual units
20
+ p Uptime.days
21
+ p Uptime.hours
22
+ p Uptime.minutes
23
+ p Uptime.seconds
24
+
25
+ # Get the boot time
26
+ p Uptime.boot_time
27
+
28
+ = Notes
29
+ On MS Windows the Uptime.uptime and Uptime_boot_time methods optionally
30
+ takes a host name as a single argument. The default is localhost.
31
+
32
+ The current time, users and load average are not included in this library
33
+ module, even though you may be used to seeing them with the command
34
+ line version of 'uptime'.
35
+
36
+ == Known Bugs
37
+ None that I am aware of. Please log any bugs you find on the project
38
+ website at http://www.rubyforge.org/projects/sysutils.
39
+
40
+ == Questions
41
+ "Doesn't Struct::Tms do this?" - No.
42
+
43
+ == License
44
+ Artistic 2.0
45
+
46
+ == Copyright
47
+ Copyright 2002-2010, Daniel J. Berger
48
+
49
+ All Rights Reserved. This module is free software. It may be used,
50
+ redistributed and/or modified under the same terms as Ruby itself.
51
+
52
+ == Warranty
53
+ This library is provided "as is" and without any express or
54
+ implied warranties, including, without limitation, the implied
55
+ warranties of merchantability and fitness for a particular purpose.
56
+
57
+ == Acknowledgements
58
+ Andrea Fazzi for help with the FFI version.
59
+
60
+ Mike Hall for help with the BSD side of things for the original C code.
61
+
62
+ Ola Eriksson, whose source code I shamelessly plagiarized to get a better
63
+ implementation for systems that have the utmpx.h header file for the
64
+ original C code.
65
+
66
+ == Author
67
+ Daniel J. Berger
data/Rakefile CHANGED
@@ -1,87 +1,44 @@
1
1
  require 'rake'
2
2
  require 'rake/clean'
3
3
  require 'rake/testtask'
4
- require 'rbconfig'
5
- include Config
6
4
 
7
- CLEAN.include(
8
- '**/*.gem', # Gem files
9
- '**/*.rbc', # Rubinius
10
- '**/*.o', # C object file
11
- '**/*.log', # Ruby extension build log
12
- '**/Makefile', # C Makefile
13
- '**/conftest.dSYM', # OS X build directory
14
- "**/*.#{CONFIG['DLEXT']}", # C shared object
15
- 'lib/sys/uptime.rb' # Renamed source file
16
- )
5
+ CLEAN.include("**/*.gem", "**/*.rbx", "**/*.rbc")
17
6
 
18
- desc "Build the sys-uptime library on UNIX systems"
19
- task :build => [:clean] do
20
- Dir.chdir('ext') do
21
- unless Config::CONFIG['host_os'] =~ /windows|mswin|win32|mingw|cygwin|dos|linux/i
22
- ruby 'extconf.rb'
23
- sh 'make'
24
- cp "uptime." + CONFIG['DLEXT'], "sys"
25
- end
26
- end
27
- end
28
-
29
- namespace :gem do
30
- desc "Create the gem for the sys-uptime library"
7
+ namespace 'gem' do
8
+ desc 'Build the sys-uptime gem'
31
9
  task :create => [:clean] do
32
10
  spec = eval(IO.read('sys-uptime.gemspec'))
33
-
34
- case Config::CONFIG['host_os']
35
- when /windows|win32|cygwin|mingw|dos|mswin/
36
- spec.platform = Gem::Platform::CURRENT
37
- spec.platform.cpu = 'universal'
38
- spec.require_paths = ['lib', 'lib/windows']
39
- when /linux/
40
- spec.platform = Gem::Platform.new('universal-linux')
41
- spec.require_paths = ['lib', 'lib/linux']
42
- else
43
- spec.platform = Gem::Platform::RUBY
44
- spec.extensions = ['ext/extconf.rb']
45
- spec.extra_rdoc_files << 'ext/sys/uptime.c'
11
+
12
+ if File::ALT_SEPARATOR
13
+ spec.require_paths = ['lib', 'lib/windows']
14
+ spec.platform = Gem::Platform::CURRENT
15
+ spec.platform.cpu = 'universal'
16
+ spec.platform.version = nil
17
+ else
18
+ spec.require_paths = ['lib', 'lib/unix']
19
+ spec.add_dependency('ffi', '>= 1.0.0')
46
20
  end
47
21
 
48
22
  Gem::Builder.new(spec).build
49
23
  end
50
24
 
51
- desc "Install the sys-uptime library"
52
- task :install => [:create] do
53
- gem_name = Dir['*.gem'].first
54
- sh "gem install #{gem_name}"
25
+ desc 'Install the sys-uptime gem'
26
+ task :install => [:build] do
27
+ file = Dir["*.gem"].first
28
+ sh "gem install #{file}"
55
29
  end
56
30
  end
57
31
 
58
- task :example => [:build] do
59
- case Config::CONFIG['host_os']
60
- when /windows|win32|cygwin|mingw|dos|mswin/
61
- path = 'lib/windows'
62
- when /linux/
63
- path = 'lib/linux'
64
- else
65
- path = 'ext'
32
+ desc "Run the test suite"
33
+ Rake::TestTask.new do |t|
34
+ if File::ALT_SEPARATOR
35
+ t.libs << 'lib/windows'
36
+ else
37
+ t.libs << 'lib/unix'
66
38
  end
67
- sh "ruby -I#{path} examples/sys_uptime_example.rb"
68
- end
69
39
 
70
- desc "Run the test suite"
71
- Rake::TestTask.new("test") do |t|
72
- task :test => :build
73
- t.libs << 'test' << '.'
74
40
  t.warning = true
75
41
  t.verbose = true
76
-
77
- case Config::CONFIG['host_os']
78
- when /windows|win32|cygwin|mingw|dos|mswin/
79
- t.libs << 'lib/windows'
80
- when /linux/
81
- t.libs << 'lib/linux'
82
- else
83
- t.libs << 'ext'
84
- end
85
42
  end
86
43
 
87
44
  task :default => :test
@@ -1,5 +1,5 @@
1
1
  ###########################################################
2
- # sys_uptime_example.rb
2
+ # uptime_test.rb
3
3
  #
4
4
  # A generic test script for general futzing. You can run
5
5
  # this script via the 'rake example' task.
@@ -0,0 +1,213 @@
1
+ require 'ffi'
2
+
3
+ # The Sys module serves as a namespace only.
4
+ module Sys
5
+
6
+ # The Uptime class encapsulates various bits of information regarding your
7
+ # system's uptime, including boot time.
8
+ class Uptime
9
+ extend FFI::Library
10
+ ffi_lib FFI::Library::LIBC
11
+
12
+ # Error typically raised in one of the Uptime methods should fail.
13
+ class Error < StandardError; end
14
+
15
+ # The version of the sys-uptime library
16
+ VERSION = '0.6.0'
17
+
18
+ private
19
+
20
+ # Hit this issue on Linux, not sure why
21
+ begin
22
+ find_type(:clock_t)
23
+ rescue TypeError
24
+ typedef(:long, :clock_t)
25
+ end
26
+
27
+ attach_function :strerror, [:int], :string
28
+ attach_function :sysconf, [:int], :long
29
+ attach_function :time, [:pointer], :time_t
30
+ attach_function :times, [:pointer], :clock_t
31
+
32
+ private_class_method :strerror, :sysconf, :time, :times
33
+
34
+ begin
35
+ attach_function :sysctl, [:pointer, :uint, :pointer, :pointer, :pointer, :size_t], :int
36
+ private_class_method :sysctl
37
+ rescue FFI::NotFoundError
38
+ attach_function :setutxent, [], :void
39
+ attach_function :getutxent, [], :pointer
40
+ attach_function :endutxent, [], :void
41
+ private_class_method :setutxent, :getutxent, :endutxent
42
+ end
43
+
44
+ CTL_KERN = 1 # Kernel
45
+ KERN_BOOTTIME = 21 # Time kernel was booted
46
+ TICKS = 100 # Ticks per second (TODO: use sysconf)
47
+ BOOT_TIME = 2 # Boot time
48
+
49
+ class Tms < FFI::Struct
50
+ layout(
51
+ :tms_utime, :clock_t,
52
+ :tms_stime, :clock_t,
53
+ :tms_cutime, :clock_t,
54
+ :tms_cstime, :clock_t
55
+ )
56
+ end
57
+
58
+ class Timeval < FFI::Struct
59
+ layout(
60
+ :tv_sec, :long,
61
+ :tv_usec, :long
62
+ )
63
+ end
64
+
65
+ class ExitStatus < FFI::Struct
66
+ layout(
67
+ :e_termination, :short,
68
+ :e_exit, :short
69
+ )
70
+ end
71
+
72
+ class Utmpx < FFI::Struct
73
+ layout(
74
+ :ut_user, [:char, 32],
75
+ :ut_id, [:char, 4],
76
+ :ut_line, [:char, 32],
77
+ :ut_pid, :pid_t,
78
+ :ut_type, :short,
79
+ :ut_exit, ExitStatus,
80
+ :ut_tv, Timeval,
81
+ :ut_session, :int,
82
+ :padding, [:int, 5],
83
+ :ut_host, [:char, 257]
84
+ )
85
+ end
86
+
87
+ public
88
+
89
+ # Returns a Time object indicating the time the system was last booted.
90
+ #
91
+ # Example:
92
+ #
93
+ # Sys::Uptime.boot_time # => Mon Jul 13 06:08:25 -0600 2009
94
+ #
95
+ def self.boot_time
96
+ if Config::CONFIG['host_os'] =~ /linux/i
97
+ Time.now - self.seconds
98
+ elsif respond_to?(:sysctl, true)
99
+ tv = Timeval.new
100
+ mib = FFI::MemoryPointer.new(:int, 2).write_array_of_int([CTL_KERN, KERN_BOOTTIME])
101
+ size = FFI::MemoryPointer.new(:long, 1).write_int(tv.size)
102
+
103
+ if sysctl(mib, 2, tv, size, nil, 0) != 0
104
+ raise SystemCallError, 'sysctl() - ' + strerror(FFI.errno)
105
+ end
106
+
107
+ Time.at(tv[:tv_sec], tv[:tv_usec])
108
+ else
109
+ begin
110
+ setutxent()
111
+ while ent = Utmpx.new(getutxent())
112
+ if ent[:ut_type] == BOOT_TIME
113
+ time = Time.at(ent[:ut_tv][:tv_sec], ent[:ut_tv][:tv_usec])
114
+ break
115
+ end
116
+ end
117
+ ensure
118
+ endutxent()
119
+ end
120
+ time
121
+ end
122
+ end
123
+
124
+ # Returns the total number of seconds of uptime.
125
+ #
126
+ # Example:
127
+ #
128
+ # Sys::Uptime.seconds => 118800
129
+ #
130
+ def self.seconds
131
+ if Config::CONFIG['host_os'] =~ /linux/i
132
+ begin
133
+ IO.read('/proc/uptime').split.first.to_i
134
+ rescue Exception => err
135
+ raise Error, err
136
+ end
137
+ elsif respond_to?(:sysctl, true)
138
+ tv = Timeval.new
139
+ mib = FFI::MemoryPointer.new(:int, 2).write_array_of_int([CTL_KERN, KERN_BOOTTIME])
140
+ size = FFI::MemoryPointer.new(:long, 1).write_int(tv.size)
141
+
142
+ if sysctl(mib, 2, tv, size, nil, 0) != 0
143
+ raise SystemCallError, 'sysctl() - ' + strerror(FFI.errno)
144
+ end
145
+
146
+ time(nil) - tv[:tv_sec]
147
+ else
148
+ tms = Tms.new
149
+ times(tms) / TICKS
150
+ end
151
+ end
152
+
153
+ # Returns the total number of minutes of uptime.
154
+ #
155
+ # Example:
156
+ #
157
+ # Sys::Uptime.minutes # => 678
158
+ #
159
+ def self.minutes
160
+ seconds / 60
161
+ end
162
+
163
+ # Returns the total number of hours of uptime.
164
+ #
165
+ # Example:
166
+ #
167
+ # Sys::Uptime.hours # => 31
168
+ #
169
+ def self.hours
170
+ seconds / 3600
171
+ end
172
+
173
+ # Returns the total number of days of uptime.
174
+ #
175
+ # Example:
176
+ #
177
+ # Sys::Uptime.days # => 2
178
+ #
179
+ def self.days
180
+ seconds / 86400
181
+ end
182
+
183
+ # Returns the uptime as a colon separated string, including days,
184
+ # hours, minutes and seconds.
185
+ #
186
+ # Example:
187
+ #
188
+ # Sys::Uptime.uptime # => "1:9:24:57"
189
+ #
190
+ def self.uptime
191
+ secs = seconds
192
+ days = secs / 86400
193
+ secs -= days * 86400
194
+ hours = secs / 3600
195
+ secs -= hours * 3600
196
+ mins = secs / 60
197
+ secs -= mins * 60
198
+
199
+ "#{days}:#{hours}:#{mins}:#{secs}"
200
+ end
201
+
202
+ # Returns the uptime as a four element array, including days, hours,
203
+ # minutes and seconds.
204
+ #
205
+ # Example:
206
+ #
207
+ # Sys::Uptime.dhms # => [1,9,24,57]
208
+ #
209
+ def self.dhms
210
+ uptime.split(':')
211
+ end
212
+ end
213
+ end
@@ -14,7 +14,7 @@ module Sys
14
14
  class Error < StandardError; end
15
15
 
16
16
  # The version of the sys-uptime library.
17
- VERSION = '0.5.4'
17
+ VERSION = '0.6.0'
18
18
 
19
19
  # Returns the boot time as a Time object.
20
20
  #
@@ -22,7 +22,7 @@ module Sys
22
22
  #
23
23
  # Sys::Uptime.boot_time # => Fri Dec 12 20:18:58 -0700 2008
24
24
  #
25
- def self.boot_time(host=Socket.gethostname)
25
+ def self.boot_time(host = Socket.gethostname)
26
26
  cs = "winmgmts://#{host}/root/cimv2"
27
27
  begin
28
28
  wmi = WIN32OLE.connect(cs)
@@ -47,7 +47,7 @@ module Sys
47
47
  #
48
48
  # Sys::Uptime.uptime # => "1:9:55:11"
49
49
  #
50
- def self.uptime(host=Socket.gethostname)
50
+ def self.uptime(host = Socket.gethostname)
51
51
  get_dhms(host).join(':')
52
52
  end
53
53
 
@@ -59,7 +59,7 @@ module Sys
59
59
  #
60
60
  # Sys::Uptime.dhms # => [1, 9, 55, 11]
61
61
  #
62
- def self.dhms(host=Socket.gethostname)
62
+ def self.dhms(host = Socket.gethostname)
63
63
  get_dhms(host)
64
64
  end
65
65
 
@@ -70,7 +70,7 @@ module Sys
70
70
  #
71
71
  # Sys::Uptime.days # => 1
72
72
  #
73
- def self.days(host=Socket.gethostname)
73
+ def self.days(host = Socket.gethostname)
74
74
  hours(host) / 24
75
75
  end
76
76
 
@@ -117,6 +117,8 @@ module Sys
117
117
  return Time.parse(str.split('.').first)
118
118
  end
119
119
 
120
+ private_class_method :parse_ms_date
121
+
120
122
  # Get the actual days, hours, minutes and seconds since boot using WMI.
121
123
  #
122
124
  def self.get_dhms(host)
@@ -132,6 +134,8 @@ module Sys
132
134
  [days, hours, minutes, seconds]
133
135
  end
134
136
 
137
+ private_class_method :get_dhms
138
+
135
139
  # Returns the number of seconds since boot.
136
140
  #
137
141
  def self.get_seconds(host)
@@ -154,5 +158,7 @@ module Sys
154
158
 
155
159
  (now - boot_time).to_i
156
160
  end
161
+
162
+ private_class_method :get_seconds
157
163
  end
158
164
  end
@@ -1,17 +1,19 @@
1
1
  require 'rubygems'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
- spec.name = 'sys-uptime'
5
- spec.version = '0.5.4'
6
- spec.author = 'Daniel J. Berger'
7
- spec.email = 'djberg96@gmail.com'
8
- spec.homepage = 'http://www.rubyforge.org/projects/sysutils'
9
- spec.platform = Gem::Platform::RUBY
10
- spec.summary = 'A Ruby interface for getting system uptime information.'
11
- spec.test_file = 'test/test_sys_uptime.rb'
12
- spec.extra_rdoc_files = ['CHANGES', 'README', 'MANIFEST', 'doc/uptime.txt']
4
+ spec.name = 'sys-uptime'
5
+ spec.version = '0.6.0'
6
+ spec.author = 'Daniel J. Berger'
7
+ spec.license = 'Artistic 2.0'
8
+ spec.email = 'djberg96@gmail.com'
9
+ spec.homepage = 'https://github.com/djberg96/sys-uptime'
10
+ spec.platform = Gem::Platform::RUBY
11
+ spec.summary = 'A Ruby interface for getting system uptime information.'
12
+ spec.test_file = 'test/test_sys_uptime.rb'
13
+ spec.files = Dir["**/*"].reject{ |f| f.include?('git') }
14
+
15
+ spec.extra_rdoc_files = ['CHANGES', 'README', 'MANIFEST']
13
16
  spec.rubyforge_project = 'sysutils'
14
- spec.files = Dir['**/*'].delete_if{ |item| item.include?('git') }
15
17
 
16
18
  spec.description = <<-EOF
17
19
  The sys-uptime library is a simple interface for gathering uptime
@@ -4,62 +4,106 @@
4
4
  # Test suite for sys-uptime. This should generally be run via the
5
5
  # 'rake test' task, since it handles the pre-setup code for you.
6
6
  #####################################################################
7
+ require 'rubygems'
8
+ gem 'test-unit'
9
+
7
10
  require 'sys/uptime'
8
11
  require 'test/unit'
12
+ require 'socket'
9
13
  include Sys
10
14
 
11
15
  class TC_Sys_Uptime < Test::Unit::TestCase
12
- def test_version
13
- assert_equal('0.5.4', Uptime::VERSION)
16
+ test "version is set to expected value" do
17
+ assert_equal('0.6.0', Uptime::VERSION)
14
18
  end
15
19
 
16
- def test_seconds
20
+ test "seconds method basic functionality" do
17
21
  assert_respond_to(Uptime, :seconds)
18
22
  assert_nothing_raised{ Uptime.seconds }
19
- assert_kind_of(Fixnum, Uptime.seconds)
20
- assert_equal(true, Uptime.seconds > 0)
21
23
  end
22
24
 
23
- def test_minutes
25
+ test "seconds method returns a plausible value" do
26
+ assert_kind_of(Integer, Uptime.seconds)
27
+ assert_true(Uptime.seconds > 300)
28
+ end
29
+
30
+ test "minutes method basic functionality" do
24
31
  assert_respond_to(Uptime, :minutes)
25
32
  assert_nothing_raised{ Uptime.minutes }
26
- assert_kind_of(Fixnum, Uptime.minutes)
27
33
  end
28
34
 
29
- def test_hours
35
+ test "minutes method returns a plausible value" do
36
+ assert_kind_of(Integer, Uptime.minutes)
37
+ assert_true(Uptime.minutes > 5)
38
+ end
39
+
40
+ test "hours method basic functionality" do
30
41
  assert_respond_to(Uptime, :hours)
31
42
  assert_nothing_raised{ Uptime.hours }
32
- assert_kind_of(Fixnum, Uptime.hours)
33
43
  end
34
44
 
35
- def test_days
36
- assert_respond_to(Uptime,:days)
45
+ test "hours method returns a plausible value" do
46
+ assert_kind_of(Integer, Uptime.hours)
47
+ assert_true(Uptime.hours > 0)
48
+ end
49
+
50
+ test "days method basic functionality" do
51
+ assert_respond_to(Uptime, :days)
37
52
  assert_nothing_raised{ Uptime.days }
53
+ end
54
+
55
+ test "days method returns a plausible value" do
38
56
  assert_kind_of(Fixnum, Uptime.days)
39
- end
57
+ assert_true(Uptime.days >= 0)
58
+ end
40
59
 
41
- def test_uptime
42
- assert_respond_to(Uptime,:uptime)
60
+ test "uptime method basic functionality" do
61
+ assert_respond_to(Uptime, :uptime)
43
62
  assert_nothing_raised{ Uptime.uptime }
63
+ end
64
+
65
+ test "uptime method returns a non-empty string" do
44
66
  assert_kind_of(String, Uptime.uptime)
45
- assert_equal(false, Uptime.uptime.empty?)
67
+ assert_false(Uptime.uptime.empty?)
46
68
  end
47
-
48
- def test_dhms
49
- assert_respond_to(Uptime,:dhms)
69
+
70
+ test "uptime method does not accept any arguments" do
71
+ omit_if(File::ALT_SEPARATOR)
72
+ assert_raise(ArgumentError){ Uptime.uptime(1) }
73
+ end
74
+
75
+ test "uptime accepts a host name on Windows" do
76
+ omit_unless(File::ALT_SEPARATOR, "MS Windows only")
77
+ assert_nothing_raised{ Uptime.uptime(Socket.gethostname) }
78
+ end
79
+
80
+ test "dhms method basic functionality" do
81
+ assert_respond_to(Uptime, :dhms)
50
82
  assert_nothing_raised{ Uptime.dhms }
51
83
  assert_kind_of(Array, Uptime.dhms)
52
- assert_equal(false, Uptime.dhms.empty?)
84
+ end
85
+
86
+ test "dhms method returns an array of four elements" do
87
+ assert_false(Uptime.dhms.empty?)
53
88
  assert_equal(4, Uptime.dhms.length)
54
89
  end
55
-
56
- def test_boot_time
57
- assert_respond_to(Uptime,:boot_time)
90
+
91
+ test "boot_time method basic functionality" do
92
+ assert_respond_to(Uptime, :boot_time)
58
93
  assert_nothing_raised{ Uptime.boot_time }
94
+ end
95
+
96
+ test "boot_time method returns a Time object" do
59
97
  assert_kind_of(Time, Uptime.boot_time)
60
98
  end
61
99
 
62
- def test_uptime_error
100
+ test "Uptime class cannot be instantiated" do
63
101
  assert_kind_of(StandardError, Uptime::Error.new)
64
102
  end
103
+
104
+ test "Ensure that ffi functions are private" do
105
+ methods = Uptime.methods(false).map{ |e| e.to_s }
106
+ assert_false(Uptime.methods.include?('time'))
107
+ assert_false(Uptime.methods.include?('times'))
108
+ end
65
109
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sys-uptime
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 5
9
- - 4
10
- version: 0.5.4
8
+ - 6
9
+ - 0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Daniel J. Berger
@@ -15,42 +15,53 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-27 00:00:00 Z
19
- dependencies: []
20
-
18
+ date: 2011-12-11 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: ffi
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 23
29
+ segments:
30
+ - 1
31
+ - 0
32
+ - 0
33
+ version: 1.0.0
34
+ type: :runtime
35
+ version_requirements: *id001
21
36
  description: " The sys-uptime library is a simple interface for gathering uptime\n information. You can retrieve data in seconds, minutes, days, hours,\n or all of the above.\n"
22
37
  email: djberg96@gmail.com
23
38
  executables: []
24
39
 
25
- extensions:
26
- - ext/extconf.rb
40
+ extensions: []
41
+
27
42
  extra_rdoc_files:
28
43
  - CHANGES
29
44
  - README
30
45
  - MANIFEST
31
- - doc/uptime.txt
32
- - ext/sys/uptime.c
33
46
  files:
34
47
  - CHANGES
35
- - doc/uptime.txt
36
- - examples/sys_uptime_example.rb
37
- - ext/extconf.rb
38
- - ext/sys/uptime.c
39
- - lib/linux/sys/uptime.rb
48
+ - examples/uptime_test.rb
49
+ - lib/unix/sys/uptime.rb
40
50
  - lib/windows/sys/uptime.rb
41
51
  - MANIFEST
42
52
  - Rakefile
43
53
  - README
44
54
  - sys-uptime.gemspec
45
55
  - test/test_sys_uptime.rb
46
- homepage: http://www.rubyforge.org/projects/sysutils
47
- licenses: []
48
-
56
+ homepage: https://github.com/djberg96/sys-uptime
57
+ licenses:
58
+ - Artistic 2.0
49
59
  post_install_message:
50
60
  rdoc_options: []
51
61
 
52
62
  require_paths:
53
63
  - lib
64
+ - lib/unix
54
65
  required_ruby_version: !ruby/object:Gem::Requirement
55
66
  none: false
56
67
  requirements:
@@ -72,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
83
  requirements: []
73
84
 
74
85
  rubyforge_project: sysutils
75
- rubygems_version: 1.8.3
86
+ rubygems_version: 1.8.10
76
87
  signing_key:
77
88
  specification_version: 3
78
89
  summary: A Ruby interface for getting system uptime information.
@@ -1,98 +0,0 @@
1
- == Description
2
- The sys-uptime library provides uptime and boot time information, similar
3
- to the 'uptime' Unix command.
4
-
5
- == Synopsis
6
- require 'sys/uptime'
7
- include Sys
8
-
9
- p Uptime.days
10
- p Uptime.hours
11
- p Uptime.minutes
12
- p Uptime.seconds
13
- p Uptime.dhms.join(', ')
14
- p Uptime.uptime
15
- p Uptime.boot_time
16
-
17
- == Constants
18
- VERSION
19
- Returns the current version number of this library.
20
-
21
- == Class Methods
22
- Uptime.boot_time
23
- Returns the boot time as a Time object.
24
-
25
- Uptime.days
26
- Returns the total number of days the system has been up.
27
-
28
- Uptime.hours
29
- Returns the total number of hours the system has been up.
30
-
31
- Uptime.minutes
32
- Returns the total number of minutes the system has been up.
33
-
34
- Uptime.seconds
35
- Returns the total number of seconds the system has been up.
36
-
37
- Uptime.dhms
38
- Calculates and returns the number of days, hours, minutes and
39
- seconds the system has been running as a four-element Array.
40
-
41
- Uptime.uptime
42
- Calculates and returns the number of days, hours, minutes and
43
- seconds the system has been running as a colon-separated string.
44
-
45
- == Exceptions
46
- Uptime::Error
47
- Raised if something goes wrong. On Unix, this would likely mean a
48
- failure of the times() function. That would be highly unusual.
49
-
50
- On Windows, it probably means you failed to connect to WMI properly. The
51
- mostly likely reason would be that the WMI service wasn't running.
52
-
53
- == Notes
54
- On MS Windows each of the class methods optionally takes a host name as
55
- a single argument. The default is localhost (or whatever the result of
56
- Socket.gethostname returns).
57
-
58
- The current time, users and load average are NOT included in this
59
- module, even though you may be used to seeing them with the command
60
- line version of 'uptime'. This is because these things have
61
- absolutely nothing to do with uptime. At least, not as I logically
62
- think of uptime.
63
-
64
- This library was tested successfully on a Solaris system with over 1600
65
- days of uptime.
66
-
67
- == Known Bugs
68
- None that I am aware of. Please log any bugs you find on the project
69
- website at http://www.rubyforge.org/projects/sysutils.
70
-
71
- == Questions
72
- "Doesn't Struct::Tms do this?" - No.
73
-
74
- == License
75
- Artistic 2.0
76
-
77
- == Copyright
78
- Copyright 2002-2011, Daniel J. Berger
79
-
80
- All Rights Reserved. This module is free software. It may be used,
81
- redistributed and/or modified under the same terms as Ruby itself.
82
-
83
- == Warranty
84
- This library is provided "as is" and without any express or
85
- implied warranties, including, without limitation, the implied
86
- warranties of merchantability and fitness for a particular purpose.
87
-
88
- == Acknowledgements
89
- Mike Hall for help with the BSD side of things.
90
- Ola Eriksson, whose source code I shamelessly plagiarized to get a better
91
- implementation for systems that have the utmpx.h header file.
92
-
93
- == Authors
94
- Daniel J. Berger
95
- Mike Hall
96
-
97
- == See Also
98
- uptime(1)
@@ -1,28 +0,0 @@
1
- ##########################################################################
2
- # extconf.rb
3
- #
4
- # Configuration & build script for sys-uptime. Generally speaking you
5
- # should build sys-uptime with the 'rake build' task instead of running
6
- # this script manually.
7
- ##########################################################################
8
- require 'mkmf'
9
- require 'fileutils'
10
-
11
- if RUBY_PLATFORM =~ /windows|win32|cygwin|mingw|dos|linux/i
12
- STDERR.puts "Do not compile on this platform. Run 'rake gem:install' instead."
13
- exit
14
- end
15
-
16
- dir_config('uptime')
17
-
18
- have_header('sys/loadavg.h')
19
-
20
- if have_func('sysctl')
21
- have_header('sys/param.h')
22
- have_header('sys/time.h')
23
- have_header('sys/types.h')
24
- else
25
- have_header('utmpx.h')
26
- end
27
-
28
- create_makefile('sys/uptime', 'sys')
@@ -1,250 +0,0 @@
1
- /******************************************************************************
2
- * uptime.c
3
- *
4
- * Authors:
5
- * Daniel Berger
6
- * Mike Hall
7
- *
8
- * sys-uptime source code for most *nix platforms
9
- *****************************************************************************/
10
- #include <ruby.h>
11
- #include <string.h>
12
- #include <errno.h>
13
-
14
- #ifdef HAVE_SYSCTL
15
-
16
- #include <sys/sysctl.h>
17
-
18
- #ifdef HAVE_SYS_PARAM_H
19
- #include <sys/param.h>
20
- #endif
21
-
22
- #ifdef HAVE_SYS_TIME_H
23
- #include <sys/time.h>
24
- #endif
25
-
26
- #ifdef HAVE_SYS_TYPES_H
27
- #include <sys/types.h>
28
- #endif
29
-
30
- #else
31
-
32
- #include <sys/times.h>
33
- #include <unistd.h>
34
- #include <time.h>
35
-
36
- #ifdef HAVE_UTMPX_H
37
- #include <utmpx.h>
38
- #endif
39
-
40
- #ifdef _SC_CLK_TCK
41
- #define TICKS sysconf(_SC_CLK_TCK)
42
- #else
43
- #define TICKS sysconf(CLOCKS_PER_SEC)
44
- #endif
45
-
46
- #endif
47
-
48
- #ifdef HAVE_SYS_LOADAVG_H
49
- #include <sys/loadavg.h>
50
- #endif
51
-
52
- #define MAXSTRINGSIZE 32 /* reasonable limit */
53
-
54
- #define SYS_UPTIME_VERSION "0.5.4"
55
-
56
- VALUE cUptimeError;
57
-
58
- unsigned long get_uptime_secs()
59
- {
60
- #ifdef HAVE_SYSCTL
61
- struct timeval tv;
62
- size_t tvlen = sizeof(tv);
63
- int mib[2];
64
-
65
- mib[0] = CTL_KERN;
66
- mib[1] = KERN_BOOTTIME;
67
-
68
- if(sysctl(mib, 2, &tv, &tvlen, NULL, 0))
69
- rb_raise(cUptimeError, "sysctl() call failed %s", strerror(errno));
70
-
71
- return time(NULL) - tv.tv_sec;
72
- #else
73
- struct tms tms;
74
- unsigned long seconds;
75
- seconds = times(&tms) / TICKS;
76
-
77
- if(-1 == seconds)
78
- rb_raise(cUptimeError, "times() function failed: %s", strerror(errno));
79
-
80
- if(seconds < 0)
81
- rb_raise(cUptimeError, "value returned larger than type could handle");
82
-
83
- return seconds;
84
- #endif
85
- }
86
-
87
- /*
88
- * call-seq:
89
- * Uptime.seconds
90
- *
91
- * Returns the total number of seconds the system has been up.
92
- */
93
- static VALUE uptime_seconds()
94
- {
95
- return UINT2NUM(get_uptime_secs());
96
- }
97
-
98
- /*
99
- * call-seq:
100
- * Uptime.minutes
101
- *
102
- * Returns the total number of minutes the system has been up.
103
- */
104
- static VALUE uptime_minutes()
105
- {
106
- return UINT2NUM(get_uptime_secs() / 60);
107
- }
108
-
109
- /*
110
- * call-seq:
111
- * Uptime.hours
112
- *
113
- * Returns the total number of hours the system has been up.
114
- */
115
- static VALUE uptime_hours()
116
- {
117
- return UINT2NUM(get_uptime_secs() / 3600);
118
- }
119
-
120
- /*
121
- * call-seq:
122
- * Uptime.days
123
- *
124
- * Returns the total number of days the system has been up.
125
- */
126
- static VALUE uptime_days()
127
- {
128
- return UINT2NUM(get_uptime_secs() / 86400);
129
- }
130
-
131
- /*
132
- * call-seq:
133
- * Uptime.uptime
134
- *
135
- * Calculates and returns the number of days, hours, minutes and
136
- * seconds the system has been running as a colon-separated string.
137
- */
138
- static VALUE uptime_uptime()
139
- {
140
- char c_string[MAXSTRINGSIZE];
141
- long seconds, days, hours, minutes;
142
-
143
- seconds = get_uptime_secs();
144
- days = seconds/86400;
145
- seconds -= days*86400;
146
- hours = seconds/3600;
147
- seconds -= hours*3600;
148
- minutes = seconds/60;
149
- seconds -= minutes*60;
150
-
151
- sprintf(c_string, "%ld:%ld:%ld:%ld", days, hours, minutes, seconds);
152
-
153
- return rb_str_new2(c_string);
154
- }
155
-
156
- /*
157
- * call-seq:
158
- * Uptime.dhms
159
- *
160
- * Calculates and returns the number of days, hours, minutes and
161
- * seconds the system has been running as a four-element Array.
162
- */
163
- static VALUE uptime_dhms()
164
- {
165
- VALUE a = rb_ary_new2(4);
166
- long s, m, h, d;
167
-
168
- s = get_uptime_secs();
169
- d = s / (24*60*60);
170
- h = (s -= d*24*60*60) / ( 60*60);
171
- m = (s -= h* 60*60) / 60;
172
- s -= m* 60;
173
-
174
- rb_ary_push(a, INT2FIX(d));
175
- rb_ary_push(a, INT2FIX(h));
176
- rb_ary_push(a, INT2FIX(m));
177
- rb_ary_push(a, INT2FIX(s));
178
-
179
- return a;
180
- }
181
-
182
- /*
183
- * call-seq:
184
- * Uptime.boot_time
185
- *
186
- * Returns the boot time as a Time object.
187
- */
188
- static VALUE uptime_btime(){
189
- VALUE v_time = Qnil;
190
-
191
- #ifdef HAVE_SYSCTL
192
- struct timeval tv;
193
- size_t tvlen = sizeof(tv);
194
- int mib[2];
195
-
196
- mib[0] = CTL_KERN;
197
- mib[1] = KERN_BOOTTIME;
198
-
199
- if(sysctl(mib, 2, &tv, &tvlen, NULL, 0))
200
- rb_raise(cUptimeError, "sysctl() call failed: %s", strerror(errno));
201
-
202
- v_time = rb_time_new(tv.tv_sec,tv.tv_usec);
203
- #else
204
- #ifdef HAVE_UTMPX_H
205
- struct utmpx* ent;
206
-
207
- setutxent();
208
-
209
- while( (ent = getutxent()) ){
210
- if(ent->ut_type == BOOT_TIME){
211
- v_time = rb_time_new(ent->ut_tv.tv_sec, ent->ut_tv.tv_usec);
212
- break;
213
- }
214
- }
215
-
216
- endutxent();
217
- #else
218
- rb_raise(cUptimeError, "boot_time method not implemented on this platform");
219
- #endif
220
- #endif
221
-
222
- return v_time;
223
- }
224
-
225
- void Init_uptime()
226
- {
227
- VALUE mSys, cUptime;
228
-
229
- /* The Sys module only serves as a toplevel namespace */
230
- mSys = rb_define_module("Sys");
231
-
232
- /* The Uptime encapsulates various bits of uptime information */
233
- cUptime = rb_define_class_under(mSys, "Uptime", rb_cObject);
234
-
235
- /* The Uptime::Error class is raised if any of the Uptime methods fail */
236
- cUptimeError = rb_define_class_under(cUptime, "Error", rb_eStandardError);
237
-
238
- /* 0.5.4: The version of this library */
239
- rb_define_const(cUptime, "VERSION", rb_str_new2(SYS_UPTIME_VERSION));
240
-
241
- /* Singleton Methods */
242
-
243
- rb_define_singleton_method(cUptime, "seconds", uptime_seconds, 0);
244
- rb_define_singleton_method(cUptime, "minutes", uptime_minutes, 0);
245
- rb_define_singleton_method(cUptime, "hours", uptime_hours, 0);
246
- rb_define_singleton_method(cUptime, "days", uptime_days, 0);
247
- rb_define_singleton_method(cUptime, "uptime", uptime_uptime, 0);
248
- rb_define_singleton_method(cUptime, "dhms", uptime_dhms, 0);
249
- rb_define_singleton_method(cUptime, "boot_time", uptime_btime, 0);
250
- }
@@ -1,101 +0,0 @@
1
- # The Sys module serves as a namespace only.
2
- module Sys
3
-
4
- # The Uptime class encapsulates various bits of information regarding your
5
- # system's uptime, including boot time.
6
- class Uptime
7
-
8
- # Error typically raised in one of the Uptime methods should fail.
9
- class Error < StandardError; end
10
-
11
- # The version of the sys-uptime library.
12
- VERSION = '0.5.4'
13
-
14
- # The file to read uptime information from.
15
- UPTIME_FILE = '/proc/uptime'
16
-
17
- # Returns the total number of seconds of uptime.
18
- #
19
- # Example:
20
- #
21
- # Sys::Uptime.seconds # => 118800
22
- #
23
- def self.seconds
24
- begin
25
- IO.read(UPTIME_FILE).split.first.to_i
26
- rescue Exception => err
27
- raise Error, err
28
- end
29
- end
30
-
31
- # Returns the total number of minutes of uptime.
32
- #
33
- # Example:
34
- #
35
- # Sys::Uptime.minutes # => 678
36
- #
37
- def self.minutes
38
- self.seconds / 60
39
- end
40
-
41
- # Returns the total number of hours of uptime.
42
- #
43
- # Example:
44
- #
45
- # Sys::Uptime.hours # => 31
46
- #
47
- def self.hours
48
- self.minutes / 60
49
- end
50
-
51
- # Returns the total number of days of uptime.
52
- #
53
- # Example:
54
- #
55
- # Sys::Uptime.days # => 2
56
- #
57
- def self.days
58
- self.hours / 24
59
- end
60
-
61
- # Returns the uptime as a colon separated string, including days,
62
- # hours, minutes and seconds.
63
- #
64
- # Example:
65
- #
66
- # Sys::Uptime.uptime # => "1:9:24:57"
67
- #
68
- def self.uptime
69
- seconds = self.seconds
70
- days = seconds / 86400
71
- seconds -= days * 86400
72
- hours = seconds / 3600
73
- seconds -= hours * 3600
74
- minutes = seconds / 60
75
- seconds -= minutes * 60
76
-
77
- "#{days}:#{hours}:#{minutes}:#{seconds}"
78
- end
79
-
80
- # Returns the uptime as a four element array, including days, hours,
81
- # minutes and seconds.
82
- #
83
- # Example:
84
- #
85
- # Sys::Uptime.dhms # => [1,9,24,57]
86
- #
87
- def self.dhms
88
- self.uptime.split(":")
89
- end
90
-
91
- # Returns the time the system was booted as a Time object.
92
- #
93
- # Example:
94
- #
95
- # Sys::Uptime.boot_time
96
- #
97
- def self.boot_time
98
- Time.now - self.seconds
99
- end
100
- end
101
- end