Skip to content

Instantly share code, notes, and snippets.

@nzlosh
Last active September 10, 2018 21:28
Show Gist options
  • Save nzlosh/1abc6aa8e4d251a06cf5c4269cdd61a1 to your computer and use it in GitHub Desktop.
Save nzlosh/1abc6aa8e4d251a06cf5c4269cdd61a1 to your computer and use it in GitHub Desktop.
YAQL use cases
Case 1: Identify hosts with faults detected by Shinken monitoring. Request made via LiveStatus.
DATA
"result": {
"result": [
[
"bench-05.example.com",
0
],
[
"bench-03.example.com",
0
],
[
"bench-10.example.com",
0
],
[
"bench-07.example.com",
1
]
]
}
YAQL
$.result.result.where($[1] > 0).select($[0])
RESULT
[
"bench-07.example.com"
]
Case 2: Fetch hosts in maintenance mode from Consul
DATA
"result": {
"result": [
"280892762",
[
{
"Node": {
"Node": "bench-03",
"Datacenter": "dc3",
"TaggedAddresses": {
"wan": "x.x.x.x",
"lan": "x.x.x.x"
},
"ModifyIndex": 254298395,
"Meta": {
"consul-network-segment": ""
},
"Address": "x.x.x.x",
"CreateIndex": 254253741,
"ID": "1961be46-28d2-4bde-e47e-02ab67dde013"
},
"Checks": [
{
"Node": "bench-03",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Definition": {},
"Notes": "",
"ModifyIndex": 279915946,
"Status": "passing",
"ServiceName": "",
"ServiceID": "",
"Output": "Agent alive and reachable",
"ServiceTags": [],
"CreateIndex": 254253741
}
],
"Service": {
"Service": "automation",
"Tags": [
"prod",
"bench"
],
"ModifyIndex": 254253741,
"EnableTagOverride": false,
"Port": 0,
"Meta": null,
"Address": "bench-03.example.com",
"CreateIndex": 254253741,
"ID": "automation"
}
},
{
"Node": {
"Node": "bench-05",
"Datacenter": "dc3",
"TaggedAddresses": {
"wan": "x.x.x.x",
"lan": "x.x.x.x"
},
"ModifyIndex": 254375045,
"Meta": {
"consul-network-segment": ""
},
"Address": "x.x.x.x",
"CreateIndex": 254253741,
"ID": "c0b6ae82-d48f-bd5f-ee96-30bf46e2fece"
},
"Checks": [
{
"Node": "bench-05",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Definition": {},
"Notes": "",
"ModifyIndex": 278822424,
"Status": "passing",
"ServiceName": "",
"ServiceID": "",
"Output": "Agent alive and reachable",
"ServiceTags": [],
"CreateIndex": 254253741
}
],
"Service": {
"Service": "automation",
"Tags": [
"prod",
"bench"
],
"ModifyIndex": 254253741,
"EnableTagOverride": false,
"Port": 0,
"Meta": null,
"Address": "bench-05.example.com",
"CreateIndex": 254253741,
"ID": "automation"
}
},
{
"Node": {
"Node": "bench-07",
"Datacenter": "dc3",
"TaggedAddresses": {
"wan": "x.x.x.x",
"lan": "x.x.x.x"
},
"ModifyIndex": 254375625,
"Meta": {
"consul-network-segment": ""
},
"Address": "x.x.x.x",
"CreateIndex": 254253741,
"ID": "dcec1af3-d7a8-5aa8-3cf4-4cbc037fc077"
},
"Checks": [
{
"Node": "bench-07",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Definition": {},
"Notes": "",
"ModifyIndex": 254253741,
"Status": "passing",
"ServiceName": "",
"ServiceID": "",
"Output": "Agent alive and reachable",
"ServiceTags": [],
"CreateIndex": 254253741
}
],
"Service": {
"Service": "automation",
"Tags": [
"prod",
"bench"
],
"ModifyIndex": 254253741,
"EnableTagOverride": false,
"Port": 0,
"Meta": null,
"Address": "bench-07.example.com",
"CreateIndex": 254253741,
"ID": "automation"
}
},
{
"Node": {
"Node": "bench-10",
"Datacenter": "dc3",
"TaggedAddresses": {
"wan": "x.x.x.x",
"lan": "x.x.x.x"
},
"ModifyIndex": 254375694,
"Meta": {
"consul-network-segment": ""
},
"Address": "x.x.x.x",
"CreateIndex": 254253741,
"ID": "18dfa090-9ccc-01ac-1c34-afa456ee39a4"
},
"Checks": [
{
"Node": "bench-10",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Definition": {},
"Notes": "",
"ModifyIndex": 254375693,
"Status": "passing",
"ServiceName": "",
"ServiceID": "",
"Output": "Agent alive and reachable",
"ServiceTags": [],
"CreateIndex": 254253741
}
],
"Service": {
"Service": "automation",
"Tags": [
"prod",
"bench"
],
"ModifyIndex": 254253741,
"EnableTagOverride": false,
"Port": 0,
"Meta": null,
"Address": "bench-10.example.com",
"CreateIndex": 254253741,
"ID": "automation"
}
}
]
]
}
YAQL
$.result.result.last().select([$.Checks.where($.CheckID = "_service_maintenance:automation" or $.CheckID = "_node_maintenance").select($.Status), $.Service.Address]).where(len($[0]) > 0).select($[1])
RESULT:
[]
Case 3: Merge three lists to produce a single list of unique hosts.
DATA
{}
VAR
farm_problem_list = ["bench-07.example.com"],
host_in_maintenance = [],
hosts_failed = ["bench-03.example.com, "bench-07.example.com"]
YAQL
($.hosts_failed + $.farm_problem_list + $.hosts_in_maintenance).distinct()
RESULT
["bench-07.example.com", "bench-03.example.com"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment