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

Return type: data

Description: Parses CSV data from an RFC 4180 compliant file filename (CRLF line endings required), 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 - File name - in the range: "?(/.*)
  • has_header: - CSV file has heading - one of
    • true
    • false
    • yes
    • no
    • on
    • off
  • class_column: int - Column index to filter by, contains classes - in the range: 0,99999999999
  • optional_sort_column: int - Column index to sort by - in the range: 0,99999999999

Example:

Prepare CSV:

code
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:

code
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:

code
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"
  }
]

Notes:

  • If the CSV file is stored in a git repository the .gitattributes file can be used to ensure proper line endings.

    For example:

    code
    # .gitattribtues
    *.csv     text eol=crlf
    RFC-4180-non-compliant-line-endings.csv eol=lf
    *.mustache text
    *.sh       text eol=lf
    

See also: data_expand(), readcsv(), classmatch()

History:

  • Introduced in CFEngine 3.14