reports

Table of Contents

Reports promises simply print messages. Outputting a message without qualification can be a dangerous operation. In a large installation it could unleash an avalanche of messaging, so it is recommended that reports are guarded appropriately.

bundle agent main
{
  reports:
      "It's recommended that you always guard reports"
        comment => "Remember by default output from cf-agent when run
                    from cf-execd will be emailed";

    DEBUG|DEBUG_main::
      "Run with --define DEBUG or --define DEBUG_main to display this report";

  methods:
      "Actuate bundle that reports with a return value"
        usebundle => bundle_with_return_value,
        useresult => "return_array",
        comment => "Reports can be used to return data into a parent bundle.
                    This is useful in some re-usable bundle patterns.";

  reports:
      "I got '$(return_array[key])' returned from bundle_with_return_value";

      "Reports can be redirected and appended to files"
        report_to_file => "$(sys.workdir)/report_output.txt",
        comment => "It's important to note that this will suppress the report
                   from stdout.";

      "Report content of a file:$(const.n)$(const.n)------------------------"
        printfile => cat( $(this.promise_filename) );
}

bundle agent bundle_with_return_value
{
  reports:
      "value from bundle_with_return_value"
        bundle_return_value_index => "key";
}

body printfile cat(file)
{
      file_to_print   => "$(file)";
      number_of_lines => "inf";
}

@if minimum_version(3.8)
body printfile head(file)
{
      inherit_from => "cat";
      # GNU head defaults to 10
      number_of_lines => "10";
}
@endif

This policy can be found in /var/cfengine/share/doc/examples/reports.cf and downloaded directly from github.

Messages output by report promises are prefixed with the letter R to distinguish them from other output.

bundle agent report
{
  reports:

    loadavg_high::

      "Processes:"
         printfile => cat("$(sys.statedir)/cf_procs");
}

Reports do not fundamentaly make changes to the system and report type promise outcomes are always considered kept.

bundle agent report
{
  vars:
    "classes" slist => classesmatching("report_.*");

  reports:
    "HI"
      classes => scoped_classes_generic("bundle", "report");

    "found class: $(classes)";
}

body classes scoped_classes_generic(scope, x)
# Define x prefixed/suffixed with promise outcome
{
  scope => "$(scope)";
  promise_repaired => { "promise_repaired_$(x)", "$(x)_repaired", "$(x)_ok", "$(x)_reached" };
  repair_failed => { "repair_failed_$(x)", "$(x)_failed", "$(x)_not_ok", "$(x)_not_kept", "$(x)_not_repaired", "$(x)_reached" };
  repair_denied => { "repair_denied_$(x)", "$(x)_denied", "$(x)_not_ok", "$(x)_not_kept", "$(x)_not_repaired", "$(x)_reached" };
  repair_timeout => { "repair_timeout_$(x)", "$(x)_timeout", "$(x)_not_ok", "$(x)_not_kept", "$(x)_not_repaired", "$(x)_reached" };
  promise_kept => { "promise_kept_$(x)", "$(x)_kept", "$(x)_ok", "$(x)_not_repaired", "$(x)_reached" };
}
$ cf-agent -KIf ./example_report_outcomes.cf -b report
2015-05-13T12:48:12-0500     info: Using command line specified bundlesequence
R: HI
R: found class: report_ok
R: found class: report_kept
R: found class: report_reached
R: found class: report_not_repaired

Attributes

Common Attributes

Common attributes are available to all promise types. Full details for common attributes can be found in the Common Promise Attributes section of the Promise Types page. The common attributes are as follows:

action

classes

comment

depends_on

handle

if

meta

with


friend_pattern

Deprecated: This attribute is kept for source compatibility, and has no effect. Deprecated in CFEngine 3.4.

intermittency

Deprecated: This attribute is kept for source compatibility, and has no effect. Deprecated in CFEngine 3.4.

printfile

Description: Outputs the content of a file to standard output

Type: body printfile

See also: Common Body Attributes

file_to_print

Description: Path name to the file that is to be sent to standard output

Include part of a file in a report.

Type: string

Allowed input range: "?(/.*)

number_of_lines

Description: Integer maximum number of lines to print from selected file

Type: int

Default value: 5

Allowed input range: -99999999999,99999999999

Note: Prints lines from end of file when a negative number is passed to argument number_of_lines

Example:

echo 'Line 1' > /tmp/example_file.txt
echo 'Line 2' >> /tmp/example_file.txt
echo 'Line 3' >> /tmp/example_file.txt
echo 'Line 4' >> /tmp/example_file.txt
echo 'Line 5' >> /tmp/example_file.txt
echo 'Line 6' >> /tmp/example_file.txt
echo 'Line 7' >> /tmp/example_file.txt
echo 'Line 8' >> /tmp/example_file.txt
echo 'Line 9' >> /tmp/example_file.txt
body common control
{
      bundlesequence  => { "example" };
}

bundle agent example
{
  vars:
      "example_file" string => "/tmp/example_file.txt";
  reports:
      "First three:"
        printfile => first_three("$(example_file)");
      "Last three:"
        printfile => last_three("$(example_file)");
}

body printfile first_three(file)
{
  file_to_print => "$(file)";
  number_of_lines => "3";
}

body printfile last_three(file)
{
  file_to_print => "$(file)";
  number_of_lines => "-3";
}

Output:

R: First three:
R: Line 1
R: Line 2
R: Line 3
R: Last three:
R: Line 7
R: Line 8
R: Line 9

History:

  • Introduced negative number support in 3.18.0

report_to_file

Description: The path and filename to which output should be appended

Append the output of the report to the named file instead of standard output. If the file cannot be opened for writing then the report defaults to the standard output.

Type: string

Allowed input range: "?(/.*)

Example:

bundle agent main
{

  reports:

      "$(sys.date),This is a report from $(sys.host)"
        report_to_file => "/tmp/test_log";
}

bundle_return_value_index

Description: The promiser is to be interpreted as a literal value that the caller can accept as a result for this bundle; in other words, a return value with array index defined by this attribute.

Return values are limited to scalars.

Type: string

Allowed input range: [a-zA-Z0-9_$(){}\[\].:]+

Example:

bundle agent main
{
  methods:

     "any"
       usebundle => child,
       useresult => "my_return_var";

  reports:

     "My return was: '$(my_return_var[1])' and '$(my_return_var[2])' and '$(my_return_var[named])'";
}

bundle agent child
{
  reports:

   # Map these indices into the useresult namespace

     "this is a return value"
        bundle_return_value_index => "1";

     "this is another return value"
        bundle_return_value_index => "2";

     "bundle_return_value_index is not required to be numerical"
        bundle_return_value_index => "named";
}

See also: methods useresult attribute

History: Introduced in 3.4.0.

lastseen

Deprecated: This attribute is kept for source compatibility, and has no effect. Deprecated in CFEngine 3.4.

showstate

Deprecated: This attribute is kept for source compatibility, and has no effect. Deprecated in CFEngine 3.5.