sys-uptime 0.5.4-x86-mingw32 → 0.6.0-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +145 -144
- data/MANIFEST +13 -12
- data/README +67 -14
- data/Rakefile +44 -87
- data/examples/{sys_uptime_example.rb → uptime_test.rb} +21 -21
- data/lib/unix/sys/uptime.rb +213 -0
- data/lib/windows/sys/uptime.rb +164 -158
- data/sys-uptime.gemspec +23 -21
- data/test/test_sys_uptime.rb +109 -65
- metadata +29 -50
- data/doc/uptime.txt +0 -98
- data/ext/extconf.rb +0 -28
- data/ext/sys/uptime.c +0 -250
- data/lib/linux/sys/uptime.rb +0 -101
@@ -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
|
data/lib/windows/sys/uptime.rb
CHANGED
@@ -1,158 +1,164 @@
|
|
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.
|
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
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
seconds -=
|
129
|
-
|
130
|
-
seconds -=
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
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.6.0'
|
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
|
+
private_class_method :parse_ms_date
|
121
|
+
|
122
|
+
# Get the actual days, hours, minutes and seconds since boot using WMI.
|
123
|
+
#
|
124
|
+
def self.get_dhms(host)
|
125
|
+
seconds = get_seconds(host)
|
126
|
+
|
127
|
+
days = (seconds / 86400).to_i
|
128
|
+
seconds -= days * 86400
|
129
|
+
hours = seconds / 3600
|
130
|
+
seconds -= hours * 3600
|
131
|
+
minutes = seconds / 60
|
132
|
+
seconds -= minutes * 60
|
133
|
+
|
134
|
+
[days, hours, minutes, seconds]
|
135
|
+
end
|
136
|
+
|
137
|
+
private_class_method :get_dhms
|
138
|
+
|
139
|
+
# Returns the number of seconds since boot.
|
140
|
+
#
|
141
|
+
def self.get_seconds(host)
|
142
|
+
cs = "winmgmts://#{host}/root/cimv2"
|
143
|
+
begin
|
144
|
+
wmi = WIN32OLE.connect(cs)
|
145
|
+
rescue WIN32OLERuntimeError => e
|
146
|
+
raise Error, e
|
147
|
+
else
|
148
|
+
query = "select LastBootupTime from Win32_OperatingSystem"
|
149
|
+
results = wmi.ExecQuery(query)
|
150
|
+
now = Time.now
|
151
|
+
|
152
|
+
results.each{ |ole|
|
153
|
+
time_array = parse_ms_date(ole.LastBootupTime)
|
154
|
+
boot_time = Time.mktime(*time_array)
|
155
|
+
break
|
156
|
+
}
|
157
|
+
end
|
158
|
+
|
159
|
+
(now - boot_time).to_i
|
160
|
+
end
|
161
|
+
|
162
|
+
private_class_method :get_seconds
|
163
|
+
end
|
164
|
+
end
|
data/sys-uptime.gemspec
CHANGED
@@ -1,21 +1,23 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
|
3
|
-
Gem::Specification.new do |spec|
|
4
|
-
spec.name
|
5
|
-
spec.version
|
6
|
-
spec.author
|
7
|
-
spec.
|
8
|
-
spec.
|
9
|
-
spec.
|
10
|
-
spec.
|
11
|
-
spec.
|
12
|
-
spec.
|
13
|
-
spec.
|
14
|
-
|
15
|
-
|
16
|
-
spec.
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
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']
|
16
|
+
spec.rubyforge_project = 'sysutils'
|
17
|
+
|
18
|
+
spec.description = <<-EOF
|
19
|
+
The sys-uptime library is a simple interface for gathering uptime
|
20
|
+
information. You can retrieve data in seconds, minutes, days, hours,
|
21
|
+
or all of the above.
|
22
|
+
EOF
|
23
|
+
end
|