Class: Gather_temperature

Inherits:
Object
  • Object
show all
Defined in:
rlib/gather_temperature.rb

Overview

Container for snmp queries retrieving temperature readings from TDC rack nodes IMM's

Constant Summary

TEMPERATURE =

should have a trailing .1, but returns nothing if we do?

'SNMPv2-SMI::enterprises.2.3.51.3.1.1.2.1.3'

Instance Method Summary (collapse)

Constructor Details

- (Gather_temperature) initialize(racks, snmp_community)

Create a Gather_temperature instance

Parameters:

  • racks (Pan_racks)

    collection of Pan_rack records, each holding an array of pan_host records

  • snmp_community (String)

    SNMP read community for the rack node's IMMs.



12
13
14
15
16
17
# File 'rlib/gather_temperature.rb', line 12

def initialize(racks, snmp_community)
  @results = {}
  @racks = racks.racks #racks in TDC Pan Pod, Hashed indexed by TDC rack designation
  @snmp_community = snmp_community
  walk
end

Instance Method Details

- (String, 0) get(manager, ifTable_columns)

SNMP query handler

Parameters:

  • manager (SNMP::Manager)

    From an SNMP::Manager.open (or new)

Returns:

  • (String, 0)

    Value from the SNMP query, or 0 if this fails



46
47
48
49
50
51
52
53
54
55
# File 'rlib/gather_temperature.rb', line 46

def get(manager, ifTable_columns)
  manager.walk(ifTable_columns) do |row|
    row.each do |vb| 
      if vb.name.to_s.split('.')[-1] == '1' #Looking for the .1 to indicate the temperature OID.
        return vb.value #Temperature
      end
    end
  end
  return 0 #No temperature.
end

- (Fixnum, String) process_host(host_name, ipmi_ip)

SNMP query wrapper to fetch temperature

Parameters:

  • hostname (String)

    Host name of the target node in the rack

  • ipmi_ip (String)

    The host's IMM ip address, which we do use

Returns:

  • (Fixnum, String)

    the temperature we fetched from the host (or 0, if we can't contact it)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'rlib/gather_temperature.rb', line 61

def process_host(host_name, ipmi_ip)
  if host_name == nil
    return 0 #No temperature
  else
    begin
      ifTable_columns = [ TEMPERATURE ] #should have a trailing .1, but returns nothing if we do?
      SNMP::Manager.open(:Host => ipmi_ip, :Community => "#{@snmp_community}", :Version => :SNMPv2c) do |manager|
        return get(manager, ifTable_columns) #The temperature
      end
    rescue SignalException => message
      $stderr.print "#{Signal} #{message}\n"
      exit -1
    rescue Exception => message
      $stderr.print "#{host_name} #{message}\n"
      return 0 #No temperature.
    end
  end
end

- (Object) to_json(file)

Generate json from the resulting SNMP queries

Parameters:

  • file (String)

    File name to write json output to



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'rlib/gather_temperature.rb', line 82

def to_json(file)
  File.open(file,"w") do |fd|
    fd.puts '{'
    fd.puts "\"datetime\": [\"#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}\"],"
    @results.each do |result, values|
      fd.print "\"#{result}\": ["
      values.each_with_index do |v,i|
        if i != 0
          fd.print "#{v},"
        end
      end
      fd.puts "],"
    end
    fd.puts '}'
  end
end

- (Object) walk

Process each rack's hosts, fetching the Ambient temperature



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'rlib/gather_temperature.rb', line 20

def walk
  @racks.each do |rack_name, rack| #for each rack in the Pan Pod
    @results[rack_name] = [0] #This is a filler for the First Array element. Rack U's use origin of 1. Arrays use an origin of 0.
    rack.hosts.each_with_index do |host, i| #For each U in the rack, we should have a Pan_host record
      if host == nil #Shouldn't be the case, as we filled in all U during the setup.
        $stderr.puts "Host[#{i}] == nil in Rack #{rack_name}"
        @results[rack_name] << 0
      elsif host.class != Pan_host
        $stderr.puts "Host[#{i}] not a Pan_host record: Rack #{rack_name}"
        @results[rack_name] << 0
      else 
        if i != 0 #No racks have U == 0, so we ignore these. 
          if host.rack_u != i #This isn't a new host, but a multi-U host, that we have already processed. (Assumes hosts labeled by lowest U)
            @results[rack_name] << @results[rack_name][host.rack_u] #Fill in host temperature from the base U of this host
          else
            @results[rack_name] << process_host(host.hostname, host.ipV4_m) #host is an array of values, which we expand into the params.
          end
        end
      end
    end
  end
end