Module: DeadBro::Collectors::Filesystem

Defined in:
lib/dead_bro/collectors/filesystem.rb

Overview

Filesystem collector exposes disk usage information using a best-effort approach. It prefers Ruby or Sys::Filesystem APIs when available and falls back to parsing ‘df` output.

Class Method Summary collapse

Class Method Details

.collectObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dead_bro/collectors/filesystem.rb', line 14

def collect
  paths = disk_paths
  return {paths: []} if paths.nil? || paths.empty?

  {
    paths: paths.map { |path| stats_for_path(path) }.compact
  }
rescue => e
  {
    error_class: e.class.name,
    error_message: e.message.to_s[0, 500]
  }
end

.df_stats(path) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/dead_bro/collectors/filesystem.rb', line 60

def df_stats(path)
  # Use POSIX df when available. Output format varies slightly by platform,
  # but we only depend on total and available in blocks.
  output = `df -k #{Shellwords.escape(path)} 2>/dev/null`
  lines = output.to_s.split("\n")
  return nil if lines.size < 2

  lines[0]
  data = lines[1]
  parts = data.split
  # POSIX df: Filesystem 1K-blocks Used Available Use% Mounted on
  total_kb = begin
    Integer(parts[1])
  rescue
    nil
  end
  avail_kb = begin
    Integer(parts[3])
  rescue
    nil
  end
  return nil unless total_kb && avail_kb

  {
    path: path,
    disk_total_bytes: total_kb * 1024,
    disk_free_bytes: avail_kb * 1024,
    disk_available_bytes: avail_kb * 1024
  }
rescue
  nil
end

.disk_pathsObject



28
29
30
31
32
33
34
35
36
# File 'lib/dead_bro/collectors/filesystem.rb', line 28

def disk_paths
  if DeadBro.configuration.respond_to?(:disk_paths)
    DeadBro.configuration.disk_paths || ["/"]
  else
    ["/"]
  end
rescue
  ["/"]
end

.stats_for_path(path) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/dead_bro/collectors/filesystem.rb', line 38

def stats_for_path(path)
  if defined?(Sys::Filesystem)
    sys_filesystem_stats(path)
  else
    df_stats(path)
  end
rescue
  nil
end

.sys_filesystem_stats(path) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dead_bro/collectors/filesystem.rb', line 48

def sys_filesystem_stats(path)
  stat = Sys::Filesystem.stat(path)
  {
    path: path,
    disk_total_bytes: stat.blocks * stat.block_size,
    disk_free_bytes: stat.blocks_available * stat.block_size,
    disk_available_bytes: stat.blocks_available * stat.block_size
  }
rescue
  nil
end