Class: Kanrisuru::Core::IP::Parser::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/kanrisuru/core/ip/parsers/base.rb

Direct Known Subclasses

Address, AddressLabel, Link, Maddress, Neighbour, Route, Rule, Version

Class Method Summary collapse

Class Method Details

.ip_address_label_result_json(rows) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/kanrisuru/core/ip/parsers/base.rb', line 9

def ip_address_label_result_json(rows)
  result = []

  rows.each do |row|
    new_row = Kanrisuru::Core::IP::IPAddressLabel.new(
      row['address'], row['prefixlen'], row['label']
    )

    result << new_row
  end

  result
end

.ip_address_label_result_parse(lines) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/kanrisuru/core/ip/parsers/base.rb', line 23

def ip_address_label_result_parse(lines)
  lines.map do |line|
    _, addr, _, label = line.split(/\s/)
    address, prefix_length = addr.split(%r{/})

    Kanrisuru::Core::IP::IPAddressLabel.new(address, prefix_length.to_i, label)
  end
end

.parse_address_info(row, line) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/kanrisuru/core/ip/parsers/base.rb', line 87

def parse_address_info(row, line)
  values = line.split
  address_info = Kanrisuru::Core::IP::IPAddressInfo.new

  values.each.with_index do |_, index|
    case values[index]
    when 'inet', 'inet6'
      address_info.family = values[index]
      address_info.ip = IPAddr.new(values[index + 1].split('/')[0])
    when 'brd'
      address_info.broadcast = values[index + 1]
    when 'scope'
      address_info.scope = values[index + 1]
    when 'dynamic'
      address_info.dynamic = true
    end
  end

  row.address_info << address_info
end

.parse_alias(row, line) ⇒ Object



32
33
34
35
# File 'lib/kanrisuru/core/ip/parsers/base.rb', line 32

def parse_alias(row, line)
  _, alias_string = line.split('alias')
  row.alias = alias_string
end

.parse_ip_maddr_name(row, line) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/kanrisuru/core/ip/parsers/base.rb', line 37

def parse_ip_maddr_name(row, line)
  index = line.match(/^\d+/).to_s.to_i
  _, name = line.split(/^\d+:\s/)

  row.index = index
  row.name = name
  row.maddr = []
end

.parse_ip_row(row, line) ⇒ Object



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
72
73
74
75
76
77
# File 'lib/kanrisuru/core/ip/parsers/base.rb', line 46

def parse_ip_row(row, line)
  index = line.match(/^\d+/).to_s.to_i
  _, string = line.split(/^\d+:\s/)
  name = string.match(/^\w+/).to_s
  _, string = string.split(/^\w+:\s/)
  _, flags, string = string.split(/(<.+>)\s/)

  flags = flags.gsub(/<?>?/, '')
  flags = flags.split(',')

  row.index = index
  row.name = name
  row.flags = flags

  values = string.split(/\s/)
  values.each.with_index do |_, i|
    case values[i]
    when 'mtu'
      row.mtu = values[i + 1].to_i
    when 'qdisc'
      row.qdisc = values[i + 1]
    when 'state'
      row.state = values[i + 1]
    when 'group'
      row.group = values[i + 1]
    when 'qlen'
      row.qlen = values[i + 1].to_i
    when 'mode'
      row.link_mode = values[i + 1]
    end
  end
end


79
80
81
82
83
84
85
# File 'lib/kanrisuru/core/ip/parsers/base.rb', line 79

def parse_link(row, line)
  _, string = line.split(%r{^link/})
  link_type, mac_address = string.split

  row.link_type = link_type
  row.mac_address = mac_address
end

.parse_rx(row, line) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/kanrisuru/core/ip/parsers/base.rb', line 121

def parse_rx(row, line)
  values = line.split

  bytes = values[0].to_i
  packets = values[1].to_i
  errors = values[2].to_i
  dropped = values[3].to_i
  over_errors = values[4].to_i
  multicast = values[5].to_i

  row.stats = Kanrisuru::Core::IP::IPStats.new
  row.stats.rx = Kanrisuru::Core::IP::IPStatRX.new(
    bytes,
    packets,
    errors,
    dropped,
    over_errors,
    multicast
  )
end

.parse_tx(row, line) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/kanrisuru/core/ip/parsers/base.rb', line 142

def parse_tx(row, line)
  values = line.split

  bytes = values[0].to_i
  packets = values[1].to_i
  errors = values[2].to_i
  dropped = values[3].to_i
  carrier_errors = values[4].to_i
  collisions = values[5].to_i

  row.stats.tx = Kanrisuru::Core::IP::IPStatTX.new(
    bytes,
    packets,
    errors,
    dropped,
    carrier_errors,
    collisions
  )
end

.parse_valid(row, line) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/kanrisuru/core/ip/parsers/base.rb', line 108

def parse_valid(row, line)
  values = line.split

  values.each.with_index do |_, index|
    case values[index]
    when 'valid_lft'
      row.address_info.last.valid_life_time = values[index + 1]
    when 'preferred_lft'
      row.address_info.last.preferred_life_time = values[index + 1]
    end
  end
end