Page 1 of 1

Service's Host's display_name?

Posted: Fri Jul 22, 2016 8:23 am
by Deantwo
Kinda a follow up to a previous thread of mind: (Thread) Usage of display_name in XI, ETA?

I started lightly modifying the Operations Center (http://<server-ip>/nagiosxi/includes/components/nocscreen/noc.php) 's PHP scripts.
I have a little experience with PHP, so I got my tiny changes working. I made all hosts that have a display_name defined to show that instead of the host's host_name.

I am however unable to do the same thing for services because they don't have the display_name of the host.
Instead it looks like the display_name of a service is just the same as the service's service_description.
Nagios Core - Object Definitions wrote:service_description;:
  • This directive is used to define the description of the service, which may contain spaces, dashes, and colons (semicolons, apostrophes, and quotation marks should be avoided). No two services associated with the same host can have the same description. Services are uniquely identified with their host_name and service_description directives.
display_name:
  • This directive is used to define an alternate name that should be displayed in the web interface for this service. If not specified, this defaults to the value you specify for the service_description directive. Note: The current CGIs do not use this option, although future versions of the web interface will.
So my question is, how would I get the service's host's display_name?
Is there no no way to make the service inherit the display_name of the host and save it somewhere?

I don't know what would be easiest and naming of possible new directives is not my say, but something along the lines of a "host_display_name" directive would be useful.
Either way, access to the host's display_name through the service would be very handy.

PS: There seems to be a stray ";" on HTML line 682 in the Nagios Core - Object Definitions document, see the quote above.

Re: Service's Host's display_name?

Posted: Fri Jul 22, 2016 10:43 am
by mcapra
The tricky thing is that a Service doesn't really know much about it's associated Host other than what the host's config name is. You would basically have to query the Host object for it's display name every time you tried to display a Service problem (pretty inefficient).

However, if style trumps efficiency for your use case you could try something like this in noc_service_row of nocscreenapi.php:

Code: Select all

$id = get_host_id($x->host_name);
$display_name = exec_sql_query(DB_NDOUTILS, 'SELECT display_name FROM nagios_hosts WHERE host_object_id=' . $id, false);
$display_name = substr($display_name, 13);
This worked for me to store the host's display name in $display_name. Used in noc_service_row line ~268:

Code: Select all

....
<td class='host_name " . host_class("$x->host_current_state") . "'><a style='color:black' href='../xicore/status.php?show=hostdetail&host={$x->host_name}'>{$display_name}</a></td>\n
....

Re: Service's Host's display_name?

Posted: Wed Jul 27, 2016 2:38 am
by Deantwo
mcapra wrote:The tricky thing is that a Service doesn't really know much about it's associated Host other than what the host's config name is. You would basically have to query the Host object for it's display name every time you tried to display a Service problem (pretty inefficient).

However, if style trumps efficiency for your use case you could try something like this in noc_service_row of nocscreenapi.php:

Code: Select all

$id = get_host_id($x->host_name);
$display_name = exec_sql_query(DB_NDOUTILS, 'SELECT display_name FROM nagios_hosts WHERE host_object_id=' . $id, false);
$display_name = substr($display_name, 13);
This worked for me to store the host's display name in $display_name. Used in noc_service_row line ~268:

Code: Select all

....
<td class='host_name " . host_class("$x->host_current_state") . "'><a style='color:black' href='../xicore/status.php?show=hostdetail&host={$x->host_name}'>{$display_name}</a></td>\n
....
Thanks, I got it to work nicely with this!

I do have a small issue when a display_name contains coma (','), the name is surrounded in quotes and I seem to be unable to really fix this as the string behaves oddly.
But I guess I can live with that until this actually gets implemented by you guys.

I have attached my little changes for interested people.

Re: Service's Host's display_name?

Posted: Wed Jul 27, 2016 9:28 am
by mcapra
str_replace should do the trick.
http://php.net/manual/en/function.str-replace.php

Code: Select all

$display_name = str_replace('"', '', $display_name);
But I don't know exactly how XI is escaping everything at the database level. This likely won't cover every possible case.

Re: Service's Host's display_name?

Posted: Wed Jul 27, 2016 9:41 am
by Deantwo
mcapra wrote:str_replace should do the trick.
http://php.net/manual/en/function.str-replace.php

Code: Select all

$display_name = str_replace('"', '', $display_name);
But I don't know exactly how XI is escaping everything at the database level. This likely won't cover every possible case.
Good Idea, forgot to try a replace method.
I was trying to use substring:

Code: Select all

if (substr($display_name, 0, 1) == '"') { $display_name = substr($display_name, 1, strlen($display_name) - 2); }
But both the if statement and the string length seemed to fail.
Would likely be easier for me if I bothered finding some debug tools, but PHP isn't something I use often, I mainly work with C#.

Anyway, I will try your suggestion in the morning.

Re: Service's Host's display_name?

Posted: Wed Jul 27, 2016 11:36 am
by mcapra
Definitely let us know if you encounter further issues!

Re: Service's Host's display_name?

Posted: Thu Jul 28, 2016 1:22 am
by Deantwo
mcapra wrote:str_replace should do the trick.
http://php.net/manual/en/function.str-replace.php

Code: Select all

$display_name = str_replace('"', '', $display_name);
This does indeed work as expected.
I guess the string contains some characters that are stripped out before it reaches HTML, so I can't see the exact string without debug tools.

Anyway, it works nicely now.
Thanks for the help.

I did also add a "Not Unknown" option to the states filter dropdown, I have a lot of services that report unknown state but can't do much about so I didn't wanna see them.

Re: Service's Host's display_name?

Posted: Thu Jul 28, 2016 9:02 am
by mcapra
Thanks again for sharing your work! Is it alright if we lock this thread and mark the issue as resolved?

Re: Service's Host's display_name?

Posted: Thu Jul 28, 2016 9:42 am
by Deantwo
mcapra wrote:Thanks again for sharing your work! Is it alright if we lock this thread and mark the issue as resolved?
I guess so.