Watcher and variables

Hi there!

I've been experimenting a little bit with watcher and I had a question.
Let's start with the code.

status=yellow
echo $status

curl -XPUT 'http://localhost:9200/_watcher/watch/cluster_health_watch3' -d '{
  "trigger" : {
    "schedule" : { "interval" : "10s" }
  },
  "input" : {
    "http" : {
      "request" : {
       "host" : "localhost",
       "port" : 9200,
       "path" : "/_cluster/health"
      }
    }
  },
  "condition" : {
    "compare" : {
      "ctx.payload.status" : { "eq" : '$status' }
    }
  },
  "actions" : {
    "send_email" : {
      "email" : {
        "to" : "myemail@gmail.com",
        "subject" : "Cluster Status Warning",
        "body" : "Cluster status is YELLOW"
      }
    }
  }
}'

As you can see, its a plain old watcher setup for emails but with a bit a tweaking. I replaced the "yellow" at ctx.payload.status next to "eq" : with '$status'

My goal here is to be able to change the values of the watcher setup using variables. I placed a $status at the top which has the value yellow. When I process this it gives me the following error.

{"error":"WatcherException[failed to put watch [cluster_health_watch3]]; nested: WatcherException[could not parse watch [cluster_health_watch3]]; nested: JsonParseException[Unrecognized token 'red': was expecting ('true', 'false' or 'null')\n at [Source: [B@4996065a; line: 16, column: 43]]; ","status":500}

So what can I do about this? Is it even possible? Thanks :smiley:

Hey,

I think it might be a shell/escaping issue here, because you are misusing ticks and that results in broken JSON. Can you try without variables first to rule this out, and if it does, then check your shell for correct escaping.

--Alex

@spinscale It has been a little while since I last worked on this problem (can still reproduce it) and I will get back to you asap with a proper response to your suggestion :slight_smile: