Module: GreenHat::Shell

Defined in:
lib/greenhat/shell.rb,
lib/greenhat/shell/cat.rb,
lib/greenhat/shell/log.rb,
lib/greenhat/shell/disk.rb,
lib/greenhat/shell/help.rb,
lib/greenhat/shell/gitlab.rb,
lib/greenhat/shell/memory.rb,
lib/greenhat/shell/report.rb,
lib/greenhat/shell/network.rb,
lib/greenhat/shell/process.rb,
lib/greenhat/shell/faststats.rb

Overview

CLI Helper

Defined Under Namespace

Modules: Cat, Disk, Faststats, Gitlab, Log, Memory, Network, Process

Class Method Summary collapse

Class Method Details

.aboutObject



54
55
56
57
58
59
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
92
93
94
95
96
# File 'lib/greenhat/shell/help.rb', line 54

def self.about
  puts "\u2500".pastel(:cyan) * 20
  puts "About GreenHat #{GreenHat::VERSION}".pastel(:yellow)
  puts "\u2500".pastel(:cyan) * 20

  puts 'TLDR; Put in SOS reports, run commands, and find stuffs'.pastel(:green)
  puts

  puts <<~BLOCK
    General overview (OS, Memory, Disk, GitLab)
    #{'report'.pastel(:bright_cyan)}

    Log Searching
    #{'log filter sidekiq/current --job_status=done --sort=duration_s,db_duration_s --slice=duration_s,db_duration_s --reverse'.pastel(:bright_cyan)}

    Read File(s) across SOS archives
    #{'cat uptime'.pastel(:bright_cyan)} or #{'cat mount etc/fstab'.pastel(:bright_cyan)}

  BLOCK

  puts 'What it does / How it works'.pastel(:blue)
  puts
  puts <<~BLOCK
    GreenHat is a support utility to enhance troubleshooting with GitLabSOS Reports and log files. Make it easy to find stuff

    Supplied input files are staged, unpacked, identified, and normalized.
    This enables other utilities to automatically find and present data. (Faststats, report, and etc)

  BLOCK

  puts 'Commands and Submodules'.pastel(:blue)
  puts
  puts <<~BLOCK
    Greenhat is broken down into different "modules". Each module has its own commands. For example: log, cat, and faststats.
    You can "cd" into or execute commands directly against with their names.

    - Direct: #{'log filter sidekiq/current'.pastel(:cyan)}
    - Or within: First #{'log'.pastel(:cyan)}, then #{'filter sidekiq/current'.pastel(:cyan)}

    You can find the list of commands and submodules of each with #{'help'.pastel(:yellow)}

  BLOCK
end

.cli_shortcutsObject

rubocop:enable Layout/LineLength



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/greenhat/shell/help.rb', line 37

def self.cli_shortcuts
  puts "\u2500".pastel(:cyan) * 25
  puts 'Nav / Keyboard Shortcuts'.pastel(:blue)
  puts "\u2500".pastel(:cyan) * 25
  puts <<~BLOCK
    | Hotkey              | Description             |
    | ------------------- | ----------------------- |
    | Ctrl + U            | Clear Input             |
    | Ctrl + A            | Go to beginning         |
    | Ctrl + E            | Go to End               |
    | Ctrl + Left/Right   | Move left/right by word |
    | Ctrl + D, Ctrl + Z  | Exit                    |
    | Ctrl + C, Shift Tab | Up one module           |
  BLOCK
  puts
end

.debugObject



45
46
47
48
49
50
51
52
53
# File 'lib/greenhat/shell.rb', line 45

def self.debug
  ENV['DEBUG'] = if ENV['DEBUG']
                   puts "GreenHat Debug Logging #{'Off'.pastel(:red)}"
                   nil
                 else
                   puts "GreenHat Debug Logging #{'On'.pastel(:green)}"
                   'true'
                 end
end

.dfObject



10
11
12
# File 'lib/greenhat/shell.rb', line 10

def self.df
  Disk.df
end

.freeObject



22
23
24
# File 'lib/greenhat/shell.rb', line 22

def self.free
  Memory.free
end

.helpObject

rubocop:disable Layout/LineLength



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/greenhat/shell/help.rb', line 5

def self.help
  puts 'Quickstart'.pastel(:blue)
  puts "  Commands are organized by submodule: #{'log'.pastel(:blue)}, #{'cat'.pastel(:blue)}, #{'faststats'.pastel(:blue)}"
  puts "  Use #{'help'.pastel(:bright_blue)} for available commands in each module"
  puts

  puts '  Example Commands'
  puts '    log filter sidekiq/current'.pastel(:yellow)
  puts '    disk free'.pastel(:yellow)
  puts

  puts 'Top Level Commands'.pastel(:blue)
  puts '  report'.pastel(:green)
  puts '    Show summary report of SOS Report. OS, CPU, Memory, Disk, and etc'
  puts '      --raw, no pagination'
  puts '      --archive=<redis/archive>, filter by archive name'
  puts

  puts '  ps,df,netstat,free,uptime,uname'.pastel(:green)
  puts '    Show common files from archives / Emulate terminal commands'
  puts

  puts '  Noisy Output'.pastel(:green)
  puts "    Use #{'quiet'.pastel(:blue)} or to #{'debug'.pastel(:blue)} to toggle greenhat logging"
  puts

  cli_shortcuts

  puts "See #{'about'.pastel(:bright_blue)} for more details about GreenHat"
end

.historyObject



83
84
85
86
87
# File 'lib/greenhat/shell.rb', line 83

def self.history
  Settings.cmd_history_clean.each_with_index do |line, i|
    puts "#{i.to_s.ljust(3).pastel(:magenta)} #{line}"
  end
end

.history_clearObject



79
80
81
# File 'lib/greenhat/shell.rb', line 79

def self.history_clear
  Settings.cmd_history_clear
end

.hostnameObject



71
72
73
# File 'lib/greenhat/shell.rb', line 71

def self.hostname
  Shell::Cat.default ['hostname']
end

.llObject



93
94
95
# File 'lib/greenhat/shell.rb', line 93

def self.ll
  GreenHat::Cli.help(false)
end

.load_localObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/greenhat/shell.rb', line 26

def self.load_local
  return false unless TTY::Prompt.new.yes?('Load local Omnibus GitLab Instance files?')

  archive_path = "#{$TMP}/#{Time.now.to_i}_local"
  Dir.mkdir(archive_path)
  # archive = Archive.new(name: 'local')
  archive = Archive.new(name: archive_path, path: archive_path)

  file_list = Dir['/var/log/gitlab/*/current'] + Dir['/var/log/gitlab/*/*.log']

  file_list.each do |file|
    next if File.size(file).zero?

    puts "- Loading #{file.pastel(:green)}"

    archive.things_create(file: file).setup
  end
end

.lsObject



89
90
91
# File 'lib/greenhat/shell.rb', line 89

def self.ls
  GreenHat::Cli.help(false)
end

.netstatObject



18
19
20
# File 'lib/greenhat/shell.rb', line 18

def self.netstat
  Network.netstat
end

.pryObject



4
5
6
7
8
# File 'lib/greenhat/shell.rb', line 4

def self.pry
  # rubocop:disable Lint/Debugger
  binding.pry
  # rubocop:enable Lint/Debugger
end

.ps(raw = {}) ⇒ Object



14
15
16
# File 'lib/greenhat/shell.rb', line 14

def self.ps(raw = {})
  Process.ps raw
end

.quietObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/greenhat/shell.rb', line 55

def self.quiet
  Cli.quiet!

  if Cli.quiet
    puts "GreenHat Quiet Logging #{'Off'.pastel(:red)}"
    nil
  else
    puts "GreenHat Quiet Logging #{'On'.pastel(:green)}"
    'true'
  end
end

.report(raw) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/greenhat/shell/report.rb', line 4

def self.report(raw)
  _files, flags, _args = Args.parse(raw)

  archives = if flags.archive
               Archive.all.select do |archive|
                 flags.archive.any? { |x| archive.name.include? x.to_s }
               end
             else
               Archive.all
             end

  ShellHelper.show(archives.map(&:report).map(&:show).flatten, flags)
end

.unameObject



75
76
77
# File 'lib/greenhat/shell.rb', line 75

def self.uname
  Shell::Cat.default ['uname']
end

.uptimeObject



67
68
69
# File 'lib/greenhat/shell.rb', line 67

def self.uptime
  Shell::Cat.default ['uptime']
end

.versionObject



97
98
99
100
# File 'lib/greenhat/shell.rb', line 97

def self.version
  puts "#{'GreenHat'.pastel(:green)}: #{GreenHat::VERSION.pastel(:blue)}"
  puts ' - https://gitlab.com/gitlab-com/support/toolbox/greenhat'.pastel(:cyan)
end