summaryrefslogtreecommitdiff
path: root/src/op_mode/sflow.py
blob: dca7f44cb0f12822df3c8037c42aa7a9bed2f4a0 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3
#
# Copyright (C) 2023 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# 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, see <http://www.gnu.org/licenses/>.

import dbus
import sys

from tabulate import tabulate

from vyos.configquery import ConfigTreeQuery
from vyos.utils.process import cmd

import vyos.opmode


def _get_raw_sflow():
    bus = dbus.SystemBus()
    config = ConfigTreeQuery()

    interfaces = config.values('system sflow interface')
    servers = config.list_nodes('system sflow server')

    sflow = bus.get_object('net.sflow.hsflowd', '/net/sflow/hsflowd')
    sflow_telemetry = dbus.Interface(
        sflow, dbus_interface='net.sflow.hsflowd.telemetry')
    agent_address = sflow_telemetry.GetAgent()
    samples_dropped = int(sflow_telemetry.Get('dropped_samples'))
    packet_drop_sent = int(sflow_telemetry.Get('event_samples'))
    samples_packet_sent = int(sflow_telemetry.Get('flow_samples'))
    samples_counter_sent = int(sflow_telemetry.Get('counter_samples'))
    datagrams_sent = int(sflow_telemetry.Get('datagrams'))
    rtmetric_samples = int(sflow_telemetry.Get('rtmetric_samples'))
    event_samples_suppressed = int(sflow_telemetry.Get('event_samples_suppressed'))
    samples_suppressed = int(sflow_telemetry.Get('flow_samples_suppressed'))
    counter_samples_suppressed = int(
        sflow_telemetry.Get("counter_samples_suppressed"))
    version = sflow_telemetry.GetVersion()

    sflow_dict = {
        'agent_address': agent_address,
        'sflow_interfaces': interfaces,
        'sflow_servers': servers,
        'counter_samples_sent': samples_counter_sent,
        'datagrams_sent': datagrams_sent,
        'packet_drop_sent': packet_drop_sent,
        'packet_samples_dropped': samples_dropped,
        'packet_samples_sent': samples_packet_sent,
        'rtmetric_samples': rtmetric_samples,
        'event_samples_suppressed': event_samples_suppressed,
        'flow_samples_suppressed': samples_suppressed,
        'counter_samples_suppressed': counter_samples_suppressed,
        'hsflowd_version': version
    }
    return sflow_dict


def _get_formatted_sflow(data):
    table = [
        ['Agent address', f'{data.get("agent_address")}'],
        ['sFlow interfaces', f'{data.get("sflow_interfaces", "n/a")}'],
        ['sFlow servers', f'{data.get("sflow_servers", "n/a")}'],
        ['Counter samples sent', f'{data.get("counter_samples_sent")}'],
        ['Datagrams sent', f'{data.get("datagrams_sent")}'],
        ['Packet samples sent', f'{data.get("packet_samples_sent")}'],
        ['Packet samples dropped', f'{data.get("packet_samples_dropped")}'],
        ['Packet drops sent', f'{data.get("packet_drop_sent")}'],
        ['Packet drops suppressed', f'{data.get("event_samples_suppressed")}'],
        ['Flow samples suppressed', f'{data.get("flow_samples_suppressed")}'],
        ['Counter samples suppressed', f'{data.get("counter_samples_suppressed")}']
    ]

    return tabulate(table)


def show(raw: bool):

    config = ConfigTreeQuery()
    if not config.exists('system sflow'):
        raise vyos.opmode.UnconfiguredSubsystem(
            '"system sflow" is not configured!')

    sflow_data = _get_raw_sflow()
    if raw:
        return sflow_data
    else:
        return _get_formatted_sflow(sflow_data)


if __name__ == '__main__':
    try:
        res = vyos.opmode.run(sys.modules[__name__])
        if res:
            print(res)
    except (ValueError, vyos.opmode.Error) as e:
        print(e)
        sys.exit(1)