Object inheritance and templates

Support requests, bug reports, questions etc.
Locked
branfarm
Object inheritance and templates

Post by branfarm » Wed Mar 18, 2009 14:49

Hi there. I just installed Nconf and I love it! I have a question though, about being able to add templates to inherit various items into service and host definitions. I'm interested in adding both a host and service definition, unregistered, that can pass an action URL element onto any of my host/services. I've successfully added a service-template class, and added a name, register, and action URL items. What I notice, however, is that it never gets written to a config anywhere. So when I try to take the next step to add it to a service, the configs will never generate because they don't know about the service template. Is there a way to configure nconf to generate a new file to contain templates?

branfarm

Re: Object inheritance and templates

Post by branfarm » Wed Mar 18, 2009 14:55

Just to clarify what I've done so far:

1. I added a new class called 'Service-template'
2. I added three attributes to the 'Service-template' class: name (the name of the service), register (whether or not to include it as an active service), and action_url (the URL for the action I would like to define).
3. I created a service template.

Up to this point the nagios configs still generate fine, although I don't get the service template output anywhere. The next steps would be to add a 'use' attribute to th service class, which would allow me to specify which service-template to use with a service. That obviously won't work at this point though, due to the fact that Nagios won't know anything about the service template I created.

User avatar
agargiulo
NConf developer
NConf developer
Posts:725
Joined:Fri Mar 06, 2009 17:50
Location:Zurich, Switzerland
Contact:

Re: Object inheritance and templates

Post by agargiulo » Thu Mar 19, 2009 12:03

Hi.

Thank you very much for this question and opportunity to explain one of the true strength of NConf: the abstract data model.
Thanks to the way the database is modelled, it is very easy to extend the functionality by adding additional classes and attributes via the GUI, just like you've done. It's also possible to link the new items with existing item types like 'hosts' or 'services'. The GUI is generated dynamically, based on the attributes you define, so there is no modification necessary there.

Theoretically it would be possible to use NConf to store any information or configuration you like. All you need is an export functionality which exports the database content to a format of your choice (we are working on a database API). In the case of Nagios, we are currently supplying a script named "generate_config.pl", which will generate your Nagios configuration files. The script is relatively static. You are welcome to make any extensions you like but please keep in mind:
We do not support any issues caused by customizations of the generate_config.pl script, or by changing any attribute names, datatypes, classes or any other changes to the schema. Also, we cannot consider any custom changes in future updates.
That said, let's talk about your specific case ;)
You have created a class named "service-template" and have added 2-3 attributes. Now you would like to have these items generated to a separate file. For this you will need to edit the generate_config.pl script.

Add the following below line 243 (within 'create_global_config' function):

Code: Select all

    # fetch all service-template ID's
    $sth = $dbh->prepare("SELECT id_item FROM ConfigItems,ConfigClasses
                            WHERE id_class=fk_id_class
                                AND config_class = 'service-template'
                                ORDER BY id_item");
    $sth->execute();
    $queryref  = $sth->fetchall_arrayref;
    my @srv_templates = @$queryref;

    push(@global_cfg_files, "$global_path/service_templates.cfg");
    &write_file("$global_path/service_templates.cfg","",\@srv_templates);
This will generate a new file called "service_templates.cfg" in the "global" folder of the 'output/NagiosConfig.tgz' package.

Next, you will probably want to link your new 'service-template' items with 'host' items. For this you will have to add a linking attribute either to the 'host' or 'service-template' class, depending on which file you would like the new information to show up in:
Via the GUI select 'Attributes' > 'Add' and create a new attribute of the type 'assign_one' (or 'assign_many'). Set the 'attribute belongs to class' and 'items of class to be assigned' flags respective to your needs. Also, don't forget to set the 'write attribute to configuration' flag to 'yes'.

That should do exactly what you were looking for. Let us know if it worked.
Regards,
Angelo

Melanie__

Re: Object inheritance and templates

Post by Melanie__ » Tue Mar 24, 2009 15:45

hi

i am at the moment working on this thing and it seems to be more tricky than expected.

if I take the code as is i get

Code: Select all

define {
but it is needed for nagios to look like this:

Code: Select all

define service {
somehow the script seems not to allow to put services in two different files because if i put the line to this

Code: Select all

push(@global_cfg_files, "$global_path/service_templates.cfg");
    &write_file("$global_path/service_templates.cfg","service",\@srv_templates);

it doesn't even create a line with a define. where i think the script passes out because of duplicate definition.

but otherwise very good work and i will start to include it in my workshops.

thanks

Melanie__

Melanie__

Re: Object inheritance and templates

Post by Melanie__ » Wed Mar 25, 2009 11:49

okay i found a way to get it how it should be done.

Code: Select all

    push(@global_cfg_files, "$global_path/service-templates.cfg");
    &write_file("$global_path/service_templates.cfg","service-tpl",\@srv_templates);
    #!/bin/bash
    /bin/sed -i {s/service-tpl/service/g} $global_path/service_templates.cfg
    #!/usr/bin/perl

not a really nice way but a fast and easy way to get it done. maybe someone finds a better way to get this done.

User avatar
agargiulo
NConf developer
NConf developer
Posts:725
Joined:Fri Mar 06, 2009 17:50
Location:Zurich, Switzerland
Contact:

Re: Object inheritance and templates

Post by agargiulo » Wed Mar 25, 2009 12:01

Hi Melanie.

Thank you for posting this. As you can see, the generate_config script is quite static. As long as you work with the default NConf functionality, you're fine, but as soon as you start extenfing the data schema (classes & attrs), you run into the problems you've mentioned.

This is something we are aware of and that we will improve step by step in the coming releases. The goal is to have a handy import / export API in the end.

Regards,
Angelo

Melanie__

Re: Object inheritance and templates

Post by Melanie__ » Wed Mar 25, 2009 14:23

yes it is kind of static but that doesn't mean it is bad it will evolve.

here is now a clean implementation of the above thing please mention that per default the file is called service-templates.cfg as which i wrote it in the script.

Code: Select all

    # fetch all service-template ID's
    $sth = $dbh->prepare("SELECT id_item FROM ConfigItems,ConfigClasses
                                WHERE id_class=fk_id_class
                                AND config_class = 'service-template'
                                ORDER BY id_item");
    $sth->execute();
    $queryref  = $sth->fetchall_arrayref;
    my @srv_templates = @$queryref;

    push(@global_cfg_files, "$global_path/service-templates.cfg");
    &write_file("$global_path/service-templates.cfg","service-tpl",\@srv_templates);
    system("/bin/sed -i {s/service-tpl/service/g} $global_path/service-templates.cfg")


maybe you want to include it in your script.

ChHager

Re: Object inheritance and templates

Post by ChHager » Wed May 06, 2009 11:34

Hi there,

I've been watching Your project for some time and am tinkering about using NConf.

Reading Your remarks about the possibility of using NConf to generate any kind of configuration (not just for Nagios) and looking at Your way of doing the sanity-check of the Nagios config files by means of Nagios itselfe, my question is: are You guys planning on implementing some kind of sanity-check for the configuration of other daemons? i.e. DNS, SSH, APACHE, ...? That would open up NConf to turn into some neat management tool or add-on to an existing management database.

Curiously,

Chris

User avatar
agargiulo
NConf developer
NConf developer
Posts:725
Joined:Fri Mar 06, 2009 17:50
Location:Zurich, Switzerland
Contact:

Re: Object inheritance and templates

Post by agargiulo » Wed May 06, 2009 12:50

Hi.

This is something we've also been thinking of. The database structure of NConf would theoretically allow to store any kind of configuration or data. We did certain tests and the outcome was that in order to offer full flexibility, we would have to do several extensions to the GUI. Users might want to configure their own views or lists, and right now NConf is primarily aimed at Nagios, so there are a few hardcoded things which would have to be changed. One of these things would also be the sanity-checks we do in the end.

To answer your question, we've thought of the potential, but there is no effort going on right now towards making the tool usable for other daemons. We also have not looked into similar projects out there. Perhaps someone has already implemented similar functionality.

One thing we will implement in the next release is that we will increase the flexibility of the generate_config script, so that users can implement additional objects more easily.

Thank you for bringing it up though.

Locked