sys-uptime 0.7.2-universal-mingw32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1a2dd1832bebec9fd06b9481d0aeb067a2155e9b09c746814bb49bf4196e24d1
4
+ data.tar.gz: 33b1ddb693e56b58f864e9fe63b84f4c7db5ee7030463c0ba2c6f3bec0652d80
5
+ SHA512:
6
+ metadata.gz: 9e9a4994e293334e196804185fafd90ea1ea0a3ddfadf22e06942f04468bd59f11da60b89c10f414655ef4225b623259bbc13064218a687498976d3f270c5ae5
7
+ data.tar.gz: 329787a710fde14f5d9e5ca40afd503b579e8225a26681f15b6a8d3f36f8d6d73b03ce993c95c6c09afeb5da3f5e463daf4aff47f6466b2bf15e9e97f69205b0
@@ -0,0 +1,2 @@
1
+ Ob=���mޱ��^,��݋�*6+�\�zC}�y�a(����O���z��_�W�c9���F3W?�`��l�wE��*a�(�
2
+ z]�yM�KA Q
Binary file
data/CHANGES ADDED
@@ -0,0 +1,169 @@
1
+ == 0.7.2 - 4-Nov-2018
2
+ * Added metadata to the gemspec.
3
+ * The VERSION constant is now frozen.
4
+ * Updated cert.
5
+
6
+ == 0.7.1 - 14-May-2016
7
+ * Altered internal layout, which also fixed a require bug. Thanks go
8
+ to jkburges for the spot.
9
+ * Moved the VERSION constant into a single file shared by all platforms.
10
+
11
+ == 0.7.0 - 3-Oct-2015
12
+ * Changed license to Apache 2.0.
13
+ * Added a cert. This gem is now signed.
14
+ * Added a sys-uptime.rb file for convenience.
15
+ * Gem related tasks in the Rakefile now assume Rubygems 2.x.
16
+
17
+ == 0.6.2 - 8-Nov-2014
18
+ * Minor updates to gemspec and Rakefile.
19
+
20
+ == 0.6.1 - 22-Oct-2012
21
+ * Refactored a private method in the MS Windows source.
22
+ * Minor fix for one private method test.
23
+ * Fixed an RbConfig vs Config warning. Thanks Pedro Carrico.
24
+
25
+ == 0.6.0 - 11-Dec-2011
26
+ * Switched Unix code to use FFI.
27
+ * Removed all of the C related tasks from the Rakefile and added the gem:build
28
+ and gem:install tasks.
29
+ * Internal directory layout changes, with appropriate changes to the gemspec.
30
+
31
+ == 0.5.3 - 7-May-2009
32
+ * Altered the Uptime.seconds implementation on Linux so that it works with
33
+ both Ruby 1.8.x and 1.9.x. Thanks go to Alexey Chebotar for the spot.
34
+
35
+ == 0.5.2 - 13-Dec-2008
36
+ * Fixed a date/time issue in the Windows version caused by Ruby itself.
37
+ * Fixed the Uptime.seconds, Uptime.minutes and Uptime.hours methods on MS
38
+ Windows.
39
+ * Renamed the test file to 'test_sys_uptime.rb'.
40
+ * Some minor updates to the Rakefile.
41
+
42
+ == 0.5.1 - 26-Jul-2007
43
+ * Fixed bug in the MS Windows version caused by incorrect parsing of an
44
+ MS specific date format (caused by a bug in Ruby 1.8.6). Thanks go to
45
+ Robert H. for the spot.
46
+ * Inlined the Rake installation tasks, so the install.rb file has been
47
+ removed.
48
+ * Added an 'install_gem' Rake task, and updated the README installation
49
+ instructions.
50
+
51
+ == 0.5.0 - 30-Mar-2007
52
+ * For platforms that use C code, the code now always uses the sysctl()
53
+ function if supported by your system. This replaces the platform specific
54
+ checks I was doing for the various BSD flavors.
55
+ * Fix for OS X - the Uptime.boot_time method now works.
56
+ * UptimeError is now Uptime::Error.
57
+ * Improved RDoc in the uptime.c source code.
58
+ * Added a Rakefile - users should now use the 'test' and 'install' rake tasks.
59
+ * Updates to the MANIFEST, README and uptime.txt files.
60
+
61
+ == 0.4.5 - 19-Nov-2006
62
+ * Internal layout changes, minor doc updates and gemspec improvements.
63
+ * No code changes.
64
+
65
+ == 0.4.4 - 30-Jun-2006
66
+ * Added inline rdoc documentation to the source files.
67
+ * Added a gemspec.
68
+
69
+ == 0.4.3 - 18-Dec-2005
70
+ * Changed the Linux version to pure Ruby. The current method of determining
71
+ uptime in unix.c does not work in Linux kernel 2.6+. So, from now on it
72
+ reads out of /proc/uptime.
73
+
74
+ == 0.4.2 - 6-May-2005
75
+ * Fixed a potential boot_time bug.
76
+ * Removed the version.h file. It's no longer needed since the Windows
77
+ version is pure Ruby.
78
+ * NetBSD 2.x and FreeBSD 5.x now supported.
79
+ * Removed the INSTALL file. Installation instructions are now in
80
+ the README file.
81
+ * Removed the uptime.rd file. You can generate html documentation by
82
+ running rdoc over the uptime.txt file.
83
+ * Made most documents rdoc friendly.
84
+ * Moved project to RubyForge.
85
+
86
+ == 0.4.1 - 14-Dec-2004
87
+ * Moved freebsd code into unix.c file.
88
+ * Should now work past 249 days (2**31) on systems that have utmpx.h.
89
+ * Fixed a bug with regards to boot_time, where it was possible that it would
90
+ simply be empty.
91
+
92
+ == 0.4.0 - 8-Jul-2004
93
+ * Removed all reference to the CLK_TCK constant, as per documentation from
94
+ Richard Stevens that it is deprecated and that sysconf() should be used
95
+ instead (well, I knew about this, but ignored it until now).
96
+ * Scrapped the C version for Windows in favor of a pure Ruby version that
97
+ uses win32ole + WMI.
98
+ * Added a boot_time method for Unix systems (Windows already had this).
99
+ * Added an UptimeError class on Unix systems (and replaced UptimeException
100
+ on Windows).
101
+ * Modified an internal function to raise an UptimeError if the times()
102
+ function fails. Also, it now returns an unsigned long instead of a plain
103
+ long.
104
+ * Replaced the two different test suites with a single, unified test suite.
105
+ * Cleaned up the extconf.rb script. I now assume that TestUnit is installed.
106
+ * Removed the load_avg method (which was never advertised and which I hope
107
+ you weren't using). You can find a load_avg method in the sys-cpu package.
108
+ * Changed uptime.rd2 to uptime.rd to be consistent with my other packages.
109
+ * Removed the uptime.html file - you may generate that on your own.
110
+ * Added warranty and license info.
111
+
112
+ == 0.3.2 - 30-Dec-2003
113
+ * Cleaned up some warnings that showed up with -Wall on some unix platforms
114
+ (int vs long format, explicit include)
115
+ * Minor test suite and extconf.rb changes
116
+
117
+ == 0.3.1 - 25-Jun-2003
118
+ * Modified test files to handle HP-UX extensions
119
+ * Minor doc updates
120
+ * Added the dhms() method. Actually, this was in the 0.3.0
121
+ release, I just forgot to mention it in this file :)
122
+
123
+ == 0.3.0 - 22-Jun-2003
124
+ * Added OS X support - thanks go to Mike Hall for the patch
125
+ * Fixed incorrect values in FreeBSD (again Mike Hall)
126
+ * Modified tc_unix.rb test suite to handle OS X, along with a bit
127
+ * of minor cleanup
128
+ * Removed VERSION class method. Use the constant instead
129
+ * Separated FreeBSD/OS X source into its own file (freebsd.c).
130
+ The #ifdefs were starting to get too ugly for me
131
+
132
+ == 0.2.1 - 13-May-2003
133
+ * Fixed bug in extconf.rb file, and made some major changes
134
+ * Modified test.rb for those without TestUnit
135
+ * Modified TestUnit tests - some bogus tests were removed
136
+ * Added a README file with some additional info
137
+ * Created a version.h file, so that I can keep the VERSION number
138
+ in one place
139
+ * Docs automatically included in doc directory (i.e. no more interactive
140
+ document creation)
141
+
142
+ == 0.2.0 - 13-Mar-2003
143
+ * Added MS Windows support
144
+ * Added a VERSION constant
145
+ * Added a test suite (for those with TestUnit installed)
146
+ * Internal directory layout change
147
+ * uptime.c is now split into unix.c and windows.c (and linked appropriately)
148
+ * Changelog and Manifest are now CHANGES and MANIFEST, respectively
149
+ * Many changes to extconf.rb
150
+
151
+ == 0.1.3 - 6-Jan-2003
152
+ * Added a VERSION class method
153
+ * Added a copyright notice
154
+ * Fixed up the docs a bit and moved them to the doc directory
155
+ * Changed the tarball name to match the RAA package name
156
+ * Modified test.rb script to make it better
157
+ * Changed install instructions slightly
158
+
159
+ == 0.1.2 - 25-Aug-2002
160
+ * Slight change to preprocessor commands to avoid redefining CLK_TCK on
161
+ those systems that define it in time.h
162
+ * Added an INSTALL file
163
+
164
+ == 0.1.1 - 21-Jun-2002
165
+ * The CLK_TCK constant wasn't necessarily being set correctly, which could
166
+ lead to some odd results. This has been fixed.
167
+
168
+ == 0.1.0 - 17-Jun-2002
169
+ * Initial release
@@ -0,0 +1,11 @@
1
+ * CHANGES
2
+ * MANIFEST
3
+ * Rakefile
4
+ * README
5
+ * sys-uptime.gemspec
6
+ * certs/djberg96_pub.pem
7
+ * examples/test.rb
8
+ * lib/sys-uptime.rb
9
+ * lib/unix/sys/uptime.rb
10
+ * lib/windows/sys/uptime.rb
11
+ * test/test_sys_uptime.rb
data/README ADDED
@@ -0,0 +1,66 @@
1
+ = Description
2
+ A Ruby interface for getting system uptime information.
3
+
4
+ = Prerequisites
5
+ ffi 0.1.0 or later on Unixy platforms.
6
+
7
+ = Installation
8
+ gem install sys-uptime
9
+
10
+ = Synopsis
11
+ require 'sys-uptime'
12
+ include Sys
13
+
14
+ # Get everything
15
+ p Uptime.uptime
16
+ p Uptime.dhms.join(', ')
17
+
18
+ # Get individual units
19
+ p Uptime.days
20
+ p Uptime.hours
21
+ p Uptime.minutes
22
+ p Uptime.seconds
23
+
24
+ # Get the boot time
25
+ p Uptime.boot_time
26
+
27
+ = Notes
28
+ On MS Windows the Uptime.uptime and Uptime_boot_time methods optionally
29
+ takes a host name as a single argument. The default is localhost.
30
+
31
+ The current time, users and load average are not included in this library
32
+ module, even though you may be used to seeing them with the command
33
+ line version of 'uptime'.
34
+
35
+ == Known Bugs
36
+ None that I am aware of. Please log any bugs you find on the project
37
+ website at https://github.com/djberg96/sys-uptime.
38
+
39
+ == Questions
40
+ "Doesn't Struct::Tms do this?" - No.
41
+
42
+ == License
43
+ Apache 2.0
44
+
45
+ == Copyright
46
+ Copyright 2002-2018, Daniel J. Berger
47
+
48
+ All Rights Reserved. This module is free software. It may be used,
49
+ redistributed and/or modified under the same terms as Ruby itself.
50
+
51
+ == Warranty
52
+ This library is provided "as is" and without any express or
53
+ implied warranties, including, without limitation, the implied
54
+ warranties of merchantability and fitness for a particular purpose.
55
+
56
+ == Acknowledgements
57
+ Andrea Fazzi for help with the FFI version.
58
+
59
+ Mike Hall for help with the BSD side of things for the original C code.
60
+
61
+ Ola Eriksson, whose source code I shamelessly plagiarized to get a better
62
+ implementation for systems that have the utmpx.h header file for the
63
+ original C code.
64
+
65
+ == Author
66
+ Daniel J. Berger
@@ -0,0 +1,42 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require 'rake/testtask'
4
+
5
+ CLEAN.include("**/*.gem", "**/*.rbx", "**/*.rbc")
6
+
7
+ namespace 'gem' do
8
+ desc 'Build the sys-uptime gem'
9
+ task :create => [:clean] do
10
+ require 'rubygems/package'
11
+ spec = eval(IO.read('sys-uptime.gemspec'))
12
+ spec.signing_key = File.join(Dir.home, '.ssh', 'gem-private_key.pem')
13
+
14
+ if File::ALT_SEPARATOR
15
+ spec.platform = Gem::Platform.new(['universal','mingw32'])
16
+ else
17
+ spec.add_dependency('ffi', '>= 1.0.0')
18
+ end
19
+
20
+ Gem::Package.build(spec, true)
21
+ end
22
+
23
+ desc 'Install the sys-uptime gem'
24
+ task :install => [:create] do
25
+ file = Dir["*.gem"].first
26
+ sh "gem install -l #{file}"
27
+ end
28
+ end
29
+
30
+ desc "Run the test suite"
31
+ Rake::TestTask.new do |t|
32
+ if File::ALT_SEPARATOR
33
+ t.libs << 'lib/windows'
34
+ else
35
+ t.libs << 'lib/unix'
36
+ end
37
+
38
+ t.warning = true
39
+ t.verbose = true
40
+ end
41
+
42
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIEcDCCAtigAwIBAgIBATANBgkqhkiG9w0BAQsFADA/MREwDwYDVQQDDAhkamJl
3
+ cmc5NjEVMBMGCgmSJomT8ixkARkWBWdtYWlsMRMwEQYKCZImiZPyLGQBGRYDY29t
4
+ MB4XDTE4MDMxODE1MjIwN1oXDTI4MDMxNTE1MjIwN1owPzERMA8GA1UEAwwIZGpi
5
+ ZXJnOTYxFTATBgoJkiaJk/IsZAEZFgVnbWFpbDETMBEGCgmSJomT8ixkARkWA2Nv
6
+ bTCCAaIwDQYJKoZIhvcNAQEBBQADggGPADCCAYoCggGBALgfaroVM6CI06cxr0/h
7
+ A+j+pc8fgpRgBVmHFaFunq28GPC3IvW7Nvc3Y8SnAW7pP1EQIbhlwRIaQzJ93/yj
8
+ u95KpkP7tA9erypn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EiMSs3alYR
24
+ ORVCZpRuCPpmC8qmqxUnARDArzucjaclkxjLWvCVHeFa9UP7K3Nl9oTjJNv+7/jM
25
+ WZs4eecIcUc4tKdHxcAJ0MO/Dkqq7hGaiHpwKY76wQ1+8xAh
26
+ -----END CERTIFICATE-----
@@ -0,0 +1,21 @@
1
+ ###########################################################
2
+ # uptime_test.rb
3
+ #
4
+ # A generic test script for general futzing. You can run
5
+ # this script via the 'rake example' task.
6
+ ###########################################################
7
+ require 'sys/uptime'
8
+ include Sys
9
+
10
+ print "\nGENERIC TEST SCRIPT FOR SYS-UPTIME\n\n"
11
+ puts 'VERSION: ' + Uptime::VERSION
12
+
13
+ puts "Days: " + Uptime.days.to_s
14
+ puts "Hours: " + Uptime.hours.to_s
15
+ puts "Minutes: " + Uptime.minutes.to_s
16
+ puts "Seconds: " + Uptime.seconds.to_s
17
+ puts "Uptime: " + Uptime.uptime
18
+ puts "DHMS: " + Uptime.dhms.join(', ')
19
+ puts "Boot Time: " + Uptime.boot_time.to_s
20
+
21
+ print "\nTest successful\n"
@@ -0,0 +1 @@
1
+ require_relative 'sys/uptime'
@@ -0,0 +1,210 @@
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
+ private
16
+
17
+ # Hit this issue on Linux, not sure why
18
+ begin
19
+ find_type(:clock_t)
20
+ rescue TypeError
21
+ typedef(:long, :clock_t)
22
+ end
23
+
24
+ attach_function :strerror, [:int], :string
25
+ attach_function :sysconf, [:int], :long
26
+ attach_function :time, [:pointer], :time_t
27
+ attach_function :times, [:pointer], :clock_t
28
+
29
+ private_class_method :strerror, :sysconf, :time, :times
30
+
31
+ begin
32
+ attach_function :sysctl, [:pointer, :uint, :pointer, :pointer, :pointer, :size_t], :int
33
+ private_class_method :sysctl
34
+ rescue FFI::NotFoundError
35
+ attach_function :setutxent, [], :void
36
+ attach_function :getutxent, [], :pointer
37
+ attach_function :endutxent, [], :void
38
+ private_class_method :setutxent, :getutxent, :endutxent
39
+ end
40
+
41
+ CTL_KERN = 1 # Kernel
42
+ KERN_BOOTTIME = 21 # Time kernel was booted
43
+ TICKS = 100 # Ticks per second (TODO: use sysconf)
44
+ BOOT_TIME = 2 # Boot time
45
+
46
+ class Tms < FFI::Struct
47
+ layout(
48
+ :tms_utime, :clock_t,
49
+ :tms_stime, :clock_t,
50
+ :tms_cutime, :clock_t,
51
+ :tms_cstime, :clock_t
52
+ )
53
+ end
54
+
55
+ class Timeval < FFI::Struct
56
+ layout(
57
+ :tv_sec, :long,
58
+ :tv_usec, :long
59
+ )
60
+ end
61
+
62
+ class ExitStatus < FFI::Struct
63
+ layout(
64
+ :e_termination, :short,
65
+ :e_exit, :short
66
+ )
67
+ end
68
+
69
+ class Utmpx < FFI::Struct
70
+ layout(
71
+ :ut_user, [:char, 32],
72
+ :ut_id, [:char, 4],
73
+ :ut_line, [:char, 32],
74
+ :ut_pid, :pid_t,
75
+ :ut_type, :short,
76
+ :ut_exit, ExitStatus,
77
+ :ut_tv, Timeval,
78
+ :ut_session, :int,
79
+ :padding, [:int, 5],
80
+ :ut_host, [:char, 257]
81
+ )
82
+ end
83
+
84
+ public
85
+
86
+ # Returns a Time object indicating the time the system was last booted.
87
+ #
88
+ # Example:
89
+ #
90
+ # Sys::Uptime.boot_time # => Mon Jul 13 06:08:25 -0600 2009
91
+ #
92
+ def self.boot_time
93
+ if RbConfig::CONFIG['host_os'] =~ /linux/i
94
+ Time.now - self.seconds
95
+ elsif respond_to?(:sysctl, true)
96
+ tv = Timeval.new
97
+ mib = FFI::MemoryPointer.new(:int, 2).write_array_of_int([CTL_KERN, KERN_BOOTTIME])
98
+ size = FFI::MemoryPointer.new(:long, 1).write_int(tv.size)
99
+
100
+ if sysctl(mib, 2, tv, size, nil, 0) != 0
101
+ raise SystemCallError, 'sysctl() - ' + strerror(FFI.errno)
102
+ end
103
+
104
+ Time.at(tv[:tv_sec], tv[:tv_usec])
105
+ else
106
+ begin
107
+ setutxent()
108
+ while ent = Utmpx.new(getutxent())
109
+ if ent[:ut_type] == BOOT_TIME
110
+ time = Time.at(ent[:ut_tv][:tv_sec], ent[:ut_tv][:tv_usec])
111
+ break
112
+ end
113
+ end
114
+ ensure
115
+ endutxent()
116
+ end
117
+ time
118
+ end
119
+ end
120
+
121
+ # Returns the total number of seconds of uptime.
122
+ #
123
+ # Example:
124
+ #
125
+ # Sys::Uptime.seconds => 118800
126
+ #
127
+ def self.seconds
128
+ if RbConfig::CONFIG['host_os'] =~ /linux/i
129
+ begin
130
+ IO.read('/proc/uptime').split.first.to_i
131
+ rescue Exception => err
132
+ raise Error, err
133
+ end
134
+ elsif respond_to?(:sysctl, true)
135
+ tv = Timeval.new
136
+ mib = FFI::MemoryPointer.new(:int, 2).write_array_of_int([CTL_KERN, KERN_BOOTTIME])
137
+ size = FFI::MemoryPointer.new(:long, 1).write_int(tv.size)
138
+
139
+ if sysctl(mib, 2, tv, size, nil, 0) != 0
140
+ raise SystemCallError, 'sysctl() - ' + strerror(FFI.errno)
141
+ end
142
+
143
+ time(nil) - tv[:tv_sec]
144
+ else
145
+ tms = Tms.new
146
+ times(tms) / TICKS
147
+ end
148
+ end
149
+
150
+ # Returns the total number of minutes of uptime.
151
+ #
152
+ # Example:
153
+ #
154
+ # Sys::Uptime.minutes # => 678
155
+ #
156
+ def self.minutes
157
+ seconds / 60
158
+ end
159
+
160
+ # Returns the total number of hours of uptime.
161
+ #
162
+ # Example:
163
+ #
164
+ # Sys::Uptime.hours # => 31
165
+ #
166
+ def self.hours
167
+ seconds / 3600
168
+ end
169
+
170
+ # Returns the total number of days of uptime.
171
+ #
172
+ # Example:
173
+ #
174
+ # Sys::Uptime.days # => 2
175
+ #
176
+ def self.days
177
+ seconds / 86400
178
+ end
179
+
180
+ # Returns the uptime as a colon separated string, including days,
181
+ # hours, minutes and seconds.
182
+ #
183
+ # Example:
184
+ #
185
+ # Sys::Uptime.uptime # => "1:9:24:57"
186
+ #
187
+ def self.uptime
188
+ secs = seconds
189
+ days = secs / 86400
190
+ secs -= days * 86400
191
+ hours = secs / 3600
192
+ secs -= hours * 3600
193
+ mins = secs / 60
194
+ secs -= mins * 60
195
+
196
+ "#{days}:#{hours}:#{mins}:#{secs}"
197
+ end
198
+
199
+ # Returns the uptime as a four element array, including days, hours,
200
+ # minutes and seconds.
201
+ #
202
+ # Example:
203
+ #
204
+ # Sys::Uptime.dhms # => [1,9,24,57]
205
+ #
206
+ def self.dhms
207
+ uptime.split(':')
208
+ end
209
+ end
210
+ end
@@ -0,0 +1,12 @@
1
+ if File::ALT_SEPARATOR
2
+ require_relative 'windows/sys/uptime'
3
+ else
4
+ require_relative 'unix/sys/uptime'
5
+ end
6
+
7
+ module Sys
8
+ class Uptime
9
+ # The version of the sys-uptime library
10
+ VERSION = '0.7.2'.freeze
11
+ end
12
+ end
@@ -0,0 +1,144 @@
1
+ require 'win32ole'
2
+ require 'socket'
3
+ require 'date'
4
+ require 'time'
5
+
6
+ # The Sys module serves as a namespace only.
7
+ module Sys
8
+
9
+ # The Uptime class encapsulates various bits of information regarding your
10
+ # system's uptime, including boot time.
11
+ class Uptime
12
+
13
+ # Error typically raised in one of the Uptime methods should fail.
14
+ class Error < StandardError; end
15
+
16
+ # Returns the boot time as a Time object.
17
+ #
18
+ # Example:
19
+ #
20
+ # Sys::Uptime.boot_time # => Fri Dec 12 20:18:58 -0700 2008
21
+ #
22
+ def self.boot_time(host = Socket.gethostname)
23
+ cs = "winmgmts://#{host}/root/cimv2"
24
+ begin
25
+ wmi = WIN32OLE.connect(cs)
26
+ rescue WIN32OLERuntimeError => e
27
+ raise Error, e
28
+ else
29
+ query = "select LastBootupTime from Win32_OperatingSystem"
30
+ results = wmi.ExecQuery(query)
31
+ results.each{ |ole|
32
+ time_array = parse_ms_date(ole.LastBootupTime)
33
+ return Time.mktime(*time_array)
34
+ }
35
+ end
36
+ end
37
+
38
+ # Calculates and returns the number of days, hours, minutes and
39
+ # seconds the +host+ has been running as a colon-separated string.
40
+ #
41
+ # The localhost is used if no +host+ is provided.
42
+ #
43
+ # Example:
44
+ #
45
+ # Sys::Uptime.uptime # => "1:9:55:11"
46
+ #
47
+ def self.uptime(host = Socket.gethostname)
48
+ get_dhms(host).join(':')
49
+ end
50
+
51
+ # Calculates and returns the number of days, hours, minutes and
52
+ # seconds the +host+ has been running as a four-element Array.
53
+ # The localhost is used if no +host+ is provided.
54
+ #
55
+ # Example:
56
+ #
57
+ # Sys::Uptime.dhms # => [1, 9, 55, 11]
58
+ #
59
+ def self.dhms(host = Socket.gethostname)
60
+ get_dhms(host)
61
+ end
62
+
63
+ # Returns the total number of days the system has been up on +host+,
64
+ # or the localhost if no host is provided.
65
+ #
66
+ # Example:
67
+ #
68
+ # Sys::Uptime.days # => 1
69
+ #
70
+ def self.days(host = Socket.gethostname)
71
+ hours(host) / 24
72
+ end
73
+
74
+ # Returns the total number of hours the system has been up on +host+,
75
+ # or the localhost if no host is provided.
76
+ #
77
+ # Example:
78
+ #
79
+ # Sys::Uptime.hours # => 33
80
+ #
81
+ def self.hours(host=Socket.gethostname)
82
+ minutes(host) / 60
83
+ end
84
+
85
+ # Returns the total number of minutes the system has been up on +host+,
86
+ # or the localhost if no host is provided.
87
+ #
88
+ # Example:
89
+ #
90
+ # Sys::Uptime.minutes # => 1980
91
+ #
92
+ def self.minutes(host=Socket.gethostname)
93
+ seconds(host) / 60
94
+ end
95
+
96
+ # Returns the total number of seconds the system has been up on +host+,
97
+ # or the localhost if no host is provided.
98
+ #
99
+ # Example:
100
+ #
101
+ # Sys::Uptime.seconds # => 118800
102
+ #
103
+ def self.seconds(host=Socket.gethostname)
104
+ get_seconds(host)
105
+ end
106
+
107
+ private
108
+
109
+ # Converts a string in the format '20040703074625.015625-360' into a
110
+ # Ruby Time object.
111
+ #
112
+ def self.parse_ms_date(str)
113
+ return if str.nil?
114
+ return Time.parse(str.split('.').first)
115
+ end
116
+
117
+ private_class_method :parse_ms_date
118
+
119
+ # Get the actual days, hours, minutes and seconds since boot using WMI.
120
+ #
121
+ def self.get_dhms(host)
122
+ seconds = get_seconds(host)
123
+
124
+ days = (seconds / 86400).to_i
125
+ seconds -= days * 86400
126
+ hours = seconds / 3600
127
+ seconds -= hours * 3600
128
+ minutes = seconds / 60
129
+ seconds -= minutes * 60
130
+
131
+ [days, hours, minutes, seconds]
132
+ end
133
+
134
+ private_class_method :get_dhms
135
+
136
+ # Returns the number of seconds since boot.
137
+ #
138
+ def self.get_seconds(host)
139
+ (Time.now - boot_time).to_i
140
+ end
141
+
142
+ private_class_method :get_seconds
143
+ end
144
+ end
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'sys-uptime'
5
+ spec.version = '0.7.2'
6
+ spec.author = 'Daniel J. Berger'
7
+ spec.license = 'Apache 2.0'
8
+ spec.email = 'djberg96@gmail.com'
9
+ spec.homepage = 'https://github.com/djberg96/sys-uptime'
10
+ spec.summary = 'A Ruby interface for getting system uptime information.'
11
+ spec.test_file = 'test/test_sys_uptime.rb'
12
+ spec.files = Dir["**/*"].reject{ |f| f.include?('git') }
13
+ spec.cert_chain = ['certs/djberg96_pub.pem']
14
+
15
+ spec.extra_rdoc_files = ['CHANGES', 'README', 'MANIFEST']
16
+
17
+ spec.metadata = {
18
+ 'homepage_uri' => 'https://github.com/djberg96/sys-uptime',
19
+ 'bug_tracker_uri' => 'https://github.com/djberg96/sys-uptime/issues',
20
+ 'changelog_uri' => 'https://github.com/djberg96/sys-uptime/blob/ffi/CHANGES',
21
+ 'documentation_uri' => 'https://github.com/djberg96/sys-uptime/wiki',
22
+ 'source_code_uri' => 'https://github.com/djberg96/sys-uptime',
23
+ 'wiki_uri' => 'https://github.com/djberg96/sys-uptime/wiki'
24
+ }
25
+
26
+ spec.description = <<-EOF
27
+ The sys-uptime library is a simple interface for gathering uptime
28
+ information. You can retrieve data in seconds, minutes, days, hours,
29
+ or all of the above.
30
+ EOF
31
+ end
@@ -0,0 +1,107 @@
1
+ #####################################################################
2
+ # test_sys_uptime.rb
3
+ #
4
+ # Test suite for sys-uptime. This should generally be run via the
5
+ # 'rake test' task, since it handles the pre-setup code for you.
6
+ #####################################################################
7
+ require 'sys/uptime'
8
+ require 'test-unit'
9
+ require 'socket'
10
+ include Sys
11
+
12
+ class TC_Sys_Uptime < Test::Unit::TestCase
13
+ test "version is set to expected value" do
14
+ assert_equal('0.7.2', Uptime::VERSION)
15
+ assert_true(Uptime::VERSION.frozen?)
16
+ end
17
+
18
+ test "seconds method basic functionality" do
19
+ assert_respond_to(Uptime, :seconds)
20
+ assert_nothing_raised{ Uptime.seconds }
21
+ end
22
+
23
+ test "seconds method returns a plausible value" do
24
+ assert_kind_of(Integer, Uptime.seconds)
25
+ assert_true(Uptime.seconds > 300)
26
+ end
27
+
28
+ test "minutes method basic functionality" do
29
+ assert_respond_to(Uptime, :minutes)
30
+ assert_nothing_raised{ Uptime.minutes }
31
+ end
32
+
33
+ test "minutes method returns a plausible value" do
34
+ assert_kind_of(Integer, Uptime.minutes)
35
+ assert_true(Uptime.minutes > 5)
36
+ end
37
+
38
+ test "hours method basic functionality" do
39
+ assert_respond_to(Uptime, :hours)
40
+ assert_nothing_raised{ Uptime.hours }
41
+ end
42
+
43
+ test "hours method returns a plausible value" do
44
+ assert_kind_of(Integer, Uptime.hours)
45
+ assert_true(Uptime.hours > 0)
46
+ end
47
+
48
+ test "days method basic functionality" do
49
+ assert_respond_to(Uptime, :days)
50
+ assert_nothing_raised{ Uptime.days }
51
+ end
52
+
53
+ test "days method returns a plausible value" do
54
+ assert_kind_of(Integer, Uptime.days)
55
+ assert_true(Uptime.days >= 0)
56
+ end
57
+
58
+ test "uptime method basic functionality" do
59
+ assert_respond_to(Uptime, :uptime)
60
+ assert_nothing_raised{ Uptime.uptime }
61
+ end
62
+
63
+ test "uptime method returns a non-empty string" do
64
+ assert_kind_of(String, Uptime.uptime)
65
+ assert_false(Uptime.uptime.empty?)
66
+ end
67
+
68
+ test "uptime method does not accept any arguments" do
69
+ omit_if(File::ALT_SEPARATOR)
70
+ assert_raise(ArgumentError){ Uptime.uptime(1) }
71
+ end
72
+
73
+ test "uptime accepts a host name on Windows" do
74
+ omit_unless(File::ALT_SEPARATOR, "MS Windows only")
75
+ assert_nothing_raised{ Uptime.uptime(Socket.gethostname) }
76
+ end
77
+
78
+ test "dhms method basic functionality" do
79
+ assert_respond_to(Uptime, :dhms)
80
+ assert_nothing_raised{ Uptime.dhms }
81
+ assert_kind_of(Array, Uptime.dhms)
82
+ end
83
+
84
+ test "dhms method returns an array of four elements" do
85
+ assert_false(Uptime.dhms.empty?)
86
+ assert_equal(4, Uptime.dhms.length)
87
+ end
88
+
89
+ test "boot_time method basic functionality" do
90
+ assert_respond_to(Uptime, :boot_time)
91
+ assert_nothing_raised{ Uptime.boot_time }
92
+ end
93
+
94
+ test "boot_time method returns a Time object" do
95
+ assert_kind_of(Time, Uptime.boot_time)
96
+ end
97
+
98
+ test "Uptime class cannot be instantiated" do
99
+ assert_kind_of(StandardError, Uptime::Error.new)
100
+ end
101
+
102
+ test "Ensure that ffi functions are private" do
103
+ methods = Uptime.methods(false).map{ |e| e.to_s }
104
+ assert_false(methods.include?('time'))
105
+ assert_false(methods.include?('times'))
106
+ end
107
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sys-uptime
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.2
5
+ platform: universal-mingw32
6
+ authors:
7
+ - Daniel J. Berger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIEcDCCAtigAwIBAgIBATANBgkqhkiG9w0BAQsFADA/MREwDwYDVQQDDAhkamJl
14
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azxxKTsVVpZD8QZxvK91OQ
30
+ h9zr9JBYqCLcCVbr8SkmYCi/laxIZxsNE5YI8cC8vvlLI7AMgSfPSnn/Epq1GjGY
31
+ 6L1iRcEDtanGCIvjqlCXO9+BmsnCfEVehqZkQHeYczA03tpOWb6pon2wzvMKSsKH
32
+ ks0ApVdstSLz1kzzAqem/uHdG9FyXdbTAwH1G4ZPv69sQAFAOCgAqYmdnzedsQtE
33
+ 1LQfaQrx0twO+CZJPcRLEESjq8ScQxWRRkfuh2VeR7cEU7L7KqT10mtUwrvw7APf
34
+ DYoeCY9KyjIBjQXfbj2ke5u1hZj94Fsq9FfbEQg8ygCgwThnmkTrrKEiMSs3alYR
35
+ ORVCZpRuCPpmC8qmqxUnARDArzucjaclkxjLWvCVHeFa9UP7K3Nl9oTjJNv+7/jM
36
+ WZs4eecIcUc4tKdHxcAJ0MO/Dkqq7hGaiHpwKY76wQ1+8xAh
37
+ -----END CERTIFICATE-----
38
+ date: 2018-11-04 00:00:00.000000000 Z
39
+ dependencies: []
40
+ description: |2
41
+ The sys-uptime library is a simple interface for gathering uptime
42
+ information. You can retrieve data in seconds, minutes, days, hours,
43
+ or all of the above.
44
+ email: djberg96@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files:
48
+ - CHANGES
49
+ - README
50
+ - MANIFEST
51
+ files:
52
+ - certs
53
+ - certs/djberg96_pub.pem
54
+ - CHANGES
55
+ - examples
56
+ - examples/uptime_test.rb
57
+ - lib
58
+ - lib/sys
59
+ - lib/sys/unix
60
+ - lib/sys/unix/sys
61
+ - lib/sys/unix/sys/uptime.rb
62
+ - lib/sys/uptime.rb
63
+ - lib/sys/windows
64
+ - lib/sys/windows/sys
65
+ - lib/sys/windows/sys/uptime.rb
66
+ - lib/sys-uptime.rb
67
+ - MANIFEST
68
+ - Rakefile
69
+ - README
70
+ - sys-uptime.gemspec
71
+ - test
72
+ - test/test_sys_uptime.rb
73
+ homepage: https://github.com/djberg96/sys-uptime
74
+ licenses:
75
+ - Apache 2.0
76
+ metadata:
77
+ homepage_uri: https://github.com/djberg96/sys-uptime
78
+ bug_tracker_uri: https://github.com/djberg96/sys-uptime/issues
79
+ changelog_uri: https://github.com/djberg96/sys-uptime/blob/ffi/CHANGES
80
+ documentation_uri: https://github.com/djberg96/sys-uptime/wiki
81
+ source_code_uri: https://github.com/djberg96/sys-uptime
82
+ wiki_uri: https://github.com/djberg96/sys-uptime/wiki
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.7.6
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: A Ruby interface for getting system uptime information.
103
+ test_files:
104
+ - test/test_sys_uptime.rb
@@ -0,0 +1,4 @@
1
+ G;��H����])����?�/�T��B�ڰԿ1�9����vL��$����.�����tB���M���}��mX� �5��Ӹ���h��:��-�~����.1eE'24ߦ�+3��U�s5� ���~O�u#�"pJ[�%� �*�6�TQ�_w���j��'�C���1��Y�L1���H�� s���K���酐j͇r񲪎10��j=Xè���AO��@�}��`t�I�;�?�Zj'�bL�((��B��2YR�
2
+ �[������[ M <�ޞ��s)Ύ0��$~�V��e��%��@
3
+ [gI+�V0�+���9
4
+ i�l�,�XA�qa��.eaf��mHd#��̌�K^�0p�p����/���O���z�k