User Tools

Site Tools


onboarding:snmpmanager:trap_rule_template

This is an old revision of the document!


Trap Rule Template

This is a template example for creating trap rules. Trap rules should generally follow the structure laid out in this template with the undefined variables filled in with information from the trap. Please follow this structure as much as possible for a given technology. The logging guidelines are very important for reporting and analysis, so keep the exact text format of the statements annotated with 'Logging Rule'. Some of the lines of code in this example are very deliberate even though the might seem insignificant. So to avoid previously encountered but now known trap processing issues, so it is important to not try to re-factor unless you understand the trap processing domain very well.

com.errigal.snmpmanager.Trap trap ->
 
  /* Logging Rule: Output information for Technology and Trap rule name */
  String techName = "TECHNOLOGY_NAME"
  log.info "Trap-${trap.id}: Starting Trap Rule: ${techName}-${trap?.name}...."
 
  com.errigal.snmpmanager.wrapper.TrapWrapper trapWrapper = new com.errigal.snmpmanager.wrapper.TrapWrapper(trap)
 
  /* Extract and Define trap varbind details */
  String trapType = trapWrapper.getVarbind("trapType")
  String alarmName = trapWrapper.getVarbind("alarmName")
  String alarmText = trapWrapper.getVarbind("alarmText")
  String alarmIdentifier = trapWrapper.getVarbind("alarmIdentifier")
  boolean isClear = //Determine if clear trap
 
  /* Logging Rule: Log Varbinds Of your technology specific Trap */
  log.info "Trap ID-${trap.id}: Trap Type = ${trapType}"
  log.info "Trap ID-${trap.id}: Alarm Name = ${alarmName}"
  log.info "Trap ID-${trap.id}: Alarm Text = ${alarmText}"
  log.info "Trap ID-${trap.id}: Alarm Identifier = ${alarmPerceivedSeverity}"
 
  com.errigal.snmpmanager.knowledge.DasHub hub
  com.errigal.snmpmanager.knowledge.DasHub.withTransaction {
    hub = com.errigal.snmpmanager.knowledge.DasHub.findByIpAddress(trap?.ipAddress)
    if (!hub) {
      /* Logging Rule: Output information hub was not found and we are creating an orphan for it */
      log.warn "Trap-${trap.id}: Hub not found for ${trap?.ipAddress}, creating ORPHAN network element for this trap"
      com.errigal.snmpmanager.NetworkElement orphans = com.errigal.snmpmanager.NetworkElement.findByName("ORPHANS")
      com.errigal.snmpmanager.Technology technology = com.errigal.snmpmanager.Technology.findByName("${techName}")
      hub = new com.errigal.snmpmanager.knowledge.DasHub(
        name: "${techName} ${trap.ipAddress}",
        neType: "Controller",
        ipAddress: trap.ipAddress,
        onAir: true,
        showInMonitor: true,
        technology: "${techName}",
        assignedTechnology: technology,
        clusterName: "ORPHANS",
        parent: orphans
      )
      orphans.addToChildren(hub)
      hub.save(failOnError: true)
      orphans.save(failOnError: true)
    }
  }
 
 
  String neName = // Determine or parse Node Name If applicable
  boolean isHub = false //Assuming it a child  element, we are checking soon
 
  if(/* Tech specific determination if Trap Is Hub Trap */) {
    log.info "Trap ID-${trap.id}: Trap association being made with hub: ${hub?.name}"
    alarmingNe = hub
    isHub = true
  } else { /* Trap Is Child Level */
    /* Logging Rule: Output information for the name of the child element which is being attempted to be looked up under the controller */
    log.info "Trap ID-${trap.id}: Attempting To find network element: (${neName}) from under parent (${hub?.name})"
    alarmingNe = com.errigal.snmpmanager.wrapper.NetworkElementWrapper.findNetworkElement(hub.name, neName, false)
  }
 
 
  /* If alarming NE is null here, we could not find a child when we expected to find one */
  if (!alarmingNe) {
    /* Logging Rule: Output warning we could not find the element with the string name used */
    log.warn "Trap ID-${trap.id}: Network Element - ${neName} not found, sending device missing trap...."
    def missingUnitTrap = new com.errigal.snmpmanager.autodiscovery.trap.ErrigalMissingUnitTrap(hub, neName, "{techName}", trap.name, com.errigal.snmpmanager.trap.AlarmStatus.resolveFromString(alarmSeverity))
    missingUnitTrap.send("Hub: ${hub?.name}, Child: ${neName} -> Not found")
    /* Fallback to the Hub and send a device missing trap */
    isHub = true
    alarmingNe = hub
    /* Logging Rule: Output warning we are falling alarm back to the hub */
    log.warn "Trap ID-${trap.id}: Alarm fallback to hub for device missing issue...."
  }
 
/* Time to process trap to gts at this stage */
  com.errigal.snmpmanager.GeneralTrapSummary gts
  com.errigal.snmpmanager.GeneralTrapSummary.withTransaction {
    gts = new com.errigal.snmpmanager.GeneralTrapSummary()
    /* Logging Rule: Output information GTS creation is in progress */
    log.info "Trap ID-${trap.id}: Creating General Trap Summary...."
    gts.trap = trap
    gts.trapType = trapType
    gts.trapName = trap.name
    gts.networkElement = alarmingNe
    gts.context = alarmText
    gts.clearTrap = isClear
    gts.alarmStatus = alarmSeverity
    def alarmStatusEnum = com.errigal.snmpmanager.trap.AlarmStatus.resolveFromString(alarmSeverity)
    gts.alarmImpacting = alarmStatusEnum ? alarmStatusEnum.isAlarmImpacting() : false
    gts.clearReason = isClear ? "Clear Trap Received" : ""
    gts.alarmIdentifier = alarmIdentifier
    gts.summary = "${hub.name}${isHub == false ? ' - '+neName : ''}: ${alarmText}"
    gts.save(failOnError: true)
    /* Logging Rule: Output information GTS creation has completed */
    log.info "Trap ID-${trap.id}: Successfully General Trap Summary...."
  }
 
  /* We need to create new alarm or access an existing alarm if already created previously */
  com.errigal.snmpmanager.wrapper.GeneralTrapSummaryWrapper gtsw = new com.errigal.snmpmanager.wrapper.GeneralTrapSummaryWrapper(gts)
  com.errigal.snmpmanager.ActiveAlarm alarm = gtsw.getActiveAlarmWrapper()?.getDomain()
 
  /* If we have a clear, we need to find the alarm corresponsing to the clear */
  if(isClear) {
    if(alarm) {
      com.errigal.snmpmanager.ActiveAlarm.withTransaction {
      com.errigal.snmpmanager.ActiveAlarm activeAlarm = com.errigal.snmpmanager.ActiveAlarm.findById(alarm.id)
      activeAlarm.cleared = true
      activeAlarm.clearedDate = new Date()
      activeAlarm.clearedReason = (gts.getClearReason() ?: "")
      activeAlarm.clearingGTS = (com.errigal.snmpmanager.GeneralTrapSummary) gtsw.getDomain()
      activeAlarm.previousStatus = activeAlarm.status
      activeAlarm.status = "cleared"
      if (!activeAlarm?.save(flush: true)) {
         /* Logging Rule: Output error alarm clearing process has failed */
        log.error "Trap ID-${trap.id}: Failed To Save Active Alarm"
      } else {
       /* Logging Rule: Output information that alarm has been cleared */
        log.info "Trap ID-${trap.id}: Cleared Active Alarm - ${activeAlarm?.id}"
        int numTickets = activeAlarm.tickets.size()
        /* Logging Rule: Output information that ticket clearing process has begun */
        log.info "Trap ID-${trap.id}: Closing ${numTickets} ticket(s) on alarm #${activeAlarm.id}"
        activeAlarm.tickets.each { com.errigal.snmpmanager.RemoteTicket ticket -> 
          def ticketId = ticket.ticketIdentifier
          /* Logging Rule: Output information about which ticket is being cleared */
          log.info "Trap ID-${trap.id}: Closing ticket ID ${ticketId}"
          com.errigal.shared.remote.RemoteTicketCreator.instance.updateTicket(ticketId, "monitor", "*", "Alarm Clear Received", true,
          com.errigal.snmpmanager.TicketerConfiguration.findByName("default"))
          }
        }
      }
    } else { /* We have a clear but no alarm for it, it has to be ignored */
      /* Logging Rule: Output warning, we have a clear but no alarm for it, we possibly missed or mishandled the original alarm */
      log.warn "Trap ID-${trap.id}: Clear Received without existing Active Alarm, ignoring clear...."
    }
    gtsw.save()
  } else { /* We have an alarm which needs a ticket scheduled */
    if ((!alarm || (!alarm?.tickets && !(alarm?.repeatsReceived > 0))) && (gtsw.getDomain().alarmStatus.equalsIgnoreCase("CRITICAL") || gtsw.getDomain().alarmStatus.equalsIgnoreCase("MAJOR") || gtsw.getDomain().alarmStatus.equalsIgnoreCase("MINOR"))) {
      def ne = gtsw.getDomain().networkElement
      def parentNe = (isHub == true) ? ne : ne.parent
      String formNeName = (!isHub) ? hub.name + " - " + alarmingNe.name : hub.name
      String summaryString = formNeName + ":" + alarmText
      int ticketCreationDelay = 60
 
      /* Logging Rule: Output information ticket scheduling is in progress for Hub/Child name with given alarm */
      log.info "Trap ID-${trap.id}: Scheduling ticket: ${formNeName} - ${alarmText}"
 
      /* In order create a ticket certain values are needed */
      String workflowName = ne.workflow ? ne.workflow : "SNMP Trap"
      HashMap < String, String > ticketValues = new HashMap < String, String > ()
      ticketValues.put("WORKFLOW", workflowName)
      ticketValues.put("MARKET", parentNe.clusterName)
      ticketValues.put("STATUS", "Alarm Received")
      ticketValues.put("CREATOR", "monitor")
      ticketValues.put("SUMMARY", summaryString)
      ticketValues.put("VISIBILITY", parentNe.clusterName)
      ticketValues.put("PRIORITY", "5")
 
      /* Value which are needed to add SNMP Trap form to ticket */
      HashMap < String, String > formValues = new HashMap < String, String > ()
      formValues.put("FORM_NAME_PARAM", "SNMP Trap")
      formValues.put("ne_name", formNeName)
      formValues.put("alarm_id", gtsw.getDomain().id + "")
      formValues.put("trap_name", gtsw.getDomain().trapName)
      formValues.put("trap_severity", gtsw.getDomain().alarmStatus)
      formValues.put("trap_text", gtsw.getDomain().summary)
      formValues.put("source", java.net.InetAddress.localHost.hostName.split('\\.')[0] ?: '')
      formValues.put("context", gtsw.domain.alarmIdentifier)
      formValues.put("received_date", trap.date)
      formValues.put("received_time", new java.sql.Time(trap.date.time))
 
      HashMap < String, String > [] formsArray = new HashMap < String, String > [1]
      formsArray[0] = formValues
      final int TO_SEC = 60
 
      if (gtsw.getDomain().alarmStatus.equalsIgnoreCase("CRITICAL")) {
        ticketValues.put("PRIORITY", "2")
        ticketCreationDelay = 15
      } else if (gtsw.getDomain().alarmStatus.equalsIgnoreCase("MAJOR")) {
        ticketValues.put("PRIORITY", "3")
        ticketCreationDelay = 30
      } else if (gtsw.getDomain().alarmStatus.equalsIgnoreCase("MINOR")) {
        ticketValues.put("PRIORITY", "4")
        ticketCreationDelay = 60
      }
 
      gtsw.scheduleAlarmAndTicketForLaterWithForms(0, ticketCreationDelay*TO_SEC, ticketValues, formsArray, com.errigal.snmpmanager.TicketerConfiguration.findByName("default"))
      /* Logging Rule: Output information ticket scheduling is complete */
      log.info "Trap ID-${trap.id}: Successfully scheduled Alarm, Ticket scheduled in ${ticketCreationDelay} min: ${summaryString}"
    } else if (!alarm || !(alarm?.repeatsReceived > 0)) {
      /* Logging Rule: Output information alarm was scheduled with no ticket to be created */
      log.info "Trap ID-${trap.id}: Scheduling Alarm with no Ticket...."
      gtsw.scheduleAlarmForLater(10)
    } else {
      /* Logging Rule: Output information alarm was marked as repeat */
      log.info "Trap ID-${trap.id}: Just a repeat, nothing to do with this alarm.  Repeat has been marked on original ActiveAlarm."
    }
    gtsw.save()
  }
onboarding/snmpmanager/trap_rule_template.1744972405.txt.gz · Last modified: 2025/04/18 11:33 by 10.91.120.100