Ejemplo n.º 1
0
    def test_alias(self):
        """Add a host with an alias"""
        okconfig.addhost('www.okconfig.org', alias="The alias host")

        hosts = Model.Host.objects.filter(host_name="www.okconfig.org")
        self.assertTrue(len(hosts) == 1)
        self.assertEqual(hosts[0].alias, "The alias host")
Ejemplo n.º 2
0
    def test_address(self):
        """Add a host with an address"""
        okconfig.addhost('www.okconfig.org', address="192.168.1.1")

        hosts = Model.Host.objects.filter(host_name="www.okconfig.org")
        self.assertTrue(len(hosts) == 1)
        self.assertEqual(hosts[0].address, "192.168.1.1")
Ejemplo n.º 3
0
    def test_alias(self):
        """Add a host with an alias"""
        okconfig.addhost('www.okconfig.org', alias="The alias host")

        hosts = Model.Host.objects.filter(host_name="www.okconfig.org")
        self.assertTrue(len(hosts) == 1)
        self.assertEqual(hosts[0].alias, "The alias host")
Ejemplo n.º 4
0
    def test_multiple_to_group(self):
        """Add 2 hosts to a group"""
        okconfig.addhost('www.okconfig.org', group_name="multigroup")
        okconfig.addhost('okconfig.org', group_name="multigroup")

        hosts = Model.Host.objects.filter(contact_groups="multigroup")
        self.assertEqual(len(hosts), 2)
Ejemplo n.º 5
0
    def test_address(self):
        """Add a host with an address"""
        okconfig.addhost('www.okconfig.org', address="192.168.1.1")

        hosts = Model.Host.objects.filter(host_name="www.okconfig.org")
        self.assertTrue(len(hosts) == 1)
        self.assertEqual(hosts[0].address, "192.168.1.1")
Ejemplo n.º 6
0
    def test_multiple_to_group(self):
        """Add 2 hosts to a group"""
        okconfig.addhost('www.okconfig.org', group_name="multigroup")
        okconfig.addhost('okconfig.org', group_name="multigroup")

        hosts = Model.Host.objects.filter(contact_groups="multigroup")
        self.assertEqual(len(hosts), 2)
Ejemplo n.º 7
0
    def test_template(self):
        """Add a host with a template"""
        okconfig.addhost('okconfig.org', templates=['linux'])

        services = [s.use for s in Model.Service.objects.filter(
            host_name='okconfig.org')]
        for template in ['okc-linux-check_disks',
                         'okc-linux-check_load']:
            self.assertTrue(template in services)
Ejemplo n.º 8
0
    def test_template(self):
        """Add a host with a template"""
        okconfig.addhost('okconfig.org', templates=['linux'])

        services = [
            s.use
            for s in Model.Service.objects.filter(host_name='okconfig.org')
        ]
        for template in ['okc-linux-check_disks', 'okc-linux-check_load']:
            self.assertTrue(template in services)
Ejemplo n.º 9
0
    def test_basic(self):
        """Basic addition of host"""
        okconfig.addhost('okconfig.org')

        hosts = Model.Host.objects.filter(host_name='okconfig.org')
        self.assertTrue(len(hosts) == 1)
        self.assertEqual(hosts[0].use, "okc-default-host")
        self.assertEqual(hosts[0].contact_groups, "default")
        self.assertEqual(hosts[0].hostgroups, "default")

        filename = hosts[0].get_filename()
        self.assertEqual(filename.split('/')[-2], 'default')
Ejemplo n.º 10
0
    def test_basic(self):
        """Basic addition of host"""
        okconfig.addhost('okconfig.org')

        hosts = Model.Host.objects.filter(host_name='okconfig.org')
        self.assertTrue(len(hosts) == 1)
        self.assertEqual(hosts[0].use, "okc-default-host")
        self.assertEqual(hosts[0].contact_groups, "default")
        self.assertEqual(hosts[0].hostgroups, "default")

        filename = hosts[0].get_filename()
        self.assertEqual(filename.split('/')[-2], 'default')
Ejemplo n.º 11
0
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')
Ejemplo n.º 12
0
    def test_group(self):
        """Addition of host within a specific group

        It should automatically create the group
        """
        okconfig.addhost('okconfig.org', group_name='testgroup')

        hosts = Model.Host.objects.filter(host_name='okconfig.org')

        self.assertTrue(len(hosts) == 1)
        self.assertEqual(hosts[0].use, "okc-default-host")
        self.assertEqual(hosts[0].contact_groups, "testgroup")
        self.assertEqual(hosts[0].hostgroups, "testgroup")

        filename = hosts[0].get_filename()
        self.assertEqual(filename.split('/')[-2], 'testgroup')
Ejemplo n.º 13
0
    def test_group(self):
        """Addition of host within a specific group

        It should automatically create the group
        """
        okconfig.addhost('okconfig.org', group_name='testgroup')

        hosts = Model.Host.objects.filter(host_name='okconfig.org')

        self.assertTrue(len(hosts) == 1)
        self.assertEqual(hosts[0].use, "okc-default-host")
        self.assertEqual(hosts[0].contact_groups, "testgroup")
        self.assertEqual(hosts[0].hostgroups, "testgroup")

        filename = hosts[0].get_filename()
        self.assertEqual(filename.split('/')[-2], 'testgroup')
Ejemplo n.º 14
0
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")
Ejemplo n.º 15
0
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'))
Ejemplo n.º 16
0
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')
Ejemplo n.º 17
0
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'))
Ejemplo n.º 18
0
def set_network_parents(host_name, method="traceroute"):
    """ Autocreates network parents for a given host """
    if method == 'traceroute':
        parents = traceroute(host=host_name)
        print len(parents), "parents found"
        previous_host = None
        for i in parents:
            hosts = Model.Host.objects.filter(address=i)
            if len(hosts) == 0:
                print "adding host ", i
                dnsname = get_hostname(i)
                if dnsname is None:
                    dnsname = i
                okconfig.addhost(host_name=dnsname, address=i)
                hosts = Model.Host.objects.filter(address=i)
            host = hosts[0]
            if previous_host is not None:
                print previous_host.address, "->", host.address
                host.parents = previous_host.host_name
                host.save()
            previous_host = host
Ejemplo n.º 19
0
def set_network_parents(host_name, method="traceroute"):
    """ Autocreates network parents for a given host """
    if method == 'traceroute':
        parents = traceroute(host=host_name)
        print(len(parents), "parents found")
        previous_host = None
        for i in parents:
            hosts = Model.Host.objects.filter(address=i)
            if len(hosts) == 0:
                print("adding host ", i)
                dnsname = get_hostname(i)
                if dnsname is None:
                    dnsname = i
                okconfig.addhost(host_name=dnsname, address=i)
                hosts = Model.Host.objects.filter(address=i)
            host = hosts[0]
            if previous_host is not None:
                print(previous_host.address, "->", host.address)
                host.parents = previous_host.host_name
                host.save()
            previous_host = host
Ejemplo n.º 20
0
    def setUp(self):
        super(Template, self).setUp()

        okconfig.addhost("www.okconfig.org")
        okconfig.addhost("okconfig.org")
        okconfig.addhost("aliased.okconfig.org",
                         address="192.168.1.1",
                         group_name="testgroup")
Ejemplo n.º 21
0
    def setUp(self, test=None):
        """
        Sets up the nagios fake environment and overrides okconfig configuration
        variables to make changes within it.
        """
        global _environment
        _environment = FakeNagiosEnvironment()
        _environment.create_minimal_environment()
        copytree(os.path.realpath("../usr/share/okconfig/templates"),
                 _environment.tempdir + "/conf.d/okconfig-templates")
        _environment.update_model()

        for var in [
                'nagios_config', 'destination_directory', 'examples_directory',
                'examples_directory_local', 'template_directory'
        ]:
            _okconfig_overridden_vars[var] = getattr(okconfig, var)

        okconfig.nagios_config = _environment.get_config().cfg_file
        config.nagios_config = okconfig.nagios_config
        config.git_commit_changes = 0
        okconfig.destination_directory = _environment.objects_dir
        okconfig.examples_directory = "../usr/share/okconfig/examples"
        okconfig.template_directory = "../usr/share/okconfig/templates"
        okconfig.examples_directory_local = _environment.tempdir + "/okconfig"

        os.mkdir(okconfig.examples_directory_local)

        okconfig.addhost("linux.okconfig.org",
                         address="192.168.1.1",
                         templates=["linux"])
        okconfig.addhost("windows.okconfig.org",
                         address="192.168.1.2",
                         templates=["windows"])
        okconfig.addhost("webserver.okconfig.org",
                         address="192.168.1.2",
                         templates=["http"])
Ejemplo n.º 22
0
    def setUp(self, test=None):
        """
        Sets up the nagios fake environment and overrides okconfig configuration
        variables to make changes within it.
        """
        global _environment
        _environment = FakeNagiosEnvironment()
        _environment.create_minimal_environment()
        copytree(os.path.realpath("../usr/share/okconfig/templates"),
                 _environment.tempdir + "/conf.d/okconfig-templates")
        _environment.update_model()

        for var in ['nagios_config', 'destination_directory',
                    'examples_directory', 'examples_directory_local',
                    'template_directory']:
            _okconfig_overridden_vars[var] = getattr(okconfig, var)

        okconfig.nagios_config = _environment.get_config().cfg_file
        config.nagios_config = okconfig.nagios_config
        config.git_commit_changes = 0
        okconfig.destination_directory = _environment.objects_dir
        okconfig.examples_directory = "../usr/share/okconfig/examples"
        okconfig.template_directory = "../usr/share/okconfig/templates"
        okconfig.examples_directory_local = _environment.tempdir + "/okconfig"

        os.mkdir(okconfig.examples_directory_local)


        okconfig.addhost("linux.okconfig.org",
                         address="192.168.1.1",
                         templates=["linux"])
        okconfig.addhost("windows.okconfig.org",
                         address="192.168.1.2",
                         templates=["windows"])
        okconfig.addhost("webserver.okconfig.org",
                         address="192.168.1.2",
                         templates=["http"])
isp_regex = "^isps.(?P<isp_id>\\d+)..* new isp.'(?P<ns1>.*?)','(?P<ns2>.*?)','(?P<ns3>.*?)','(?P<ns4>.*?)'.;"


hostnames = map(lambda x: x.host_name, pynag.Model.Host.objects.all)
# Lets iterate through 
for isp_line in isp_lines:
  m = re.search(isp_regex, isp_line)
  id = m.group('isp_id')
  ns1 = m.group('ns1')
  ns2 = m.group('ns2')
  ns3 = m.group('ns3')
  ns4 = m.group('ns4')
  #print companynames[id], ns1,ns2,ns3,ns4
  alias = companynames[id].encode('utf-8')
  print str(alias)
  for host in ns1,ns2,ns3,ns4:
    if not host:
      continue  # skip empty hostnames
    elif host in hostnames:
      print host, "already here, skipping"
      continue
    else:
      print host, "not found... adding it...",
      try:
        okconfig.addhost(host_name=host, alias=alias, group_name="nameservers", templates=["dnsserver"])
        print "done"
      except Exception, e:
        print "Could not add host:", e

Ejemplo n.º 24
0
isp_regex = "^isps.(?P<isp_id>\\d+)..* new isp.'(?P<ns1>.*?)','(?P<ns2>.*?)','(?P<ns3>.*?)','(?P<ns4>.*?)'.;"

hostnames = map(lambda x: x.host_name, pynag.Model.Host.objects.all)
# Lets iterate through
for isp_line in isp_lines:
    m = re.search(isp_regex, isp_line)
    id = m.group('isp_id')
    ns1 = m.group('ns1')
    ns2 = m.group('ns2')
    ns3 = m.group('ns3')
    ns4 = m.group('ns4')
    #print companynames[id], ns1,ns2,ns3,ns4
    alias = companynames[id].encode('utf-8')
    print str(alias)
    for host in ns1, ns2, ns3, ns4:
        if not host:
            continue  # skip empty hostnames
        elif host in hostnames:
            print host, "already here, skipping"
            continue
        else:
            print host, "not found... adding it...",
            try:
                okconfig.addhost(host_name=host,
                                 alias=alias,
                                 group_name="nameservers",
                                 templates=["dnsserver"])
                print "done"
            except Exception, e:
                print "Could not add host:", e
Ejemplo n.º 25
0
    def test_conflict(self):
        """Try to add a host that already exists"""
        okconfig.addhost('okconfig.org')

        self.assertRaises(okconfig.OKConfigError,
                          okconfig.addhost, 'okconfig.org')
Ejemplo n.º 26
0
    def test_force(self):
        """Test force adding a host"""
        okconfig.addhost('www.okconfig.org')

        self.assertRaises(okconfig.OKConfigError, okconfig.addhost,
                          'www.okconfig.org')
Ejemplo n.º 27
0
    def test_force(self):
        """Test force adding a host"""
        okconfig.addhost('www.okconfig.org')

        self.assertRaises(okconfig.OKConfigError, okconfig.addhost,
                          'www.okconfig.org')
Ejemplo n.º 28
0
    def test_conflict(self):
        """Try to add a host that already exists"""
        okconfig.addhost('okconfig.org')

        self.assertRaises(okconfig.OKConfigError, okconfig.addhost,
                          'okconfig.org')