sys-uptime 0.5.3 → 0.5.4
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.
- data/CHANGES +5 -0
- data/MANIFEST +4 -5
- data/Rakefile +87 -0
- data/doc/uptime.txt +54 -55
- data/examples/sys_uptime_example.rb +21 -0
- data/ext/extconf.rb +6 -6
- data/ext/sys/uptime.c +2 -2
- data/lib/linux/sys/uptime.rb +101 -0
- data/lib/windows/sys/uptime.rb +158 -0
- data/sys-uptime.gemspec +21 -0
- data/test/test_sys_uptime.rb +47 -47
- metadata +28 -13
data/CHANGES
CHANGED
@@ -1,3 +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.
|
5
|
+
|
1
6
|
== 0.5.3 - 7-May-2009
|
2
7
|
* Altered the Uptime.seconds implementation on Linux so that it works with
|
3
8
|
both Ruby 1.8.x and 1.9.x. Thanks go to Alexey Chebotar for the spot.
|
data/MANIFEST
CHANGED
@@ -2,12 +2,11 @@
|
|
2
2
|
* MANIFEST
|
3
3
|
* Rakefile
|
4
4
|
* README
|
5
|
-
* install.rb
|
6
5
|
* sys-uptime.gemspec
|
7
6
|
* doc/uptime.txt
|
8
|
-
* examples/
|
7
|
+
* examples/sys_uptime_example.rb
|
9
8
|
* ext/extconf.rb
|
10
9
|
* ext/sys/uptime.c
|
11
|
-
* lib/sys/
|
12
|
-
* lib/sys/
|
13
|
-
* test/test_sys_uptime.rb
|
10
|
+
* lib/linux/sys/uptime.rb
|
11
|
+
* lib/windows/sys/uptime.rb
|
12
|
+
* test/test_sys_uptime.rb
|
data/Rakefile
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/clean'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rbconfig'
|
5
|
+
include Config
|
6
|
+
|
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
|
+
)
|
17
|
+
|
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"
|
31
|
+
task :create => [:clean] do
|
32
|
+
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'
|
46
|
+
end
|
47
|
+
|
48
|
+
Gem::Builder.new(spec).build
|
49
|
+
end
|
50
|
+
|
51
|
+
desc "Install the sys-uptime library"
|
52
|
+
task :install => [:create] do
|
53
|
+
gem_name = Dir['*.gem'].first
|
54
|
+
sh "gem install #{gem_name}"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
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'
|
66
|
+
end
|
67
|
+
sh "ruby -I#{path} examples/sys_uptime_example.rb"
|
68
|
+
end
|
69
|
+
|
70
|
+
desc "Run the test suite"
|
71
|
+
Rake::TestTask.new("test") do |t|
|
72
|
+
task :test => :build
|
73
|
+
t.libs << 'test' << '.'
|
74
|
+
t.warning = true
|
75
|
+
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
|
+
end
|
86
|
+
|
87
|
+
task :default => :test
|
data/doc/uptime.txt
CHANGED
@@ -1,99 +1,98 @@
|
|
1
|
-
==
|
2
|
-
|
3
|
-
|
1
|
+
== Description
|
2
|
+
The sys-uptime library provides uptime and boot time information, similar
|
3
|
+
to the 'uptime' Unix command.
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
p Uptime.seconds
|
9
|
-
p Uptime.dhms.join(', ')
|
10
|
-
p Uptime.uptime
|
11
|
-
p Uptime.boot_time
|
5
|
+
== Synopsis
|
6
|
+
require 'sys/uptime'
|
7
|
+
include Sys
|
12
8
|
|
13
|
-
|
14
|
-
|
15
|
-
|
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
16
|
|
17
17
|
== Constants
|
18
18
|
VERSION
|
19
|
-
|
19
|
+
Returns the current version number of this library.
|
20
20
|
|
21
21
|
== Class Methods
|
22
22
|
Uptime.boot_time
|
23
|
-
|
23
|
+
Returns the boot time as a Time object.
|
24
24
|
|
25
25
|
Uptime.days
|
26
|
-
|
26
|
+
Returns the total number of days the system has been up.
|
27
27
|
|
28
28
|
Uptime.hours
|
29
|
-
|
29
|
+
Returns the total number of hours the system has been up.
|
30
30
|
|
31
31
|
Uptime.minutes
|
32
|
-
|
32
|
+
Returns the total number of minutes the system has been up.
|
33
33
|
|
34
34
|
Uptime.seconds
|
35
|
-
|
35
|
+
Returns the total number of seconds the system has been up.
|
36
36
|
|
37
37
|
Uptime.dhms
|
38
|
-
|
39
|
-
|
38
|
+
Calculates and returns the number of days, hours, minutes and
|
39
|
+
seconds the system has been running as a four-element Array.
|
40
40
|
|
41
41
|
Uptime.uptime
|
42
|
-
|
43
|
-
|
42
|
+
Calculates and returns the number of days, hours, minutes and
|
43
|
+
seconds the system has been running as a colon-separated string.
|
44
44
|
|
45
45
|
== Exceptions
|
46
46
|
Uptime::Error
|
47
|
-
|
48
|
-
|
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
49
|
|
50
|
-
|
51
|
-
|
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
52
|
|
53
53
|
== Notes
|
54
|
-
|
55
|
-
|
56
|
-
|
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
57
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
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
63
|
|
64
|
-
|
65
|
-
|
64
|
+
This library was tested successfully on a Solaris system with over 1600
|
65
|
+
days of uptime.
|
66
66
|
|
67
67
|
== Known Bugs
|
68
|
-
|
69
|
-
|
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
70
|
|
71
71
|
== Questions
|
72
|
-
|
72
|
+
"Doesn't Struct::Tms do this?" - No.
|
73
73
|
|
74
74
|
== License
|
75
|
-
|
75
|
+
Artistic 2.0
|
76
76
|
|
77
77
|
== Copyright
|
78
|
-
|
78
|
+
Copyright 2002-2011, Daniel J. Berger
|
79
79
|
|
80
|
-
|
81
|
-
|
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
82
|
|
83
83
|
== Warranty
|
84
|
-
|
85
|
-
|
86
|
-
|
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
87
|
|
88
88
|
== Acknowledgements
|
89
|
-
|
90
|
-
|
91
|
-
|
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
92
|
|
93
|
-
==
|
94
|
-
|
95
|
-
|
96
|
-
imperator on IRC (Freenode)
|
93
|
+
== Authors
|
94
|
+
Daniel J. Berger
|
95
|
+
Mike Hall
|
97
96
|
|
98
97
|
== See Also
|
99
|
-
|
98
|
+
uptime(1)
|
@@ -0,0 +1,21 @@
|
|
1
|
+
###########################################################
|
2
|
+
# sys_uptime_example.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"
|
data/ext/extconf.rb
CHANGED
@@ -9,8 +9,8 @@ require 'mkmf'
|
|
9
9
|
require 'fileutils'
|
10
10
|
|
11
11
|
if RUBY_PLATFORM =~ /windows|win32|cygwin|mingw|dos|linux/i
|
12
|
-
|
13
|
-
|
12
|
+
STDERR.puts "Do not compile on this platform. Run 'rake gem:install' instead."
|
13
|
+
exit
|
14
14
|
end
|
15
15
|
|
16
16
|
dir_config('uptime')
|
@@ -18,11 +18,11 @@ dir_config('uptime')
|
|
18
18
|
have_header('sys/loadavg.h')
|
19
19
|
|
20
20
|
if have_func('sysctl')
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
have_header('sys/param.h')
|
22
|
+
have_header('sys/time.h')
|
23
|
+
have_header('sys/types.h')
|
24
24
|
else
|
25
|
-
|
25
|
+
have_header('utmpx.h')
|
26
26
|
end
|
27
27
|
|
28
28
|
create_makefile('sys/uptime', 'sys')
|
data/ext/sys/uptime.c
CHANGED
@@ -51,7 +51,7 @@
|
|
51
51
|
|
52
52
|
#define MAXSTRINGSIZE 32 /* reasonable limit */
|
53
53
|
|
54
|
-
#define SYS_UPTIME_VERSION "0.5.
|
54
|
+
#define SYS_UPTIME_VERSION "0.5.4"
|
55
55
|
|
56
56
|
VALUE cUptimeError;
|
57
57
|
|
@@ -235,7 +235,7 @@ void Init_uptime()
|
|
235
235
|
/* The Uptime::Error class is raised if any of the Uptime methods fail */
|
236
236
|
cUptimeError = rb_define_class_under(cUptime, "Error", rb_eStandardError);
|
237
237
|
|
238
|
-
/* 0.5.
|
238
|
+
/* 0.5.4: The version of this library */
|
239
239
|
rb_define_const(cUptime, "VERSION", rb_str_new2(SYS_UPTIME_VERSION));
|
240
240
|
|
241
241
|
/* Singleton Methods */
|
@@ -0,0 +1,101 @@
|
|
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
|
@@ -0,0 +1,158 @@
|
|
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
|
+
# The version of the sys-uptime library.
|
17
|
+
VERSION = '0.5.4'
|
18
|
+
|
19
|
+
# Returns the boot time as a Time object.
|
20
|
+
#
|
21
|
+
# Example:
|
22
|
+
#
|
23
|
+
# Sys::Uptime.boot_time # => Fri Dec 12 20:18:58 -0700 2008
|
24
|
+
#
|
25
|
+
def self.boot_time(host=Socket.gethostname)
|
26
|
+
cs = "winmgmts://#{host}/root/cimv2"
|
27
|
+
begin
|
28
|
+
wmi = WIN32OLE.connect(cs)
|
29
|
+
rescue WIN32OLERuntimeError => e
|
30
|
+
raise Error, e
|
31
|
+
else
|
32
|
+
query = "select LastBootupTime from Win32_OperatingSystem"
|
33
|
+
results = wmi.ExecQuery(query)
|
34
|
+
results.each{ |ole|
|
35
|
+
time_array = parse_ms_date(ole.LastBootupTime)
|
36
|
+
return Time.mktime(*time_array)
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Calculates and returns the number of days, hours, minutes and
|
42
|
+
# seconds the +host+ has been running as a colon-separated string.
|
43
|
+
#
|
44
|
+
# The localhost is used if no +host+ is provided.
|
45
|
+
#
|
46
|
+
# Example:
|
47
|
+
#
|
48
|
+
# Sys::Uptime.uptime # => "1:9:55:11"
|
49
|
+
#
|
50
|
+
def self.uptime(host=Socket.gethostname)
|
51
|
+
get_dhms(host).join(':')
|
52
|
+
end
|
53
|
+
|
54
|
+
# Calculates and returns the number of days, hours, minutes and
|
55
|
+
# seconds the +host+ has been running as a four-element Array.
|
56
|
+
# The localhost is used if no +host+ is provided.
|
57
|
+
#
|
58
|
+
# Example:
|
59
|
+
#
|
60
|
+
# Sys::Uptime.dhms # => [1, 9, 55, 11]
|
61
|
+
#
|
62
|
+
def self.dhms(host=Socket.gethostname)
|
63
|
+
get_dhms(host)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Returns the total number of days the system has been up on +host+,
|
67
|
+
# or the localhost if no host is provided.
|
68
|
+
#
|
69
|
+
# Example:
|
70
|
+
#
|
71
|
+
# Sys::Uptime.days # => 1
|
72
|
+
#
|
73
|
+
def self.days(host=Socket.gethostname)
|
74
|
+
hours(host) / 24
|
75
|
+
end
|
76
|
+
|
77
|
+
# Returns the total number of hours the system has been up on +host+,
|
78
|
+
# or the localhost if no host is provided.
|
79
|
+
#
|
80
|
+
# Example:
|
81
|
+
#
|
82
|
+
# Sys::Uptime.hours # => 33
|
83
|
+
#
|
84
|
+
def self.hours(host=Socket.gethostname)
|
85
|
+
minutes(host) / 60
|
86
|
+
end
|
87
|
+
|
88
|
+
# Returns the total number of minutes the system has been up on +host+,
|
89
|
+
# or the localhost if no host is provided.
|
90
|
+
#
|
91
|
+
# Example:
|
92
|
+
#
|
93
|
+
# Sys::Uptime.minutes # => 1980
|
94
|
+
#
|
95
|
+
def self.minutes(host=Socket.gethostname)
|
96
|
+
seconds(host) / 60
|
97
|
+
end
|
98
|
+
|
99
|
+
# Returns the total number of seconds the system has been up on +host+,
|
100
|
+
# or the localhost if no host is provided.
|
101
|
+
#
|
102
|
+
# Example:
|
103
|
+
#
|
104
|
+
# Sys::Uptime.seconds # => 118800
|
105
|
+
#
|
106
|
+
def self.seconds(host=Socket.gethostname)
|
107
|
+
get_seconds(host)
|
108
|
+
end
|
109
|
+
|
110
|
+
private
|
111
|
+
|
112
|
+
# Converts a string in the format '20040703074625.015625-360' into a
|
113
|
+
# Ruby Time object.
|
114
|
+
#
|
115
|
+
def self.parse_ms_date(str)
|
116
|
+
return if str.nil?
|
117
|
+
return Time.parse(str.split('.').first)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Get the actual days, hours, minutes and seconds since boot using WMI.
|
121
|
+
#
|
122
|
+
def self.get_dhms(host)
|
123
|
+
seconds = get_seconds(host)
|
124
|
+
|
125
|
+
days = (seconds / 86400).to_i
|
126
|
+
seconds -= days * 86400
|
127
|
+
hours = seconds / 3600
|
128
|
+
seconds -= hours * 3600
|
129
|
+
minutes = seconds / 60
|
130
|
+
seconds -= minutes * 60
|
131
|
+
|
132
|
+
[days, hours, minutes, seconds]
|
133
|
+
end
|
134
|
+
|
135
|
+
# Returns the number of seconds since boot.
|
136
|
+
#
|
137
|
+
def self.get_seconds(host)
|
138
|
+
cs = "winmgmts://#{host}/root/cimv2"
|
139
|
+
begin
|
140
|
+
wmi = WIN32OLE.connect(cs)
|
141
|
+
rescue WIN32OLERuntimeError => e
|
142
|
+
raise Error, e
|
143
|
+
else
|
144
|
+
query = "select LastBootupTime from Win32_OperatingSystem"
|
145
|
+
results = wmi.ExecQuery(query)
|
146
|
+
now = Time.now
|
147
|
+
|
148
|
+
results.each{ |ole|
|
149
|
+
time_array = parse_ms_date(ole.LastBootupTime)
|
150
|
+
boot_time = Time.mktime(*time_array)
|
151
|
+
break
|
152
|
+
}
|
153
|
+
end
|
154
|
+
|
155
|
+
(now - boot_time).to_i
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
data/sys-uptime.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
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']
|
13
|
+
spec.rubyforge_project = 'sysutils'
|
14
|
+
spec.files = Dir['**/*'].delete_if{ |item| item.include?('git') }
|
15
|
+
|
16
|
+
spec.description = <<-EOF
|
17
|
+
The sys-uptime library is a simple interface for gathering uptime
|
18
|
+
information. You can retrieve data in seconds, minutes, days, hours,
|
19
|
+
or all of the above.
|
20
|
+
EOF
|
21
|
+
end
|
data/test/test_sys_uptime.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#####################################################################
|
2
|
-
#
|
2
|
+
# test_sys_uptime.rb
|
3
3
|
#
|
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.
|
@@ -8,58 +8,58 @@ require 'sys/uptime'
|
|
8
8
|
require 'test/unit'
|
9
9
|
include Sys
|
10
10
|
|
11
|
-
class
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
class TC_Sys_Uptime < Test::Unit::TestCase
|
12
|
+
def test_version
|
13
|
+
assert_equal('0.5.4', Uptime::VERSION)
|
14
|
+
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
16
|
+
def test_seconds
|
17
|
+
assert_respond_to(Uptime, :seconds)
|
18
|
+
assert_nothing_raised{ Uptime.seconds }
|
19
|
+
assert_kind_of(Fixnum, Uptime.seconds)
|
20
|
+
assert_equal(true, Uptime.seconds > 0)
|
21
|
+
end
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
23
|
+
def test_minutes
|
24
|
+
assert_respond_to(Uptime, :minutes)
|
25
|
+
assert_nothing_raised{ Uptime.minutes }
|
26
|
+
assert_kind_of(Fixnum, Uptime.minutes)
|
27
|
+
end
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
29
|
+
def test_hours
|
30
|
+
assert_respond_to(Uptime, :hours)
|
31
|
+
assert_nothing_raised{ Uptime.hours }
|
32
|
+
assert_kind_of(Fixnum, Uptime.hours)
|
33
|
+
end
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
35
|
+
def test_days
|
36
|
+
assert_respond_to(Uptime,:days)
|
37
|
+
assert_nothing_raised{ Uptime.days }
|
38
|
+
assert_kind_of(Fixnum, Uptime.days)
|
39
|
+
end
|
40
40
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
41
|
+
def test_uptime
|
42
|
+
assert_respond_to(Uptime,:uptime)
|
43
|
+
assert_nothing_raised{ Uptime.uptime }
|
44
|
+
assert_kind_of(String, Uptime.uptime)
|
45
|
+
assert_equal(false, Uptime.uptime.empty?)
|
46
|
+
end
|
47
47
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
48
|
+
def test_dhms
|
49
|
+
assert_respond_to(Uptime,:dhms)
|
50
|
+
assert_nothing_raised{ Uptime.dhms }
|
51
|
+
assert_kind_of(Array, Uptime.dhms)
|
52
|
+
assert_equal(false, Uptime.dhms.empty?)
|
53
|
+
assert_equal(4, Uptime.dhms.length)
|
54
|
+
end
|
55
55
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
56
|
+
def test_boot_time
|
57
|
+
assert_respond_to(Uptime,:boot_time)
|
58
|
+
assert_nothing_raised{ Uptime.boot_time }
|
59
|
+
assert_kind_of(Time, Uptime.boot_time)
|
60
|
+
end
|
61
61
|
|
62
|
-
|
63
|
-
|
64
|
-
|
62
|
+
def test_uptime_error
|
63
|
+
assert_kind_of(StandardError, Uptime::Error.new)
|
64
|
+
end
|
65
65
|
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sys-uptime
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 3
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 5
|
9
|
+
- 4
|
10
|
+
version: 0.5.4
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Daniel J. Berger
|
@@ -9,11 +15,10 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
13
|
-
default_executable:
|
18
|
+
date: 2011-07-27 00:00:00 Z
|
14
19
|
dependencies: []
|
15
20
|
|
16
|
-
description: "
|
21
|
+
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"
|
17
22
|
email: djberg96@gmail.com
|
18
23
|
executables: []
|
19
24
|
|
@@ -26,14 +31,18 @@ extra_rdoc_files:
|
|
26
31
|
- doc/uptime.txt
|
27
32
|
- ext/sys/uptime.c
|
28
33
|
files:
|
29
|
-
- doc/uptime.txt
|
30
|
-
- test/test_sys_uptime.rb
|
31
34
|
- CHANGES
|
32
|
-
-
|
33
|
-
-
|
35
|
+
- doc/uptime.txt
|
36
|
+
- examples/sys_uptime_example.rb
|
34
37
|
- ext/extconf.rb
|
35
38
|
- ext/sys/uptime.c
|
36
|
-
|
39
|
+
- lib/linux/sys/uptime.rb
|
40
|
+
- lib/windows/sys/uptime.rb
|
41
|
+
- MANIFEST
|
42
|
+
- Rakefile
|
43
|
+
- README
|
44
|
+
- sys-uptime.gemspec
|
45
|
+
- test/test_sys_uptime.rb
|
37
46
|
homepage: http://www.rubyforge.org/projects/sysutils
|
38
47
|
licenses: []
|
39
48
|
|
@@ -43,21 +52,27 @@ rdoc_options: []
|
|
43
52
|
require_paths:
|
44
53
|
- lib
|
45
54
|
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
46
56
|
requirements:
|
47
57
|
- - ">="
|
48
58
|
- !ruby/object:Gem::Version
|
49
|
-
|
50
|
-
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
51
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
52
65
|
requirements:
|
53
66
|
- - ">="
|
54
67
|
- !ruby/object:Gem::Version
|
68
|
+
hash: 3
|
69
|
+
segments:
|
70
|
+
- 0
|
55
71
|
version: "0"
|
56
|
-
version:
|
57
72
|
requirements: []
|
58
73
|
|
59
74
|
rubyforge_project: sysutils
|
60
|
-
rubygems_version: 1.3
|
75
|
+
rubygems_version: 1.8.3
|
61
76
|
signing_key:
|
62
77
|
specification_version: 3
|
63
78
|
summary: A Ruby interface for getting system uptime information.
|