linux_stat 1.6.0 → 2.2.0
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.
- checksums.yaml +4 -4
- data/README.md +160 -126
- data/bin/console +7 -1
- data/exe/linuxstat.rb +6 -0
- data/ext/procfs/extconf.rb +11 -0
- data/ext/procfs/loadavg_pid.h +11 -0
- data/ext/procfs/procfs.c +43 -0
- data/ext/procfs/stat.h +119 -0
- data/ext/procfs/statm.h +109 -0
- data/ext/procfs/uptime.h +12 -0
- data/lib/linux_stat.rb +4 -1
- data/lib/linux_stat/battery.rb +8 -1
- data/lib/linux_stat/net.rb +5 -5
- data/lib/linux_stat/os.rb +16 -15
- data/lib/linux_stat/process.rb +17 -27
- data/lib/linux_stat/process_info.rb +124 -158
- data/lib/linux_stat/swap.rb +23 -7
- data/lib/linux_stat/version.rb +1 -1
- metadata +11 -4
data/bin/console
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env ruby
|
|
2
2
|
$-v = true
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
begin
|
|
5
|
+
%w(linux_stat irb).each(&method(:require))
|
|
6
|
+
rescue LoadError
|
|
7
|
+
abort "LinuxStat and IRB shoud be installed before you can run LinuxStat console"
|
|
8
|
+
end
|
|
9
|
+
|
|
4
10
|
IRB.start(__FILE__)
|
data/exe/linuxstat.rb
CHANGED
|
@@ -7,6 +7,12 @@ rescue LoadError
|
|
|
7
7
|
abort "The Gem needs to be installed before this test can be run!"
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
+
Integer.class_exec do
|
|
11
|
+
define_method(:clamp) { |min, max|
|
|
12
|
+
self < min ? min : self > max ? max : self
|
|
13
|
+
}
|
|
14
|
+
end unless 1.respond_to?(:clamp)
|
|
15
|
+
|
|
10
16
|
# Gradient colour to strings
|
|
11
17
|
class String
|
|
12
18
|
def colourize(colour = 1, flip: false)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
require 'mkmf'
|
|
2
|
+
|
|
3
|
+
unless have_const('linux') || RbConfig::CONFIG['arch'].to_s[/linux/]
|
|
4
|
+
abort('Platform is not linux')
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
unless have_header('unistd.h') && have_header('ruby.h')
|
|
8
|
+
abort('Missing header')
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
create_makefile 'linux_stat/procfs'
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
VALUE last_pid(VALUE obj) {
|
|
2
|
+
FILE *f = fopen("/proc/loadavg", "r") ;
|
|
3
|
+
if (!f) return Qnil ;
|
|
4
|
+
|
|
5
|
+
unsigned long long _last_pid ;
|
|
6
|
+
char status = fscanf(f, "%*f %*f %*f %*s %llu", &_last_pid) ;
|
|
7
|
+
fclose(f) ;
|
|
8
|
+
if (status != 1) return Qnil ;
|
|
9
|
+
|
|
10
|
+
return ULL2NUM(_last_pid) ;
|
|
11
|
+
}
|
data/ext/procfs/procfs.c
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// ProcFS doesn't include all the /proc/ files.
|
|
2
|
+
// It includes some of them that can add potential
|
|
3
|
+
// performance boost to the LinuxStat modules.
|
|
4
|
+
//
|
|
5
|
+
// Some LinuxStat modules reads /proc/ files
|
|
6
|
+
// that are not present here. That is because
|
|
7
|
+
// Ruby can handle them well with little wastage.
|
|
8
|
+
// Adding C may be a potential risk in such places.
|
|
9
|
+
//
|
|
10
|
+
// In this ProcFS module, we will define the methods
|
|
11
|
+
// that really gets the benefit.
|
|
12
|
+
|
|
13
|
+
#include <stdio.h>
|
|
14
|
+
#include <unistd.h>
|
|
15
|
+
#include <string.h>
|
|
16
|
+
#include "ruby.h"
|
|
17
|
+
#include "uptime.h"
|
|
18
|
+
#include "statm.h"
|
|
19
|
+
#include "stat.h"
|
|
20
|
+
#include "loadavg_pid.h"
|
|
21
|
+
|
|
22
|
+
int Init_procfs() {
|
|
23
|
+
VALUE _linux_stat = rb_define_module("LinuxStat") ;
|
|
24
|
+
VALUE _procfs = rb_define_module_under(_linux_stat, "ProcFS") ;
|
|
25
|
+
|
|
26
|
+
// uptime
|
|
27
|
+
rb_define_module_function(_procfs, "uptime_f", uptime_f, 0) ;
|
|
28
|
+
|
|
29
|
+
// statm
|
|
30
|
+
rb_define_module_function(_procfs, "statm_memory", statm_memory, 1) ;
|
|
31
|
+
rb_define_module_function(_procfs, "statm", statm, 1) ;
|
|
32
|
+
rb_define_module_function(_procfs, "statm_virtual", statm_virtual, 1) ;
|
|
33
|
+
rb_define_module_function(_procfs, "statm_resident", statm_resident, 1) ;
|
|
34
|
+
rb_define_module_function(_procfs, "statm_shared", statm_shared, 1) ;
|
|
35
|
+
|
|
36
|
+
// loadavg last PID
|
|
37
|
+
rb_define_module_function(_procfs, "last_pid", last_pid, 0) ;
|
|
38
|
+
|
|
39
|
+
// stat
|
|
40
|
+
rb_define_module_function(_procfs, "ps_state", ps_state, 1) ;
|
|
41
|
+
rb_define_module_function(_procfs, "ps_times", ps_times, 1) ;
|
|
42
|
+
rb_define_module_function(_procfs, "ps_stat", ps_stat, 1) ;
|
|
43
|
+
}
|
data/ext/procfs/stat.h
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
VALUE ps_state(VALUE obj, VALUE pid) {
|
|
2
|
+
int _pid = FIX2INT(pid) ;
|
|
3
|
+
if (_pid < 0) return rb_str_new_cstr("") ;
|
|
4
|
+
|
|
5
|
+
char _path[22] ;
|
|
6
|
+
sprintf(_path, "/proc/%d/stat", _pid) ;
|
|
7
|
+
|
|
8
|
+
FILE *f = fopen(_path, "r") ;
|
|
9
|
+
if (!f) return rb_str_new_cstr("") ;
|
|
10
|
+
|
|
11
|
+
char _s[1] ;
|
|
12
|
+
|
|
13
|
+
char status = fscanf(f, "%*llu (%*[^)]%*[)] %s", _s) ;
|
|
14
|
+
fclose(f) ;
|
|
15
|
+
|
|
16
|
+
if (status != 1) return rb_str_new_cstr("") ;
|
|
17
|
+
return rb_str_new_cstr(_s) ;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
VALUE ps_times(VALUE obj, VALUE pid) {
|
|
21
|
+
int _pid = FIX2INT(pid) ;
|
|
22
|
+
if (_pid < 0) return Qnil ;
|
|
23
|
+
|
|
24
|
+
char _path[22] ;
|
|
25
|
+
sprintf(_path, "/proc/%d/stat", _pid) ;
|
|
26
|
+
|
|
27
|
+
FILE *f = fopen(_path, "r") ;
|
|
28
|
+
if (!f) return Qnil ;
|
|
29
|
+
|
|
30
|
+
unsigned long utime, stime ;
|
|
31
|
+
|
|
32
|
+
char status = fscanf(f, "%*llu (%*[^)]%*[)] %*c %*d %*d %*d %*d %*d %*u %*lu %*lu %*lu %*lu %lu %lu", &utime, &stime) ;
|
|
33
|
+
fclose(f) ;
|
|
34
|
+
|
|
35
|
+
if (status != 2) return Qnil ;
|
|
36
|
+
double total_time = (utime + stime) / (float)sysconf(_SC_CLK_TCK);
|
|
37
|
+
|
|
38
|
+
return DBL2NUM(total_time) ;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
VALUE ps_stat(VALUE obj, VALUE pid) {
|
|
42
|
+
int _pid = FIX2INT(pid) ;
|
|
43
|
+
if (_pid < 0) return rb_str_new_cstr("") ;
|
|
44
|
+
|
|
45
|
+
char _path[22] ;
|
|
46
|
+
sprintf(_path, "/proc/%d/stat", _pid) ;
|
|
47
|
+
|
|
48
|
+
FILE *f = fopen(_path, "r") ;
|
|
49
|
+
|
|
50
|
+
if (!f)
|
|
51
|
+
return rb_ary_new() ;
|
|
52
|
+
|
|
53
|
+
// ?? JEEZ !!
|
|
54
|
+
// We need to do this because the datatypes are different
|
|
55
|
+
// Can't make an array like Ruby out of String
|
|
56
|
+
// This is hard to write and check, but tested, and works.
|
|
57
|
+
//
|
|
58
|
+
// For this struct,
|
|
59
|
+
// follow https://man7.org/linux/man-pages/man5/proc.5.html
|
|
60
|
+
int ppid, pgrp, session, tty_nr, tpgid ;
|
|
61
|
+
unsigned flags ;
|
|
62
|
+
long unsigned minflt, cminflt, majflt, cmajflt, utime, stime ;
|
|
63
|
+
long cutime, cstime, priority, nice, num_threads, itrealvalue ;
|
|
64
|
+
long long unsigned starttime ;
|
|
65
|
+
long unsigned vsize ;
|
|
66
|
+
long rss ;
|
|
67
|
+
long unsigned rsslim, startcode, endcode, startstack, kstkesp, kstkeip ;
|
|
68
|
+
long unsigned signal, blocked, sigignore, sigcatch, wchan, nswap, cnswap ;
|
|
69
|
+
int exit_signal, processor ;
|
|
70
|
+
unsigned rt_priority, policy ;
|
|
71
|
+
long long unsigned delayacct_blkio_ticks ;
|
|
72
|
+
long unsigned guest_time ;
|
|
73
|
+
long cguest_time ;
|
|
74
|
+
long unsigned start_data, end_data, start_brk, arg_start, arg_end ;
|
|
75
|
+
long unsigned env_start, env_end ;
|
|
76
|
+
int exit_code ;
|
|
77
|
+
|
|
78
|
+
char status = fscanf(
|
|
79
|
+
f, "%*llu (%*[^)]%*[)] %*c "
|
|
80
|
+
"%d %d %d %d %d %u "
|
|
81
|
+
"%lu %lu %lu %lu %lu %lu "
|
|
82
|
+
"%ld %ld %ld %ld %ld %ld "
|
|
83
|
+
"%llu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu "
|
|
84
|
+
"%d %d %u %u %llu %lu %ld %lu %lu %lu %lu %lu %lu %lu %d", &ppid, &pgrp,
|
|
85
|
+
&session, &tty_nr, &tpgid, &flags, &minflt, &cminflt, &majflt, &cmajflt, &utime, &stime, &cutime,
|
|
86
|
+
&cstime, &priority, &nice, &num_threads, &itrealvalue, &starttime, &vsize, &rss, &rsslim,
|
|
87
|
+
&startcode, &endcode, &startstack, &kstkesp, &kstkeip, &signal, &blocked, &sigignore,
|
|
88
|
+
&sigcatch, &wchan, &nswap, &cnswap, &exit_signal, &processor, &rt_priority, &policy,
|
|
89
|
+
&delayacct_blkio_ticks, &guest_time, &cguest_time, &start_data, &end_data,
|
|
90
|
+
&start_brk, &arg_start, &arg_end, &env_start, &env_end, &exit_code
|
|
91
|
+
) ;
|
|
92
|
+
|
|
93
|
+
fclose(f) ;
|
|
94
|
+
|
|
95
|
+
if (status != 49)
|
|
96
|
+
return rb_ary_new() ;
|
|
97
|
+
|
|
98
|
+
return rb_ary_new_from_args(49,
|
|
99
|
+
INT2NUM(ppid), INT2NUM(pgrp), INT2NUM(session), INT2NUM(tty_nr), INT2NUM(tpgid),
|
|
100
|
+
UINT2NUM(flags),
|
|
101
|
+
ULONG2NUM(minflt), ULONG2NUM(cminflt), ULONG2NUM(majflt), ULONG2NUM(cmajflt),
|
|
102
|
+
ULONG2NUM(utime), ULONG2NUM(stime),
|
|
103
|
+
LONG2NUM(cutime), LONG2NUM(cstime), LONG2NUM(priority),
|
|
104
|
+
LONG2NUM(nice), LONG2NUM(num_threads), LONG2NUM(itrealvalue),
|
|
105
|
+
ULL2NUM(starttime),
|
|
106
|
+
ULONG2NUM(vsize),
|
|
107
|
+
LONG2NUM(rss),
|
|
108
|
+
ULONG2NUM(rsslim), ULONG2NUM(startcode), ULONG2NUM(endcode),
|
|
109
|
+
ULONG2NUM(startstack), ULONG2NUM(kstkesp), ULONG2NUM(kstkeip),
|
|
110
|
+
ULONG2NUM(signal), ULONG2NUM(blocked), ULONG2NUM(sigignore), ULONG2NUM(sigcatch),
|
|
111
|
+
ULONG2NUM(wchan), ULONG2NUM(nswap), ULONG2NUM(cnswap),
|
|
112
|
+
INT2NUM(exit_signal), INT2NUM(processor),
|
|
113
|
+
UINT2NUM(rt_priority), UINT2NUM(policy),
|
|
114
|
+
ULL2NUM(delayacct_blkio_ticks), ULONG2NUM(guest_time), LONG2NUM(cguest_time), ULONG2NUM(start_data),
|
|
115
|
+
ULONG2NUM(end_data), ULONG2NUM(start_brk), ULONG2NUM(arg_start), ULONG2NUM(arg_end),
|
|
116
|
+
ULONG2NUM(env_start), ULONG2NUM(env_end),
|
|
117
|
+
INT2NUM(exit_code)
|
|
118
|
+
) ;
|
|
119
|
+
}
|
data/ext/procfs/statm.h
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
#define PAGESIZE sysconf(_SC_PAGESIZE)
|
|
2
|
+
|
|
3
|
+
VALUE statm(VALUE obj, VALUE pid) {
|
|
4
|
+
VALUE hash = rb_hash_new() ;
|
|
5
|
+
|
|
6
|
+
int _pid = FIX2INT(pid) ;
|
|
7
|
+
if (_pid < 0) return hash ;
|
|
8
|
+
|
|
9
|
+
char _path[22] ;
|
|
10
|
+
sprintf(_path, "/proc/%d/statm", _pid) ;
|
|
11
|
+
|
|
12
|
+
FILE *f = fopen(_path, "r") ;
|
|
13
|
+
if (!f) return hash ;
|
|
14
|
+
|
|
15
|
+
unsigned int _virtual, resident, shared ;
|
|
16
|
+
char status = fscanf(f, "%u %u %u", &_virtual, &resident, &shared) ;
|
|
17
|
+
fclose(f) ;
|
|
18
|
+
|
|
19
|
+
if (status != 3) return hash ;
|
|
20
|
+
|
|
21
|
+
int pagesize = PAGESIZE ;
|
|
22
|
+
|
|
23
|
+
_virtual *= pagesize ;
|
|
24
|
+
resident *= pagesize ;
|
|
25
|
+
shared *= pagesize ;
|
|
26
|
+
|
|
27
|
+
unsigned int v = resident - shared ;
|
|
28
|
+
|
|
29
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("memory")), UINT2NUM(v)) ;
|
|
30
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("virtual_memory")), UINT2NUM(_virtual)) ;
|
|
31
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("resident_memory")), UINT2NUM(resident)) ;
|
|
32
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("shared_memory")), UINT2NUM(shared)) ;
|
|
33
|
+
|
|
34
|
+
return hash ;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
VALUE statm_virtual(VALUE obj, VALUE pid) {
|
|
38
|
+
int _pid = FIX2INT(pid) ;
|
|
39
|
+
if (_pid < 0) return Qnil ;
|
|
40
|
+
|
|
41
|
+
char _path[22] ;
|
|
42
|
+
sprintf(_path, "/proc/%d/statm", _pid) ;
|
|
43
|
+
|
|
44
|
+
FILE *f = fopen(_path, "r") ;
|
|
45
|
+
if (!f) return Qnil ;
|
|
46
|
+
|
|
47
|
+
unsigned int _virtual ;
|
|
48
|
+
char status = fscanf(f, "%u", &_virtual) ;
|
|
49
|
+
fclose(f) ;
|
|
50
|
+
|
|
51
|
+
if (status != 1) return Qnil ;
|
|
52
|
+
return UINT2NUM(_virtual * PAGESIZE) ;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
VALUE statm_resident(VALUE obj, VALUE pid) {
|
|
56
|
+
int _pid = FIX2INT(pid) ;
|
|
57
|
+
if (_pid < 0) return Qnil ;
|
|
58
|
+
|
|
59
|
+
char _path[22] ;
|
|
60
|
+
sprintf(_path, "/proc/%d/statm", _pid) ;
|
|
61
|
+
|
|
62
|
+
FILE *f = fopen(_path, "r") ;
|
|
63
|
+
if (!f) return Qnil ;
|
|
64
|
+
|
|
65
|
+
unsigned int resident ;
|
|
66
|
+
char status = fscanf(f, "%*u %u", &resident) ;
|
|
67
|
+
fclose(f) ;
|
|
68
|
+
|
|
69
|
+
if (status != 1) return Qnil ;
|
|
70
|
+
return UINT2NUM(resident * PAGESIZE) ;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
VALUE statm_shared(VALUE obj, VALUE pid) {
|
|
74
|
+
int _pid = FIX2INT(pid) ;
|
|
75
|
+
if (_pid < 0) return Qnil ;
|
|
76
|
+
|
|
77
|
+
char _path[22] ;
|
|
78
|
+
sprintf(_path, "/proc/%d/statm", _pid) ;
|
|
79
|
+
|
|
80
|
+
FILE *f = fopen(_path, "r") ;
|
|
81
|
+
if (!f) return Qnil ;
|
|
82
|
+
|
|
83
|
+
unsigned int shared ;
|
|
84
|
+
char status = fscanf(f, "%*u %*u %u", &shared) ;
|
|
85
|
+
fclose(f) ;
|
|
86
|
+
|
|
87
|
+
if (status != 1) return Qnil ;
|
|
88
|
+
return UINT2NUM(shared * PAGESIZE) ;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
VALUE statm_memory(VALUE obj, VALUE pid) {
|
|
92
|
+
int _pid = FIX2INT(pid) ;
|
|
93
|
+
if (_pid < 0) return Qnil ;
|
|
94
|
+
|
|
95
|
+
char _path[22] ;
|
|
96
|
+
sprintf(_path, "/proc/%d/statm", _pid) ;
|
|
97
|
+
|
|
98
|
+
FILE *f = fopen(_path, "r") ;
|
|
99
|
+
if (!f) return Qnil ;
|
|
100
|
+
|
|
101
|
+
unsigned int resident, shared ;
|
|
102
|
+
char status = fscanf(f, "%*u %u %u", &resident, &shared) ;
|
|
103
|
+
fclose(f) ;
|
|
104
|
+
|
|
105
|
+
if (status != 2) return Qnil ;
|
|
106
|
+
|
|
107
|
+
unsigned int v = (resident - shared) * PAGESIZE ;
|
|
108
|
+
return UINT2NUM(v) ;
|
|
109
|
+
}
|
data/ext/procfs/uptime.h
ADDED
data/lib/linux_stat.rb
CHANGED
|
@@ -25,13 +25,16 @@ require "linux_stat/battery"
|
|
|
25
25
|
require "linux_stat/bios"
|
|
26
26
|
require "linux_stat/net"
|
|
27
27
|
require "linux_stat/pci"
|
|
28
|
-
require "linux_stat/process"
|
|
29
28
|
require "linux_stat/thermal"
|
|
30
29
|
require "linux_stat/usb"
|
|
31
30
|
|
|
32
31
|
# Dependent Modules
|
|
33
32
|
# Modules that can have reverse dependency
|
|
34
33
|
|
|
34
|
+
# LinuxStat::ProcFS dependent modules
|
|
35
|
+
require "linux_stat/procfs"
|
|
36
|
+
require "linux_stat/process"
|
|
37
|
+
|
|
35
38
|
# LinuxStat::CPU.sysinfo dependent modules
|
|
36
39
|
require "linux_stat/sysinfo"
|
|
37
40
|
require "linux_stat/swap"
|
data/lib/linux_stat/battery.rb
CHANGED
|
@@ -178,7 +178,14 @@ module LinuxStat
|
|
|
178
178
|
# For example, a sample output can be like this (taken on a test system):
|
|
179
179
|
#
|
|
180
180
|
# LinuxStat::Battery.devices_stat
|
|
181
|
-
# => {:AC=>{:type=>"Mains", :online=>1},
|
|
181
|
+
# => {:AC=>{:type=>"Mains", :online=>1},
|
|
182
|
+
# :BAT0=>{:model=>"DELL CYMGM77", :manufacturer=>"Samsung SDI",
|
|
183
|
+
# :type=>"Battery", :status=>"Full", :capacity=>100, :voltage_min_design=>11.4,
|
|
184
|
+
# :charge_full_design=>3.684, :charge_full_design_wh=>42.0,
|
|
185
|
+
# :voltage_now=>12.558, :charge_now=>2.087, :charge_now_wh=>26.21,
|
|
186
|
+
# :charge_full_wh=>23.79, :charge_percentage=>100.0},
|
|
187
|
+
# :hidpp_battery_0=>{:model=>"Wireless Keyboard", :manufacturer=>"Logitech",
|
|
188
|
+
# :type=>"Battery", :status=>"Discharging", :online=>1}}
|
|
182
189
|
#
|
|
183
190
|
# If you need info about lots of batteries, use this method.
|
|
184
191
|
# If the informations are not available, it will return empty Hash for each devices.
|
data/lib/linux_stat/net.rb
CHANGED
|
@@ -30,7 +30,7 @@ module LinuxStat
|
|
|
30
30
|
data = IO.readlines(DEV).drop(2)
|
|
31
31
|
indices = find_index_of_bytes
|
|
32
32
|
data.reject! { |x| x.strip.start_with?('lo:') }
|
|
33
|
-
r, t = data.map { |x| x.split.values_at(*indices).map(&:to_i) }.transpose.map(
|
|
33
|
+
r, t = data.map { |x| x.split.values_at(*indices).map(&:to_i) }.transpose.map { |x| x.reduce(:+) }
|
|
34
34
|
|
|
35
35
|
{
|
|
36
36
|
received: r,
|
|
@@ -48,7 +48,7 @@ module LinuxStat
|
|
|
48
48
|
data = IO.readlines(DEV).drop(2)
|
|
49
49
|
index = find_index_of_bytes[0]
|
|
50
50
|
data.reject! { |x| x.strip.start_with?('lo:') }
|
|
51
|
-
data.map { |x| x.split[index].to_i }.
|
|
51
|
+
data.map { |x| x.split[index].to_i }.reduce(:+)
|
|
52
52
|
end
|
|
53
53
|
|
|
54
54
|
##
|
|
@@ -61,7 +61,7 @@ module LinuxStat
|
|
|
61
61
|
data = IO.readlines(DEV).drop(2)
|
|
62
62
|
index = find_index_of_bytes[-1]
|
|
63
63
|
data.reject! { |x| x.strip.start_with?('lo:') }
|
|
64
|
-
data.map { |x| x.split[index].to_i }.
|
|
64
|
+
data.map { |x| x.split[index].to_i }.reduce(:+)
|
|
65
65
|
end
|
|
66
66
|
|
|
67
67
|
##
|
|
@@ -89,13 +89,13 @@ module LinuxStat
|
|
|
89
89
|
data = IO.readlines(DEV).drop(2)
|
|
90
90
|
indices = find_index_of_bytes
|
|
91
91
|
data.reject! { |x| x.strip.start_with?('lo:'.freeze) }
|
|
92
|
-
r, t = data.map { |x| x.split.values_at(*indices).map(&:to_i) }.transpose.map(
|
|
92
|
+
r, t = data.map { |x| x.split.values_at(*indices).map(&:to_i) }.transpose.map { |x| x.reduce(:+) }
|
|
93
93
|
|
|
94
94
|
sleep(interval)
|
|
95
95
|
|
|
96
96
|
data2 = IO.readlines(DEV).drop(2)
|
|
97
97
|
data2.reject! { |x| x.strip.start_with?('lo:'.freeze) }
|
|
98
|
-
r2, t2 = data2.map { |x| x.split.values_at(*indices).map(&:to_i) }.transpose.map(
|
|
98
|
+
r2, t2 = data2.map { |x| x.split.values_at(*indices).map(&:to_i) }.transpose.map { |x| x.reduce(:+) }
|
|
99
99
|
|
|
100
100
|
# Measure the difference
|
|
101
101
|
dr, dt = r2.-(r).fdiv(interval), t2.-(t).fdiv(interval)
|
data/lib/linux_stat/os.rb
CHANGED
|
@@ -7,7 +7,11 @@ module LinuxStat
|
|
|
7
7
|
# Reads /etc/os-release and returns a Hash. For example:
|
|
8
8
|
# LinuxStat::OS.os_release
|
|
9
9
|
#
|
|
10
|
-
# => {:NAME=>"Arch Linux", :PRETTY_NAME=>"Arch Linux", :ID=>"arch", :BUILD_ID=>"rolling",
|
|
10
|
+
# => {:NAME=>"Arch Linux", :PRETTY_NAME=>"Arch Linux", :ID=>"arch", :BUILD_ID=>"rolling",
|
|
11
|
+
# :ANSI_COLOR=>"38;2;23;147;209", :HOME_URL=>"https://www.archlinux.org/",
|
|
12
|
+
# :DOCUMENTATION_URL=>"https://wiki.archlinux.org/",
|
|
13
|
+
# :SUPPORT_URL=>"https://bbs.archlinux.org/", :BUG_REPORT_URL=>"https://bugs.archlinux.org/",
|
|
14
|
+
# :LOGO=>"archlinux"}
|
|
11
15
|
#
|
|
12
16
|
# If the info isn't available, it will return an empty Hash.
|
|
13
17
|
#
|
|
@@ -127,7 +131,7 @@ module LinuxStat
|
|
|
127
131
|
#
|
|
128
132
|
# The return type is strictly Integer and doesn't fail.
|
|
129
133
|
def bits
|
|
130
|
-
@@bits ||= if
|
|
134
|
+
@@bits ||= if machine.end_with?('64') || RbConfig::CONFIG['host_cpu'].end_with?('64') || RUBY_PLATFORM.end_with?('64')
|
|
131
135
|
64
|
|
132
136
|
else
|
|
133
137
|
32
|
|
@@ -138,26 +142,28 @@ module LinuxStat
|
|
|
138
142
|
# Reads /proc/uptime and returns the system uptime:
|
|
139
143
|
# LinuxStat::OS.uptime
|
|
140
144
|
#
|
|
141
|
-
# => {:hour=>
|
|
145
|
+
# => {:hour=>16, :minute=>10, :second=>11, :jiffy=>20}
|
|
142
146
|
#
|
|
143
147
|
# Using uptime is 10x slower than using uptime_i
|
|
144
148
|
#
|
|
145
149
|
# If the stat isn't available, an empty hash is returned.
|
|
146
150
|
def uptime
|
|
147
|
-
|
|
151
|
+
_uptime = LinuxStat::ProcFS.uptime_f
|
|
152
|
+
return {} unless _uptime
|
|
148
153
|
|
|
149
|
-
|
|
150
|
-
uptime_i = uptime.to_i
|
|
154
|
+
uptime_i = _uptime.to_i
|
|
151
155
|
|
|
152
156
|
h = uptime_i / 3600
|
|
153
157
|
m = uptime_i % 3600 / 60
|
|
154
|
-
s =
|
|
158
|
+
s = uptime_i.%(60)
|
|
159
|
+
j = _uptime.-(uptime_i) * 100
|
|
155
160
|
|
|
156
161
|
{
|
|
157
162
|
hour: h,
|
|
158
163
|
minute: m,
|
|
159
|
-
second: s
|
|
160
|
-
|
|
164
|
+
second: s,
|
|
165
|
+
jiffy: j.to_i
|
|
166
|
+
}
|
|
161
167
|
end
|
|
162
168
|
|
|
163
169
|
##
|
|
@@ -172,8 +178,7 @@ module LinuxStat
|
|
|
172
178
|
#
|
|
173
179
|
# If the stat isn't available, nil is returned.
|
|
174
180
|
def uptime_f
|
|
175
|
-
|
|
176
|
-
IO.foreach('/proc/uptime'.freeze, ' ').next.to_f
|
|
181
|
+
LinuxStat::ProcFS.uptime_f
|
|
177
182
|
end
|
|
178
183
|
|
|
179
184
|
##
|
|
@@ -232,10 +237,6 @@ module LinuxStat
|
|
|
232
237
|
h.merge!( key.to_sym => value )
|
|
233
238
|
}
|
|
234
239
|
end
|
|
235
|
-
|
|
236
|
-
def uptime_readable?
|
|
237
|
-
@@uptime_readable ||= File.readable?('/proc/uptime')
|
|
238
|
-
end
|
|
239
240
|
end
|
|
240
241
|
end
|
|
241
242
|
end
|