Class: GmonPacket

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/inputs/ganglia/gmondpacket.rb

Instance Method Summary collapse

Constructor Details

#initialize(packet) ⇒ GmonPacket

Returns a new instance of GmonPacket.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/logstash/inputs/ganglia/gmondpacket.rb', line 15

def initialize(packet)
  @xdr=XDR::Reader.new(StringIO.new(packet))

  # Read packet type
  type=@xdr.uint32
  case type
  when 128
    @type=:meta
  when 132
    @type=:heartbeat
  when 133..134
    @type=:data
  when 135
    @type=:gexec
  else
    @type=:unknown
  end
end

Instance Method Details

#data?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/logstash/inputs/ganglia/gmondpacket.rb', line 38

def data?
  @type == :data
end

#heartbeat?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/logstash/inputs/ganglia/gmondpacket.rb', line 34

def heartbeat?
  @type == :hearbeat
end

#meta?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/logstash/inputs/ganglia/gmondpacket.rb', line 42

def meta?
  @type == :meta
end

#name_to_type(name, metadata) ⇒ Object

Does lookup of metricname in metadata table to find the correct type



138
139
140
141
142
143
144
# File 'lib/logstash/inputs/ganglia/gmondpacket.rb', line 138

def name_to_type(name,)
  # Lookup this metric metadata
  meta=[name]
  return nil if meta.nil?

  return meta['type']
end

#parse_data(metadata) ⇒ Object

Parsing a data packet : type 133..135 Requires metadata to be available for correct parsing of the value



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/logstash/inputs/ganglia/gmondpacket.rb', line 87

def parse_data()
  data=Hash.new
  data['hostname']=@xdr.string

  metricname=@xdr.string
  data['name']=metricname

  data['spoof']=@xdr.uint32
  data['format']=@xdr.string

  metrictype=name_to_type(metricname,)

  if metrictype.nil?
    # Probably we got a data packet before a metadata packet
    #puts "Received datapacket without metadata packet"
    return nil
  end

  data['val']=parse_value(metrictype)

  # If we received a packet, last update was 0 time ago
  data['tn']=0
  return data
end

#parse_metadataObject

Parsing a metadata packet : type 128



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
78
79
80
81
82
83
# File 'lib/logstash/inputs/ganglia/gmondpacket.rb', line 47

def 
  meta=Hash.new
  meta['hostname']=@xdr.string
  meta['name']=@xdr.string
  meta['spoof']=@xdr.uint32
  meta['type']=@xdr.string
  meta['name2']=@xdr.string
  meta['units']=@xdr.string
  slope=@xdr.uint32

  case slope
  when 0
    meta['slope']= 'zero'
  when 1
    meta['slope']= 'positive'
  when 2
    meta['slope']= 'negative'
  when 3
    meta['slope']= 'both'
  when 4
    meta['slope']= 'unspecified'
  end

  meta['tmax']=@xdr.uint32
  meta['dmax']=@xdr.uint32
  nrelements=@xdr.uint32
  meta['nrelements']=nrelements
  unless nrelements.nil?
    extra={}
    for i in 1..nrelements
      name=@xdr.string
      extra[name]=@xdr.string
    end
    meta['extra']=extra
  end
  return meta
end

#parse_value(type) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/logstash/inputs/ganglia/gmondpacket.rb', line 114

def parse_value(type)
  value=:unknown
  case type
  when "int16"
    value=@xdr.int16
  when "uint16"
    value=@xdr.uint16
  when "uint32"
    value=@xdr.uint32
  when "int32"
    value=@xdr.int32
  when "float"
    value=@xdr.float32
  when "double"
    value=@xdr.float64
  when "string"
    value=@xdr.string
  else
    #puts "Received unknown type #{type}"
  end
  return value
end