some python lang cleanup

This commit is contained in:
wh1te909 2020-06-15 10:15:28 +00:00
parent 2a529d851c
commit 86c436e0ea
2 changed files with 48 additions and 52 deletions

View File

@ -53,8 +53,7 @@ class Policy(models.Model):
enforced_checks = list() enforced_checks = list()
policy_checks = list() policy_checks = list()
if agent_policy != None: if agent_policy and agent_policy.active:
if agent_policy.active:
if agent_policy.enforced: if agent_policy.enforced:
for check in agent_policy.policychecks.all(): for check in agent_policy.policychecks.all():
enforced_checks.append(check) enforced_checks.append(check)
@ -62,8 +61,7 @@ class Policy(models.Model):
for check in agent_policy.policychecks.all(): for check in agent_policy.policychecks.all():
policy_checks.append(check) policy_checks.append(check)
if site_policy != None: if site_policy and site_policy.active:
if site_policy.active:
if site_policy.enforced: if site_policy.enforced:
for check in site_policy.policychecks.all(): for check in site_policy.policychecks.all():
enforced_checks.append(check) enforced_checks.append(check)
@ -71,8 +69,7 @@ class Policy(models.Model):
for check in site_policy.policychecks.all(): for check in site_policy.policychecks.all():
policy_checks.append(check) policy_checks.append(check)
if client_policy != None: if client_policy and client_policy.active:
if client_policy.active:
if client_policy.enforced: if client_policy.enforced:
for check in client_policy.policychecks.all(): for check in client_policy.policychecks.all():
enforced_checks.append(check) enforced_checks.append(check)
@ -105,9 +102,9 @@ class Policy(models.Model):
if check.disk not in added_diskspace_checks: if check.disk not in added_diskspace_checks:
added_diskspace_checks.append(check.disk) added_diskspace_checks.append(check.disk)
# Dont create the check if it is an agent check # Dont create the check if it is an agent check
if check.agent == None: if not check.agent:
diskspace_checks.append(check) diskspace_checks.append(check)
elif check.agent != None: elif check.agent:
check.overriden_by_policy = True check.overriden_by_policy = True
check.save() check.save()
@ -116,32 +113,32 @@ class Policy(models.Model):
if check.ip not in added_ping_checks: if check.ip not in added_ping_checks:
added_ping_checks.append(check.ip) added_ping_checks.append(check.ip)
# Dont create the check if it is an agent check # Dont create the check if it is an agent check
if check.agent == None: if not check.agent:
ping_checks.append(check) ping_checks.append(check)
added_ping_checks.append(check.ip) added_ping_checks.append(check.ip)
elif check.agent != None: elif check.agent:
check.overriden_by_policy = True check.overriden_by_policy = True
check.save() check.save()
if check.check_type == "cpuload": if check.check_type == "cpuload":
# Check if cpuload check exists # Check if cpuload check exists
if len(added_cpuload_checks) == 0: if added_cpuload_checks:
added_cpuload_checks.append(check) added_cpuload_checks.append(check)
# Dont create the check if it is an agent check # Dont create the check if it is an agent check
if check.agent == None: if not check.agent:
cpuload_checks.append(check) cpuload_checks.append(check)
elif check.agent != None: elif check.agent:
check.overriden_by_policy = True check.overriden_by_policy = True
check.save() check.save()
if check.check_type == "memory": if check.check_type == "memory":
# Check if memory check exists # Check if memory check exists
if len(added_memory_checks) == 0: if added_memory_checks:
added_memory_checks.append(check) added_memory_checks.append(check)
# Dont create the check if it is an agent check # Dont create the check if it is an agent check
if check.agent == None: if not check.agent:
memory_checks.append(check) memory_checks.append(check)
elif check.agent != None: elif check.agent:
check.overriden_by_policy = True check.overriden_by_policy = True
check.save() check.save()
@ -150,9 +147,9 @@ class Policy(models.Model):
if check.svc_name not in added_winsvc_checks: if check.svc_name not in added_winsvc_checks:
added_winsvc_checks.append(check.svc_name) added_winsvc_checks.append(check.svc_name)
# Dont create the check if it is an agent check # Dont create the check if it is an agent check
if check.agent == None: if not check.agent:
winsvc_checks.append(check) winsvc_checks.append(check)
elif check.agent != None: elif check.agent:
check.overriden_by_policy = True check.overriden_by_policy = True
check.save() check.save()
@ -161,9 +158,9 @@ class Policy(models.Model):
if check.script not in added_script_checks: if check.script not in added_script_checks:
added_script_checks.append(check.script) added_script_checks.append(check.script)
# Dont create the check if it is an agent check # Dont create the check if it is an agent check
if check.agent == None: if not check.agent:
script_checks.append(check) script_checks.append(check)
elif check.agent != None: elif check.agent:
check.overriden_by_policy = True check.overriden_by_policy = True
check.save() check.save()
@ -171,9 +168,9 @@ class Policy(models.Model):
# Check if events were already added # Check if events were already added
if [check.log_name, check.event_id] not in added_eventlog_checks: if [check.log_name, check.event_id] not in added_eventlog_checks:
added_eventlog_checks.append([check.log_name, check.event_id]) added_eventlog_checks.append([check.log_name, check.event_id])
if check.agent == None: if not check.agent:
eventlog_checks.append(check) eventlog_checks.append(check)
elif check.agent != None: elif check.agent:
check.overriden_by_policy = True check.overriden_by_policy = True
check.save() check.save()
@ -191,8 +188,7 @@ class Policy(models.Model):
def generate_policy_checks(agent): def generate_policy_checks(agent):
checks = Policy.cascade_policy_checks(agent) checks = Policy.cascade_policy_checks(agent)
if checks != None: if checks:
if len(checks) > 0:
for check in checks: for check in checks:
check.create_policy_check(agent) check.create_policy_check(agent)

View File

@ -10,19 +10,19 @@ from .tasks import generate_agent_checks_from_policies_task
def post_save_check_handler(sender, instance, created, **kwargs): def post_save_check_handler(sender, instance, created, **kwargs):
# don't run when policy managed check is saved # don't run when policy managed check is saved
if instance.managed_by_policy == True: if instance.managed_by_policy:
return return
# For created checks # For created checks
if created: if created:
if instance.policy != None: if instance.policy:
generate_agent_checks_from_policies_task.delay(policypk=instance.pk) generate_agent_checks_from_policies_task.delay(policypk=instance.pk)
elif instance.agent != None: elif instance.agent:
instance.agent.generate_checks_from_policies() instance.agent.generate_checks_from_policies()
# Checks that are updated except for agent # Checks that are updated except for agent
else: else:
if instance.policy != None: if instance.policy:
generate_agent_checks_from_policies_task.delay(policypk=instance.pk) generate_agent_checks_from_policies_task.delay(policypk=instance.pk)
@ -30,15 +30,15 @@ def post_save_check_handler(sender, instance, created, **kwargs):
def pre_delete_check_handler(sender, instance, **kwargs): def pre_delete_check_handler(sender, instance, **kwargs):
# don't run when policy managed check is saved # don't run when policy managed check is saved
if instance.managed_by_policy == True: if instance.managed_by_policy:
return return
# Policy check deleted # Policy check deleted
if instance.policy != None: if instance.policy:
generate_agent_checks_from_policies_task.delay(policypk=instance.pk) generate_agent_checks_from_policies_task.delay(policypk=instance.pk)
# Agent check deleted # Agent check deleted
elif instance.agent != None: elif instance.agent:
instance.agent.generate_checks_from_policies() instance.agent.generate_checks_from_policies()