ping.py 2.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------
# Rotek.RTBSv1.ping
# ---------------------------------------------------------------------
# Copyright (C) 2007-2017 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------

# Python modules
import re
Dmitry Volodin's avatar
Dmitry Volodin committed
11

12
13
14
15
16
17
18
19
# NOC modules
from noc.core.script.base import BaseScript
from noc.sa.interfaces.iping import IPing


class Script(BaseScript):
    name = "Rotek.RTBSv1.ping"
    interface = IPing
20
    cache = True
21
22

    rx_result = re.compile(
23
        r"^(?P<count>\d+) packets transmitted, (?P<success>\d+) (packets received|received),"
Andrey Vertiprahov's avatar
Andrey Vertiprahov committed
24
25
        r"(?:\s|\s\S+ errors, )\d+% packet loss.",
        re.MULTILINE,
Dmitry Volodin's avatar
Dmitry Volodin committed
26
    )
27
    rx_stat = re.compile(
Andrey Vertiprahov's avatar
Andrey Vertiprahov committed
28
        r"^rtt min/avg/max/mdev = (?P<min>.+)/(?P<avg>.+)/(?P<max>.+)/(?P<mdev>.+)\s.", re.MULTILINE
Dmitry Volodin's avatar
Dmitry Volodin committed
29
    )
30

Dmitry Volodin's avatar
Dmitry Volodin committed
31
    def execute(self, address, count=None, source_address=None, size=None, df=None):
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        try:
            # Linux mode
            cmd = "ping "
            if count:
                cmd += "-c %d " % count
            if size:
                cmd += "-s %d " % size
            cmd += address
            ping = self.cli(cmd)
        except self.CLISyntaxError:
            cmd = "ping %s" % address
            ping = self.cli(cmd)
        # Not works
        # if count:
        #     cmd += " count %d" % int(count)
        # if size:
        #     cmd += " size %d" % int(size)
49
50
51
52
53
54
        # Don't implemented, may be in future firmware revisions ?
        # if source_address:
        #    cmd+=" source %s" % source_address
        # if df:
        #    cmd+=" df-bit"
        result = self.rx_result.search(ping)
Dmitry Volodin's avatar
Dmitry Volodin committed
55
        r = {"success": result.group("success"), "count": result.group("count")}
56
57
        stat = self.rx_stat.search(ping)
        if stat:
Dmitry Volodin's avatar
Dmitry Volodin committed
58
59
60
61
62
63
64
65
            r.update(
                {
                    "min": stat.group("min"),
                    "avg": stat.group("avg"),
                    "max": stat.group("max"),
                    "mdev": stat.group("mdev"),
                }
            )
66
        return r