sys-uptime 0.4.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.
Files changed (8) hide show
  1. data/CHANGES +105 -0
  2. data/MANIFEST +15 -0
  3. data/README +20 -0
  4. data/doc/uptime.txt +97 -0
  5. data/extconf.rb +36 -0
  6. data/lib/os/unix.c +231 -0
  7. data/test/tc_uptime.rb +82 -0
  8. metadata +53 -0
data/CHANGES ADDED
@@ -0,0 +1,105 @@
1
+ == 0.4.4 - 30-Jun-2006
2
+ * Added inline rdoc documentation to the source files.
3
+ * Added a gemspec.
4
+
5
+ == 0.4.3 - 18-Dec-2005
6
+ * Changed the Linux version to pure Ruby. The current method of determining
7
+ uptime in unix.c does not work in Linux kernel 2.6+. So, from now on it
8
+ reads out of /proc/uptime.
9
+
10
+ == 0.4.2 - 6-May-2005
11
+ * Fixed a potential boot_time bug.
12
+ * Removed the version.h file. It's no longer needed since the Windows
13
+ version is pure Ruby.
14
+ * NetBSD 2.x and FreeBSD 5.x now supported.
15
+ * Removed the INSTALL file. Installation instructions are now in
16
+ the README file.
17
+ * Removed the uptime.rd file. You can generate html documentation by
18
+ running rdoc over the uptime.txt file.
19
+ * Made most documents rdoc friendly.
20
+ * Moved project to RubyForge.
21
+
22
+ == 0.4.1 - 14-Dec-2004
23
+ * Moved freebsd code into unix.c file.
24
+ * Should now work past 249 days (2**31) on systems that have utmpx.h.
25
+ * Fixed a bug with regards to boot_time, where it was possible that it would
26
+ simply be empty.
27
+
28
+ == 0.4.0 - 8-Jul-2004
29
+ * Removed all reference to the CLK_TCK constant, as per documentation from
30
+ Richard Stevens that it is deprecated and that sysconf() should be used
31
+ instead (well, I knew about this, but ignored it until now).
32
+ * Scrapped the C version for Windows in favor of a pure Ruby version that
33
+ uses win32ole + WMI.
34
+ * Added a boot_time method for Unix systems (Windows already had this).
35
+ * Added an UptimeError class on Unix systems (and replaced UptimeException
36
+ on Windows).
37
+ * Modified an internal function to raise an UptimeError if the times()
38
+ function fails. Also, it now returns an unsigned long instead of a plain
39
+ long.
40
+ * Replaced the two different test suites with a single, unified test suite.
41
+ * Cleaned up the extconf.rb script. I now assume that TestUnit is installed.
42
+ * Removed the load_avg method (which was never advertised and which I hope
43
+ you weren't using). You can find a load_avg method in the sys-cpu package.
44
+ * Changed uptime.rd2 to uptime.rd to be consistent with my other packages.
45
+ * Removed the uptime.html file - you may generate that on your own.
46
+ * Added warranty and license info.
47
+
48
+ == 0.3.2 - 30-Dec-2003
49
+ * Cleaned up some warnings that showed up with -Wall on some unix platforms
50
+ (int vs long format, explicit include)
51
+ * Minor test suite and extconf.rb changes
52
+
53
+ == 0.3.1 - 25-Jun-2003
54
+ * Modified test files to handle HP-UX extensions
55
+ * Minor doc updates
56
+ * Added the dhms() method. Actually, this was in the 0.3.0
57
+ release, I just forgot to mention it in this file :)
58
+
59
+ == 0.3.0 - 22-Jun-2003
60
+ * Added OS X support - thanks go to Mike Hall for the patch
61
+ * Fixed incorrect values in FreeBSD (again Mike Hall)
62
+ * Modified tc_unix.rb test suite to handle OS X, along with a bit
63
+ * of minor cleanup
64
+ * Removed VERSION class method. Use the constant instead
65
+ * Separated FreeBSD/OS X source into its own file (freebsd.c).
66
+ The #ifdefs were starting to get too ugly for me
67
+
68
+ == 0.2.1 - 13-May-2003
69
+ * Fixed bug in extconf.rb file, and made some major changes
70
+ * Modified test.rb for those without TestUnit
71
+ * Modified TestUnit tests - some bogus tests were removed
72
+ * Added a README file with some additional info
73
+ * Created a version.h file, so that I can keep the VERSION number
74
+ in one place
75
+ * Docs automatically included in doc directory (i.e. no more interactive
76
+ document creation)
77
+
78
+ == 0.2.0 - 13-Mar-2003
79
+ * Added MS Windows support
80
+ * Added a VERSION constant
81
+ * Added a test suite (for those with TestUnit installed)
82
+ * Internal directory layout change
83
+ * uptime.c is now split into unix.c and windows.c (and linked appropriately)
84
+ * Changelog and Manifest are now CHANGES and MANIFEST, respectively
85
+ * Many changes to extconf.rb
86
+
87
+ == 0.1.3 - 6-Jan-2003
88
+ * Added a VERSION class method
89
+ * Added a copyright notice
90
+ * Fixed up the docs a bit and moved them to the doc directory
91
+ * Changed the tarball name to match the RAA package name
92
+ * Modified test.rb script to make it better
93
+ * Changed install instructions slightly
94
+
95
+ == 0.1.2 - 25-Aug-2002
96
+ * Slight change to preprocessor commands to avoid redefining CLK_TCK on
97
+ those systems that define it in time.h
98
+ * Added an INSTALL file
99
+
100
+ == 0.1.1 - 21-Jun-2002
101
+ * The CLK_TCK constant wasn't necessarily being set correctly, which could
102
+ lead to some odd results. This has been fixed.
103
+
104
+ == 0.1.0 - 17-Jun-2002
105
+ * Initial release
@@ -0,0 +1,15 @@
1
+ CHANGES
2
+ MANIFEST
3
+ README
4
+ extconf.rb
5
+ install.rb
6
+ sys-uptime.gemspec
7
+
8
+ doc/uptime.txt
9
+
10
+ examples/test.rb
11
+
12
+ lib/os/unix.c
13
+ lib/os/windows.rb
14
+
15
+ test/tc_uptime.rb
data/README ADDED
@@ -0,0 +1,20 @@
1
+ == Description
2
+ A Ruby interface for getting system uptime information.
3
+
4
+ == Prerequisites
5
+ Ruby 1.8.0 or later.
6
+ On Win32 systems, Ruby 1.8.2 or later is recommended.
7
+
8
+ == Installation
9
+ === Unix:
10
+ ruby extconf.rb
11
+ make
12
+ ruby test/tc_uptime.rb (optional)
13
+ make site-install
14
+
15
+ === Windows and Linux:
16
+ ruby test\tc_uptime.rb (optional)
17
+ ruby install.rb
18
+
19
+ == Notes
20
+ For additional documentation see doc/uptime.txt.
@@ -0,0 +1,97 @@
1
+ == Synopsis
2
+ require 'sys/uptime'
3
+ include Sys
4
+
5
+ p Uptime.days
6
+ p Uptime.hours
7
+ p Uptime.minutes
8
+ p Uptime.seconds
9
+ p Uptime.dhms.join(', ')
10
+ p Uptime.uptime
11
+ p Uptime.boot_time
12
+
13
+ == Description
14
+ This package provides uptime and boot time information, similar to the
15
+ 'uptime' Unix command.
16
+
17
+ == Constants
18
+ VERSION
19
+ Returns the current version number of this package (as a string).
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
+ UptimeError
47
+ Raised if something goes wrong. On Unix, this would likely mean a
48
+ failure of the times() function. On Windows, it probably means you
49
+ failed to connect to WMI properly.
50
+
51
+ == Notes
52
+ On MS Windows each of the class methods optionally takes a host name as
53
+ a single argument. The default is localhost (or whatever the result of
54
+ Socket.gethostname returns).
55
+
56
+ The current time, users and load average are NOT included in this
57
+ module, even though you may be used to seeing them with the command
58
+ line version of 'uptime'. This is because these things have
59
+ _absolutely nothing_ to do with uptime (at least, not as I
60
+ logically think of uptime).
61
+
62
+ This package was tested successfully on a Solaris system with over 1600
63
+ days of uptime.
64
+
65
+ == Known Bugs
66
+ None that I am aware of. Please log any bugs you find on the project
67
+ website at http://www.rubyforge.org/projects/sysutils.
68
+
69
+ == Questions
70
+ "Doesn't Struct::Tms do this?" - No.
71
+
72
+ == License
73
+ Ruby's
74
+
75
+ == Copyright
76
+ Copyright 2002-2006, Daniel J. Berger
77
+
78
+ All Rights Reserved. This module is free software. It may be used,
79
+ redistributed and/or modified under the same terms as Ruby itself.
80
+
81
+ == Warranty
82
+ This package is provided "as is" and without any express or
83
+ implied warranties, including, without limitation, the implied
84
+ warranties of merchantability and fitness for a particular purpose.
85
+
86
+ == Acknowledgements
87
+ Mike Hall for help with the BSD side of things.
88
+ Ola Eriksson, whose source code I shamelessly plagiarized to get a better
89
+ implementation for systems that have the utmpx.h header file.
90
+
91
+ == Author
92
+ Daniel J. Berger
93
+ djberg96 at gmail dot com
94
+ imperator on IRC (Freenode)
95
+
96
+ == See Also
97
+ uptime
@@ -0,0 +1,36 @@
1
+ ##############################################
2
+ # extconf.rb
3
+ #
4
+ # Configuration & build script for sys-uptime
5
+ ##############################################
6
+ require 'mkmf'
7
+ require 'ftools'
8
+
9
+ #######################################################################
10
+ # In case this is run more than one time, delete the link/copy if it
11
+ # already exists.
12
+ #######################################################################
13
+ uptime_file = "uptime.c"
14
+ File.delete(uptime_file) if File.exists?(uptime_file)
15
+
16
+ case RUBY_PLATFORM
17
+ when /windows|win32|cygwin|mingw|dos|linux/i
18
+ STDERR.puts "Run the 'install.rb' script instead on this platform"
19
+ exit
20
+ else
21
+ have_header("sys/loadavg.h")
22
+ c_file = "lib/os/unix.c"
23
+
24
+ File.symlink(c_file, uptime_file)
25
+
26
+ if File.exists?("lib/os/windows.rb")
27
+ File.move("lib/os/windows.rb", "lib/os/windows.orig") # Don't install
28
+ end
29
+
30
+ if File.exists?("lib/os/linux.rb")
31
+ File.move("lib/os/linux.rb", "lib/os/linux.orig") # Don't install
32
+ end
33
+ end
34
+
35
+ have_header("utmpx.h")
36
+ create_makefile('sys/uptime')
@@ -0,0 +1,231 @@
1
+ /******************************************************************************
2
+ * unix.c (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.4"
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
+ }
@@ -0,0 +1,82 @@
1
+ #################################
2
+ # tc_uptime.rb
3
+ #
4
+ # Test suite for sys-uptime
5
+ #################################
6
+ base = File.basename(Dir.pwd)
7
+ if base == "test" || base =~ /sys-uptime.*/
8
+ require "ftools"
9
+ Dir.chdir("..") if base == "test"
10
+
11
+ extension = ".so"
12
+ extension = ".bundle" if RUBY_PLATFORM =~ /powerpc|darwin/i
13
+ extension = ".sl" if RUBY_PLATFORM =~ /hpux/i
14
+
15
+ Dir.mkdir("sys") unless File.exists?("sys")
16
+
17
+ if PLATFORM.match("mswin")
18
+ File.copy("lib/os/windows.rb", "sys/uptime.rb")
19
+ elsif PLATFORM.match("linux")
20
+ File.copy("lib/os/linux.rb", "sys/uptime.rb")
21
+ else
22
+ file = "uptime" + extension
23
+ File.copy(file,"sys")
24
+ end
25
+ $LOAD_PATH.unshift Dir.pwd
26
+ end
27
+
28
+ require "sys/uptime"
29
+ require "test/unit"
30
+ include Sys
31
+
32
+ class TC_Uptime < Test::Unit::TestCase
33
+ def test_version
34
+ assert_equal('0.4.4', Uptime::VERSION, "Bad VERSION")
35
+ end
36
+
37
+ def test_seconds
38
+ assert_respond_to(Uptime, :seconds)
39
+ assert_nothing_raised{ Uptime.seconds }
40
+ assert_kind_of(Fixnum, Uptime.seconds,"Bad seconds() type")
41
+ assert_equal(true, Uptime.seconds > 0, "Bad seconds() return value")
42
+ end
43
+
44
+ def test_minutes
45
+ assert_respond_to(Uptime, :minutes)
46
+ assert_nothing_raised{ Uptime.minutes }
47
+ assert_kind_of(Fixnum, Uptime.minutes,"Bad minutes() type")
48
+ end
49
+
50
+ def test_hours
51
+ assert_respond_to(Uptime, :hours)
52
+ assert_nothing_raised{ Uptime.hours }
53
+ assert_kind_of(Fixnum, Uptime.hours,"Bad hours() type")
54
+ end
55
+
56
+ def test_days
57
+ assert_respond_to(Uptime,:days)
58
+ assert_nothing_raised{ Uptime.days }
59
+ assert_kind_of(Fixnum, Uptime.days,"Bad days() type")
60
+ end
61
+
62
+ def test_uptime
63
+ assert_respond_to(Uptime,:uptime)
64
+ assert_nothing_raised{ Uptime.uptime }
65
+ assert_kind_of(String, Uptime.uptime,"Bad uptime() type")
66
+ assert_equal(false, Uptime.uptime.empty?,"Empty string uptime()")
67
+ end
68
+
69
+ def test_dhms
70
+ assert_respond_to(Uptime,:dhms)
71
+ assert_nothing_raised{ Uptime.dhms }
72
+ assert_kind_of(Array, Uptime.dhms,"Bad uptime() type")
73
+ assert_equal(false, Uptime.dhms.empty?,"Empty array from dhms()")
74
+ assert_equal(4, Uptime.dhms.length)
75
+ end
76
+
77
+ def test_boot_time
78
+ assert_respond_to(Uptime,:boot_time)
79
+ assert_nothing_raised{ Uptime.boot_time }
80
+ assert_kind_of(Time, Uptime.boot_time)
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: sys-uptime
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.4.4
7
+ date: 2006-06-30 00:00:00 -06:00
8
+ summary: A Ruby interface for getting system uptime information.
9
+ require_paths:
10
+ - lib
11
+ email: djberg96@gmail.com
12
+ homepage: http://www.rubyforge.org/projects/sysutils
13
+ rubyforge_project: sysutils
14
+ description: A Ruby interface for getting system uptime information.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.8.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Daniel J. Berger
31
+ files:
32
+ - doc/uptime.txt
33
+ - test/tc_uptime.rb
34
+ - CHANGES
35
+ - MANIFEST
36
+ - README
37
+ - extconf.rb
38
+ - lib/os/unix.c
39
+ test_files:
40
+ - test/tc_uptime.rb
41
+ rdoc_options: []
42
+
43
+ extra_rdoc_files:
44
+ - CHANGES
45
+ - README
46
+ executables: []
47
+
48
+ extensions:
49
+ - extconf.rb
50
+ requirements: []
51
+
52
+ dependencies: []
53
+