Difference between revisions of "Transforms - Nice Interface Events"

From Zenoss Wiki
Jump to: navigation, search
(Rewritten/refactored to be more performant)
Line 3: Line 3:
 
Event Class: Perf > Interface
 
Event Class: Perf > Interface
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
# Transform interface usage for /Perf/Interface threshold events into readable format
+
#Transform interface usage into readable format
 
+
import re
# Check if this actually is on an interface component with a speed attribute
+
if getattr(component, 'speed', None) is not None:
+
fs_id = device.prepId(evt.component)
 
+
for f in device.os.interfaces():
  # Extract the percent and utilization from the summary
+
    if f.id != fs_id: continue
  import re
+
  m = re.search("threshold of [^:]+: current value ([\d\.]+)", evt.message)
+
    # Extract the percent and utilization from the summary
  currentusage = (float(m.group(1))) * 8
+
    m = re.search("threshold of [^:]+: current value ([\d\.]+)", evt.message)
 
+
    if not m: continue
  # Convert to a percentage of speed. Like threshold, use default speed of 1e09 if speed attribute is not modelled
+
    currentusage = (float(m.group(1))) * 8
  p = (currentusage / (component.speed or 1e09)) * 100
+
    p = (currentusage / f.speed) * 100
 
+
    evtKey = evt.eventKey
  evtKey = evt.eventKey
+
 
    
 
    
  # Whether Input or Output Traffic
+
    # Whether Input or Output Traffic
  if evtKey == "ifInOctets_ifInOctets|high utilization" or evtKey == "ifHCInOctets_ifHCInOctets|high utilization":
+
    if evtKey == "ifInOctets_ifInOctets|high utilization" or evtKey == "ifHCInOctets_ifHCInOctets|high utilization":
      evtNewKey = "Input"
+
        evtNewKey = "Input"
  elif evtKey == "ifOutOctets_ifOutOctets|high utilization" or evtKey == "ifHCOutOctets_ifHCOutOctets|high utilization":
+
    elif evtKey == "ifOutOctets_ifOutOctets|high utilization" or evtKey == "ifHCOutOctets_ifHCOutOctets|high utilization":
      evtNewKey = "Output"
+
        evtNewKey = "Output"
 
+
  # Change the summary to display input/ouput and correct units
+
    # Check the speed to determine the appropriate conversion
  from Products.ZenUtils.Utils import convToUnits
+
    # Gbps utilization
  evt.summary = "High Interface " + evtNewKey + " Utilization: Currently %3.0f%% used (%s)" % (p, convToUnits(currentusage,1024,'bps'))
+
    if currentusage > 1000000000:
 
+
        Usage = currentusage / 1000000000
  # Update the message to the summary
+
        evt.summary = "High Interface " + evtNewKey + " Utilization: Currently %3.0f%% used (%3.2f Gbps)" % (p, Usage)
  evt.message = evt.summary
+
        evt.message = evt.summary
 +
 +
    # Mbps utilization
 +
    elif currentusage > 1000000:
 +
        Usage = currentusage / 1000000
 +
        evt.summary = "High Interface " + evtNewKey + " Utilization: Currently %3.0f%% used (%3.2f Mbps)" % (p, Usage)
 +
        evt.message = evt.summary
 +
 +
    # Kbps utilization
 +
    elif currentusage > 1000:
 +
        Usage = currentusage / 1000
 +
        evt.summary = "High Interface " + evtNewKey + " Utilization: Currently %3.0f%% used (%3.2f Kbps)" % (p, Usage)
 +
        evt.message = evt.summary
 +
 +
    # bps  utilization
 +
    elif currentusage < 1000:
 +
        Usage = currentusage
 +
        evt.summary = "High Interface " + evtNewKey + " Utilization: Currently %3.0f%% used (%3.2f bps)" % (p, Usage)
 +
        evt.message = evt.summary
 +
    break
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Transforms]]
 
[[Category:Transforms]]

Revision as of 17:38, 6 May 2014

This converts interface utilization into a percentage and Kb, Mb, Gb, etc. raw amount based on link speed.

Event Class: Perf > Interface

#Transform interface usage into readable format
import re
 
fs_id = device.prepId(evt.component)
for f in device.os.interfaces():
    if f.id != fs_id: continue
 
    # Extract the percent and utilization from the summary
    m = re.search("threshold of [^:]+: current value ([\d\.]+)", evt.message)
    if not m: continue
    currentusage = (float(m.group(1))) * 8
    p = (currentusage / f.speed) * 100
    evtKey = evt.eventKey
 
    # Whether Input or Output Traffic
    if evtKey == "ifInOctets_ifInOctets|high utilization" or evtKey == "ifHCInOctets_ifHCInOctets|high utilization":
        evtNewKey = "Input"
    elif evtKey == "ifOutOctets_ifOutOctets|high utilization" or evtKey == "ifHCOutOctets_ifHCOutOctets|high utilization":
        evtNewKey = "Output"
 
    # Check the speed to determine the appropriate conversion
    # Gbps utilization
    if currentusage > 1000000000:
        Usage = currentusage / 1000000000
        evt.summary = "High Interface " + evtNewKey + " Utilization: Currently %3.0f%% used (%3.2f Gbps)" % (p, Usage)
        evt.message = evt.summary
 
    # Mbps utilization
    elif currentusage > 1000000:
        Usage = currentusage / 1000000
        evt.summary = "High Interface " + evtNewKey + " Utilization: Currently %3.0f%% used (%3.2f Mbps)" % (p, Usage)
        evt.message = evt.summary
 
    # Kbps utilization
    elif currentusage > 1000:
        Usage = currentusage / 1000
        evt.summary = "High Interface " + evtNewKey + " Utilization: Currently %3.0f%% used (%3.2f Kbps)" % (p, Usage)
        evt.message = evt.summary
 
    # bps  utilization
    elif currentusage < 1000:
        Usage = currentusage
        evt.summary = "High Interface " + evtNewKey + " Utilization: Currently %3.0f%% used (%3.2f bps)" % (p, Usage)
        evt.message = evt.summary
    break