sys-uptime 0.4.5-i586-linux → 0.5.0-i586-linux
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +10 -0
- data/MANIFEST +13 -17
- data/README +8 -10
- data/doc/uptime.txt +8 -6
- data/lib/sys/uptime.rb +8 -2
- data/test/tc_uptime.rb +9 -24
- metadata +2 -4
- data/ext/uptime.c +0 -231
data/CHANGES
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
== 0.5.0 - 30-Mar-2007
|
2
|
+
* For platforms that use C code, the code now always uses the sysctl()
|
3
|
+
function if supported by your system. This replaces the platform specific
|
4
|
+
checks I was doing for the various BSD flavors.
|
5
|
+
* Fix for OS X - the Uptime.boot_time method now works.
|
6
|
+
* UptimeError is now Uptime::Error.
|
7
|
+
* Improved RDoc in the uptime.c source code.
|
8
|
+
* Added a Rakefile - users should now use the 'test' and 'install' rake tasks.
|
9
|
+
* Updates to the MANIFEST, README and uptime.txt files.
|
10
|
+
|
1
11
|
== 0.4.5 - 19-Nov-2006
|
2
12
|
* Internal layout changes, minor doc updates and gemspec improvements.
|
3
13
|
* No code changes.
|
data/MANIFEST
CHANGED
@@ -1,17 +1,13 @@
|
|
1
|
-
CHANGES
|
2
|
-
MANIFEST
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
doc/uptime.txt
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
lib/sys/linux.rb
|
15
|
-
lib/sys/windows.rb
|
16
|
-
|
17
|
-
test/tc_uptime.rb
|
1
|
+
* CHANGES
|
2
|
+
* MANIFEST
|
3
|
+
* Rakefile
|
4
|
+
* README
|
5
|
+
* install.rb
|
6
|
+
* sys-uptime.gemspec
|
7
|
+
* doc/uptime.txt
|
8
|
+
* examples/test.rb
|
9
|
+
* ext/extconf.rb
|
10
|
+
* ext/uptime.c
|
11
|
+
* lib/sys/linux.rb
|
12
|
+
* lib/sys/windows.rb
|
13
|
+
* test/tc_uptime.rb
|
data/README
CHANGED
@@ -4,17 +4,15 @@
|
|
4
4
|
= Prerequisites
|
5
5
|
Ruby 1.8.0 or later.
|
6
6
|
Ruby 1.8.2 or later is recommended on MS Windows.
|
7
|
+
A C compiler, except for MS Windows.
|
7
8
|
|
8
9
|
= Installation
|
9
|
-
===
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
=== Windows and Linux:
|
16
|
-
ruby test\tc_uptime.rb (optional)
|
17
|
-
ruby install.rb
|
10
|
+
=== Manual
|
11
|
+
rake test (optional)
|
12
|
+
rake install
|
13
|
+
=== Gems
|
14
|
+
ruby sys-uptime.gemspec
|
15
|
+
gem install sys-uptime-X.Y.Z.gem # Where 'X.Y.Z' are version numbers
|
18
16
|
|
19
17
|
== Notes
|
20
|
-
For additional documentation see doc/uptime.txt.
|
18
|
+
For additional documentation see doc/uptime.txt.
|
data/doc/uptime.txt
CHANGED
@@ -43,10 +43,12 @@ Uptime.uptime
|
|
43
43
|
seconds the system has been running as a colon-separated string.
|
44
44
|
|
45
45
|
== Exceptions
|
46
|
-
|
47
|
-
Raised if something goes wrong.
|
48
|
-
failure of the times() function.
|
49
|
-
|
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.
|
50
52
|
|
51
53
|
== Notes
|
52
54
|
On MS Windows each of the class methods optionally takes a host name as
|
@@ -73,7 +75,7 @@ UptimeError
|
|
73
75
|
Ruby's
|
74
76
|
|
75
77
|
== Copyright
|
76
|
-
Copyright 2002-
|
78
|
+
Copyright 2002-2007, Daniel J. Berger
|
77
79
|
|
78
80
|
All Rights Reserved. This module is free software. It may be used,
|
79
81
|
redistributed and/or modified under the same terms as Ruby itself.
|
@@ -90,7 +92,7 @@ UptimeError
|
|
90
92
|
|
91
93
|
== Author
|
92
94
|
Daniel J. Berger
|
93
|
-
djberg96 at gmail dot com
|
95
|
+
djberg96 at nospam at gmail dot com
|
94
96
|
imperator on IRC (Freenode)
|
95
97
|
|
96
98
|
== See Also
|
data/lib/sys/uptime.rb
CHANGED
@@ -1,14 +1,20 @@
|
|
1
1
|
# On Linux, parse /proc/uptime
|
2
2
|
module Sys
|
3
3
|
class Uptime
|
4
|
-
|
4
|
+
class Error < StandardError; end
|
5
|
+
|
6
|
+
VERSION = '0.5.0'
|
5
7
|
|
6
8
|
@@file = '/proc/uptime'
|
7
9
|
|
8
10
|
# Returns the uptime in seconds
|
9
11
|
#
|
10
12
|
def self.seconds
|
11
|
-
|
13
|
+
begin
|
14
|
+
IO.readlines(@@file).to_s.split.first.to_i
|
15
|
+
rescue Exception => err
|
16
|
+
raise Error, err
|
17
|
+
end
|
12
18
|
end
|
13
19
|
|
14
20
|
# Returns the uptime in minutes
|
data/test/tc_uptime.rb
CHANGED
@@ -1,35 +1,16 @@
|
|
1
|
-
|
1
|
+
#####################################################################
|
2
2
|
# tc_uptime.rb
|
3
3
|
#
|
4
|
-
# Test suite for sys-uptime.
|
5
|
-
|
6
|
-
|
7
|
-
if base == 'test' || base =~ /sys-uptime.*/
|
8
|
-
require 'fileutils'
|
9
|
-
Dir.chdir('..') if base == 'test'
|
10
|
-
Dir.mkdir('sys') unless File.exists?('sys')
|
11
|
-
|
12
|
-
if RUBY_PLATFORM.match('mswin')
|
13
|
-
FileUtils.cp('lib/sys/windows.rb', 'sys/uptime.rb') rescue nil
|
14
|
-
elsif RUBY_PLATFORM.match('linux')
|
15
|
-
FileUtils.cp('lib/sys/linux.rb', 'sys/uptime.rb') rescue nil
|
16
|
-
else
|
17
|
-
require 'rbconfig'
|
18
|
-
file = 'ext/uptime.' + Config::CONFIG['DLEXT']
|
19
|
-
FileUtils.cp(file, 'sys')
|
20
|
-
end
|
21
|
-
|
22
|
-
$LOAD_PATH.unshift(Dir.pwd)
|
23
|
-
$LOAD_PATH.unshift(Dir.pwd + '/lib')
|
24
|
-
end
|
25
|
-
|
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
|
+
#####################################################################
|
26
7
|
require 'sys/uptime'
|
27
8
|
require 'test/unit'
|
28
9
|
include Sys
|
29
10
|
|
30
11
|
class TC_Uptime < Test::Unit::TestCase
|
31
12
|
def test_version
|
32
|
-
assert_equal('0.
|
13
|
+
assert_equal('0.5.0', Uptime::VERSION)
|
33
14
|
end
|
34
15
|
|
35
16
|
def test_seconds
|
@@ -77,4 +58,8 @@ class TC_Uptime < Test::Unit::TestCase
|
|
77
58
|
assert_nothing_raised{ Uptime.boot_time }
|
78
59
|
assert_kind_of(Time, Uptime.boot_time)
|
79
60
|
end
|
61
|
+
|
62
|
+
def test_uptime_error
|
63
|
+
assert_kind_of(StandardError, Uptime::Error.new)
|
64
|
+
end
|
80
65
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: sys-uptime
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date:
|
6
|
+
version: 0.5.0
|
7
|
+
date: 2007-03-30 00:00:00 -05:00
|
8
8
|
summary: A Ruby interface for getting system uptime information.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -34,7 +34,6 @@ files:
|
|
34
34
|
- CHANGES
|
35
35
|
- README
|
36
36
|
- MANIFEST
|
37
|
-
- ext/uptime.c
|
38
37
|
- lib/sys/uptime.rb
|
39
38
|
test_files:
|
40
39
|
- test/tc_uptime.rb
|
@@ -45,7 +44,6 @@ extra_rdoc_files:
|
|
45
44
|
- README
|
46
45
|
- MANIFEST
|
47
46
|
- doc/uptime.txt
|
48
|
-
- ext/uptime.c
|
49
47
|
executables: []
|
50
48
|
|
51
49
|
extensions: []
|
data/ext/uptime.c
DELETED
@@ -1,231 +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
|
-
|
12
|
-
#if defined (__FreeBSD__) || defined (__NetBSD__)
|
13
|
-
#include <sys/time.h>
|
14
|
-
|
15
|
-
#if defined (__NetBSD__)
|
16
|
-
#include <sys/param.h>
|
17
|
-
#endif
|
18
|
-
|
19
|
-
#if (__FreeBSD >= 5)
|
20
|
-
#include <sys/types.h>
|
21
|
-
#endif
|
22
|
-
|
23
|
-
#include <sys/sysctl.h>
|
24
|
-
|
25
|
-
#else
|
26
|
-
#include <sys/times.h>
|
27
|
-
#include <unistd.h>
|
28
|
-
#include <time.h>
|
29
|
-
|
30
|
-
#ifdef HAVE_UTMPX_H
|
31
|
-
#include <utmpx.h>
|
32
|
-
#endif
|
33
|
-
|
34
|
-
#ifdef HAVE_SYS_LOADAVG_H
|
35
|
-
#include <sys/loadavg.h>
|
36
|
-
#endif
|
37
|
-
|
38
|
-
#ifdef _SC_CLK_TCK
|
39
|
-
#define TICKS sysconf(_SC_CLK_TCK)
|
40
|
-
#else
|
41
|
-
#define TICKS sysconf(CLOCKS_PER_SEC)
|
42
|
-
#endif
|
43
|
-
|
44
|
-
#endif
|
45
|
-
|
46
|
-
#define MAXSTRINGSIZE 32 /* reasonable limit */
|
47
|
-
|
48
|
-
#define SYS_UPTIME_VERSION "0.4.5"
|
49
|
-
|
50
|
-
VALUE cUptimeError;
|
51
|
-
|
52
|
-
unsigned long get_uptime_secs()
|
53
|
-
{
|
54
|
-
#if defined (__FreeBSD__) || defined (__NetBSD__)
|
55
|
-
struct timeval tv;
|
56
|
-
size_t tvlen = sizeof(tv);
|
57
|
-
int mib[2];
|
58
|
-
|
59
|
-
mib[0] = CTL_KERN;
|
60
|
-
mib[1] = KERN_BOOTTIME;
|
61
|
-
if(sysctl(mib, 2, &tv, &tvlen, NULL, 0)){
|
62
|
-
rb_raise(cUptimeError, "sysctl() call failed");
|
63
|
-
}
|
64
|
-
return time(NULL) - tv.tv_sec;
|
65
|
-
#else
|
66
|
-
struct tms tms;
|
67
|
-
unsigned long seconds;
|
68
|
-
seconds = times(&tms) / TICKS;
|
69
|
-
|
70
|
-
if(-1 == seconds)
|
71
|
-
rb_raise(cUptimeError,"times() function failed");
|
72
|
-
|
73
|
-
if(seconds < 0)
|
74
|
-
rb_raise(cUptimeError,"value returned larger than type could handle");
|
75
|
-
|
76
|
-
return seconds;
|
77
|
-
#endif
|
78
|
-
}
|
79
|
-
|
80
|
-
/*
|
81
|
-
* call-seq:
|
82
|
-
* Uptime.seconds
|
83
|
-
*
|
84
|
-
* Returns the total number of seconds the system has been up.
|
85
|
-
*/
|
86
|
-
static VALUE uptime_seconds()
|
87
|
-
{
|
88
|
-
return UINT2NUM(get_uptime_secs());
|
89
|
-
}
|
90
|
-
|
91
|
-
/*
|
92
|
-
* call-seq:
|
93
|
-
* Uptime.minutes
|
94
|
-
*
|
95
|
-
* Returns the total number of minutes the system has been up.
|
96
|
-
*/
|
97
|
-
static VALUE uptime_minutes()
|
98
|
-
{
|
99
|
-
return UINT2NUM(get_uptime_secs() / 60);
|
100
|
-
}
|
101
|
-
|
102
|
-
/*
|
103
|
-
* call-seq:
|
104
|
-
* Uptime.hours
|
105
|
-
*
|
106
|
-
* Returns the total number of hours the system has been up.
|
107
|
-
*/
|
108
|
-
static VALUE uptime_hours()
|
109
|
-
{
|
110
|
-
return UINT2NUM(get_uptime_secs() / 3600);
|
111
|
-
}
|
112
|
-
|
113
|
-
/*
|
114
|
-
* call-seq:
|
115
|
-
* Uptime.days
|
116
|
-
*
|
117
|
-
* Returns the total number of days the system has been up.
|
118
|
-
*/
|
119
|
-
static VALUE uptime_days()
|
120
|
-
{
|
121
|
-
return UINT2NUM(get_uptime_secs() / 86400);
|
122
|
-
}
|
123
|
-
|
124
|
-
/*
|
125
|
-
* call-seq:
|
126
|
-
* Uptime.uptime
|
127
|
-
*
|
128
|
-
* Calculates and returns the number of days, hours, minutes and
|
129
|
-
* seconds the system has been running as a colon-separated string.
|
130
|
-
*/
|
131
|
-
static VALUE uptime_uptime()
|
132
|
-
{
|
133
|
-
char c_string[MAXSTRINGSIZE];
|
134
|
-
long seconds, days, hours, minutes;
|
135
|
-
|
136
|
-
seconds = get_uptime_secs();
|
137
|
-
days = seconds/86400;
|
138
|
-
seconds -= days*86400;
|
139
|
-
hours = seconds/3600;
|
140
|
-
seconds -= hours*3600;
|
141
|
-
minutes = seconds/60;
|
142
|
-
seconds -= minutes*60;
|
143
|
-
|
144
|
-
sprintf(c_string, "%ld:%ld:%ld:%ld", days, hours, minutes, seconds);
|
145
|
-
return rb_str_new2(c_string);
|
146
|
-
}
|
147
|
-
|
148
|
-
/*
|
149
|
-
* call-seq:
|
150
|
-
* Uptime.dhms
|
151
|
-
*
|
152
|
-
* Calculates and returns the number of days, hours, minutes and
|
153
|
-
* seconds the system has been running as a four-element Array.
|
154
|
-
*/
|
155
|
-
static VALUE uptime_dhms()
|
156
|
-
{
|
157
|
-
VALUE a = rb_ary_new2(4);
|
158
|
-
long s, m, h, d;
|
159
|
-
|
160
|
-
s = get_uptime_secs();
|
161
|
-
d = s / (24*60*60);
|
162
|
-
h = (s -= d*24*60*60) / ( 60*60);
|
163
|
-
m = (s -= h* 60*60) / 60;
|
164
|
-
s -= m* 60;
|
165
|
-
|
166
|
-
rb_ary_push(a, INT2FIX(d));
|
167
|
-
rb_ary_push(a, INT2FIX(h));
|
168
|
-
rb_ary_push(a, INT2FIX(m));
|
169
|
-
rb_ary_push(a, INT2FIX(s));
|
170
|
-
return a;
|
171
|
-
}
|
172
|
-
|
173
|
-
/*
|
174
|
-
* call-seq:
|
175
|
-
* Uptime.boot_time
|
176
|
-
*
|
177
|
-
* Returns the boot time as a Time object.
|
178
|
-
*/
|
179
|
-
static VALUE uptime_btime(){
|
180
|
-
#if defined (__FreeBSD__) || defined (__NetBSD__)
|
181
|
-
struct timeval tv;
|
182
|
-
size_t tvlen = sizeof(tv);
|
183
|
-
int mib[2];
|
184
|
-
|
185
|
-
mib[0] = CTL_KERN;
|
186
|
-
mib[1] = KERN_BOOTTIME;
|
187
|
-
if(sysctl(mib, 2, &tv, &tvlen, NULL, 0)){
|
188
|
-
rb_raise(cUptimeError,"sysctl() call failed");
|
189
|
-
}
|
190
|
-
return rb_time_new(tv.tv_sec,tv.tv_usec);
|
191
|
-
#else
|
192
|
-
#ifdef HAVE_UTMPX_H
|
193
|
-
struct utmpx* ent;
|
194
|
-
|
195
|
-
setutxent();
|
196
|
-
|
197
|
-
while( (ent = getutxent()) ){
|
198
|
-
if(ent->ut_type == BOOT_TIME){
|
199
|
-
return rb_time_new(ent->ut_tv.tv_sec,ent->ut_tv.tv_usec);
|
200
|
-
}
|
201
|
-
}
|
202
|
-
|
203
|
-
endutxent();
|
204
|
-
#else
|
205
|
-
rb_raise(cUptimeError,"boot_time() not implemented on this platform");
|
206
|
-
#endif
|
207
|
-
#endif
|
208
|
-
return Qnil;
|
209
|
-
}
|
210
|
-
|
211
|
-
void Init_uptime()
|
212
|
-
{
|
213
|
-
VALUE mSys, cUptime;
|
214
|
-
|
215
|
-
/* Modules and Classes */
|
216
|
-
mSys = rb_define_module("Sys");
|
217
|
-
cUptime = rb_define_class_under(mSys, "Uptime", rb_cObject);
|
218
|
-
cUptimeError = rb_define_class_under(mSys, "UptimeError", rb_eStandardError);
|
219
|
-
|
220
|
-
/* Constants */
|
221
|
-
rb_define_const(cUptime, "VERSION", rb_str_new2(SYS_UPTIME_VERSION));
|
222
|
-
|
223
|
-
/* Class Methods */
|
224
|
-
rb_define_singleton_method(cUptime, "seconds", uptime_seconds, 0);
|
225
|
-
rb_define_singleton_method(cUptime, "minutes", uptime_minutes, 0);
|
226
|
-
rb_define_singleton_method(cUptime, "hours", uptime_hours, 0);
|
227
|
-
rb_define_singleton_method(cUptime, "days", uptime_days, 0);
|
228
|
-
rb_define_singleton_method(cUptime, "uptime", uptime_uptime, 0);
|
229
|
-
rb_define_singleton_method(cUptime, "dhms", uptime_dhms, 0);
|
230
|
-
rb_define_singleton_method(cUptime, "boot_time", uptime_btime, 0);
|
231
|
-
}
|