Commit 521aa877 by Őry Máté

firewall: add get_dhcp_clients

parent 0dbc3afe
...@@ -18,9 +18,9 @@ ...@@ -18,9 +18,9 @@
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>. # with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
from string import ascii_letters from string import ascii_letters
from itertools import islice, ifilter from itertools import islice, ifilter, chain
import logging import logging
from netaddr import IPSet, EUI, IPNetwork, IPAddress import random
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.db import models from django.db import models
...@@ -34,10 +34,12 @@ from firewall.fields import (MACAddressField, val_alfanum, val_reverse_domain, ...@@ -34,10 +34,12 @@ from firewall.fields import (MACAddressField, val_alfanum, val_reverse_domain,
from django.core.validators import MinValueValidator, MaxValueValidator from django.core.validators import MinValueValidator, MaxValueValidator
import django.conf import django.conf
from django.db.models.signals import post_save, post_delete from django.db.models.signals import post_save, post_delete
import random from celery.exceptions import TimeoutError
from netaddr import IPSet, EUI, IPNetwork, IPAddress
from common.models import method_cache, WorkerNotFound, HumanSortField from common.models import method_cache, WorkerNotFound, HumanSortField
from firewall.tasks.local_tasks import reloadtask from firewall.tasks.local_tasks import reloadtask
from firewall.tasks.remote_tasks import get_dhcp_clients
from .iptables import IptRule from .iptables import IptRule
from acl.models import AclBase from acl.models import AclBase
...@@ -419,6 +421,13 @@ class Vlan(AclBase, models.Model): ...@@ -419,6 +421,13 @@ class Vlan(AclBase, models.Model):
nums = {ascii_letters[i]: int(ipv4.words[i]) for i in range(4)} nums = {ascii_letters[i]: int(ipv4.words[i]) for i in range(4)}
return IPAddress(self.ipv6_template % nums) return IPAddress(self.ipv6_template % nums)
def get_dhcp_clients(self):
macs = set(i.mac for i in self.host_set.all())
return [{"mac": k, "ip": v["ip"], "hostname": v["hostname"]}
for k, v in chain(*(fw.get_dhcp_clients().iteritems()
for fw in Firewall.objects.all() if fw))
if v["interface"] == self.name and EUI(k) not in macs]
class VlanGroup(models.Model): class VlanGroup(models.Model):
""" """
...@@ -845,7 +854,7 @@ class Firewall(models.Model): ...@@ -845,7 +854,7 @@ class Firewall(models.Model):
return self.name return self.name
@method_cache(30) @method_cache(30)
def get_remote_queue_name(self, queue_id): def get_remote_queue_name(self, queue_id="firewall"):
"""Returns the name of the remote celery queue for this node. """Returns the name of the remote celery queue for this node.
Throws Exception if there is no worker on the queue. Throws Exception if there is no worker on the queue.
...@@ -858,6 +867,14 @@ class Firewall(models.Model): ...@@ -858,6 +867,14 @@ class Firewall(models.Model):
else: else:
raise WorkerNotFound() raise WorkerNotFound()
@method_cache(20)
def get_dhcp_clients(self):
try:
return get_dhcp_clients.apply_async(
queue=self.get_remote_queue_name(), expires=60).get(timeout=2)
except TimeoutError:
return None
class Domain(models.Model): class Domain(models.Model):
name = models.CharField(max_length=40, validators=[val_domain], name = models.CharField(max_length=40, validators=[val_domain],
......
...@@ -63,4 +63,5 @@ def reload_blacklist(data): ...@@ -63,4 +63,5 @@ def reload_blacklist(data):
@celery.task(name='firewall.get_dhcp_clients') @celery.task(name='firewall.get_dhcp_clients')
def get_dhcp_clients(): def get_dhcp_clients():
# {'00:21:5a:73:72:cd': {'interface': 'OFF', 'ip': None, 'hostname': None}}
pass pass
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment