Host monitoring

This commit is contained in:
Eduardo Silva 2024-03-29 23:36:40 -03:00
parent c4bc233bc1
commit 60e1d557aa
10 changed files with 177 additions and 23 deletions

View file

@ -54,24 +54,16 @@ class RouterForm(forms.ModelForm):
cleaned_data = super().clean()
name = cleaned_data.get('name')
ssh_key = cleaned_data.get('ssh_key')
username = cleaned_data.get('username')
password = cleaned_data.get('password')
address = cleaned_data.get('address')
router_type = cleaned_data.get('router_type')
backup_profile = cleaned_data.get('backup_profile')
if name:
name = name.strip()
cleaned_data['name'] = name
if ssh_key and password:
raise forms.ValidationError('You must provide a password or an SSH Key, not both')
if not ssh_key and not password and not self.instance.password:
raise forms.ValidationError('You must provide a password or an SSH Key')
if not password and self.instance.password:
cleaned_data['password'] = self.instance.password
if ssh_key and not password:
cleaned_data['password'] = ''
if address:
address = address.lower()
cleaned_data['address'] = address
@ -83,9 +75,27 @@ class RouterForm(forms.ModelForm):
ipaddress.ip_address(address)
except ValueError:
raise forms.ValidationError('The address field must be a valid hostname or IP address.')
if router_type == 'monitoring':
cleaned_data['password'] = ''
cleaned_data['ssh_key'] = None
if backup_profile:
raise forms.ValidationError('Monitoring only routers cannot have a backup profile')
return cleaned_data
if ssh_key and password:
raise forms.ValidationError('You must provide a password or an SSH Key, not both')
if not ssh_key and not password and not self.instance.password:
raise forms.ValidationError('You must provide a password or an SSH Key')
if not password and self.instance.password:
cleaned_data['password'] = self.instance.password
if ssh_key and not password:
cleaned_data['password'] = ''
test_authentication_success, test_authentication_message = test_authentication(
cleaned_data['router_type'], cleaned_data['address'], cleaned_data['username'], cleaned_data['password'],
cleaned_data['ssh_key']
router_type, cleaned_data['address'], username, cleaned_data['password'], ssh_key
)
if not test_authentication_success:
if test_authentication_message:

View file

@ -26,7 +26,7 @@ class Router(models.Model):
monitoring = models.BooleanField(default=True)
backup_profile = models.ForeignKey(BackupProfile, on_delete=models.SET_NULL, null=True, blank=True)
router_type = models.CharField(max_length=100, choices=(('routeros', 'Mikrotik (RouterOS)'), ('openwrt', 'OpenWRT')))
router_type = models.CharField(max_length=100, choices=(('monitoring', 'Monitoring Only'), ('routeros', 'Mikrotik (RouterOS)'), ('openwrt', 'OpenWRT')))
enabled = models.BooleanField(default=True)
updated = models.DateTimeField(auto_now=True)

View file

@ -58,6 +58,7 @@ def view_manage_router(request):
if form.is_valid():
form.save()
messages.success(request, 'Router saved successfully')
router_status, router_status_created = RouterStatus.objects.get_or_create(router=form.instance)
return redirect('router_list')
context = {