Skip to content

Conditions Overview

Gurpartap edited this page Mar 18, 2013 · 9 revisions

Conditions provide a way to monitor and act on more than just the state of a process. For example, conditions can monitor the resource utilization (RAM, CPU, etc.) of the application process and restart it if it matches a condition.

Internally, conditions are of two types: poll and trigger. Poll conditions include cpu usage, memory usage, etc. and trigger condition includes flapping (repeated restart attempts on a process). Triggers provide actions based on changes in state of a process. Triggers can also be used to, say, notify system administrator when a process' state changes.

One or more conditions may be specified for a program using an attribute as illustrated in the examples below:

Specifying conditions in daemon init configuration

# see examples/cognizantd.yml
...
applications: {
  redis-example: {
    monitor: {
      redis-server-1: {
        start_command: /usr/local/bin/redis-server,
        checks: {
          always_true: {
            every: 2,
            times: 3,
            do: restart
          },
          memory_usage: {
            every: 5, # Seconds.
            above: 1048576, # Bytes.
            times: [3, 5], # Three out of five times.
            do: restart # Or stop (restart is the default though).
          }
        }
      }
    }
  }
}

Specifying conditions in Ruby based configuration

Cognizant.application("redis-example") do |app|
  app.monitor "redis-server-1" do |process|
    process.start_command = "/usr/local/bin/redis-server"

    process.check :always_true, :every => 2.seconds, :times => 3 do |p|
      `echo "Boom!"`
    end

    process.check(:cpu_usage, :every => 3.seconds, :above => 60, :times => 3, :do => :restart)
  end
end

Follow on to the Using the Built-in Conditions for more examples.