summaryrefslogtreecommitdiffstats
path: root/plugins/network.py
blob: d25c848a690fd571d93976460769d26f719afdd4 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/python
# Copyright (C) 2014 Red Hat Inc
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#

import sadf
import argparse
from glusternagios import utils

_sadfNetCommand = ["sadf", "-x", "--", "-n", "DEV"]


def parse_input():
    parser = argparse.ArgumentParser()
    group = parser.add_mutually_exclusive_group()
    group.add_argument("-e", "--exclude", action="append",
                       help="Parameters to be excluded")
    group.add_argument("-i", "--include", action="append",
                       help="Parameters to be included")
    sadf.add_common_args(parser)
    args = parser.parse_args()
    return args


def showNetStat(s, iface_list=None, list_type=None):
    pl_op = {}
    if not s:
        pl_op["message"] = ("IFACE UNKNOWN")
        pl_op['exit_status'] = utils.PluginStatusCode.UNKNOWN
        return pl_op
    devNames = []
    perfLines = []
    try:
        for dev in s['network']['net-dev']:
            if list_type == "exclude":
                if dev['iface'] in iface_list:
                    continue
            elif list_type == "include":
                if dev['iface'] not in iface_list:
                    continue
            devNames.append(dev['iface'])
            perfLines.append("%s.rxpck=%s %s.txpck=%s %s.rxkB=%s %s.txkB=%s"
                             % (dev['iface'], dev['rxpck'],
                                dev['iface'], dev['txpck'],
                                dev['iface'], dev['rxkB'],
                                dev['iface'], dev['txkB']))
    except (KeyError, ValueError, TypeError) as e:
        pl_op["message"] = "key: %s not found" % str(e)
        pl_op["exit_status"] = utils.PluginStatusCode.UNKNOWN
        return pl_op

    pl_op["message"] = ("IFACE OK: %s |%s" % (", ".join(devNames),
                                              " ".join(perfLines)))
    pl_op['exit_status'] = utils.PluginStatusCode.OK
    return pl_op

if __name__ == '__main__':
    args = parse_input()
    try:
        st = sadf.getLatestStat(sadf.sadfExecCmd(_sadfNetCommand),
                                args.interval if args.interval else 1)
    except (sadf.SadfCmdExecFailedException,
            sadf.SadfXmlErrorException) as e:
        print str(e)
        exit(utils.PluginStatusCode.UNKNOWN)
    if args.exclude:
        d = showNetStat(st, args.exclude, "exclude")
    elif args.include:
        d = showNetStat(st, args.include, "include")
    else:
        d = showNetStat(st)
    print d["message"]
    exit(d['exit_status'])