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/network.rb,
lib/greenhat/shell/process.rb,
lib/greenhat/shell/reports.rb,
lib/greenhat/shell/faststats.rb

Overview

CLI Helper

Defined Under Namespace

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

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



70
71
72
73
74
75
76
77
78
# File 'lib/greenhat/shell.rb', line 70

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

.default(raw_list = []) ⇒ Object

Append Log Location so follow up commands are executed there



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

def self.default(raw_list = [])
  Cli.move_shell Shell::Log
  Log.default(raw_list)
end

.dfObject



31
32
33
# File 'lib/greenhat/shell.rb', line 31

def self.df
  Disk.df
end

.freeObject



47
48
49
# File 'lib/greenhat/shell.rb', line 47

def self.free
  Memory.free
end

.gitlab_rbObject



104
105
106
# File 'lib/greenhat/shell.rb', line 104

def self.gitlab_rb
  Shell::Cat.default ['gitlab/gitlab.rb', '--page']
end

.grep(raw_list = []) ⇒ Object



27
28
29
# File 'lib/greenhat/shell.rb', line 27

def self.grep(raw_list = [])
  Grep.grep(raw_list)
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



120
121
122
123
124
# File 'lib/greenhat/shell.rb', line 120

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



116
117
118
# File 'lib/greenhat/shell.rb', line 116

def self.history_clear
  Settings.cmd_history_clear
end

.hostnameObject



108
109
110
# File 'lib/greenhat/shell.rb', line 108

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

.ifconfigObject



100
101
102
# File 'lib/greenhat/shell.rb', line 100

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

.ip_addressObject



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

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

.llObject



135
136
137
# File 'lib/greenhat/shell.rb', line 135

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

.load_local(prompt = true) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/greenhat/shell.rb', line 51

def self.load_local(prompt = true)
  return false if prompt && !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: 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.empty?(file)

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

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

.ls(raw = []) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/greenhat/shell.rb', line 126

def self.ls(raw = [])
  if raw.empty?
    GreenHat::Cli.help(false)
  else
    Cli.move_shell Shell::Log
    Log.ls(raw)
  end
end

.netstatObject



43
44
45
# File 'lib/greenhat/shell.rb', line 43

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



39
40
41
# File 'lib/greenhat/shell.rb', line 39

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

.quietObject



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

def self.quiet
  Cli.quiet_toggle

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

.quit(_raw = {}) ⇒ Object



35
36
37
# File 'lib/greenhat/shell.rb', line 35

def self.quit(_raw = {})
  exit 0
end

.report(_raw = ['default']) ⇒ Object

Make report a default top layer command



12
13
14
15
16
17
18
# File 'lib/greenhat/shell/reports.rb', line 12

def self.report(_raw = ['default'])
  # Move
  Cli.move_shell Shell::Reports

  # Run
  Reports.default ['full']
end

.settingsObject



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

def self.settings
  Settings.configure_settings
end

.unameObject



112
113
114
# File 'lib/greenhat/shell.rb', line 112

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

.uptimeObject



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

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

.versionObject



139
140
141
142
# File 'lib/greenhat/shell.rb', line 139

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

.webObject



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

def self.web
  # Load Required Files
  require 'greenhat/web'

  GreenHat::Web.toggle
end