Module: LinuxStat::Filesystem

Defined in:
lib/linux_stat/filesystem.rb

Overview

Shows various Filesystem related information of the current system.

Class Method Summary collapse

Class Method Details

.available(fs = ?..freeze) ⇒ Object

available(fs = ‘.’)

Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).

It returns the total free space in a disk in bytes.

It is to be noted that free is not same as available.

Available returns the size of free blocks for unpriviledged users.

If the stat can’t be acquired, this method will return an empty Hash.



94
95
96
97
98
99
# File 'lib/linux_stat/filesystem.rb', line 94

def available(fs = ?..freeze)
  s = stat_raw(fs)
  return nil if s.empty?
  s.default = 0
  s[:block_size] * s[:block_avail_unpriv]
end

.free(fs = ?..freeze) ⇒ Object

free(fs = ‘.’)

Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).

It returns the total free space in a disk in bytes.

It is to be noted that free is not same as available.

Free returns the size of free blocks.

If the stat can’t be acquired, this method will return an empty Hash.



60
61
62
63
64
65
# File 'lib/linux_stat/filesystem.rb', line 60

def free(fs = ?..freeze)
  s = stat_raw(fs)
  return nil if s.empty?
  s.default = 0
  s[:block_size] * s[:block_free]
end

.io_total(path = LinuxStat::Mounts.root) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/linux_stat/filesystem.rb', line 118

def io_total(path = LinuxStat::Mounts.root)
  p = File.split(path)[-1]
  _io_total = LinuxStat::FS.total_io(p)

  return {} if _io_total.empty?

  sector_size = LinuxStat::FS.sectors(path)
  return {} unless sector_size

  {
    read: _io_total[0] &.*(sector_size),
    write: _io_total[1] &.*(sector_size),
  }
end

.io_usage(path = LinuxStat::Mounts.root, interval = 0.1) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/linux_stat/filesystem.rb', line 133

def io_usage(path = LinuxStat::Mounts.root, interval = 0.1)
  p = File.split(path)[-1]

  measure1 = LinuxStat::FS.total_io(p)
  sleep(interval)
  measure2 = LinuxStat::FS.total_io(p)

  return {} if measure1.empty? || measure2.empty?

  sector_size = LinuxStat::FS.sectors(path)
  return {} unless sector_size

  m1r = measure1[0]
  m1w = measure1[1]

  m2r = measure2[0]
  m2w = measure2[1]

  {
    read: m2r.-(m1r).*(sector_size).fdiv(interval),
    write: m2w.-(m1w).*(sector_size).fdiv(interval)
  }
end

.sector_size(path = LinuxStat::Mounts.root) ⇒ Object Also known as: sectors



114
115
116
# File 'lib/linux_stat/filesystem.rb', line 114

def sector_size(path = LinuxStat::Mounts.root)
  LinuxStat::FS.sectors(path)
end

.stat(fs = ?..freeze) ⇒ Object

stat(fs = ‘.’)

Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).

  • It returns a Hash with the following info:

    1. total size of the device (in bytes)

    2. free space (in kilobytes)

    3. used space (in kilobytes)

In a hash format:

{:total=>119981191168, :free=>43155574784, :used=>76825616384, :available=>43155574784}

If the stat can’t be acquired, this method will return an empty Hash.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/linux_stat/filesystem.rb', line 21

def stat(fs = ?..freeze)
  s = stat_raw(fs)
  return {} if s.empty?
  s.default = 0

  {
    total: s[:block_size] * s[:blocks],
    free: s[:block_size] * s[:block_free],
    used: s[:blocks].-(s[:block_free]) * s[:block_size]
  }
end

.stat_raw(fs = ?..freeze) ⇒ Object

stat_raw(fs = ‘.’)

Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).

It returns a Hash with the following data (for example):

{:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>10535967, :block_avail_unpriv=>10535967, :inodes=>58612160, :free_inodes=>56718550, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}

If the stat can’t be acquired, this method will return an empty Hash.



110
111
112
# File 'lib/linux_stat/filesystem.rb', line 110

def stat_raw(fs = ?..freeze)
  LinuxStat::FS.stat(fs)
end

.total(fs = ?..freeze) ⇒ Object

total(fs = ‘.’)

Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).

It returns the total size of a given disk in bytes.

If the stat can’t be acquired, this method will return nil.



41
42
43
44
45
46
# File 'lib/linux_stat/filesystem.rb', line 41

def total(fs = ?..freeze)
  s = stat_raw(fs)
  return nil if s.empty?
  s.default = 0
  s[:block_size] * s[:blocks]
end

.used(fs = ?..freeze) ⇒ Object

used(fs = ‘.’)

Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).

It returns the used space of a given disk in bytes.

If the stat can’t be acquired, this method will return nil.



75
76
77
78
79
80
# File 'lib/linux_stat/filesystem.rb', line 75

def used(fs = ?..freeze)
  s = stat_raw(fs)
  return nil if s.empty?
  s.default = 0
  s[:blocks].-(s[:block_free]) * s[:block_size]
end