Browse Source

improve ping function. First time do simple ping, and if not response then try arp ping

devel
Dmitry Novikov 7 years ago
parent
commit
97b9b171b5
  1. 12
      abonapp/locale/ru/LC_MESSAGES/django.po
  2. 46
      abonapp/views.py
  3. 5
      gw_app/models.py
  4. 3
      gw_app/nas_managers/core.py
  5. 13
      gw_app/nas_managers/mod_mikrotik.py

12
abonapp/locale/ru/LC_MESSAGES/django.po

@ -992,18 +992,18 @@ msgstr "пингуется"
#: views.py:698 #: views.py:698
#, python-format #, python-format
msgid "IP Conflict! %(all)d/%(return)d results"
msgstr "IP Конфликт! ping %(all)d из %(return)d"
msgid "IP Conflict! %(return)d/%(all)d results"
msgstr "IP Конфликт! ping %(return)d из %(all)d"
#: views.py:701 #: views.py:701
#, python-format #, python-format
msgid "ok ping, %(all)d/%(return)d loses"
msgstr "пингуется, %(all)d/%(return)d"
msgid "ok ping, %(return)d/%(all)d loses"
msgstr "пингуется, %(return)d/%(all)d"
#: views.py:705 #: views.py:705
#, python-format #, python-format
msgid "no ping, %(all)d/%(return)d loses"
msgstr "не пингуется, %(all)d/%(return)d"
msgid "no ping, %(return)d/%(all)d loses"
msgstr "не пингуется, %(return)d/%(all)d"
#: views.py:811 #: views.py:811
msgid "Method is not POST" msgid "Method is not POST"

46
abonapp/views.py

@ -694,8 +694,7 @@ def clear_dev(request, gid: int, uname):
def abon_ping(request, gid: int, uname): def abon_ping(request, gid: int, uname):
ip = request.GET.get('cmd_param') ip = request.GET.get('cmd_param')
status = False status = False
text = '<span class="glyphicon glyphicon-exclamation-sign"></span> %s' % _(
'no ping')
text = '<span class="glyphicon glyphicon-exclamation-sign"></span> %s' % _('no ping')
abon = get_object_or_404(models.Abon, username=uname) abon = get_object_or_404(models.Abon, username=uname)
try: try:
if ip is None: if ip is None:
@ -710,35 +709,44 @@ def abon_ping(request, gid: int, uname):
mngr = abon.nas.get_nas_manager() mngr = abon.nas.get_nas_manager()
ping_result = mngr.ping(ip) ping_result = mngr.ping(ip)
if ping_result is None: if ping_result is None:
if ping(ip, 10):
status = True
text = '<span class="glyphicon glyphicon-ok"></span> %s' % _(
'ping ok')
return {
'status': False,
'dat': text
}
if isinstance(ping_result, tuple):
received, sent = ping_result
print(ping_result)
if received == 0:
ping_result = mngr.ping(ip, arp=True)
if ping_result is not None and isinstance(ping_result, tuple):
received, sent = ping_result
else: else:
if type(ping_result) is tuple:
return {
'status': False,
'dat': text
}
loses_percent = ( loses_percent = (
ping_result[0] / ping_result[1] if ping_result[
1] != 0 else 1)
ping_result = {'all': ping_result[0], 'return': ping_result[1]}
received / sent if sent != 0 else 1
)
ping_result = {'return': received, 'all': sent}
if loses_percent > 1.0: if loses_percent > 1.0:
text = '<span class="glyphicon glyphicon-exclamation-sign"></span> %s' % _( text = '<span class="glyphicon glyphicon-exclamation-sign"></span> %s' % _(
'IP Conflict! %(all)d/%(return)d results') % ping_result
'IP Conflict! %(return)d/%(all)d results'
) % ping_result
elif loses_percent > 0.5: elif loses_percent > 0.5:
text = '<span class="glyphicon glyphicon-ok"></span> %s' % _( text = '<span class="glyphicon glyphicon-ok"></span> %s' % _(
'ok ping, %(all)d/%(return)d loses') % ping_result
'ok ping, %(return)d/%(all)d loses'
) % ping_result
status = True status = True
else: else:
text = '<span class="glyphicon glyphicon-exclamation-sign"></span> %s' % _( text = '<span class="glyphicon glyphicon-exclamation-sign"></span> %s' % _(
'no ping, %(all)d/%(return)d loses') % ping_result
else:
text = '<span class="glyphicon glyphicon-ok"></span> %s' % _(
'ping ok') + ' ' + str(ping_result)
status = True
'no ping, %(return)d/%(all)d loses'
) % ping_result
except (NasFailedResult, lib.LogicError) as e: except (NasFailedResult, lib.LogicError) as e:
messages.error(request, e)
text = str(e)
except NasNetworkError as e: except NasNetworkError as e:
messages.warning(request, e)
text = str(e)
return { return {
'status': 0 if status else 1, 'status': 0 if status else 1,

5
gw_app/models.py

@ -5,7 +5,7 @@ from django.shortcuts import resolve_url
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.db import models from django.db import models
from djing.lib import MyChoicesAdapter from djing.lib import MyChoicesAdapter
from gw_app.nas_managers import NAS_TYPES
from gw_app.nas_managers import NAS_TYPES, NasNetworkError
class NASModel(models.Model): class NASModel(models.Model):
@ -25,6 +25,7 @@ class NASModel(models.Model):
raise TypeError(_('One of nas types implementation is not found')) raise TypeError(_('One of nas types implementation is not found'))
def get_nas_manager(self): def get_nas_manager(self):
try:
klass = self.get_nas_manager_klass() klass = self.get_nas_manager_klass()
if hasattr(self, '_nas_mngr'): if hasattr(self, '_nas_mngr'):
o = getattr(self, '_nas_mngr') o = getattr(self, '_nas_mngr')
@ -38,6 +39,8 @@ class NASModel(models.Model):
) )
setattr(self, '_nas_mngr', o) setattr(self, '_nas_mngr', o)
return o return o
except ConnectionResetError:
raise NasNetworkError('ConnectionResetError')
def get_absolute_url(self): def get_absolute_url(self):
return resolve_url('gw_app:edit', self.pk) return resolve_url('gw_app:edit', self.pk)

3
gw_app/nas_managers/core.py

@ -67,10 +67,11 @@ class BaseTransmitter(ABC):
""" """
@abstractmethod @abstractmethod
def ping(self, host: str, count=10) -> Optional[Tuple[int, int]]:
def ping(self, host: str, count=10, arp=False) -> Optional[Tuple[int, int]]:
""" """
:param host: ip address in text view, for example '192.168.0.1' :param host: ip address in text view, for example '192.168.0.1'
:param count: count of ping queries :param count: count of ping queries
:param arp: Is use arp ping
:return: None if not response, else tuple it contains count returned and count sent :return: None if not response, else tuple it contains count returned and count sent
for example (received, sent) -> (7, 10). for example (received, sent) -> (7, 10).
""" """

13
gw_app/nas_managers/mod_mikrotik.py

@ -430,7 +430,12 @@ class MikrotikTransmitter(core.BaseTransmitter, ApiRos,
if res_ips: if res_ips:
self.remove_ip(res_ips.get('=.id')) self.remove_ip(res_ips.get('=.id'))
def ping(self, host, count=10) -> Optional[Tuple[int, int]]:
def ping(self, host, count=10, arp=False) -> Optional[Tuple[int, int]]:
params = [
'/ping', '=address=%s' % host,
'=interval=100ms', '=count=%d' % count
]
if arp:
r = self._exec_cmd(( r = self._exec_cmd((
'/ip/arp/print', '/ip/arp/print',
'?address=%s' % host '?address=%s' % host
@ -438,11 +443,11 @@ class MikrotikTransmitter(core.BaseTransmitter, ApiRos,
if r == {}: if r == {}:
return return
interface = r['!re'].get('=interface') interface = r['!re'].get('=interface')
r = self._exec_cmd((
'/ping', '=address=%s' % host, '=arp-ping=yes', '=interval=100ms',
'=count=%d' % count,
params.extend((
'=arp-ping=yes',
'=interface=%s' % interface '=interface=%s' % interface
)) ))
r = self._exec_cmd(params)
res = r.get('!re') res = r.get('!re')
if res is not None: if res is not None:
received, sent = int(res.get('=received')), int(res.get('=sent')) received, sent = int(res.get('=received')), int(res.get('=sent'))

Loading…
Cancel
Save