Browse Source

fetch device status from model field instead of sync request

devel
bashmak 8 years ago
parent
commit
174e9c43dd
  1. 29
      devapp/models.py
  2. 4
      mapapp/templates/maps/map_tooltip.html
  3. 1
      mapapp/views.py

29
devapp/models.py

@ -1,14 +1,12 @@
# -*- coding: utf-8 -*-
import requests
from django.db import models, ProgrammingError
from django.db import models
from djing.fields import MACAddressField
from .base_intr import DevBase
from mydefs import MyGenericIPAddressField, MyChoicesAdapter, ip2int
from mydefs import MyGenericIPAddressField, MyChoicesAdapter
from . import dev_types
from subprocess import run
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from json.decoder import JSONDecodeError
from group_app.models import Group
@ -28,24 +26,6 @@ class DeviceMonitoringException(Exception):
pass
class DeviceManager(models.Manager):
@staticmethod
def wrap_monitoring_info(devices_queryset):
nag_url = getattr(settings, 'NAGIOS_URL', None)
if nag_url is not None:
addrs = ['h=%s' % hex(ip2int(dev.ip_address))[2:] for dev in devices_queryset]
url = '%s/host/status/arr?%s' % (nag_url, '&'.join(addrs))
try:
res = requests.get(url).json()
except (requests.exceptions.ConnectionError, JSONDecodeError) as e:
raise DeviceMonitoringException(e)
for dev in devices_queryset:
inf = [x for x in res if x.get('address') == dev.ip_address]
if len(inf) > 0:
setattr(dev, 'mon', inf[0].get('current_status'))
return devices_queryset
class Device(models.Model):
ip_address = MyGenericIPAddressField(verbose_name=_('Ip address'))
mac_addr = MACAddressField(verbose_name=_('Mac address'), null=True, blank=True, unique=True)
@ -67,8 +47,6 @@ class Device(models.Model):
is_noticeable = models.BooleanField(_('Send notify when monitoring state changed'), default=False)
objects = DeviceManager()
class Meta:
db_table = 'dev'
permissions = (
@ -159,6 +137,3 @@ class Port(models.Model):
)
verbose_name = _('Port')
verbose_name_plural = _('Ports')

4
mapapp/templates/maps/map_tooltip.html

@ -3,9 +3,9 @@
<div class="list-group">
{% for dev in devs %}
<a class="list-group-item" href="{% url 'devapp:view' dev.group.pk dev.pk %}" target="_blank" title="{{ dev.mon.plugin_output }}">
{% if dev.mon.current_state == '0' %}
{% if dev.status == 'up' %}
<span class="glyphicon glyphicon-ok-circle text-success"></span>
{% else %}
{% elif dev.status == 'dwn' or dev.status == 'unr' %}
<span class="glyphicon glyphicon-exclamation-sign text-danger"></span>
{% endif %}
<b>{{ dev.comment }}</b> {{ dev.mon.plugin_output }}

1
mapapp/views.py

@ -177,7 +177,6 @@ def dot_tooltip(request):
try:
dot = Dot.objects.get(id=d)
devs = dot.devices.all()
devs = Device.objects.wrap_monitoring_info(devs)
except Dot.DoesNotExist:
pass
return render_to_text('maps/map_tooltip.html', {

Loading…
Cancel
Save