linux_stat 0.4.0 → 0.6.2
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/LICENSE.txt +21 -0
- data/README.md +465 -52
- data/bin/console +1 -1
- data/bin/setup +0 -2
- data/{bin → exe}/linuxstat.rb +42 -7
- data/ext/fs_stat/fs_stat.c +2 -0
- data/ext/sysconf/sysconf.c +30 -2
- data/ext/utsname/utsname.c +2 -0
- data/lib/linux_stat.rb +11 -4
- data/lib/linux_stat/cpu.rb +14 -5
- data/lib/linux_stat/kernel.rb +1 -2
- data/lib/linux_stat/mounts.rb +152 -1
- data/lib/linux_stat/prettify_bytes.rb +75 -0
- data/lib/linux_stat/process_info.rb +108 -46
- data/lib/linux_stat/user.rb +298 -0
- data/lib/linux_stat/version.rb +1 -1
- metadata +11 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 98045c378f59845c66b01f0e311faed27a4def546bea99354bfd04a4883a5d38
|
4
|
+
data.tar.gz: fe4413395bca5cae357413deba7bd3f1316c845d4652289688fe8c15ee38cc13
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fef16c595ecac5b2c5ad86851cd6dd8fe27211e7905f315c68ce5cb451e3ee75bf0feff1913ae9b1b66e1fdc4a935d456b4f62b758ee19f592e378c8aa2f78d1
|
7
|
+
data.tar.gz: 84cc82dbd7fe6a080f1ddd1b5c7ea4aa613ea58816893ae681187e859676c111d0cdb988fb26a8f15fd607cd37ec951f30c0492ed722e0b46bfa56449ff69eb8
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Sourav Goswami
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,22 +1,24 @@
|
|
1
1
|
# LinuxStat
|
2
2
|

|
3
3
|
|
4
|
-
For reading the HTML version, visit [linux_stat](https://souravgoswami.github.io/linux_stat/).
|
4
|
+
For reading the eyecandy HTML version, visit [linux_stat](https://souravgoswami.github.io/linux_stat/).
|
5
5
|
|
6
6
|
LinuxStat lets you read status of a Linux system. It can show you cpu stats and usages, memory stats and usages, swap stats and usages, battery usage, bios info, kernel details, local ip, os details and parse os-release + lsb-release, etc.
|
7
7
|
|
8
8
|
It only works for Linux, and detecting the OS is upto the user of this gem.
|
9
9
|
|
10
|
+
---
|
11
|
+
|
10
12
|
## Dependencies:
|
11
13
|
+ You need to have the C compile to be able to compile the C extension.
|
12
14
|
On Arch Linux:
|
13
15
|
```
|
14
|
-
# pacman -S gcc
|
16
|
+
# pacman -S gcc make
|
15
17
|
```
|
16
18
|
|
17
19
|
On Debian based systems:
|
18
20
|
```
|
19
|
-
# apt install gcc
|
21
|
+
# apt install gcc build-essential
|
20
22
|
```
|
21
23
|
|
22
24
|
+ You might also require ruby-dev in Debian based systems which provides support for ruby.h header file:
|
@@ -26,6 +28,8 @@ On Debian based systems:
|
|
26
28
|
|
27
29
|
+ Once your are done, and you can compile the C file, you can follow the installation!
|
28
30
|
|
31
|
+
---
|
32
|
+
|
29
33
|
## Installation
|
30
34
|
|
31
35
|
Add this line to your application's Gemfile:
|
@@ -99,7 +103,7 @@ LinuxStat::CPU.count
|
|
99
103
|
=> 4
|
100
104
|
|
101
105
|
LinuxStat::CPU.cur_freq
|
102
|
-
=> [
|
106
|
+
=> [1863999, 1934584, 1996927, 1978102]
|
103
107
|
|
104
108
|
LinuxStat::CPU.max_freq
|
105
109
|
=> [2000000, 2000000, 2000000, 2000000]
|
@@ -108,48 +112,48 @@ LinuxStat::CPU.model
|
|
108
112
|
=> "Intel(R) Core(TM) i3-6006U CPU @ 2.00GHz"
|
109
113
|
|
110
114
|
LinuxStat::CPU.stat
|
111
|
-
=> {0=>
|
115
|
+
=> {0=>20.0, 1=>50.0, 2=>0.0, 3=>0.0, 4=>0.0}
|
112
116
|
|
113
117
|
LinuxStat::CPU.total_usage
|
114
|
-
=>
|
118
|
+
=> 0.0
|
115
119
|
|
116
120
|
LinuxStat::CPU.usage
|
117
|
-
=>
|
121
|
+
=> 25.0
|
118
122
|
|
119
123
|
LinuxStat::CPU.usages
|
120
|
-
=> {0=>
|
124
|
+
=> {0=>0.0, 1=>0.0, 2=>0.0, 3=>0.0, 4=>0.0}
|
121
125
|
|
122
126
|
```
|
123
127
|
|
124
128
|
### LinuxStat::Filesystem
|
125
129
|
```
|
126
130
|
LinuxStat::Filesystem.available
|
127
|
-
=>
|
131
|
+
=> 34612666368
|
128
132
|
|
129
133
|
LinuxStat::Filesystem.free
|
130
|
-
=>
|
134
|
+
=> 34612666368
|
131
135
|
|
132
136
|
LinuxStat::Filesystem.stat
|
133
|
-
=> {:total=>119981191168, :free=>
|
137
|
+
=> {:total=>119981191168, :free=>34612666368, :used=>85368524800}
|
134
138
|
|
135
139
|
LinuxStat::Filesystem.stat_raw
|
136
|
-
=> {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>
|
140
|
+
=> {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>8450358, :block_avail_unpriv=>8450358, :inodes=>58612160, :free_inodes=>56582049, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
|
137
141
|
|
138
142
|
LinuxStat::Filesystem.total
|
139
143
|
=> 119981191168
|
140
144
|
|
141
145
|
LinuxStat::Filesystem.used
|
142
|
-
=>
|
146
|
+
=> 85368524800
|
143
147
|
|
144
148
|
```
|
145
149
|
|
146
150
|
### LinuxStat::Kernel
|
147
151
|
```
|
148
152
|
LinuxStat::Kernel.build_date
|
149
|
-
=> 2020-
|
153
|
+
=> 2020-11-20 07:44:55 +0000
|
150
154
|
|
151
155
|
LinuxStat::Kernel.build_date_string
|
152
|
-
=> "
|
156
|
+
=> "20 Nov 2020 07:44:55 +0000"
|
153
157
|
|
154
158
|
LinuxStat::Kernel.build_user
|
155
159
|
=> "souravgoswami@archlinux"
|
@@ -164,45 +168,60 @@ LinuxStat::Kernel.compiler_version
|
|
164
168
|
=> "10.2.0"
|
165
169
|
|
166
170
|
LinuxStat::Kernel.release
|
167
|
-
=> "5.9.
|
171
|
+
=> "5.9.9-xanmod1-1"
|
168
172
|
|
169
173
|
LinuxStat::Kernel.string
|
170
|
-
=> "Linux version 5.9.
|
174
|
+
=> "Linux version 5.9.9-xanmod1-1 (souravgoswami@archlinux) (gcc (GCC) 10.2.0, GNU ld (GNU Binutils) 2.35.1) #1 SMP PREEMPT Fri, 20 Nov 2020 07:44:55 +0000"
|
171
175
|
|
172
176
|
LinuxStat::Kernel.ticks
|
173
177
|
=> 100
|
174
178
|
|
175
179
|
LinuxStat::Kernel.version
|
176
|
-
=> "5.9.
|
180
|
+
=> "5.9.9-xanmod1-1"
|
177
181
|
|
178
182
|
```
|
179
183
|
|
180
184
|
### LinuxStat::Memory
|
181
185
|
```
|
182
186
|
LinuxStat::Memory.available
|
183
|
-
=>
|
187
|
+
=> 185060
|
184
188
|
|
185
189
|
LinuxStat::Memory.percent_available
|
186
|
-
=>
|
190
|
+
=> 4.82
|
187
191
|
|
188
192
|
LinuxStat::Memory.percent_used
|
189
|
-
=>
|
193
|
+
=> 95.18
|
190
194
|
|
191
195
|
LinuxStat::Memory.stat
|
192
|
-
=> {:total=>
|
196
|
+
=> {:total=>3836228, :used=>3651168, :available=>185060, :percent_used=>95.18, :percent_available=>4.82}
|
193
197
|
|
194
198
|
LinuxStat::Memory.total
|
195
|
-
=>
|
199
|
+
=> 3836228
|
196
200
|
|
197
201
|
LinuxStat::Memory.used
|
198
|
-
=>
|
202
|
+
=> 3651168
|
199
203
|
|
200
204
|
```
|
201
205
|
|
202
206
|
### LinuxStat::Mounts
|
203
207
|
```
|
208
|
+
LinuxStat::Mounts.device_stat
|
209
|
+
=> {:mountpoint=>"/", :total=>119981191168, :free=>34098106368, :available=>34098106368, :used=>85883084800, :percent_used=>71.58, :percent_free=>28.42, :percent_available=>28.42}
|
210
|
+
|
211
|
+
LinuxStat::Mounts.devices_stat
|
212
|
+
=> {"proc"=>{:mountpoint=>"/proc", :total=>0, :free=>0, :available=>0, :used=>0, :percent_used=>NaN, :percent_free=>NaN, :percent_available=>NaN}, "sys"=>{:mountpoint=>"/sys", :total=>0, :free=>0, :available=>0, :used=>0, :percent_used=>NaN, :percent_fre...
|
213
|
+
|
204
214
|
LinuxStat::Mounts.list
|
205
|
-
=> ["proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0", "sys /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0", "dev /dev devtmpfs rw,nosuid,relatime,size=
|
215
|
+
=> ["proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0", "sys /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0", "dev /dev devtmpfs rw,nosuid,relatime,size=1892904k,nr_inodes=473226,mode=755 0 0", "run /run tmpfs rw,nosuid,nodev,relatime,mode=755 0 0", "...
|
216
|
+
|
217
|
+
LinuxStat::Mounts.list_devices
|
218
|
+
=> ["proc", "sys", "dev", "run", "/dev/sda2", "securityfs", "tmpfs", "devpts", "tmpfs", "cgroup2", "cgroup", "pstore", "none", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "systemd-1", "deb...
|
219
|
+
|
220
|
+
LinuxStat::Mounts.list_devices_mount_point
|
221
|
+
=> {"proc"=>"/proc", "sys"=>"/sys", "dev"=>"/dev", "run"=>"/run", "/dev/sda2"=>"/", "securityfs"=>"/sys/kernel/security", "tmpfs"=>"/run/user/1000", "devpts"=>"/dev/pts", "cgroup2"=>"/sys/fs/cgroup/unified", "cgroup"=>"/sys/fs/cgroup/perf_event", "pstore...
|
222
|
+
|
223
|
+
LinuxStat::Mounts.mount_point
|
224
|
+
=> "/"
|
206
225
|
|
207
226
|
LinuxStat::Mounts.root
|
208
227
|
=> "/dev/sda2"
|
@@ -221,7 +240,7 @@ LinuxStat::Mounts.tmpfs
|
|
221
240
|
### LinuxStat::Net
|
222
241
|
```
|
223
242
|
LinuxStat::Net.ipv4_private
|
224
|
-
=> "192.168.0.
|
243
|
+
=> "192.168.0.102"
|
225
244
|
|
226
245
|
```
|
227
246
|
|
@@ -249,29 +268,29 @@ LinuxStat::OS.os_release
|
|
249
268
|
=> {:NAME=>"Arch Linux", :PRETTY_NAME=>"Arch Linux", :ID=>"arch", :BUILD_ID=>"rolling", :ANSI_COLOR=>"38;2;23;147;209", :HOME_URL=>"https://www.archlinux.org/", :DOCUMENTATION_URL=>"https://wiki.archlinux.org/", :SUPPORT_URL=>"https://bbs.archlinux.org/"...
|
250
269
|
|
251
270
|
LinuxStat::OS.uptime
|
252
|
-
=> {:hour=>9, :minute=>
|
271
|
+
=> {:hour=>9, :minute=>21, :second=>17.73}
|
253
272
|
|
254
273
|
```
|
255
274
|
|
256
275
|
### LinuxStat::Process
|
257
276
|
```
|
258
277
|
LinuxStat::Process.count
|
259
|
-
=>
|
278
|
+
=> 214
|
260
279
|
|
261
280
|
LinuxStat::Process.idle
|
262
|
-
=> [3, 4, 6, 9, 12, 23, 30, 37, 39, 49, 102, 103, 104, 106, 107, 108, 109, 110, 117, 118,
|
281
|
+
=> [3, 4, 6, 9, 12, 23, 30, 37, 39, 49, 102, 103, 104, 106, 107, 108, 109, 110, 117, 118, 119, 121, 131, 134, 140, 152, 153, 178, 180, 183, 184, 188, 192, 193, 194, 195, 196, 197, 199, 241, 287, 337, 341, 349, 3799, 3800, 3801, 3802, 3803, 3804, 17865, 2...
|
263
282
|
|
264
283
|
LinuxStat::Process.list
|
265
|
-
=> [1, 2, 3, 4, 6, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 37, 38, 39, 40, 41, 42, 46, 47, 48, 49, 50, 51, 52, 102, 103, 104, 106, 107, 108, 109, 110, 112, 114, 115, 117, 118,
|
284
|
+
=> [1, 2, 3, 4, 6, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 37, 38, 39, 40, 41, 42, 46, 47, 48, 49, 50, 51, 52, 102, 103, 104, 106, 107, 108, 109, 110, 112, 114, 115, 117, 118, 119, 121, 131, 134,...
|
266
285
|
|
267
286
|
LinuxStat::Process.names
|
268
287
|
=> {1=>"systemd", 2=>"kthreadd", 3=>"rcu_gp", 4=>"rcu_par_gp", 6=>"kworker/0:0H-kblockd", 9=>"mm_percpu_wq", 10=>"ksoftirqd/0", 11=>"rcuc/0", 12=>"rcu_preempt", 13=>"rcub/0", 14=>"migration/0", 15=>"idle_inject/0", 16=>"cpuhp/0", 17=>"cpuhp/1", 18=>"idle...
|
269
288
|
|
270
289
|
LinuxStat::Process.running
|
271
|
-
=> [
|
290
|
+
=> [24893]
|
272
291
|
|
273
292
|
LinuxStat::Process.sleeping
|
274
|
-
=> [1, 2, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 38, 40, 41, 42, 46, 47, 48, 50, 51, 52, 112, 114, 115,
|
293
|
+
=> [1, 2, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 38, 40, 41, 42, 46, 47, 48, 50, 51, 52, 112, 114, 115, 177, 179, 198, 228, 246, 310, 316, 323, 324, 325, 336, 338, 370, 371, 372, 373, 380, 384, 388, 399, 410,...
|
275
294
|
|
276
295
|
LinuxStat::Process.types
|
277
296
|
=> {1=>:sleeping, 2=>:sleeping, 3=>:idle, 4=>:idle, 6=>:idle, 9=>:idle, 10=>:sleeping, 11=>:sleeping, 12=>:idle, 13=>:sleeping, 14=>:sleeping, 15=>:sleeping, 16=>:sleeping, 17=>:sleeping, 18=>:sleeping, 19=>:sleeping, 20=>:sleeping, 21=>:sleeping, 23=>:i...
|
@@ -284,7 +303,7 @@ LinuxStat::Process.zombie
|
|
284
303
|
### LinuxStat::ProcessInfo
|
285
304
|
```
|
286
305
|
LinuxStat::ProcessInfo.cmdline
|
287
|
-
=> "ruby
|
306
|
+
=> "ruby exe/linuxstat.rb -md"
|
288
307
|
|
289
308
|
LinuxStat::ProcessInfo.command_name
|
290
309
|
=> "ruby"
|
@@ -295,17 +314,23 @@ LinuxStat::ProcessInfo.cpu_stat
|
|
295
314
|
LinuxStat::ProcessInfo.cpu_usage
|
296
315
|
=> 0.0
|
297
316
|
|
317
|
+
LinuxStat::ProcessInfo.gid
|
318
|
+
=> {:real=>1000, :effective=>1000, :saved_set=>1000, :filesystem_uid=>1000}
|
319
|
+
|
298
320
|
LinuxStat::ProcessInfo.last_executed_cpu
|
299
321
|
=> 2
|
300
322
|
|
301
323
|
LinuxStat::ProcessInfo.mem_stat
|
302
|
-
=> {:memory=>
|
324
|
+
=> {:memory=>9752, :virtual_memory=>79824, :resident_memory=>15564}
|
303
325
|
|
304
326
|
LinuxStat::ProcessInfo.memory
|
305
|
-
=>
|
327
|
+
=> 9752
|
328
|
+
|
329
|
+
LinuxStat::ProcessInfo.owner
|
330
|
+
=> "sourav"
|
306
331
|
|
307
332
|
LinuxStat::ProcessInfo.resident_memory
|
308
|
-
=>
|
333
|
+
=> 15564
|
309
334
|
|
310
335
|
LinuxStat::ProcessInfo.threads
|
311
336
|
=> 1
|
@@ -313,8 +338,11 @@ LinuxStat::ProcessInfo.threads
|
|
313
338
|
LinuxStat::ProcessInfo.total_io
|
314
339
|
=> {:read_bytes=>0, :write_bytes=>0}
|
315
340
|
|
341
|
+
LinuxStat::ProcessInfo.uid
|
342
|
+
=> {:real=>1000, :effective=>1000, :saved_set=>1000, :filesystem_uid=>1000}
|
343
|
+
|
316
344
|
LinuxStat::ProcessInfo.virtual_memory
|
317
|
-
=>
|
345
|
+
=> 79824
|
318
346
|
|
319
347
|
```
|
320
348
|
|
@@ -324,25 +352,25 @@ LinuxStat::Swap.any?
|
|
324
352
|
=> true
|
325
353
|
|
326
354
|
LinuxStat::Swap.available
|
327
|
-
=>
|
355
|
+
=> 1929788
|
328
356
|
|
329
357
|
LinuxStat::Swap.list
|
330
|
-
=> {"/dev/zram0"=>[:partition, 4194300,
|
358
|
+
=> {"/dev/zram0"=>[:partition, 4194300, 2264512, -2]}
|
331
359
|
|
332
360
|
LinuxStat::Swap.percent_available
|
333
|
-
=>
|
361
|
+
=> 46.01
|
334
362
|
|
335
363
|
LinuxStat::Swap.percent_used
|
336
|
-
=>
|
364
|
+
=> 53.99
|
337
365
|
|
338
366
|
LinuxStat::Swap.stat
|
339
|
-
=> {:total=>4194300, :used=>
|
367
|
+
=> {:total=>4194300, :used=>2264512, :available=>1929788, :percent_used=>53.99, :percent_available=>46.01}
|
340
368
|
|
341
369
|
LinuxStat::Swap.total
|
342
370
|
=> 4194300
|
343
371
|
|
344
372
|
LinuxStat::Swap.used
|
345
|
-
=>
|
373
|
+
=> 2264512
|
346
374
|
|
347
375
|
```
|
348
376
|
|
@@ -351,6 +379,21 @@ LinuxStat::Swap.used
|
|
351
379
|
LinuxStat::Sysconf.child_max
|
352
380
|
=> 2000000
|
353
381
|
|
382
|
+
LinuxStat::Sysconf.get_euid
|
383
|
+
=> 1000
|
384
|
+
|
385
|
+
LinuxStat::Sysconf.get_gid
|
386
|
+
=> 1000
|
387
|
+
|
388
|
+
LinuxStat::Sysconf.get_login
|
389
|
+
=> "sourav"
|
390
|
+
|
391
|
+
LinuxStat::Sysconf.get_uid
|
392
|
+
=> 1000
|
393
|
+
|
394
|
+
LinuxStat::Sysconf.get_user
|
395
|
+
=> "sourav"
|
396
|
+
|
354
397
|
LinuxStat::Sysconf.hostname_max
|
355
398
|
=> 64
|
356
399
|
|
@@ -358,7 +401,7 @@ LinuxStat::Sysconf.login_name_max
|
|
358
401
|
=> 256
|
359
402
|
|
360
403
|
LinuxStat::Sysconf.open_max
|
361
|
-
=>
|
404
|
+
=> 1024
|
362
405
|
|
363
406
|
LinuxStat::Sysconf.page_size_max
|
364
407
|
=> 4096
|
@@ -386,16 +429,368 @@ LinuxStat::Uname.nodename
|
|
386
429
|
=> "archlinux"
|
387
430
|
|
388
431
|
LinuxStat::Uname.release
|
389
|
-
=> "5.9.
|
432
|
+
=> "5.9.9-xanmod1-1"
|
390
433
|
|
391
434
|
LinuxStat::Uname.sysname
|
392
435
|
=> "Linux"
|
393
436
|
|
394
437
|
LinuxStat::Uname.version
|
395
|
-
=> "#1 SMP PREEMPT
|
438
|
+
=> "#1 SMP PREEMPT Fri, 20 Nov 2020 07:44:55 +0000"
|
439
|
+
|
440
|
+
```
|
441
|
+
|
442
|
+
### LinuxStat::User
|
443
|
+
```
|
444
|
+
LinuxStat::User.get_current_user
|
445
|
+
=> "sourav"
|
446
|
+
|
447
|
+
LinuxStat::User.get_euid
|
448
|
+
=> 1000
|
449
|
+
|
450
|
+
LinuxStat::User.get_gid
|
451
|
+
=> 1000
|
452
|
+
|
453
|
+
LinuxStat::User.get_login
|
454
|
+
=> "sourav"
|
455
|
+
|
456
|
+
LinuxStat::User.get_uid
|
457
|
+
=> 1000
|
458
|
+
|
459
|
+
LinuxStat::User.get_user
|
460
|
+
=> "sourav"
|
461
|
+
|
462
|
+
LinuxStat::User.gid_by_username
|
463
|
+
=> 1000
|
464
|
+
|
465
|
+
LinuxStat::User.gids
|
466
|
+
=> {:root=>0, :bin=>1, :daemon=>2, :mail=>12, :ftp=>11, :http=>33, :nobody=>65534, :dbus=>81, :"systemd-journal-remote"=>982, :"systemd-network"=>981, :"systemd-resolve"=>980, :"systemd-timesync"=>979, :"systemd-coredump"=>978, :uuidd=>68, :avahi=>977, :...
|
467
|
+
|
468
|
+
LinuxStat::User.home_by_gid
|
469
|
+
=> "/home/sourav"
|
470
|
+
|
471
|
+
LinuxStat::User.home_by_username
|
472
|
+
=> "/home/sourav"
|
473
|
+
|
474
|
+
LinuxStat::User.home_directories
|
475
|
+
=> {:root=>"/root", :bin=>"/", :daemon=>"/", :mail=>"/var/spool/mail", :ftp=>"/srv/ftp", :http=>"/srv/http", :nobody=>"/", :dbus=>"/", :"systemd-journal-remote"=>"/", :"systemd-network"=>"/", :"systemd-resolve"=>"/", :"systemd-timesync"=>"/", :"systemd-c...
|
476
|
+
|
477
|
+
LinuxStat::User.homes_by_uid
|
478
|
+
=> ["/home/sourav"]
|
479
|
+
|
480
|
+
LinuxStat::User.ids
|
481
|
+
=> {:root=>{:uid=>0, :gid=>0}, :bin=>{:uid=>1, :gid=>1}, :daemon=>{:uid=>2, :gid=>2}, :mail=>{:uid=>8, :gid=>12}, :ftp=>{:uid=>14, :gid=>11}, :http=>{:uid=>33, :gid=>33}, :nobody=>{:uid=>65534, :gid=>65534}, :dbus=>{:uid=>81, :gid=>81}, :"systemd-journal...
|
482
|
+
|
483
|
+
LinuxStat::User.list
|
484
|
+
=> ["root", "bin", "daemon", "mail", "ftp", "http", "nobody", "dbus", "systemd-journal-remote", "systemd-network", "systemd-resolve", "systemd-timesync", "systemd-coredump", "uuidd", "avahi", "colord", "git", "lxdm", "polkitd", "rtkit", "usbmux", "sourav...
|
485
|
+
|
486
|
+
LinuxStat::User.uid_by_username
|
487
|
+
=> 1000
|
488
|
+
|
489
|
+
LinuxStat::User.uids
|
490
|
+
=> {:root=>0, :bin=>1, :daemon=>2, :mail=>8, :ftp=>14, :http=>33, :nobody=>65534, :dbus=>81, :"systemd-journal-remote"=>982, :"systemd-network"=>981, :"systemd-resolve"=>980, :"systemd-timesync"=>979, :"systemd-coredump"=>978, :uuidd=>68, :avahi=>977, :c...
|
491
|
+
|
492
|
+
LinuxStat::User.username_by_gid
|
493
|
+
=> "sourav"
|
494
|
+
|
495
|
+
LinuxStat::User.usernames_by_uid
|
496
|
+
=> ["sourav"]
|
497
|
+
|
498
|
+
```
|
499
|
+
|
500
|
+
---
|
501
|
+
|
502
|
+
## Note 1: Filesystem
|
503
|
+
|
504
|
+
Filesystem can take arguments. By default it's '/' or the root of the system...
|
505
|
+
|
506
|
+
But for the sake of example, to get the free disk space of /, you do:
|
507
|
+
|
508
|
+
```
|
509
|
+
$ irb
|
510
|
+
irb(main):001:0> require 'linux_stat'
|
511
|
+
=> true
|
512
|
+
|
513
|
+
irb(main):002:0> LinuxStat::Filesystem.free('/').fdiv(1024 ** 3).to_s << " GiB"
|
514
|
+
=> "35.666873931884766 GiB"
|
515
|
+
```
|
516
|
+
|
517
|
+
To see the free and total space of a thumbdrive:
|
518
|
+
|
519
|
+
```
|
520
|
+
$ irb
|
521
|
+
irb(main):001:0> require 'linux_stat'
|
522
|
+
=> true
|
523
|
+
|
524
|
+
irb(main):002:0> LinuxStat::Mounts.list.find { |x| x.include?('/run/media/sourav') }.split[1]
|
525
|
+
=> "/run/media/sourav/5c2b7af7-d4c3-4ab4-a035-06d18ffc8e6f"
|
526
|
+
|
527
|
+
irb(main):003:0> thumbdrive = _
|
528
|
+
=> "/run/media/sourav/5c2b7af7-d4c3-4ab4-a035-06d18ffc8e6f"
|
529
|
+
|
530
|
+
irb(main):004:0> LinuxStat::Filesystem.free(thumbdrive).fdiv(1024 ** 3).to_s << " GiB"
|
531
|
+
=> "2.504791259765625 GiB"
|
532
|
+
|
533
|
+
irb(main):005:0> LinuxStat::Filesystem.total(thumbdrive).fdiv(1024 ** 3).to_s << " GiB"
|
534
|
+
=> "29.305004119873047 GiB"
|
535
|
+
```
|
536
|
+
|
537
|
+
## Note 2: ProcessInfo
|
538
|
+
|
539
|
+
All the methods LinuxStat::ProcessInfo can take an argument containing the Process ID of a process.
|
540
|
+
By default it's $$ or the PID of the current process, ruby, itself.
|
541
|
+
|
542
|
+
Example:
|
543
|
+
Say you want to see how much CPU Firefox is consuming, for that you have to do the following (firefox can create a lot of child process though):
|
544
|
+
|
545
|
+
1. Get the PID of Firefox:
|
546
|
+
```
|
547
|
+
LinuxStat::Process.names.find { |x| x[1].include? 'firefox' }[0]
|
548
|
+
=> 770 # but this differs all the time
|
549
|
+
```
|
550
|
+
|
551
|
+
2. Get the CPU usage:
|
552
|
+
```
|
553
|
+
$ irb
|
554
|
+
irb(main):001:0> require 'linux_stat'
|
555
|
+
=> true
|
556
|
+
|
557
|
+
irb(main):002:0> pid = LinuxStat::Process.names.find { |x| x[1].include? 'firefox' }[0]
|
558
|
+
=> 770
|
559
|
+
|
560
|
+
irb(main):003:0> LinuxStat::ProcessInfo.cpu_usage(pid: pid)
|
561
|
+
=> 0.0
|
562
|
+
|
563
|
+
irb(main):004:0> LinuxStat::ProcessInfo.cpu_usage(pid: pid)
|
564
|
+
=> 15.0
|
565
|
+
```
|
566
|
+
|
567
|
+
To get the memory usage of Firefox (for example):
|
568
|
+
|
569
|
+
```
|
570
|
+
$ irb
|
571
|
+
irb(main):001:0> require 'linux_stat'
|
572
|
+
=> true
|
573
|
+
|
574
|
+
irb(main):002:0> LinuxStat::ProcessInfo.mem_stat(LinuxStat::Process.names.find { |x| x[1].include? 'firefox'.freeze }[0])
|
575
|
+
=> {:memory=>468472, :virtual_memory=>4754080, :resident_memory=>814388}
|
576
|
+
```
|
577
|
+
|
578
|
+
To get ONLY the memory usage in MiB:
|
579
|
+
|
580
|
+
```
|
581
|
+
$ irb
|
582
|
+
irb(main):001:0> require 'linux_stat'
|
583
|
+
=> true
|
584
|
+
|
585
|
+
irb(main):002:0> LinuxStat::ProcessInfo.memory(LinuxStat::Process.names.find { |x| x[1].include? 'firefox'.freeze }[0]).fdiv(1024).round(2).to_s << " MiB"
|
586
|
+
=> "467.51 MiB"
|
587
|
+
```
|
588
|
+
|
589
|
+
## Note 3: FS
|
590
|
+
|
591
|
+
LinuxStat::FS module gives you the raw info in Hash collected from statvfs.
|
592
|
+
|
593
|
+
It's not documented above because it's not suggested to run this directly. But it shouldn't cause any issue. `LinuxStat::Filesystem.stat_raw(fs = '/')` does that automatically.
|
594
|
+
|
595
|
+
It always requires an argument, and it's very fast. It directly calls the C API without any intermediate Ruby code.
|
596
|
+
|
597
|
+
For example, to get the info about '/' or root:
|
598
|
+
|
599
|
+
```
|
600
|
+
$ irb
|
601
|
+
irb(main):001:0> require 'linux_stat'
|
602
|
+
=> true
|
603
|
+
|
604
|
+
irb(main):002:0> LinuxStat::FS.stat('/')
|
605
|
+
=> {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>9349843, :block_avail_unpriv=>9349843, :inodes=>58612160, :free_inodes=>56708247, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
|
606
|
+
|
607
|
+
irb(main):003:0> t = Time.now ; puts LinuxStat::FS.stat('/') ; Time.now - t
|
608
|
+
{:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>9349843, :block_avail_unpriv=>9349843, :inodes=>58612160, :free_inodes=>56708247, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
|
609
|
+
=> 5.0468e-05
|
610
|
+
```
|
611
|
+
|
612
|
+
To learn more about them, just run ri and the method name. To see all available methods.
|
613
|
+
|
614
|
+
## Note 4: User
|
615
|
+
Most of the LinuxStat::User supports arguments.
|
616
|
+
|
617
|
+
For example, to get a user's home by the username:
|
618
|
+
|
619
|
+
```
|
620
|
+
$ irb
|
621
|
+
irb(main):001:0> require 'linux_stat'
|
622
|
+
=> true
|
623
|
+
|
624
|
+
irb(main):002:0> LinuxStat::User.home_by_username('root')
|
625
|
+
=> "/root"
|
626
|
+
|
627
|
+
irb(main):003:0> LinuxStat::User.home_by_username('ftp')
|
628
|
+
=> "/srv/ftp"
|
629
|
+
|
630
|
+
irb(main):004:0> LinuxStat::User.home_by_username('mail')
|
631
|
+
=> "/var/spool/mail"
|
632
|
+
```
|
633
|
+
|
634
|
+
Or to get the user's home by the GID/UID:
|
635
|
+
|
636
|
+
```
|
637
|
+
$ irb
|
638
|
+
irb(main):001:0> require 'linux_stat'
|
639
|
+
=> true
|
640
|
+
|
641
|
+
irb(main):002:0> LinuxStat::User.homes_by_uid(1001)
|
642
|
+
=> ["/home/userx", "/home/userz"]
|
643
|
+
|
644
|
+
irb(main):003:0> LinuxStat::User.homes_by_uid(1000)
|
645
|
+
=> ["/home/sourav"]
|
646
|
+
|
647
|
+
irb(main):004:0> LinuxStat::User.home_by_gid(1001)
|
648
|
+
=> "/home/userx"
|
649
|
+
|
650
|
+
irb(main):005:0> LinuxStat::User.home_by_gid(1000)
|
651
|
+
=> "/home/sourav"
|
652
|
+
|
653
|
+
irb(main):006:0> LinuxStat::User.home_by_gid(0)
|
654
|
+
=> "/root"
|
655
|
+
```
|
656
|
+
|
657
|
+
Or to get the UID/GID by username:
|
658
|
+
|
659
|
+
```
|
660
|
+
$ irb
|
661
|
+
irb(main):001:0> require 'linux_stat'
|
662
|
+
=> true
|
663
|
+
|
664
|
+
irb(main):002:0> LinuxStat::User.uid_by_username('root')
|
665
|
+
=> 0
|
666
|
+
|
667
|
+
irb(main):003:0> LinuxStat::User.uid_by_username('ftp')
|
668
|
+
=> 14
|
669
|
+
|
670
|
+
irb(main):004:0> LinuxStat::User.gid_by_username('ftp')
|
671
|
+
=> 11
|
672
|
+
|
673
|
+
irb(main):005:0> LinuxStat::User.gid_by_username('InvalidUser')
|
674
|
+
=> nil
|
675
|
+
```
|
676
|
+
|
677
|
+
Or to get the current user (in docker for example):
|
678
|
+
|
679
|
+
```
|
680
|
+
$ irb
|
681
|
+
irb(main):001:0> require 'linux_stat'
|
682
|
+
=> true
|
683
|
+
|
684
|
+
irb(main):002:0> LinuxStat::User.get_current_user
|
685
|
+
=> "x"
|
686
|
+
|
687
|
+
irb(main):003:0> LinuxStat::User.get_user
|
688
|
+
=> "x"
|
689
|
+
|
690
|
+
irb(main):004:0> LinuxStat::User.get_login
|
691
|
+
=> ""
|
692
|
+
```
|
693
|
+
|
694
|
+
Right, the get_login() can return an empty string. But LinuxStat::User.get_user also aliased as LinuxStat::User.get_current_user shouldn't return an empty string under most circumstances.
|
695
|
+
|
696
|
+
## Note 5: PrettifyBytes
|
697
|
+
Often times we need to work with KB, MB GB, TB, or KiB, MiB, GiB, TiB, etc.
|
698
|
+
And we need some work to convert bytes to those units.
|
699
|
+
Because LinuxStat provides a lot of data in bytes, and kilobytes, it's quite tedious to convert them all the time.
|
700
|
+
To avoid such duplication, it comes with a PrettifyBytes module.
|
701
|
+
|
702
|
+
For example, to convert bytes to decimal suffixes:
|
703
|
+
|
704
|
+
```
|
705
|
+
$irb
|
706
|
+
irb(main):001:0> require 'linux_stat'
|
707
|
+
=> true
|
708
|
+
|
709
|
+
irb(main):002:0> LinuxStat::PrettifyBytes.convert_decimal(1000)
|
710
|
+
=> "1.00 kilobyte"
|
711
|
+
|
712
|
+
irb(main):003:0> LinuxStat::PrettifyBytes.convert_decimal(10000)
|
713
|
+
=> "10.00 kilobytes"
|
714
|
+
|
715
|
+
irb(main):004:0> LinuxStat::PrettifyBytes.convert_decimal(100000)
|
716
|
+
=> "100.00 kilobytes"
|
717
|
+
|
718
|
+
irb(main):005:0> LinuxStat::PrettifyBytes.convert_decimal(10 ** 13)
|
719
|
+
=> "10.00 terabytes"
|
720
|
+
```
|
721
|
+
|
722
|
+
To convert bytes to binary suffixes:
|
723
|
+
|
724
|
+
```
|
725
|
+
irb(main):006:0> LinuxStat::PrettifyBytes.convert_binary(1000)
|
726
|
+
=> "1000.00 bytes"
|
727
|
+
|
728
|
+
irb(main):007:0> LinuxStat::PrettifyBytes.convert_binary(10000)
|
729
|
+
=> "9.77 kibibytes"
|
730
|
+
|
731
|
+
irb(main):008:0> LinuxStat::PrettifyBytes.convert_binary(100000)
|
732
|
+
=> "97.66 kibibytes"
|
396
733
|
|
734
|
+
irb(main):009:0> LinuxStat::PrettifyBytes.convert_binary(10 ** 13)
|
735
|
+
=> "9.09 tebibytes"
|
397
736
|
```
|
398
737
|
|
738
|
+
To convert them to short Metric decimal suffixes:
|
739
|
+
|
740
|
+
```
|
741
|
+
irb(main):010:0> LinuxStat::PrettifyBytes.convert_short_decimal(1000)
|
742
|
+
=> "1.00 kB"
|
743
|
+
|
744
|
+
irb(main):011:0> LinuxStat::PrettifyBytes.convert_short_decimal(10000)
|
745
|
+
=> "10.00 kB"
|
746
|
+
|
747
|
+
irb(main):012:0> LinuxStat::PrettifyBytes.convert_short_decimal(100000)
|
748
|
+
=> "100.00 kB"
|
749
|
+
|
750
|
+
irb(main):013:0> LinuxStat::PrettifyBytes.convert_short_decimal(10 ** 13)
|
751
|
+
=> "10.00 TB"
|
752
|
+
```
|
753
|
+
|
754
|
+
To convert them to short IEC binary suffixes:
|
755
|
+
|
756
|
+
```
|
757
|
+
irb(main):014:0> LinuxStat::PrettifyBytes.convert_short_binary(1000)
|
758
|
+
=> "1000 B"
|
759
|
+
|
760
|
+
irb(main):015:0> LinuxStat::PrettifyBytes.convert_short_binary(10000)
|
761
|
+
=> "9.77 KiB"
|
762
|
+
|
763
|
+
irb(main):016:0> LinuxStat::PrettifyBytes.convert_short_binary(100000)
|
764
|
+
=> "97.66 KiB"
|
765
|
+
|
766
|
+
irb(main):017:0> LinuxStat::PrettifyBytes.convert_short_binary(10 ** 13)
|
767
|
+
=> "9.09 TiB"
|
768
|
+
```
|
769
|
+
|
770
|
+
It can support values upto hundreds of yottabytes and yobibytes, or yb and yib. You can also do stuff like:
|
771
|
+
|
772
|
+
```
|
773
|
+
$ irb
|
774
|
+
irb(main):001:0> require 'linux_stat'
|
775
|
+
=> true
|
776
|
+
|
777
|
+
irb(main):002:0> LinuxStat::PrettifyBytes.convert_short_decimal(LinuxStat::Mounts.device_stat('/dev/sdb1')[:total])
|
778
|
+
=> "31.47 GB"
|
779
|
+
|
780
|
+
irb(main):003:0> LinuxStat::PrettifyBytes.convert_short_binary(LinuxStat::Mounts.device_stat('/dev/sdb1')[:total])
|
781
|
+
=> "29.31 GiB"
|
782
|
+
|
783
|
+
irb(main):004:0> LinuxStat::PrettifyBytes.convert_short_binary(LinuxStat::Mounts.device_stat('/dev/sdb1')[:used])
|
784
|
+
=> "26.80 GiB"
|
785
|
+
|
786
|
+
irb(main):005:0> LinuxStat::PrettifyBytes.convert_short_binary(LinuxStat::Mounts.device_stat('/dev/sdb1')[:available])
|
787
|
+
=> "2.51 GiB"
|
788
|
+
```
|
789
|
+
|
790
|
+
Read the ri documentation for more info.
|
791
|
+
|
792
|
+
---
|
793
|
+
|
399
794
|
## Return Types
|
400
795
|
+ In general, if a method returns either a Float or a Integer or a Time, it will return a Float or Integer or Time in all cases. But if the status isn't available, it will return nil.
|
401
796
|
|
@@ -409,6 +804,8 @@ LinuxStat::Uname.version
|
|
409
804
|
|
410
805
|
If some error is *raised* it should be reported as a bug.
|
411
806
|
|
807
|
+
---
|
808
|
+
|
412
809
|
## Ruby on Rails
|
413
810
|
|
414
811
|
1. Just add `gem linux_stat`:
|
@@ -421,48 +818,64 @@ You can use LinuxStat directly in rails.
|
|
421
818
|
|
422
819
|

|
423
820
|
|
821
|
+
---
|
424
822
|
|
425
823
|
## Android
|
426
824
|
|
427
825
|
LinuxStat does support Android OS. But it's not rigorously tested on all device like android apps.
|
428
826
|
|
429
827
|
But in Termux you can just run LinuxStat without facing issues.
|
828
|
+
Note that the CPU count can differ due to hotplugging feature. So if you see the CPU count changes, there's not really nothing to do about that.
|
430
829
|
|
431
830
|

|
432
831
|
|
433
832
|
Issues regarding running LinuxStat on termux are also welcomed.
|
434
833
|
|
834
|
+
---
|
835
|
+
|
435
836
|
## Development
|
837
|
+
After checking out the repo, compile and install this gem onto your local machine with `bundle exec rake install`
|
838
|
+
|
839
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
436
840
|
|
437
|
-
|
841
|
+
To test all modules, run `rake install` and then `exe/linuxstat.rb`. Also check "Testing" below.
|
438
842
|
|
439
|
-
|
843
|
+
---
|
440
844
|
|
441
845
|
## Testing
|
442
|
-
Like other gems, this doesn't have a test like RSpec.
|
846
|
+
Like other gems, this doesn't have a test like RSpec.
|
847
|
+
|
848
|
+
We suggest using the exe/linuxstat.rb file on various Linux systems to test.
|
849
|
+
|
850
|
+
First you need to execute `bundle exec rake install` to compile and install this gem.
|
851
|
+
|
443
852
|
If you need to test a specific module, say the CPU, just run it like this:
|
444
853
|
|
445
854
|
```
|
446
|
-
$ ruby
|
855
|
+
$ ruby exe/linuxstat.rb CPU
|
447
856
|
```
|
448
857
|
|
449
858
|
Or:
|
450
859
|
```
|
451
|
-
$ ruby
|
860
|
+
$ ruby exe/linuxstat.rb cpu
|
452
861
|
```
|
453
862
|
|
454
863
|
That is, the argument passed is not case-sensitive.
|
455
864
|
But if the argument passed isn't available and outright wrong, it will run all the module methods. For example, you can't do:
|
456
865
|
|
457
866
|
```
|
458
|
-
$ ruby
|
867
|
+
$ ruby exe/linuxstat.rb upc
|
459
868
|
```
|
460
869
|
This is not a valid module and can't be run.
|
461
870
|
|
871
|
+
---
|
872
|
+
|
462
873
|
## Contributing
|
463
874
|
|
464
875
|
Bug reports and pull requests are welcome on GitHub at https://github.com/Souravgoswami/linux_stat.
|
465
876
|
|
877
|
+
---
|
878
|
+
|
466
879
|
## License
|
467
880
|
|
468
881
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|