Skip to content

Commit

Permalink
refactor cpu load cisco
Browse files Browse the repository at this point in the history
  • Loading branch information
babos77 committed Sep 15, 2021
1 parent 2025ed3 commit bb54e2e
Show file tree
Hide file tree
Showing 3 changed files with 420 additions and 6 deletions.
87 changes: 81 additions & 6 deletions config/codecommunicator/ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,92 @@ func (c *iosCommunicator) GetCPUComponentCPULoad(ctx context.Context) ([]device.
}
var cpus []device.CPU

res, _ := con.SNMP.SnmpClient.SNMPWalk(ctx, "1.3.6.1.4.1.9.9.109.1.1.1.1.5")
for _, snmpres := range res {
s, err := snmpres.GetValueString()
cpuLoad5minDeprecated, err1 := con.SNMP.SnmpClient.SNMPWalk(ctx, "1.3.6.1.4.1.9.9.109.1.1.1.1.5")
cpuLoad5min, err2 := con.SNMP.SnmpClient.SNMPWalk(ctx, "1.3.6.1.4.1.9.9.109.1.1.1.1.8")
if err1 != nil && err2 != nil {
return nil, errors.New("snmpwalks failed")
}

indices := make(map[string]int)

// save cpus load result for cpuLoad5min
for _, cpuLoadResponse := range cpuLoad5min {
cpu, err := c.getCPUBySNMPResponse(cpuLoadResponse)
if err != nil {
return nil, err
}
f, err := strconv.ParseFloat(s, 64)
cpus = append(cpus, cpu)
indices[cpuLoadResponse.GetOIDIndex()] = len(cpus) - 1 //current entry
}

// check deprecated cpu load oid. if one of the entries does not already exist in the cpu arr, add it
for _, cpuLoadResponseDeprecated := range cpuLoad5minDeprecated {
idx := cpuLoadResponseDeprecated.GetOIDIndex()

if _, ok := indices[idx]; ok {
continue
}

cpu, err := c.getCPUBySNMPResponse(cpuLoadResponseDeprecated)
if err != nil {
return nil, errors.Wrap(err, "failed to parse snmp response to float64")
return nil, err
}
cpus = append(cpus, device.CPU{Load: &f})
cpus = append(cpus, cpu)
indices[cpuLoadResponseDeprecated.GetOIDIndex()] = len(cpus) - 1 //current entry
}

// read out physical indices for cpus
physicalIndicesResult, err := con.SNMP.SnmpClient.SNMPWalk(ctx, "1.3.6.1.4.1.9.9.109.1.1.1.1.2")
if err != nil {
// cannot determine cpu physical indices, return cpu loads without labels
return cpus, nil
}

for _, physicalIndexResult := range physicalIndicesResult {
idx := physicalIndexResult.GetOIDIndex()
cpuIndex, ok := indices[idx]
if !ok {
continue
}

physicalIndex, err := physicalIndexResult.GetValueString()
if err != nil {
return nil, errors.Wrap(err, "failed to get physical index as string")
}

// 0 == physical entry not supported
if physicalIndex == "0" {
continue
}

physicalNameResponse, err := con.SNMP.SnmpClient.SNMPGet(ctx, "1.3.6.1.2.1.47.1.1.1.1.7."+physicalIndex)
if err != nil {
// cannot get physical name, continue
continue
}

physicalName, err := physicalNameResponse[0].GetValueString()
if err != nil {
return nil, errors.Wrap(err, "physical name is not a string")
}

cpus[cpuIndex].Label = &physicalName
}

return cpus, nil
}

func (c *iosCommunicator) getCPUBySNMPResponse(res network.SNMPResponse) (device.CPU, error) {
val, err := res.GetValueString()
if err != nil {
return device.CPU{}, errors.Wrap(err, "failed to get cpu load value")
}
valFloat, err := strconv.ParseFloat(val, 64)
if err != nil {
return device.CPU{}, errors.Wrap(err, "cpu load is not a float value")
}
return device.CPU{
Label: nil,
Load: &valFloat,
}, nil
}
Loading

0 comments on commit bb54e2e

Please sign in to comment.