Class: Kscript::KkMacStatusUtils

Inherits:
Base
  • Object
show all
Defined in:
lib/kscript/plugins/kk_mac_status_utils.rb

Instance Attribute Summary

Attributes inherited from Base

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#human_output?, inherited, #with_error_handling

Constructor Details

#initialize(*_args, **opts) ⇒ KkMacStatusUtils

Returns a new instance of KkMacStatusUtils.



12
13
14
# File 'lib/kscript/plugins/kk_mac_status_utils.rb', line 12

def initialize(*_args, **opts)
  super(**opts.merge(service: 'kk_mac_status'))
end

Class Method Details

.argumentsObject



96
97
98
# File 'lib/kscript/plugins/kk_mac_status_utils.rb', line 96

def self.arguments
  ''
end

.authorObject



108
109
110
# File 'lib/kscript/plugins/kk_mac_status_utils.rb', line 108

def self.author
  'kk'
end

.descriptionObject



92
93
94
# File 'lib/kscript/plugins/kk_mac_status_utils.rb', line 92

def self.description
  'Show macOS system resource monitor report.'
end

.groupObject



104
105
106
# File 'lib/kscript/plugins/kk_mac_status_utils.rb', line 104

def self.group
  'macos'
end

.usageObject



100
101
102
# File 'lib/kscript/plugins/kk_mac_status_utils.rb', line 100

def self.usage
  "kscript mac_status\nkscript mac_status --detail"
end

Instance Method Details

#checkObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/kscript/plugins/kk_mac_status_utils.rb', line 22

def check
  logger.kinfo('======= šŸŽ macOS System Resource Monitor Report =======')
  logger.kinfo("šŸ“… Date Time: #{Time.now}")
  logger.kinfo('')

  # Top 10 by CPU
  print_header('Top 10 Processes by CPU Usage')
  print_process_list(:cpu)
  # Top 10 by Memory
  print_header('Top 10 Processes by Memory Usage')
  print_process_list(:mem)

  # GPU Usage
  if `which powermetrics`.strip.empty?
    logger.kinfo("\nšŸ‘‰ GPU Usage:")
    logger.kwarn('āš ļø powermetrics not installed, please run: xcode-select --install')
  else
    logger.kinfo('===============================')
    logger.kinfo(' GPU Usage')
    logger.kinfo('===============================')
    gpu_output = `sudo powermetrics --samplers gpu_power -n 1 2>/dev/null`
    logger.kinfo(gpu_output)
  end

  # Network Connections
  logger.kinfo("\nšŸ‘‰ Top 10 Processes by Network Connections:")
  lsof_output = `lsof -i -nP | grep ESTABLISHED`
  counts = Hash.new(0)
  lsof_output.each_line do |line|
    process = line.split.first
    counts[process] += 1
  end
  counts.sort_by { |_, v| -v }.first(10).each do |proc, count|
    logger.kinfo("#{proc}: #{count} connections")
  end

  # System Overview
  logger.kinfo("\nšŸ‘‰ System Overview:")
  cpu_core = `sysctl -n hw.ncpu`.strip
  mem_size = `sysctl -n hw.memsize`.to_i / 1024 / 1024 / 1024
  logger.kinfo("CPU Cores: #{cpu_core}")
  logger.kinfo("Physical Memory: #{mem_size} GB")

  vm_stats = `vm_stat`
  vm_stats.each_line do |line|
    logger.kinfo(line) if line =~ /Pages (active|wired down|free):/
  end

  logger.kinfo("\nāœ… System resource check completed!")
end


73
74
75
76
77
78
# File 'lib/kscript/plugins/kk_mac_status_utils.rb', line 73

def print_header(title)
  logger.kinfo('')
  logger.kinfo('===============================')
  logger.kinfo(" #{title}")
  logger.kinfo('===============================')
end


80
81
82
83
84
85
86
87
88
89
90
# File 'lib/kscript/plugins/kk_mac_status_utils.rb', line 80

def print_process_list(sort_field)
  lines = `ps aux`.split("\n")
  lines.shift
  processes = lines.map { |line| line.split(/\s+/, 11) }
  index = sort_field == :cpu ? 2 : 3
  top = processes.sort_by { |p| -p[index].to_f }.first(10)
  logger.kinfo('USER       PID      %CPU  %MEM  COMMAND   ')
  top.each do |p|
    logger.kinfo(format('%-10s %-8s %-5s %-5s %-10s', p[0], p[1], p[2], p[3], p[10][0..30]))
  end
end

#runObject



16
17
18
19
20
# File 'lib/kscript/plugins/kk_mac_status_utils.rb', line 16

def run
  with_error_handling do
    check
  end
end