def get(self, request): host_id = request.GET.get('id', None) model_host = models.Hosts() status = False owner_id = model_host.get_owner(host_id) if owner_id == request.user.id: host_name = model_host.get_host_name(request.user.id, host_id) models_pm = models.PortMonitor() protocol, ports = models_pm.get_protocol_and_ports(host_id) protocol = helper.format_protocols_to_array(protocol) task.stop_port(host_name, str(request.user.id), str(host_id), str(ports), protocol) key = "p_" + str(host_id) session_exist = request.session.get(key) if session_exist: result = helper.get_port_monitor_results( host_id, request.user.id) del request.session[key] pm = models.PortMonitor() pm.update_params(host_id, str(result)) model_host.update_is_port(host_id, False) messages.success(request, 'You are not port monitoring now!') status = False else: messages.error(request, 'Something went wrong!') data = {'stopped': status} return JsonResponse(data)
def start_port(request, pk, template_name='modals/start_port.html'): model_pm = models.PortMonitor() model_host = models.Hosts() owner_id = model_host.get_owner(pk) if owner_id == request.user.id: form = PortMonitorForm(request.POST or None) if form.is_valid(): model_host = models.Hosts() host_name = model_host.get_host_name(request.user.id, pk) date = form.cleaned_data['end_time'] ports = form.cleaned_data['ports'] prot = form.cleaned_data['protocol'] protocol = helper.get_protocol(prot) port_list = helper.prepare_port_list(ports) key = "p_" + str(pk) session_exist = request.session.get(key) if not session_exist: request.session[key] = "{}" task.check_port_3_sec(host_name, request.user.id, pk, port_list, protocol, repeat=1, repeat_until=date) if len(protocol) == 2: protocol = "tcp/udp" else: protocol = protocol[0] model_pm.create_port_monitor(pk, date, str(port_list), protocol) model_host.update_is_port(pk, True) return HttpResponse( render_to_string('modals/item_edit_form_success.html')) return render(request, template_name, {'form': form, 'id': pk}) raise Http404
def send_email_ping(user_id, host_id): h = models.Hosts() host_name = h.get_host_name(user_id, host_id) us = models.User() fn, ln = us.get_user_name(user_id) subject, from_email, to = 'Your host is down!', '*****@*****.**', '*****@*****.**' html_content = '<p>Hello <b>' + fn + ' ' + ln + ' </b>! <br> I want to notify you that your host <b>' + host_name + '</b> is <b>DOWN</b>' text_content = 'This is an important message.' msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) msg.attach_alternative(html_content, "text/html") msg.send()
def test_ping(request): h_id = request.GET.get('id', None) model_host = models.Hosts() data = {'response': "Not your host"} owner_id = model_host.get_owner(h_id) if owner_id == request.user.id: host_name = model_host.get_host_name(request.user.id, h_id) ms, timeout, msg = helper.test_ping(host_name, 4, 1) data = {'response': msg} return JsonResponse(data)
def start_ping(request, pk, template_name='modals/start_ping.html'): model_hum = models.HostUsersMonitor() host_m = models.Hosts() owner_id = host_m.get_owner(pk) if owner_id == request.user.id: form = HostUsersMonitorForm(request.POST or None) if form.is_valid(): date = form.cleaned_data['end_time'] if date == None: date = timezone.now() + timedelta(1) model_hum.create_monitor(pk, date) model_host = models.Hosts() model_host.update_is_ping(pk, True) host_name = model_host.get_host_name(request.user.id, pk) session_exist = request.session.get(str(pk)) if not session_exist: request.session[str(pk)] = { 'num_of_seq': 0, 'max_ms': 0, 'min_ms': 9999, 'avg_ms': 0, 'num_timeout': 0, 'is_pinging': 1, 'down_seq': 0 } task.ping_every_2_sec(host_name, str(request.user.id), str(pk), repeat=2, repeat_until=date) return HttpResponse( render_to_string('modals/item_edit_form_success.html')) return render(request, template_name, {'form': form, 'id': pk}) raise Http404
def stat_list_ping(request, pk): hum = models.HostUsersMonitor() stats = hum.get_stats(pk, 'id') hm = models.Hosts() owner_id = hm.get_owner(pk) if owner_id == request.user.id: name = hm.get_host_name(request.user.id, pk) return render(request, 'stat_list_ping.html', { 'id': int(pk), 'stats': stats, 'name': name }) raise Http404
def stat_list_port(request, pk): pm = models.PortMonitor() stats = pm.get_stats(pk, 'id') hm = models.Hosts() owner_id = hm.get_owner(pk) if owner_id == request.user.id: name = hm.get_host_name(request.user.id, pk) stats = helper.format_port_monitor_results(stats) return render(request, 'stat_list_port.html', { 'id': int(pk), 'stats': stats, 'name': name }) raise Http404
def check_if_expired(request): ids = json.loads(request.GET.get('ids')) hum = models.HostUsersMonitor() pm = models.PortMonitor() success = True for idm in ids: end_time = hum.get_end_time(idm) st, end_time_port = pm.get_times(idm) model_host = models.Hosts() host_name = model_host.get_host_name(request.user.id, idm) if end_time: if end_time < timezone.now(): success = task.stop_ping(host_name, str(request.user.id), str(idm)) if success: min_ms, avg_ms, max_ms, transmited_pac, lost_pac = helper.get_ping_params( str(idm), str(request.user.id)) hum.update_params(idm, timezone.now(), min_ms, avg_ms, max_ms, transmited_pac, lost_pac) session_exist = request.session.get(str(idm)) if session_exist: del request.session[str(idm)] model_host.update_is_ping(idm, False) if end_time_port: if end_time_port < timezone.now(): protocol, ports = pm.get_protocol_and_ports(idm) protocol = helper.format_protocols_to_array(protocol) success = task.stop_port(host_name, str(request.user.id), str(idm), str(ports), protocol) if success: key = "p_" + str(idm) session_exist = request.session.get(key) if session_exist: result = helper.get_port_monitor_results( idm, request.user.id) del request.session[key] pm.update_params(idm, str(result)) model_host.update_is_port(idm, False) data = {'status': success} return JsonResponse(data)
def send_email_ports(user_id, host_id, ports): h = models.Hosts() host_name = h.get_host_name(user_id, host_id) us = models.User() fn, ln = us.get_user_name(user_id) subject, from_email, to = 'Some port(s) are down!', '*****@*****.**', '*****@*****.**' html_content = '<p>Hello <b>' + fn + ' ' + ln + ' </b>! <br> I want to notify you that port number(s) ' text_content = 'This is an important message.' ports_str = '' for port in ports: ports_str += '<b>' + ports[port]['port'] + '(' + ports[port][ 'protocol'] + ')' + '</b>, ' html_content += ports_str + ' on your host <b>' + host_name + '</b> is (are) <b> DOWN </b>!</p>' msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) msg.attach_alternative(html_content, "text/html") msg.send()
def get_host_geoinfo(request): h_id = request.GET.get('id', None) model_host = models.Hosts() owner_id = model_host.get_owner(h_id) if owner_id == request.user.id: host_name = model_host.get_host_name(request.user.id, h_id) ip_addr = ping._to_ip(host_name, False) url = 'https://extreme-ip-lookup.com/json/' + str(ip_addr) r = requests.get(url) data = json.loads(r.content.decode()) data = {'response': data} else: data = {'response': None} return JsonResponse(data)
def view_host_settings(request, pk): host_m = models.Hosts() owner_id = host_m.get_owner(pk) if owner_id == request.user.id: hum = models.HostUsersMonitor() user_hosts = hum.get_info(pk) pm = models.PortMonitor() port_m_info = pm.get_info(pk) else: raise Http404 return render( request, 'settings_list.html', { 'id': int(pk), 'host': user_hosts, 'user_id': request.user.id, 'p_times': port_m_info })
def get(self, request): host_id = request.GET.get('id', None) model_host = models.Hosts() host_name = model_host.get_host_name(request.user.id, host_id) success = task.stop_ping(host_name, str(request.user.id), str(host_id)) if success: session_exist = request.session.get(str(host_id)) if session_exist: del request.session[str(host_id)] hum = models.HostUsersMonitor() min_ms, avg_ms, max_ms, transmited_pac, lost_pac = helper.get_ping_params( str(host_id), str(request.user.id)) hum.update_params(host_id, timezone.now(), min_ms, avg_ms, max_ms, transmited_pac, lost_pac) model_host.update_is_ping(host_id, False) messages.success(request, 'You are not pinging now!') else: messages.error(request, 'Something went wrong!') data = {'stopped': success} return JsonResponse(data)
def delete_host(request): id_h = request.GET.get('id') hm = models.Hosts() owner_id = hm.get_owner(id_h) stat = False if owner_id == request.user.id: is_mon = hm.is_monitoring(id_h) if is_mon: messages.error( request, 'Please stop all monitoring activities before delete') stat = True else: hm.delete_host(id_h) messages.success(request, 'Success!') data = {'stats': stat} return JsonResponse(data, safe=True)
def scan_given_ports(request): port_list = request.GET.get('ports', None) h_id = request.GET.get('id', None) model_host = models.Hosts() owner_id = model_host.get_owner(h_id) data = {'response': [' none']} if owner_id == request.user.id: host_name = model_host.get_host_name(request.user.id, h_id) prot = request.GET.get('protocol', None) get_states = request.GET.get('state', None) protocol = helper.get_protocol(prot) port_list = helper.prepare_port_list(port_list) if port_list != 0: threads = helper.get_tread_counts(len(port_list)) states = task.port_scan(protocol, port_list, host_name, threads, get_states) data = {'response': helper.Sort(states)} return JsonResponse(data)
def view_hosts(request, pk): modelHosts = models.Hosts() user_hosts = modelHosts.get_hosts_by_user_id(user_id=request.user.id) return render(request, 'hosts_list.html', {'id': pk, 'hosts': user_hosts})