Browse Source

try ip instead mac in device notify

devel
Dmitry Novikov 7 years ago
parent
commit
fb39a6dd33
  1. 17
      agent/monitoring_agent.py
  2. 22
      devapp/views.py

17
agent/monitoring_agent.py

@ -11,7 +11,12 @@ API_AUTH_SECRET = 'your api key'
SERVER_DOMAIN = 'http://127.0.0.1:8000' SERVER_DOMAIN = 'http://127.0.0.1:8000'
MAC_ADDR_REGEX = r'^([0-9A-Fa-f]{1,2}[:-]){5}([0-9A-Fa-f]{1,2})$'
IP_ADDR_REGEX = (
'^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'
'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'
'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'
'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
)
def calc_hash(data): def calc_hash(data):
@ -40,11 +45,11 @@ def validate_status(text: str):
return text return text
def send_request(mac, stat, sign_hash):
def send_request(ip_addr, stat, sign_hash):
r = requests.get( r = requests.get(
os.path.join(SERVER_DOMAIN, 'dev', 'on_device_event'), os.path.join(SERVER_DOMAIN, 'dev', 'on_device_event'),
params={ params={
'mac': mac,
'ip_addr': ip_addr,
'status': stat, 'status': stat,
'sign': sign_hash 'sign': sign_hash
}) })
@ -63,12 +68,12 @@ if __name__ == '__main__':
if API_AUTH_SECRET == 'your api key': if API_AUTH_SECRET == 'your api key':
raise NotImplementedError('You must specified secret api key') raise NotImplementedError('You must specified secret api key')
dev_mac = validate(MAC_ADDR_REGEX, sys.argv[1])
dev_ip = validate(IP_ADDR_REGEX, sys.argv[1])
status = validate_status(sys.argv[2]) status = validate_status(sys.argv[2])
vars_to_hash = [dev_mac, status]
vars_to_hash = [dev_ip, status]
vars_to_hash.sort() vars_to_hash.sort()
vars_to_hash.append(API_AUTH_SECRET) vars_to_hash.append(API_AUTH_SECRET)
sign = calc_hash('_'.join(vars_to_hash)) sign = calc_hash('_'.join(vars_to_hash))
send_request(dev_mac, status, sign)
send_request(dev_ip, status, sign)

22
devapp/views.py

@ -13,7 +13,7 @@ from django.shortcuts import render, redirect, get_object_or_404, resolve_url
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.utils.translation import gettext_lazy as _, gettext from django.utils.translation import gettext_lazy as _, gettext
from django.views.generic import DetailView, DeleteView, UpdateView, CreateView from django.views.generic import DetailView, DeleteView, UpdateView, CreateView
from djing import global_base_views, MAC_ADDR_REGEX, ping, get_object_or_None
from djing import global_base_views, IP_ADDR_REGEX, ping, get_object_or_None
from djing.lib import safe_int, ProcessLocked, DuplicateEntry from djing.lib import safe_int, ProcessLocked, DuplicateEntry
from djing.lib.decorators import json_view from djing.lib.decorators import json_view
from djing.lib.decorators import only_admins, hash_auth_view from djing.lib.decorators import only_admins, hash_auth_view
@ -726,19 +726,20 @@ class OnDeviceMonitoringEvent(global_base_views.SecureApiView):
@method_decorator(json_view) @method_decorator(json_view)
def get(self, request): def get(self, request):
try: try:
dev_mac = request.GET.get('mac')
dev_ip = request.GET.get('dev_ip')
dev_status = request.GET.get('status') dev_status = request.GET.get('status')
if dev_mac is None or dev_mac == '':
return {'text': 'mac does not passed'}
if dev_ip is None or dev_ip == '':
return {'text': 'ip does not passed'}
if not re.match(MAC_ADDR_REGEX, dev_mac):
return {'text': 'mac address %s is not valid' % dev_mac}
if not re.match(IP_ADDR_REGEX, dev_ip):
return {'text': 'ip address %s is not valid' % dev_ip}
device_down = Device.objects.filter(mac_addr=dev_mac).defer(
'extra_data').first()
device_down = Device.objects.filter(ip_address=dev_ip).defer(
'extra_data'
).first()
if device_down is None: if device_down is None:
return {'text': 'Devices with mac %s does not exist' % dev_mac}
return {'text': 'Devices with ip %s does not exist' % dev_ip}
if dev_status == 'UP': if dev_status == 'UP':
device_down.status = 'up' device_down.status = 'up'
@ -762,7 +763,8 @@ class OnDeviceMonitoringEvent(global_base_views.SecureApiView):
} }
recipients = UserProfile.objects.get_profiles_by_group( recipients = UserProfile.objects.get_profiles_by_group(
device_down.group.pk).filter(flags=UserProfile.flags.notify_mon)
device_down.group.pk
).filter(flags=UserProfile.flags.notify_mon)
user_ids = tuple(recipient.pk for recipient in recipients.only('pk').iterator()) user_ids = tuple(recipient.pk for recipient in recipients.only('pk').iterator())
text = gettext(notify_text) % { text = gettext(notify_text) % {

Loading…
Cancel
Save