def addgroup(request): """ Add a new okconfig group """ c = {} c['messages'] = [] c['errors'] = [] # If there is a problem with the okconfig setup, lets display an error if not okconfig.is_valid(): return verify_okconfig(request) if request.method == 'GET': f = forms.AddGroupForm(initial=request.GET) elif request.method == 'POST': f = forms.AddGroupForm(request.POST) if f.is_valid(): group_name = f.cleaned_data['group_name'] alias = f.cleaned_data['alias'] force = f.cleaned_data['force'] try: c['filelist'] = okconfig.addgroup(group_name=group_name, alias=alias, force=force) c['group_name'] = group_name return addcomplete(request, c) except Exception as e: c['errors'].append(_("error adding group: %s") % e) else: c['errors'].append(_('Could not validate input')) else: raise Exception("Sorry i only support GET or POST") c['form'] = f return render_to_response('addgroup.html', c, context_instance=RequestContext(request))
def addhost(request): c = {} c['messages'] = [] c['errors'] = [] # If there is a problem with the okconfig setup, lets display an error if not okconfig.is_valid(): return verify_okconfig(request) if request.method == 'GET': f = forms.AddHostForm(initial=request.GET) elif request.method == 'POST': f = forms.AddHostForm(request.POST) if f.is_valid(): host_name = f.cleaned_data['host_name'] group_name = f.cleaned_data['group_name'] address = f.cleaned_data['address'] templates = f.cleaned_data['templates'] #description = f.cleaned_data['description'] force = f.cleaned_data['force'] try: c['filelist'] = okconfig.addhost(host_name=host_name, group_name=group_name, address=address, force=force, templates=templates) c['host_name'] = host_name return HttpResponseRedirect( reverse('okconfig_.views.edit', args=[host_name])) except Exception, e: c['errors'].append("error adding host: %s" % e) else: c['errors'].append('Could not validate input')
def addtemplate(request, host_name=None): """ Add a new okconfig template to a host """ c = {} c['messages'] = [] c['errors'] = [] # If there is a problem with the okconfig setup, lets display an error if not okconfig.is_valid(): return verify_okconfig(request) c['form'] = forms.AddTemplateForm(initial=request.GET) if request.method == 'POST': c['form'] = f = forms.AddTemplateForm(request.POST) if f.is_valid(): try: f.save() c['host_name'] = host_name = f.cleaned_data['host_name'] c['filelist'] = f.filelist c['messages'].append( _("Template was successfully added to host.")) return HttpResponseRedirect(reverse('adagios.okconfig_.views.edit', args=[host_name])) except Exception, e: c['errors'].append(e) else: c['errors'].append(_("Could not validate form"))
def addgroup(request): """ Add a new okconfig group """ c = {} c['messages'] = [] c['errors'] = [] # If there is a problem with the okconfig setup, lets display an error if not okconfig.is_valid(): return verify_okconfig(request) if request.method == 'GET': f = forms.AddGroupForm(initial=request.GET) elif request.method == 'POST': f = forms.AddGroupForm(request.POST) if f.is_valid(): group_name = f.cleaned_data['group_name'] alias = f.cleaned_data['alias'] #description = f.cleaned_data['description'] force = f.cleaned_data['force'] try: c['filelist'] = okconfig.addgroup( group_name=group_name, alias=alias, force=force) c['group_name'] = group_name return addcomplete(request, c) except Exception, e: c['errors'].append(_("error adding group: %s") % e) else: c['errors'].append(_('Could not validate input'))
def scan_network(request): """ Scan a single network and show hosts that are alive """ c = {} c['errors'] = [] if not okconfig.is_valid(): return verify_okconfig(request) if request.method == 'GET': if request.GET.has_key('network_address'): initial = request.GET else: my_ip = okconfig.network_scan.get_my_ip_address() network_address = "%s/28" % my_ip initial = {'network_address': network_address} c['form'] = forms.ScanNetworkForm(initial=initial) elif request.method == 'POST': c['form'] = forms.ScanNetworkForm(request.POST) if not c['form'].is_valid(): c['errors'].append(_("could not validate form")) else: network = c['form'].cleaned_data['network_address'] try: c['scan_results'] = okconfig.network_scan.get_all_hosts( network) for i in c['scan_results']: i.check() except Exception, e: c['errors'].append(_("Error running scan"))
def scan_network(request): """ Scan a single network and show hosts that are alive """ c = {} c['errors'] = [] if not okconfig.is_valid(): return verify_okconfig(request) if request.method == 'GET': if 'network_address' in request.GET: initial = request.GET else: my_ip = okconfig.network_scan.get_my_ip_address() network_address = "%s/28" % my_ip initial = {'network_address': network_address} c['form'] = forms.ScanNetworkForm(initial=initial) elif request.method == 'POST': c['form'] = forms.ScanNetworkForm(request.POST) if not c['form'].is_valid(): c['errors'].append(_("could not validate form")) else: network = c['form'].cleaned_data['network_address'] try: c['scan_results'] = okconfig.network_scan.get_all_hosts( network) for i in c['scan_results']: i.check() except Exception as e: c['errors'].append(_("Error running scan")) return render_to_response('scan_network.html', c, context_instance=RequestContext(request))
def addgroup(request): c = {} c['messages'] = [] c['errors'] = [] # If there is a problem with the okconfig setup, lets display an error if not okconfig.is_valid(): return verify_okconfig(request) if request.method == 'GET': f = forms.AddGroupForm(initial=request.GET) elif request.method == 'POST': f = forms.AddGroupForm(request.POST) if f.is_valid(): group_name = f.cleaned_data['group_name'] alias = f.cleaned_data['alias'] #description = f.cleaned_data['description'] force = f.cleaned_data['force'] try: c['filelist'] = okconfig.addgroup(group_name=group_name, alias=alias, force=force) c['group_name'] = group_name return addcomplete(request, c) except Exception, e: c['errors'].append("error adding group: %s" % e) else: c['errors'].append('Could not validate input')
def addgroup(request): """ Add a new okconfig group """ c = {} c["messages"] = [] c["errors"] = [] # If there is a problem with the okconfig setup, lets display an error if not okconfig.is_valid(): return verify_okconfig(request) if request.method == "GET": f = forms.AddGroupForm(initial=request.GET) elif request.method == "POST": f = forms.AddGroupForm(request.POST) if f.is_valid(): group_name = f.cleaned_data["group_name"] alias = f.cleaned_data["alias"] # description = f.cleaned_data['description'] force = f.cleaned_data["force"] try: c["filelist"] = okconfig.addgroup(group_name=group_name, alias=alias, force=force) c["group_name"] = group_name return addcomplete(request, c) except Exception, e: c["errors"].append("error adding group: %s" % e) else: c["errors"].append("Could not validate input")
def addhost(request): """ Add a new host from an okconfig template """ c = {} c["messages"] = [] c["errors"] = [] # If there is a problem with the okconfig setup, lets display an error if not okconfig.is_valid(): return verify_okconfig(request) if request.method == "GET": f = forms.AddHostForm(initial=request.GET) elif request.method == "POST": f = forms.AddHostForm(request.POST) if f.is_valid(): host_name = f.cleaned_data["host_name"] group_name = f.cleaned_data["group_name"] address = f.cleaned_data["address"] templates = f.cleaned_data["templates"] # description = f.cleaned_data['description'] force = f.cleaned_data["force"] try: c["filelist"] = okconfig.addhost( host_name=host_name, group_name=group_name, address=address, force=force, templates=templates ) c["host_name"] = host_name return addcomplete(request, c) except Exception, e: c["errors"].append("error adding host: %s" % e) else: c["errors"].append("Could not validate input")
def addhost(request): c = {} c['messages'] = [] c['errors'] = [] # If there is a problem with the okconfig setup, lets display an error if not okconfig.is_valid(): return verify_okconfig(request) if request.method == 'GET': f = forms.AddHostForm(initial=request.GET) elif request.method == 'POST': f = forms.AddHostForm(request.POST) if f.is_valid(): host_name = f.cleaned_data['host_name'] group_name = f.cleaned_data['group_name'] address = f.cleaned_data['address'] templates = f.cleaned_data['templates'] #description = f.cleaned_data['description'] force = f.cleaned_data['force'] try: c['filelist'] = okconfig.addhost(host_name=host_name,group_name=group_name,address=address,force=force,templates=templates) c['host_name'] = host_name return HttpResponseRedirect( reverse('okconfig_.views.edit', args=[host_name] ) ) except Exception, e: c['errors'].append( "error adding host: %s" % e ) else: c['errors'].append( 'Could not validate input')
def addhost(request): """ Add a new host from an okconfig template """ c = {} c['messages'] = [] c['errors'] = [] # If there is a problem with the okconfig setup, lets display an error if not okconfig.is_valid(): return verify_okconfig(request) if request.method == 'GET': f = forms.AddHostForm(initial=request.GET) elif request.method == 'POST': f = forms.AddHostForm(request.POST) if f.is_valid(): host_name = f.cleaned_data['host_name'] group_name = f.cleaned_data['group_name'] address = f.cleaned_data['address'] templates = f.cleaned_data['templates'] #description = f.cleaned_data['description'] force = f.cleaned_data['force'] try: c['filelist'] = okconfig.addhost(host_name=host_name, group_name=group_name, address=address, force=force, templates=templates) c['host_name'] = host_name return addcomplete(request, c) except Exception, e: c['errors'].append(_("error adding host: %s") % e) else: c['errors'].append(_('Could not validate input'))
def config_health( request ): c = dict() c['messages'] = m = [] c['object_health'] = s = {} c['booleans'] = {} services_no_description = Model.Service.objects.filter(register="1", service_description=None) hosts_without_contacts = [] hosts_without_services =[] objects_with_invalid_parents = [] services_without_contacts = [] services_using_hostgroups = [] services_without_icon_image = [] c['booleans']['Nagios Service has been reloaded since last configuration change'] = not Model.config.needs_reload() c['booleans']['Adagios configuration cache is up-to-date'] = not Model.config.needs_reparse() c['errors'] = Model.config.errors try: import okconfig c['booleans']['OKConfig is installed and working'] = okconfig.is_valid() except Exception: c['booleans']['OKConfig is installed and working'] = False s['Parser errors'] = Model.config.errors s['Services with no "service_description"'] = services_no_description s['Hosts without any contacts'] = hosts_without_contacts s['Services without any contacts'] = services_without_contacts s['Objects with invalid "use" attribute'] = objects_with_invalid_parents s['Services applied to hostgroups'] = services_using_hostgroups s['Services without a logo'] = services_without_icon_image s['Hosts without Service Checks'] = hosts_without_services if request.GET.has_key('show') and s.has_key( request.GET['show'] ): objects = s[request.GET['show']] return list_objects(request,display_these_objects=objects ) else: return render_to_response('suggestions.html', c, context_instance = RequestContext(request))
def addtemplate(request, host_name=None): """ Add a new okconfig template to a host """ c = {} c['messages'] = [] c['errors'] = [] # If there is a problem with the okconfig setup, lets display an error if not okconfig.is_valid(): return verify_okconfig(request) c['form'] = forms.AddTemplateForm(initial=request.GET) if request.method == 'POST': c['form'] = f = forms.AddTemplateForm(request.POST) if f.is_valid(): try: f.save() c['host_name'] = host_name = f.cleaned_data['host_name'] c['filelist'] = f.filelist c['messages'].append( "Template was successfully added to host.") return HttpResponseRedirect( reverse('adagios.okconfig_.views.edit', args=[host_name])) except Exception, e: c['errors'].append(e) else: c['errors'].append("Could not validate form")
def scan_network(request): c = {} c['errors'] = [] if not okconfig.is_valid(): return verify_okconfig(request) if request.method == 'GET': if request.GET.has_key('network_address'): initial = request.GET else: my_ip = okconfig.network_scan.get_my_ip_address() network_address = "%s/28" % my_ip initial = {'network_address': network_address} c['form'] = forms.ScanNetworkForm(initial=initial) elif request.method == 'POST': c['form'] = forms.ScanNetworkForm(request.POST) if not c['form'].is_valid(): c['errors'].append("could not validate form") else: network = c['form'].cleaned_data['network_address'] try: c['scan_results'] = okconfig.network_scan.get_all_hosts( network) for i in c['scan_results']: i.check() except Exception, e: c['errors'].append("Error running scan")
def config_health(request): """ Display possible errors in your nagios config """ c = dict() c['messages'] = [] c['object_health'] = s = {} c['booleans'] = {} services_no_description = Model.Service.objects.filter( register="1", service_description=None) hosts_without_contacts = [] hosts_without_services = [] objects_with_invalid_parents = [] services_without_contacts = [] services_using_hostgroups = [] services_without_icon_image = [] c['booleans'][ 'Nagios Service has been reloaded since last configuration change'] = not Model.config.needs_reload( ) c['booleans'][ 'Adagios configuration cache is up-to-date'] = not Model.config.needs_reparse( ) for i in Model.config.errors: if i.item: Class = Model.string_to_class[i.item['meta']['object_type']] i.model = Class(item=i.item) c['parser_errors'] = Model.config.errors try: import okconfig c['booleans']['OKConfig is installed and working'] = okconfig.is_valid( ) except Exception: c['booleans']['OKConfig is installed and working'] = False s['Parser errors'] = Model.config.errors s['Services with no "service_description"'] = services_no_description s['Hosts without any contacts'] = hosts_without_contacts s['Services without any contacts'] = services_without_contacts s['Objects with invalid "use" attribute'] = objects_with_invalid_parents s['Services applied to hostgroups'] = services_using_hostgroups s['Services without a logo'] = services_without_icon_image s['Hosts without Service Checks'] = hosts_without_services if request.GET.has_key('show') and s.has_key(request.GET['show']): objects = s[request.GET['show']] return list_objects(request, display_these_objects=objects) else: return render_to_response('suggestions.html', c, context_instance=RequestContext(request))
def config_health(request): """ Display possible errors in your nagios config """ c = dict() c["messages"] = [] c["object_health"] = s = {} c["booleans"] = {} services_no_description = Model.Service.objects.filter(register="1", service_description=None) hosts_without_contacts = [] hosts_without_services = [] objects_with_invalid_parents = [] services_without_contacts = [] services_using_hostgroups = [] services_without_icon_image = [] c["booleans"][ _("Nagios Service has been reloaded since last configuration change") ] = not Model.config.needs_reload() c["booleans"][_("Adagios configuration cache is up-to-date")] = not Model.config.needs_reparse() for i in Model.config.errors: if i.item: Class = Model.string_to_class[i.item["meta"]["object_type"]] i.model = Class(item=i.item) c["parser_errors"] = Model.config.errors try: import okconfig c["booleans"][_("OKConfig is installed and working")] = okconfig.is_valid() except Exception: c["booleans"][_("OKConfig is installed and working")] = False s["Parser errors"] = Model.config.errors s['Services with no "service_description"'] = services_no_description s["Hosts without any contacts"] = hosts_without_contacts s["Services without any contacts"] = services_without_contacts s['Objects with invalid "use" attribute'] = objects_with_invalid_parents s["Services applied to hostgroups"] = services_using_hostgroups s["Services without a logo"] = services_without_icon_image s["Hosts without Service Checks"] = hosts_without_services if request.GET.has_key("show") and s.has_key(request.GET["show"]): objects = s[request.GET["show"]] return search_objects(request, objects=objects) else: return render_to_response("suggestions.html", c, context_instance=RequestContext(request))
def test_valid(self): """Is the overall configuration valid""" self.assertTrue(okconfig.is_valid())
def test_invalid_okconfig_destination_directory(self): """Is the destination directory and parent writable""" okconfig.destination_directory = "invalid" self.assertFalse(okconfig.is_valid())
def test_invalid_okconfig_template_dir(self): """Is the okconfig template directory valid""" okconfig.template_directory = "invalid" self.assertFalse(okconfig.is_valid())
def test_invalid_main_config(self): """Is the nagios config there and writable""" okconfig.nagios_config = "invalid" self.assertFalse(okconfig.is_valid())