classfiltercsv

Table of Contents

Prototype: classfiltercsv(filename, has_header, class_column, optional_sort_column)

Return type: data

Description: Parses CSV data from the file filename, and returns a data variable that is filtered by defined classes. If has_header is set to true, the columns in the first line of the CSV file are used as keys for the data. class_column specifies which column contains class names to filter by.

If optional_sort_column is defined, the data containers will be sorted by the given column. Both class_column and optional_sort_column must be integer indices starting from 0, and must be at most the total amount of columns minus 1.

Arguments:

  • filename: string, in the range: "?(/.*)
  • has_header: one of
    • true
    • false
    • yes
    • no
    • on
    • off
  • class_column: int, in the range: 0,99999999999
  • optional_sort_column: int, in the range: 0,99999999999

Example:

Prepare:

echo 'ClassExpr,Sort,Token,Value'                        > /tmp/classfiltercsv.csv
echo '# This is a comment'                              >> /tmp/classfiltercsv.csv
echo 'any,A,net.ipv4.ip_forward,ANYVALUE'               >> /tmp/classfiltercsv.csv
echo 'example_class1,z,net.ipv4.ip_forward,ANYVALUE'    >> /tmp/classfiltercsv.csv
echo 'example_class2,a,net.ipv4.ip_forward,127.0.0.3'   >> /tmp/classfiltercsv.csv
echo 'not_defined,Z,net.ipv4.ip_forward,NOT_DEFINED'    >> /tmp/classfiltercsv.csv
echo 'example_class3,1,net.ipv4.ip_forward,127.0.0.4'   >> /tmp/classfiltercsv.csv
echo 'also_undefined,0,net.ipv4.ip_forward,NOT_DEFINED'  >> /tmp/classfiltercsv.csv
sed -i 's/$/\r/' /tmp/classfiltercsv.csv

Policy:

bundle agent example_classfiltercsv
{
  classes:
      "example_class1";
      "example_class2";
      "example_class3";

  vars:
      "data_file" string => "/tmp/classfiltercsv.csv";
      "d" data => classfiltercsv($(data_file), "true", 0, 1);

  reports:
      "Filtered data: $(with)" with => string_mustache("", d);
}
bundle agent __main__
{
  methods:
      "example_classfiltercsv";
}

Output:

R: Filtered data: [
  {
    "Sort": "1",
    "Token": "net.ipv4.ip_forward",
    "Value": "127.0.0.4"
  },
  {
    "Sort": "A",
    "Token": "net.ipv4.ip_forward",
    "Value": "ANYVALUE"
  },
  {
    "Sort": "a",
    "Token": "net.ipv4.ip_forward",
    "Value": "127.0.0.3"
  },
  {
    "Sort": "z",
    "Token": "net.ipv4.ip_forward",
    "Value": "ANYVALUE"
  }
]

See also: readcsv(), classmatch()

History:

  • Introduced in CFEngine 3.14