# ============LICENSE_START======================================================= # Copyright (c) 2020 AT&T Intellectual Property. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============LICENSE_END========================================================= import time from pysnmp.hlapi import * from pysnmp.error import PySnmpError import argparse import os parser = argparse.ArgumentParser() parser.add_argument("dest", help="trap receiver hostname or ip address") parser.add_argument("port", help="trap receiver port number", type=int) args = parser.parse_args() env_service_name = os.getenv('SERVICE_NAME') env_service_tags = os.getenv('SERVICE_TAGS') if env_service_name is None or env_service_tags is None: now = time.strftime("%Y-%m-%d %H:%M:%S %Z", time.localtime()) print('%s Cannot get SERVICE_NAME and/or SERVICE_TAGS env vars' % now) if env_service_name is None: env_service_name = 'SERVICE_NAME N/A' if env_service_tags is None: env_service_tags = 'SERVICE_TAGS N/A' try: errorIndication, errorStatus, errorIndex, varBinds = next( sendNotification( SnmpEngine(), CommunityData('public', mpModel=1), UdpTransportTarget((args.dest, args.port)), ContextData(), 'trap', NotificationType( ObjectIdentity('.1.3.6.1.4.1.74.2.46.12.1.1') ).addVarBinds( ('.1.3.6.1.4.1.74.2.46.12.1.1.1', OctetString('onap trapd heartbeat')), ('.1.3.6.1.4.1.74.2.46.12.1.1.2', OctetString(time.ctime())), ('.1.3.6.1.4.1.74.2.46.12.1.1.3', OctetString(env_service_name)), ('.1.3.6.1.4.1.74.2.46.12.1.1.4', OctetString(env_service_tags)) ) ) ) if errorIndication: print(errorIndication) except PySnmpError as e: print("Exception from sendNotification: %s" % e)