#  Copyright 2021 Northern.tech AS
#  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; version 3.
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
# To the extent this program is licensed as part of the Enterprise
# versions of Cfengine, the applicable Commercial Open Source License
# (COSL) may apply to this file if you as a licensee so wish it. See
# included file COSL.txt.
#cop measurements,example
#######################################################
#
# Test file:
#
# First line
# Blonk blonk bnklkygsuilnm
#
#######################################################
body common control
{
  bundlesequence => { "report" };
}

#######################################################
body monitor control
{
  forgetrate => "0.7";
  histograms => "true";
}

#######################################################
bundle agent report
{
  reports:
    "
   Free memory read at $(mon.av_free_memory_watch)
   cf_monitord read $(mon.value_monitor_self_watch)   
   ";
}

#######################################################
bundle monitor watch
{
  measurements:
    # Test 1 - extract string matching
    "/home/mark/tmp/testmeasure"
      handle => "blonk_watch",
      stream_type => "file",
      data_type => "string",
      history_type => "weekly",
      units => "blonks",
      match_value => find_blonks,
      action => sample_min("10");

    # Test 2 - follow a special process over time
    # using cfengine's process cache to avoid resampling
    "/var/cfengine/state/cf_rootprocs"
      handle => "monitor_self_watch",
      stream_type => "file",
      data_type => "int",
      history_type => "static",
      units => "kB",
      match_value => proc_value(
        ".*cf-monitord.*",
        "root\s+[0-9.]+\s+[0-9.]+\s+[0-9.]+\s+[0-9.]+\s+([0-9]+).*"
      );

    # Test 3, discover disk device information
    "/bin/df"
      handle => "free_disk_watch",
      stream_type => "pipe",
      data_type => "slist",
      history_type => "static",
      units => "device",
      match_value => file_system;

    # Update this as often as possible
    # Test 4
    "/tmp/file"
      handle => "line_counter",
      stream_type => "file",
      data_type => "counter",
      match_value => scanlines("MYLINE.*"),
      history_type => "log";
}

##########################################################
body match_value scanlines(x)
{
  select_line_matching => "^$(x)$";
}

##########################################################
body action sample_min(x)
{
  ifelapsed => "$(x)";
  expireafter => "$(x)";
}

##########################################################
body match_value find_blonks
{
  select_line_number => "2";
  extraction_regex => "Blonk blonk ([blonk]+).*";
}

##########################################################
body match_value free_memory # not willy!
{
  select_line_matching => "MemFree:.*";
  extraction_regex => "MemFree:\s+([0-9]+).*";
}

##########################################################
body match_value proc_value(x, y)
{
  select_line_matching => "$(x)";
  extraction_regex => "$(y)";
}

##########################################################
body match_value file_system
{
  select_line_matching => "/.*";
  extraction_regex => "(.*)";
}
