Can hostgroup be listed in the order of its alias?

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
staceygong
Posts: 10
Joined: Tue Nov 01, 2016 6:19 am

Can hostgroup be listed in the order of its alias?

Post by staceygong »

Hi,

I am using Nagios® Core™ Version 3.5.1.

Now when I log on to http://mynagiosserver/nagios, the hostgroup is listed in the alphabetical order of the hostgroup_name as definied in the /etc/nagios/objects/hostgroups.cfg file.

I am wondering if there is a way to list the hostgroups in the alphabetical order of the hostgroup alias? Because I can't change my hostgroup_name, but I can change their alias.

Or is there a way to have the web page list the hostgroup in the order same as the order in the file /etc/nagios/objects/hostgroups.cfg?

Thanks,
Stacey
Last edited by dwhitfield on Tue Jan 10, 2017 11:27 am, edited 1 time in total.
Reason: marking with green check mark
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Can hostgroup be listed in the order of its alias?

Post by rkennedy »

As Core is open source, you could technically do this, but it is beyond what we can help with on the Support side of things.

If you'd like it built in, feel free to file a FR on our GitHub @ https://github.com/NagiosEnterprises/nagioscore/issues
Former Nagios Employee
staceygong
Posts: 10
Joined: Tue Nov 01, 2016 6:19 am

Re: Can hostgroup be listed in the order of its alias?

Post by staceygong »

Hi,

We figured it out. I will post our solution here, in case it may help other people.

Problem:
By default, the nagios server sorts the based on the hostgroup_name, which is a fixed value specified in database computers table "nagios_hostgroup". This does not supply enough flexibility for our needs.
Rather, we would like to have control of the display sorting so that we can arrange the hostgroups in the order that we prefer.

Solution:
The nagios server source code has been modified (xodtemplate.c, xodtemplate.h) and a newly compiled status.cgi is used in place of the standard status.cgi supplied by nagios (in their source code version or rpm package).

Result:
We now use the "notes" field. We do this by supplying an arbitrary number in the notes field for sorting the hostgroup_name in the hostgroup.cfg file. Note that, with this new mechanism in place, if the notes field has no value, we place a value of "0" by default. The sort is an ASCII character sort, therefore, numbers come before letters, numbers are in the order of 0-9, capital letters come before lower case, therefore, A-Z precedes a-z.

What was done in the source code (xodtemplate.c, xodtemplate.h):

1. The nagios status.cgi file reads a static configuration file. The static file is created each time the nagios server is restarted.
2. The server builds a data structure, adding to it each time it reads a value for the hostgroup_name. As the data structure is filled in, the list ends up being sorted (let's not worry about the sort routine for now, it is enough to know that the list is sorted alphabetically each time a node is added, therefore, no complete sort is done at the end, it is sorted as each element gets added).
3. When a hostgroup_name is read, an entry is made into the data structure holding the hostgroups. We now add a value of "0", by default, as a value for the notes field.
4. If a value is supplied for the notes field, the value of "0" is removed and the value supplied in the configuration files is used.
5. Once the configuration file reading has been completed and the data structures completely filled in, we apply our own sort.
--Two structures are maintained for a list, therefore, two structures need to be sorted (we use a sort for one, we wipe and recreate the second). This is done in a new function that has been added, xodtemplate_resort_hostgroup_by_notes():
------The first structure, xodtemplate_hostgroup_list, is sorted in place (this is a linked list, a bubblesort is used to complete the sort)
------The second structure, xobject_skiplists[X_HOSTGROUP_SKIPLIST] (a skiplist), is wiped, and then reconstructed from the xodtemplate_hostgroup_list
6.In short, two places of the source code needed to be modified:
--The place where the notes field gets a default value (as well as freed if there is a value supplied in the configuration files)
--The sort function mentioned above
--The node sort comparison routine, which uses the notes field rather than the hostgroup_name field.

Regards,
Stacey
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: Can hostgroup be listed in the order of its alias?

Post by dwhitfield »

Thanks so much for posting the solution.

For those that are coming to this in the future, please note that 3.5.1 is very old, and you should upgrade to the latest version before posting on github.

I'm going to lock this up.
Locked