Class: Kanrisuru::Core::System::Parser::Last

Inherits:
Object
  • Object
show all
Defined in:
lib/kanrisuru/core/system/parsers/last.rb

Class Method Summary collapse

Class Method Details

.parse(command, opts) ⇒ Object



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
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kanrisuru/core/system/parsers/last.rb', line 9

def parse(command, opts)
  lines = command.to_a

  mapping = {}

  lines.each do |line|
    next if Kanrisuru::Util.blank?(line)
    next if line.include?('wtmp') || line.include?('btmp')

    line = line.gsub('  still logged in', '- still logged in') if line.include?('still logged in')

    values = line.split(/\s{2,}/, 4)
    user   = values[0]
    tty    = values[1]
    ip     = IPAddr.new(values[2])

    date_range = values[3]
    , logout = date_range.split(' - ')

     = parse_last_date() if 
    logout = parse_last_date(logout) if logout

    detail = Kanrisuru::Core::System::SessionDetail.new
    detail.tty = tty
    detail.ip_address = ip
    detail. = 
    detail.logout_at = logout

    detail.success = !Kanrisuru::Util.present?(opts[:failed_attemps])

    mapping[user] = Kanrisuru::Core::System::LoginUser.new(user, []) unless mapping.key?(user)

    mapping[user].sessions << detail
  end

  mapping.values
end

.parse_last_date(string) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kanrisuru/core/system/parsers/last.rb', line 47

def parse_last_date(string)
  tokens = string.split

  return if tokens.length < 4

  month_abbr = tokens[1]
  day = tokens[2]
  timestamp = tokens[3]
  year = tokens[4]

  DateTime.parse("#{day} #{month_abbr} #{year} #{timestamp}")
end