aboutsummaryrefslogtreecommitdiffstats
path: root/app/monitoring/checks/check_vnic_vconnector.py
blob: b0f96cda2ec72bdf07d56f62bcf12ab1094f1474 (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
66
67
68
69
70
71
72
#!/usr/bin/env python3
###############################################################################
# Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems)   #
# and others                                                                  #
#                                                                             #
# All rights reserved. This program and the accompanying materials            #
# are made available under the terms of the Apache License, Version 2.0       #
# which accompanies this distribution, and is available at                    #
# http://www.apache.org/licenses/LICENSE-2.0                                  #
###############################################################################

# find status of vnic-vconnector link
# vconnector object name defines name of bridge
# use "brctl showmacs <bridge>", then look for the MAC address

import re
import sys
import subprocess

from binary_converter import binary2str


if len(sys.argv) < 3:
    print('usage: ' + sys.argv[0] + ' <bridge> <mac_address>')
    exit(2)
bridge_name = str(sys.argv[1])
mac_address = str(sys.argv[2])

rc = 0

try:
    out = subprocess.check_output(["brctl showmacs " + bridge_name],
                                  stderr=subprocess.STDOUT,
                                  shell=True)
    out = binary2str(out)
    lines = out.splitlines()
    line_number = 1
    line = ''
    found = False
    while line_number < len(lines):
        line = lines[line_number]
        if mac_address in line:
            found = True
            break
        line_number += 1
    state_match = re.match('^\W+([A-Z]+)', line)
    if not found:
        rc = 2
        print('Error: failed to find MAC {}:\n{}\n'
              .format(mac_address, out))
    else:
        # grab "is local?" and "ageing timer" values
        line_parts = line.split()  # port, mac address, is local?, ageing timer
        is_local = line_parts[2]
        ageing_timer = line_parts[3]
        msg_format =\
            'vConnector bridge name: {}\n'\
            'vNIC MAC address: {}\n'\
            'is local: {}\n'\
            'ageing timer: {}\n'\
            'vNIC MAC address: {}\n'\
            'command: brctl showmacs {}\n'\
            'output:\n{}'
        msg = msg_format.format(bridge_name, mac_address, is_local,
                                ageing_timer, mac_address, bridge_name, out)
        print(msg)
except subprocess.CalledProcessError as e:
    print("Error finding MAC {}: {}\n"
          .format(mac_address, binary2str(e.output)))
    rc = 2

exit(rc)