Report Templates

Problem:

Hello! I’m having trouble writing ERB report templates. I’m trying to output:

  • The full list of hosts

  • The host status (green, red, gray)

  • The date of the last report

Thanks in advance!

There is a “Host - Statuses” pre-made template which should include most of this. What specific issues are you running into?

The output is not informative. For example:
Name,Global,Build,Configuration
test,0,0,0
test2,0,0,24
test3,2,0,4096

In the “Configuration” column, what do the values 24 or 4096 mean, and there are different values found. In the “Global” column, 0 or 2 is not very clear either…

I would like to make the output like this:
Name, Status, Configuration, LastReport
test1,Warning,No reports,01.01.2024
test2,OK,No changes,01.01.2024
test3,Warning,Out of sync,01.01.2024
test4,Error,No reports,01.01.2024

Which version of Foreman do you use? For “Host - Statuses” template there is Output style input, which is Human-readable by default, so the numbers are automatically converted into labels exactly how you described.

System Information

Version 2.1.4

That’s pretty ancient, but there still should be a macro called host_status, which takes a host and a name of the status as a string and should return the status in human-readable form. You can clone the template and adjust it to use this macro.

Unfortunately, I don’t really understand how to use the host_status macro. Could you please write the whole code?

Thank you, I finally got something to work, but not everything.
I got it like this:

   -%>
   <%- report_headers 'Name', 'Global', 'Configuration', 'Last Report' -%>
   <%- load_hosts\(search: input\('hosts'\), includes: :host_statuses\).each_record do \|host\| -%>
   <%-   report_row\({
         'Name': host.name,
         'Global': host.global_status,
         'Configuration': host_status\(host, 'Configuration'\),
         'Last Report': host.last_report,
       }\) -%>
   <%- end -%>
   <%= report_render -%>

This code works and displays:

Name,Global,Configuration,Last Report
test1,0,No changes,2024-08-12 12:42:17 +0300
test2,0,No changes,2024-08-12 12:44:29 +0300

Great, but I still need to display status (ok, warning, error) in the host_status macro, using either Build or Configuration.

1 Like

Sorry I didn’t respond earlier, glad you’ve figured it out by yourself :slight_smile:

If I understood you right, you also want the global status to be a string instead of a number. You should be able to grab the global status the same way you did for configuration status, e.g. Global: host_status(host, 'Global')

   -%>
   <%- report_headers 'Name', 'Global', 'Configuration', 'Last Report' -%>
   <%- load_hosts(search: input('hosts'), includes: :host_statuses).each_record do |host| -%>
   <%-   report_row({
         'Name': host.name,
         'Global': host_status(host, 'Global'),
         'Configuration': host_status(host, 'Configuration'),
         'Last Report': host.last_report,
       }) -%>
   <%- end -%>
   <%= report_render -%>

Error:

ERF89-2590 Foreman::Renderer::Errors::UnknownHostStatusError: Unknown host status “Global” was specified in host_status macro, use one of Build,Configuration

Sorry for misleading, there should be also a different macro called global_status_label that you can use on a host object: Global: host.global_status_label

   -%>
   <%- report_headers 'Name', 'Global', 'Build', 'Configuration', 'Last Report' -%>
   <%- load_hosts(search: input('hosts'), includes: :host_statuses).each_record do |host| -%>
   <%-   report_row({
         'Name': host.name,
         'Global': host.global_status_label,
         'Build': host_status(host, 'Build'),
         'Configuration': host_status(host, 'Configuration'),
         'Last Report': host.last_report,
       }) -%>
   <%- end -%>
   <%= report_render -%>

Error:

There was an error rendering the Host - Statuses clone template: undefined method '#global_status_label' for Host::Managed::Jail (Host::Managed)

Okay, this gets weird… It seems quite useful macros were added much later…

You can workaround it though, it’s not ideal and probably can’t be used forever, but it’ll do until you can be able to upgrade your instance:

# ...
   <%- load_hosts(search: input('hosts'), includes: :host_statuses).each_record do |host| -%>
  <% global_status = host.global_status == 0 ? 'OK' : host.global_status == 1 ? 'Warning' : 'Error' -%>
   <%-   report_row({
         'Name': host.name,
         'Global': global_status,
         'Build': host_status(host, 'Build'),
         'Configuration': host_status(host, 'Configuration'),
         'Last Report': host.last_report,
       }) -%>
   <%- end -%>
   <%= report_render -%>

The second line will get host’s saved global status and will map its number to a label it implies and save it to global_status local variable, which gets then used in 'Global': global_status line.

Thank you so much for your help and writing the code.

1 Like