sys-uptime 0.5.4-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,144 @@
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
+
6
+ == 0.5.3 - 7-May-2009
7
+ * Altered the Uptime.seconds implementation on Linux so that it works with
8
+ both Ruby 1.8.x and 1.9.x. Thanks go to Alexey Chebotar for the spot.
9
+
10
+ == 0.5.2 - 13-Dec-2008
11
+ * Fixed a date/time issue in the Windows version caused by Ruby itself.
12
+ * Fixed the Uptime.seconds, Uptime.minutes and Uptime.hours methods on MS
13
+ Windows.
14
+ * Renamed the test file to 'test_sys_uptime.rb'.
15
+ * Some minor updates to the Rakefile.
16
+
17
+ == 0.5.1 - 26-Jul-2007
18
+ * Fixed bug in the MS Windows version caused by incorrect parsing of an
19
+ MS specific date format (caused by a bug in Ruby 1.8.6). Thanks go to
20
+ Robert H. for the spot.
21
+ * Inlined the Rake installation tasks, so the install.rb file has been
22
+ removed.
23
+ * Added an 'install_gem' Rake task, and updated the README installation
24
+ instructions.
25
+
26
+ == 0.5.0 - 30-Mar-2007
27
+ * For platforms that use C code, the code now always uses the sysctl()
28
+ function if supported by your system. This replaces the platform specific
29
+ checks I was doing for the various BSD flavors.
30
+ * Fix for OS X - the Uptime.boot_time method now works.
31
+ * UptimeError is now Uptime::Error.
32
+ * Improved RDoc in the uptime.c source code.
33
+ * Added a Rakefile - users should now use the 'test' and 'install' rake tasks.
34
+ * Updates to the MANIFEST, README and uptime.txt files.
35
+
36
+ == 0.4.5 - 19-Nov-2006
37
+ * Internal layout changes, minor doc updates and gemspec improvements.
38
+ * No code changes.
39
+
40
+ == 0.4.4 - 30-Jun-2006
41
+ * Added inline rdoc documentation to the source files.
42
+ * Added a gemspec.
43
+
44
+ == 0.4.3 - 18-Dec-2005
45
+ * Changed the Linux version to pure Ruby. The current method of determining
46
+ uptime in unix.c does not work in Linux kernel 2.6+. So, from now on it
47
+ reads out of /proc/uptime.
48
+
49
+ == 0.4.2 - 6-May-2005
50
+ * Fixed a potential boot_time bug.
51
+ * Removed the version.h file. It's no longer needed since the Windows
52
+ version is pure Ruby.
53
+ * NetBSD 2.x and FreeBSD 5.x now supported.
54
+ * Removed the INSTALL file. Installation instructions are now in
55
+ the README file.
56
+ * Removed the uptime.rd file. You can generate html documentation by
57
+ running rdoc over the uptime.txt file.
58
+ * Made most documents rdoc friendly.
59
+ * Moved project to RubyForge.
60
+
61
+ == 0.4.1 - 14-Dec-2004
62
+ * Moved freebsd code into unix.c file.
63
+ * Should now work past 249 days (2**31) on systems that have utmpx.h.
64
+ * Fixed a bug with regards to boot_time, where it was possible that it would
65
+ simply be empty.
66
+
67
+ == 0.4.0 - 8-Jul-2004
68
+ * Removed all reference to the CLK_TCK constant, as per documentation from
69
+ Richard Stevens that it is deprecated and that sysconf() should be used
70
+ instead (well, I knew about this, but ignored it until now).
71
+ * Scrapped the C version for Windows in favor of a pure Ruby version that
72
+ uses win32ole + WMI.
73
+ * Added a boot_time method for Unix systems (Windows already had this).
74
+ * Added an UptimeError class on Unix systems (and replaced UptimeException
75
+ on Windows).
76
+ * Modified an internal function to raise an UptimeError if the times()
77
+ function fails. Also, it now returns an unsigned long instead of a plain
78
+ long.
79
+ * Replaced the two different test suites with a single, unified test suite.
80
+ * Cleaned up the extconf.rb script. I now assume that TestUnit is installed.
81
+ * Removed the load_avg method (which was never advertised and which I hope
82
+ you weren't using). You can find a load_avg method in the sys-cpu package.
83
+ * Changed uptime.rd2 to uptime.rd to be consistent with my other packages.
84
+ * Removed the uptime.html file - you may generate that on your own.
85
+ * Added warranty and license info.
86
+
87
+ == 0.3.2 - 30-Dec-2003
88
+ * Cleaned up some warnings that showed up with -Wall on some unix platforms
89
+ (int vs long format, explicit include)
90
+ * Minor test suite and extconf.rb changes
91
+
92
+ == 0.3.1 - 25-Jun-2003
93
+ * Modified test files to handle HP-UX extensions
94
+ * Minor doc updates
95
+ * Added the dhms() method. Actually, this was in the 0.3.0
96
+ release, I just forgot to mention it in this file :)
97
+
98
+ == 0.3.0 - 22-Jun-2003
99
+ * Added OS X support - thanks go to Mike Hall for the patch
100
+ * Fixed incorrect values in FreeBSD (again Mike Hall)
101
+ * Modified tc_unix.rb test suite to handle OS X, along with a bit
102
+ * of minor cleanup
103
+ * Removed VERSION class method. Use the constant instead
104
+ * Separated FreeBSD/OS X source into its own file (freebsd.c).
105
+ The #ifdefs were starting to get too ugly for me
106
+
107
+ == 0.2.1 - 13-May-2003
108
+ * Fixed bug in extconf.rb file, and made some major changes
109
+ * Modified test.rb for those without TestUnit
110
+ * Modified TestUnit tests - some bogus tests were removed
111
+ * Added a README file with some additional info
112
+ * Created a version.h file, so that I can keep the VERSION number
113
+ in one place
114
+ * Docs automatically included in doc directory (i.e. no more interactive
115
+ document creation)
116
+
117
+ == 0.2.0 - 13-Mar-2003
118
+ * Added MS Windows support
119
+ * Added a VERSION constant
120
+ * Added a test suite (for those with TestUnit installed)
121
+ * Internal directory layout change
122
+ * uptime.c is now split into unix.c and windows.c (and linked appropriately)
123
+ * Changelog and Manifest are now CHANGES and MANIFEST, respectively
124
+ * Many changes to extconf.rb
125
+
126
+ == 0.1.3 - 6-Jan-2003
127
+ * Added a VERSION class method
128
+ * Added a copyright notice
129
+ * Fixed up the docs a bit and moved them to the doc directory
130
+ * Changed the tarball name to match the RAA package name
131
+ * Modified test.rb script to make it better
132
+ * Changed install instructions slightly
133
+
134
+ == 0.1.2 - 25-Aug-2002
135
+ * Slight change to preprocessor commands to avoid redefining CLK_TCK on
136
+ those systems that define it in time.h
137
+ * Added an INSTALL file
138
+
139
+ == 0.1.1 - 21-Jun-2002
140
+ * The CLK_TCK constant wasn't necessarily being set correctly, which could
141
+ lead to some odd results. This has been fixed.
142
+
143
+ == 0.1.0 - 17-Jun-2002
144
+ * Initial release
data/MANIFEST ADDED
@@ -0,0 +1,12 @@
1
+ * CHANGES
2
+ * MANIFEST
3
+ * Rakefile
4
+ * README
5
+ * sys-uptime.gemspec
6
+ * doc/uptime.txt
7
+ * examples/sys_uptime_example.rb
8
+ * ext/extconf.rb
9
+ * ext/sys/uptime.c
10
+ * lib/linux/sys/uptime.rb
11
+ * lib/windows/sys/uptime.rb
12
+ * test/test_sys_uptime.rb
data/README ADDED
@@ -0,0 +1,14 @@
1
+ = Description
2
+ A Ruby interface for getting system uptime information.
3
+
4
+ = Prerequisites
5
+ Ruby 1.8.0 or later.
6
+ Ruby 1.8.2 or later is recommended on MS Windows.
7
+ A C compiler, except for MS Windows.
8
+
9
+ = Installation
10
+ rake test (optional)
11
+ rake install (non-gem) or rake install_gem (gem).
12
+
13
+ == Notes
14
+ For additional documentation see doc/uptime.txt.
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 ADDED
@@ -0,0 +1,98 @@
1
+ == Description
2
+ The sys-uptime library provides uptime and boot time information, similar
3
+ to the 'uptime' Unix command.
4
+
5
+ == Synopsis
6
+ require 'sys/uptime'
7
+ include Sys
8
+
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
+
17
+ == Constants
18
+ VERSION
19
+ Returns the current version number of this library.
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
+ 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.
52
+
53
+ == Notes
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
+
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
+
64
+ This library was tested successfully on a Solaris system with over 1600
65
+ days of uptime.
66
+
67
+ == Known Bugs
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
+
71
+ == Questions
72
+ "Doesn't Struct::Tms do this?" - No.
73
+
74
+ == License
75
+ Artistic 2.0
76
+
77
+ == Copyright
78
+ Copyright 2002-2011, Daniel J. Berger
79
+
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
+
83
+ == Warranty
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
+
88
+ == Acknowledgements
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
+
93
+ == Authors
94
+ Daniel J. Berger
95
+ Mike Hall
96
+
97
+ == See Also
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 ADDED
@@ -0,0 +1,28 @@
1
+ ##########################################################################
2
+ # extconf.rb
3
+ #
4
+ # Configuration & build script for sys-uptime. Generally speaking you
5
+ # should build sys-uptime with the 'rake build' task instead of running
6
+ # this script manually.
7
+ ##########################################################################
8
+ require 'mkmf'
9
+ require 'fileutils'
10
+
11
+ if RUBY_PLATFORM =~ /windows|win32|cygwin|mingw|dos|linux/i
12
+ STDERR.puts "Do not compile on this platform. Run 'rake gem:install' instead."
13
+ exit
14
+ end
15
+
16
+ dir_config('uptime')
17
+
18
+ have_header('sys/loadavg.h')
19
+
20
+ if have_func('sysctl')
21
+ have_header('sys/param.h')
22
+ have_header('sys/time.h')
23
+ have_header('sys/types.h')
24
+ else
25
+ have_header('utmpx.h')
26
+ end
27
+
28
+ create_makefile('sys/uptime', 'sys')
data/ext/sys/uptime.c ADDED
@@ -0,0 +1,250 @@
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
+ #include <string.h>
12
+ #include <errno.h>
13
+
14
+ #ifdef HAVE_SYSCTL
15
+
16
+ #include <sys/sysctl.h>
17
+
18
+ #ifdef HAVE_SYS_PARAM_H
19
+ #include <sys/param.h>
20
+ #endif
21
+
22
+ #ifdef HAVE_SYS_TIME_H
23
+ #include <sys/time.h>
24
+ #endif
25
+
26
+ #ifdef HAVE_SYS_TYPES_H
27
+ #include <sys/types.h>
28
+ #endif
29
+
30
+ #else
31
+
32
+ #include <sys/times.h>
33
+ #include <unistd.h>
34
+ #include <time.h>
35
+
36
+ #ifdef HAVE_UTMPX_H
37
+ #include <utmpx.h>
38
+ #endif
39
+
40
+ #ifdef _SC_CLK_TCK
41
+ #define TICKS sysconf(_SC_CLK_TCK)
42
+ #else
43
+ #define TICKS sysconf(CLOCKS_PER_SEC)
44
+ #endif
45
+
46
+ #endif
47
+
48
+ #ifdef HAVE_SYS_LOADAVG_H
49
+ #include <sys/loadavg.h>
50
+ #endif
51
+
52
+ #define MAXSTRINGSIZE 32 /* reasonable limit */
53
+
54
+ #define SYS_UPTIME_VERSION "0.5.4"
55
+
56
+ VALUE cUptimeError;
57
+
58
+ unsigned long get_uptime_secs()
59
+ {
60
+ #ifdef HAVE_SYSCTL
61
+ struct timeval tv;
62
+ size_t tvlen = sizeof(tv);
63
+ int mib[2];
64
+
65
+ mib[0] = CTL_KERN;
66
+ mib[1] = KERN_BOOTTIME;
67
+
68
+ if(sysctl(mib, 2, &tv, &tvlen, NULL, 0))
69
+ rb_raise(cUptimeError, "sysctl() call failed %s", strerror(errno));
70
+
71
+ return time(NULL) - tv.tv_sec;
72
+ #else
73
+ struct tms tms;
74
+ unsigned long seconds;
75
+ seconds = times(&tms) / TICKS;
76
+
77
+ if(-1 == seconds)
78
+ rb_raise(cUptimeError, "times() function failed: %s", strerror(errno));
79
+
80
+ if(seconds < 0)
81
+ rb_raise(cUptimeError, "value returned larger than type could handle");
82
+
83
+ return seconds;
84
+ #endif
85
+ }
86
+
87
+ /*
88
+ * call-seq:
89
+ * Uptime.seconds
90
+ *
91
+ * Returns the total number of seconds the system has been up.
92
+ */
93
+ static VALUE uptime_seconds()
94
+ {
95
+ return UINT2NUM(get_uptime_secs());
96
+ }
97
+
98
+ /*
99
+ * call-seq:
100
+ * Uptime.minutes
101
+ *
102
+ * Returns the total number of minutes the system has been up.
103
+ */
104
+ static VALUE uptime_minutes()
105
+ {
106
+ return UINT2NUM(get_uptime_secs() / 60);
107
+ }
108
+
109
+ /*
110
+ * call-seq:
111
+ * Uptime.hours
112
+ *
113
+ * Returns the total number of hours the system has been up.
114
+ */
115
+ static VALUE uptime_hours()
116
+ {
117
+ return UINT2NUM(get_uptime_secs() / 3600);
118
+ }
119
+
120
+ /*
121
+ * call-seq:
122
+ * Uptime.days
123
+ *
124
+ * Returns the total number of days the system has been up.
125
+ */
126
+ static VALUE uptime_days()
127
+ {
128
+ return UINT2NUM(get_uptime_secs() / 86400);
129
+ }
130
+
131
+ /*
132
+ * call-seq:
133
+ * Uptime.uptime
134
+ *
135
+ * Calculates and returns the number of days, hours, minutes and
136
+ * seconds the system has been running as a colon-separated string.
137
+ */
138
+ static VALUE uptime_uptime()
139
+ {
140
+ char c_string[MAXSTRINGSIZE];
141
+ long seconds, days, hours, minutes;
142
+
143
+ seconds = get_uptime_secs();
144
+ days = seconds/86400;
145
+ seconds -= days*86400;
146
+ hours = seconds/3600;
147
+ seconds -= hours*3600;
148
+ minutes = seconds/60;
149
+ seconds -= minutes*60;
150
+
151
+ sprintf(c_string, "%ld:%ld:%ld:%ld", days, hours, minutes, seconds);
152
+
153
+ return rb_str_new2(c_string);
154
+ }
155
+
156
+ /*
157
+ * call-seq:
158
+ * Uptime.dhms
159
+ *
160
+ * Calculates and returns the number of days, hours, minutes and
161
+ * seconds the system has been running as a four-element Array.
162
+ */
163
+ static VALUE uptime_dhms()
164
+ {
165
+ VALUE a = rb_ary_new2(4);
166
+ long s, m, h, d;
167
+
168
+ s = get_uptime_secs();
169
+ d = s / (24*60*60);
170
+ h = (s -= d*24*60*60) / ( 60*60);
171
+ m = (s -= h* 60*60) / 60;
172
+ s -= m* 60;
173
+
174
+ rb_ary_push(a, INT2FIX(d));
175
+ rb_ary_push(a, INT2FIX(h));
176
+ rb_ary_push(a, INT2FIX(m));
177
+ rb_ary_push(a, INT2FIX(s));
178
+
179
+ return a;
180
+ }
181
+
182
+ /*
183
+ * call-seq:
184
+ * Uptime.boot_time
185
+ *
186
+ * Returns the boot time as a Time object.
187
+ */
188
+ static VALUE uptime_btime(){
189
+ VALUE v_time = Qnil;
190
+
191
+ #ifdef HAVE_SYSCTL
192
+ struct timeval tv;
193
+ size_t tvlen = sizeof(tv);
194
+ int mib[2];
195
+
196
+ mib[0] = CTL_KERN;
197
+ mib[1] = KERN_BOOTTIME;
198
+
199
+ if(sysctl(mib, 2, &tv, &tvlen, NULL, 0))
200
+ rb_raise(cUptimeError, "sysctl() call failed: %s", strerror(errno));
201
+
202
+ v_time = rb_time_new(tv.tv_sec,tv.tv_usec);
203
+ #else
204
+ #ifdef HAVE_UTMPX_H
205
+ struct utmpx* ent;
206
+
207
+ setutxent();
208
+
209
+ while( (ent = getutxent()) ){
210
+ if(ent->ut_type == BOOT_TIME){
211
+ v_time = rb_time_new(ent->ut_tv.tv_sec, ent->ut_tv.tv_usec);
212
+ break;
213
+ }
214
+ }
215
+
216
+ endutxent();
217
+ #else
218
+ rb_raise(cUptimeError, "boot_time method not implemented on this platform");
219
+ #endif
220
+ #endif
221
+
222
+ return v_time;
223
+ }
224
+
225
+ void Init_uptime()
226
+ {
227
+ VALUE mSys, cUptime;
228
+
229
+ /* The Sys module only serves as a toplevel namespace */
230
+ mSys = rb_define_module("Sys");
231
+
232
+ /* The Uptime encapsulates various bits of uptime information */
233
+ cUptime = rb_define_class_under(mSys, "Uptime", rb_cObject);
234
+
235
+ /* The Uptime::Error class is raised if any of the Uptime methods fail */
236
+ cUptimeError = rb_define_class_under(cUptime, "Error", rb_eStandardError);
237
+
238
+ /* 0.5.4: The version of this library */
239
+ rb_define_const(cUptime, "VERSION", rb_str_new2(SYS_UPTIME_VERSION));
240
+
241
+ /* Singleton Methods */
242
+
243
+ rb_define_singleton_method(cUptime, "seconds", uptime_seconds, 0);
244
+ rb_define_singleton_method(cUptime, "minutes", uptime_minutes, 0);
245
+ rb_define_singleton_method(cUptime, "hours", uptime_hours, 0);
246
+ rb_define_singleton_method(cUptime, "days", uptime_days, 0);
247
+ rb_define_singleton_method(cUptime, "uptime", uptime_uptime, 0);
248
+ rb_define_singleton_method(cUptime, "dhms", uptime_dhms, 0);
249
+ rb_define_singleton_method(cUptime, "boot_time", uptime_btime, 0);
250
+ }
@@ -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
@@ -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
@@ -0,0 +1,65 @@
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
+ include Sys
10
+
11
+ class TC_Sys_Uptime < Test::Unit::TestCase
12
+ def test_version
13
+ assert_equal('0.5.4', Uptime::VERSION)
14
+ end
15
+
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
+
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
+
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
+
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
+
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
+
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
+
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
+
62
+ def test_uptime_error
63
+ assert_kind_of(StandardError, Uptime::Error.new)
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sys-uptime
3
+ version: !ruby/object:Gem::Version
4
+ hash: 3
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 5
9
+ - 4
10
+ version: 0.5.4
11
+ platform: x86-mingw32
12
+ authors:
13
+ - Daniel J. Berger
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-27 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ 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"
23
+ email: djberg96@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - CHANGES
30
+ - README
31
+ - MANIFEST
32
+ - doc/uptime.txt
33
+ files:
34
+ - CHANGES
35
+ - doc/uptime.txt
36
+ - examples/sys_uptime_example.rb
37
+ - ext/extconf.rb
38
+ - ext/sys/uptime.c
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
46
+ has_rdoc: true
47
+ homepage: http://www.rubyforge.org/projects/sysutils
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ - lib/windows
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project: sysutils
77
+ rubygems_version: 1.3.7
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: A Ruby interface for getting system uptime information.
81
+ test_files:
82
+ - test/test_sys_uptime.rb