Prometheus Alertmanager Expression Problem/Question(s) concerning Lists - expression

I am trying to create an prometheus-alert-expression that checks if a mountpoint is mounted and sends an alert if the mountpoint is missing .. The Idea was something like this:
groups:
- name: mountpoints
rules:
- alert: /ghome missing
expr: absent(node_filesystem_avail_bytes{mountpoint="/ghome", instance="my.machine.org:9100"})
for: 60s
labels:
severity: critical
annotations:
summary: "/ghome missing on ({{ $labels.instance }})."
description: "VALUE = {{ $value }}\n LABELS = {{ $labels }}"
This kinda works. But is there a way of passing a list/vector to the mountpoint(s) and/or instance(s).
Using this Expression I'll have to write an alert-rule for each instance and each mountpoint.
I had the Idea of trying regular-Expressions like
expr: absent(node_filesystem_avail_bytes{mountpoint=~"/ghome|/something|/other", instance=~"my.machine.org:9100|another.machine.org:9100"})
.. but this obviously does not work.
Does anybody have an idea how to implement this ?
Greetings
Volker

When we pass an expression such as absent(my_metric{label=~"1|2"}), this is evaluated as such:
my_metric{label=~"1|2"} might return 4 possible results:
No result
my_metric{label="1"}
my_metric{label="2"}
both my_metric{label="1"} and my_metric{label="2"}
And the absent function is then called upon these results, and for absent to return "1" it will only do so when there are no results. Missing the case when 1 of them is absent.
Unfortunately there's no one-liner for this, we'll have to be explict with absent, we can either have multiple alert-rules or use the or operator such as:
absent(my_metric{label="1"}) or absnet(my_metrci{label="2"})

Related

Azure Pipelines - "While parsing a block mapping, did not find expected key" when setting variables

I have a strange problem that I can't seem to get my head around. I am trying to define some variables for use as part of the job that will deploy bicep files via Azure CLI and execute PowerShell tasks.
I get this validation error when I try and execute the pipeline: While parsing a block mapping, did not find expected key
The line that it refers to is: - name: managementResourceDNSPrivateResolverName
On the research that I have done on this problem, it sounds like an indentation problem but on the face of it, it seems to look fine.
jobs:
- job: 'Deploy_Management_Resources'
pool:
vmImage: ${{ parameters.buildAgent }}
variables:
- name: managementResourceDNSPrivateResolverName
value: 'acme-$[ lower(parameters['environmentCode']) ]-$[ lower(variables['resourceLocationShort']) ]-private-dns-resolver'
- name: managementResourceGroupManagement
value: 'acme-infrastructure-rg-management'
- name: managementResourceRouteTableName
value: 'acme-$[ lower(variables['subscriptionCode']) ]-$[ lower(variables['resourceLocationShort']) ]-route-table'
- name: managementResourceVirtualNetworkName
value: 'acme-$[ lower(variables['subscriptionCode']) ]-$[ lower(variables['resourceLocationShort']) ]-vnet-internal-mng'
Thanks!
The error message ...parsing a block mapping, did not find expected key is usually a side-effect of malformed yaml. You'll see if often with variables if you have mixed formats of arrays and property elements
variables: # an array of objects
# variable group reference object
- group: myvariablegroup
# variable template reference object
- template: my-variables.yml
# variable object
- name: myVariable
value: 'value1'
# variable shorthand syntax
myVariable: 'value1' # this fails because it's a property instead of an array element
While it doesn't appear that the sample you've provided is malformed, I am curious about the use of $[ ] which is a runtime expression. The expression $[ lower(parameters['environmentcode']) ] refers to parameters which is are only available at compile time.
Change:
$[ lower(parameters['environmentCode']) ] to ${{ lower(parameters.environmentCode) }}

how to use $val to get value from map in helm for kubernetes?

I got a map in values.yaml:
Schedule:
app1: node01
app2: node07
app3: node13
and I want to use it in template/app.yaml:
{{- $tuplei := untilStep 1 4 1 -}}
{{- range $keyi, $vali := $tuplei }}
---
spec:
template:
spec:
nodeName: {{ $.Values.Schedule.node$vali }}
It can't work:
Error: parse error at (xxx/templates/app.yaml:51): bad character U+0024 '$'
helm.go:94: [debug] parse error at (xxx/templates/app.yaml:51): bad character U+0024 '$'
I have tried some ways, but still can't make it.
#{{- $ScheduleName := printf "app%d" $vali }}
#nodeName: get $.Values.Schedule "$ScheduleName"
#can't work, too.
The Go text/template language includes an index function, which does an arbitrary lookup by key or index. So your last form is almost correct: you need to construct the key in a string, and then use index to retrieve it.
{{- $scheduleName := printf "app%d" $vali -}}
nodeName: {{ index $.Values.Schedule $scheduleName }}
Make sure to not quote the $scheduleName variable reference, lest the template language interpret it as a string literal.

ERROR! Unexpected Exception, this is probably a bug: argument of type 'bool' is not iterable

I've got an error with Ansible, that I don't understand:
ERROR! Unexpected Exception, this is probably a bug: argument of type 'bool' is not iterable
It happens in in role where I call a var file:
- name: import pdt vars
include_vars:
file: "{{ pdt_type }}.yml"
The "{{ pdt_type }}.yml" contains:
pdt_pkg:
- { name: "zzz-libs" }
- { name: "zzz-core" }
What is wrong with that? Is it really a bug?
The { } is supposed to declare a dictionary, using a flow collection syntax
You seem to declare two dictionaries, each with the same key "name"
Check if the content if {{ pdt_type }}.yml is actually the issue, by using a simpler content:
pdt_pkg:
- name1: "zzz-libs"
- name2: "zzz-core"

Ansible: Implementing mapping algorithm for producers to consumers

So, the scenario is I have producers and consumers in ratio 7:1, and I want to have a consistent and deterministic multilple mapping b/w the producers and consumers in my service. List of consumers is provided in the config to each of the producer, which is done via ansible. So, I try to implement the mapping logic in the ansible itself, rather than passing the entire list of consumers, and doing it inside producer service. So, I thought of using a custom filter to filter out from the list of consumers, and assign it to producer. Below is the custom filter I wrote:
#!/usr/bin/python
class FilterModule(object):
def filters(self):
return { 'map_producer_to_consumer': self.map_producer_to_consumer }
# consumer_servers: complete list of consumers servers
# producer_id: provided to each producer for mapping purpose
# producer_count: total no. of producers
# map_consumer_count: no. of consumers need to be mapped to each producer
# consumer_count: total no. of consumers
def map_producer_to_consumer(self, consumer_servers, producer_id, producer_count, map_consumer_count):
consumer_count = len(consumer_servers)
index_offset = 0 if producer_count%consumer_count else 1
rotation_count = (producer_id/consumer_count) % (map_consumer_count-1) # used for left rotation of mapped servers
map_consumer_indexes = [ (producer_count*i + producer_id + index_offset*i) % consumer_count for i in xrange(map_consumer_count)]
mapped_consumer_servers = [consumer_servers[map_consumer_indexes[0]]]
for i in xrange(1, map_consumer_count):
index = (i + rotation_count) % map_consumer_count
if i + rotation_count >= map_consumer_count:
mapped_consumer_servers.append( consumer_servers[map_consumer_indexes[index] + 1] )
else:
mapped_consumer_servers.append( consumer_servers[map_consumer_indexes[index]] )
return (',').join(mapped_consumer_servers)
This filter is working as expected, when using with static arguements like this:
"{{ tsdb_boxes | map_producer_to_consumer(2,3,3) }}"
but I want to make it use dynamic arguments via jinja2 templating, something like:
"{{ groups['producers'] | map_producer_to_consumer ({{ consumer_servers }}, {{ producer_id }}, {{ producer_count }}, {{ map_consumer_count }}) }}"
but its resulting in errors due to nesting of variables, which is not allowed in Jinja2. If I try something like this:
"{{ groups['producers'] }} | map_producer_to_consumer ({{ consumer_servers }}, {{ producer_id }}, {{ producer_count }}, {{ map_consumer_count }})"
it results in printing out the string like this:
['ip-1', 'ip-2'...] | map_producer_to_consumer (1000, 10, 150, 3)
Can someone please suggest what should be the best ansible way to achieve this. Should I use script module and convert the logic in bash, or it will be better to keep this inside the service only.
Answer from comments:
Why not try {{ groups['producers'] | map_producer_to_consumer(consumer_servers, producer_id, producer_count, map_consumer_count) }}
And link from #techraf about nesting.

How to use symfony callback constraint with validation group in yaml?

I'm using Symfony 2.6 and I'm following these tutorials how to use the validation callback constraint:
http://symfony.com/blog/new-in-symfony-2-4-a-better-callback-constraint
http://symfony.com/doc/current/reference/constraints/Callback.html#external-callbacks-and-closures
To invoke an external validation call I'm trying to use following yaml configuration:
App\APIBundle\Entity\Order:
properties:
id:
- Type:
type: integer
message: "Der Wert {{ value }} ist kein gültiger {{ type }}."
amount:
- Type:
type: integer
message: "Der Wert {{ value }} ist kein gültiger {{ type }}."
groups: [ "AppOrder", "AppOrderbasket" ]
- Callback: [App\APIBundle\Validator\Validator, validate]
groups: [ "AppOrder", "AppOrderbasket" ]
I run into following problems when trying to validate the amount property with an external callback validation class:
The function "validate" within the validation class App\APIBundle\Validator\Validator doesn't get invoked at all. I've tried to add validation groups by adding the "groups" property to the callback constraint. This seems not to be valid as i get this warning (Warning: trim() expects parameter 1 to be string, array given);
If I remove the "groups" property the warning dissapears but the validator is still not invoked.
Any ideas?
Thanks in advance
ninsky
You are now mixing the default option syntax with the normal syntax. That doesn't work.
If you only need to specify the default option (which is the callback option in case of the Callback constraint), you can use Callback: [App\APIBundle\Validator\Validator, validate]. However, if you have to define 2 options (in your case callback and groups), you have to use the normal syntax:
- Callback:
callback: [App\APIBundle\Validator\Validator, validate]
groups: [AppOrder, AppOrderbasket]

Resources