aboutsummaryrefslogtreecommitdiffstats
path: root/snmptrap/send_hb_trap
blob: efec02c4d21bbc58205d94eb34ff63925ef6f74c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# ============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)