How to write not condition in alert manager route matches? - prometheus-alertmanager

How to write not condition in alert manager route matches?
I want to execute this condition only when severity is not critical..
routes
- match_re:
severity: ^(?!critical)
receiver: teams-channel-webhook-high-priority
I tried the above regex condition .But it didnt work.
I also tried with
not_match_re:
severity: critical
Even that didnt work.

Answer:
You can make a nested tree of routes, which in effect gives you an "if ... then ... else ..." capability.
 
routes:
    - ... something to do always
      continue: true
    - match:
        severity: critical
      routes:
        - ... things to do when critical
        - ...
    - ... things to do when not critical
    - ...
Leaving out the "match" condition entirely makes a rule match always.
Note that "continue: true" carries on to the next rule after a match, but only at the same level.  It will only fall out to the parent ruleset if no matches occur.
Or of course, you can explicitly match all non-critical severities e.g. match_re: severity: 'debug|informational|warning'

match and match_re have been deprecated in route in favor of using matcher
matchers takes a list of conditions (and) and supports regex match =~ and NOT match !~ operators.
route:
# root route
routes:
- receiver: recvA
matchers:
- severity!~"^critical$"
- receiver: recvB
# route:receiverB with no matchers will always match if the previous routes don't

Related

Converting Dictionary to Yaml Object - not preserving quotations

I am trying to dynamically create stubby.yml configuration file. I have a template yaml file as follows:
resolution_type: GETDNS_RESOLUTION_STUB
dns_transport_list:
  - GETDNS_TRANSPORT_TLS
tls_authentication: GETDNS_AUTHENTICATION_REQUIRED
tls_query_padding_blocksize: 128
edns_client_subnet_private : 1
round_robin_upstreams: 1
idle_timeout: 10000
listen_addresses:
  - 10.7.0.1
appdata_dir: "/var/cache/stubby"
I am trying to append this native yaml configuration
upstream_recursive_servers:
  - address_data: 185.228.168.168
    tls_auth_name: "family-filter-dns.cleanbrowsing.org"
  - address_data: 185.228.169.168
    tls_auth_name: "family-filter-dns.cleanbrowsing.org"
The stubby configuration will not work without the tls_auth_name values being wrapped in quotations, and also appdata_dir. Quotations are being stripped and i cannot figure out how to dump the yaml without the " being stripped. Even chatgpt is struggling with this one!
def get_dns_config(dns_option) -> dict:
    # return stubby.yml configuration
    with open('dns.yml', 'r') as f:
        data = yaml.safe_load(f)
    with open('dns_template.yml', 'r') as f:
        template = yaml.safe_load(f)
       
    template['upstream_recursive_servers'] = data[dns_option]['servers']
    logging.debug(json.dumps(template, indent=4))
    logging.debug(yaml.safe_dump(template, default_flow_style=False,indent=2))
    return yaml.safe_dump(template, default_flow_style=False,indent=2)
This is the dict after adding the upstream_recursive_servers value:
{
"resolution_type": "GETDNS_RESOLUTION_STUB",
"dns_transport_list": [
"GETDNS_TRANSPORT_TLS"
],
"tls_authentication": "GETDNS_AUTHENTICATION_REQUIRED",
"tls_query_padding_blocksize": 128,
"edns_client_subnet_private": 1,
"round_robin_upstreams": 1,
"idle_timeout": 10000,
"listen_addresses": [
"10.7.0.1"
],
"appdata_dir": "/var/cache/stubby",
"upstream_recursive_servers": [
{
"address_data": "185.228.168.168",
"tls_auth_name": "family-filter-dns.cleanbrowsing.org"
},
{
"address_data": "185.228.169.168",
"tls_auth_name": "family-filter-dns.cleanbrowsing.org"
}
]
}
This is the result from yaml.safe_load()
appdata_dir: /var/cache/stubby
dns_transport_list:
- GETDNS_TRANSPORT_TLS
edns_client_subnet_private: 1
idle_timeout: 10000
listen_addresses:
- 10.7.0.1
resolution_type: GETDNS_RESOLUTION_STUB
round_robin_upstreams: 1
tls_authentication: GETDNS_AUTHENTICATION_REQUIRED
tls_query_padding_blocksize: 128
upstream_recursive_servers:
- address_data: 185.228.168.168
tls_auth_name: family-filter-dns.cleanbrowsing.org
- address_data: 185.228.169.168
tls_auth_name: family-filter-dns.cleanbrowsing.org
Apologies for the long post - didn't want to miss anything out.
In the YAML file the record
"family-filter-dns.cleanbrowsing.org"
denotes the string family-filter-dns.cleanbrowsing.org without quotes: the quotes at the beginning and at the end are part of the representation, but aren't a part of the data.
For make the record to denote the string "family-filter-dns.cleanbrowsing.org" (with quotes) you could write whole that string inside single quotes:
'"family-filter-dns.cleanbrowsing.org"'
That way is described in YAML specification in the Example 2.17.
Alternatively, you may write whole that string inside double quotes, but escape the inner double quotes:
"\"family-filter-dns.cleanbrowsing.org\""

How to write rules that skip the job when any of the specified files doesn't exist?

Here I have rules for a job so that it's added to the pipeline if and only if do_job is set and skip_job is not set.
rules:
- if: $skip_job == "true"
when: never
- if: $do_job != "false"
How to add a rule that also skips the job when any of the two files is missing?
(the two files are tasks.yml and inputs.yml)
Something like that:
rules:
- if: $skip_job == "true"
when: never
- does_not_exist:
- tasks.yml
when: never
- does_not_exist:
- inputs.yml
when: never
- if: $do_job != "false"

Assert if any TestStep failed

how can I assert if any of the TestSteps failed using Groovy Script? I have written a groovy script script that among other things run another TestCase and I want it to assert every TestStep (in called TestCase) if it failed. Currently it check TestSteps but it does not include Groovy Script Steps (Groovy Script steps that have asserts inside of them).
This is what I have so far...
//Get TestStepList from called Project
def assertFinishedStatus = testRunner.testCase.testSuite.project.workspace.getProjectByName(projectName).getTestSuiteByName(testSuitetName).getTestCaseByName(testCaseName).getTestStepList()
//Assert if any step with assertions failed in called TestCase
def moduleStatus = true;
assertFinishedStatus.each{
// check that testStep has assertionStatus
// (for example groovy testSteps hasn't this property since
// there is no asserts on its)
if(it.metaClass.hasProperty(it,'assertionStatus')){
if(it.assertionStatus == AssertionStatus.FAILED){
log.info "TestStep: ${it.name} - - - - - - - - - - - - Status: FAILED"
return moduleStatus = false;
}else if(it.assertionStatus == AssertionStatus.VALID || it.assertionStatus == AssertionStatus.UNKNOWN){
log.info "TestStep: ${it.name} - - - - - - - - - - - - Status: OK!"
return moduleStatus = true;
}
}
}
assert(moduleStatus != false)
UPDATE:
I hope that description below will clarify thing that I want to achieve
I use Z_TC_DodawanieDwochLiczb_exp to run other TestCases (in different projects). Groovy Script EXECUTE does all the work (get properties from one TestCase and sets them in diifferent one, run that other TestCase and so on...). M_TC_Dodwanie is TestCase that I run by that Groovy Script TestStep EXECUTE_Dodawanie, as You can see it failed on AssertResult Groovy Script Step, but didn't fail Z_TC_DodawanieDwochLiczb_exp (which I want it to do if any TestStep in other TestCases like M_TC_Dodawanie failes). And I want it done in EXECUTE_Dodawanie TestStep in Z_TC_DodawanieDwochLiczb_exp.
I know I shouldn't do any dependencies between other TestCases/Projects etc. however I have to, because it's not up to me
Groovy script test steps have assertions within their code, but you can check their results:
for (r in testRunner.results ) {
// log.info "TestStep " + r.testStep.name + " status: ${r.getStatus()}"
assert r.getStatus().toString().equals("OK")
}
Checking the results is more reliable, since Groovy script test step and some others do not have assertion properties.

livecode urlProgress for mobile download file

i am using the bellow code to download a file from my ftp server.The file is downloaded on Mobile devices.
i am try to use a progress bar to show up the amount of data downloaded
The problem is always i get "cached".
this is my code
constant FTPHOST = "myIp"
constant FTPUSER = "user"
constant FTPPASS = "password"
global sDownloadStart
on mouseUp
put "test.zip" into tFileName
put "ftp://" & FTPUSER & ":" & FTPPASS & "#" & FTPHOST & "/" & tFileName into pUrl
-- start the download process, telling Rev to call the "downloadComplete" handler when finished
Load URL pUrl with message "urlProgress"
set the startvalue of sb "progressbar" to 0
end mouseUp
on urlProgress pURL, pStatus, pBytesDone, pBytesTotal
put pStatus into fld "pf1"
switch item 1 of pStatus
      case "loading"
set the thumbPosition of sb "Progressbar" to round((pBytesDone / pBytesTotal) * 100)
         put the round of (item 1 of pBytesDone / 1024)&& "KB Downloaded" into field "status"
         break
      case "cached"
     --   if pStatus is "cached" then
-- work out a file name for saving this file to the desktop
set the itemDel to slash
put "binfile:" & specialfolderpath("documents") & "/test.zip" into myPath
//put "data.file" into tFileName
put url pUrl into url myPath
answer "Download completed" with ok
-- to save memory, now unload the URL from memory
-- this also means that if you want to run the test again, it does not just use a cached version
unload pURL
--end if
         break
      case "error"
      if pStatus = "error" or pStatus = "timeout" then
answer error "The file could not be downloaded." with "ok"
end if
         break
     
   end switch
end urlProgress
Your urlProgresshandler is called after the loading of the URL finishes. To get a different status, you have to call another message or call the message in a different way, e.g.
on mouseUp
//delete URL "binfile:" & specialfolderpath("documents") & "/data.file"
put "data.file" into tFileName
put "ftp://" & FTPUSER & ":" & FTPPASS & "#" & FTPHOST & "/" & tFileName into pUrl
-- start the download process, telling Rev to call the "downloadComplete" handler when finished
// the callback message has only 2 parameters: url and urlStatus
Load URL pUrl with message "downloadComplete"
send "urlProgress pUrl" to me in 200 millisecs
end mouseUp
on urlProgress pUrl //,pStatus, pMessage,pbytesR,pbytesT
put urlStatus(pUrl) into pStatus
if item 1 of pUrl is "loading" then
put item 2 of pStatus into pBytesR
put item 3 of pStatus into pbytesT
put item 1 of pStatus into pStatus
send "urlProgress pUrl" to me in 200 millisecs
end if
end urlProgress
on downloadComplete theUrl,theUrlStatus
if theUrlStatus is "cached" then
-- work out a file name for saving this file to the desktop
set the itemDel to slash
put "binfile:" & specialfolderpath("documents") & "/data.file" into myPath
put "data.file" into tFileName
put url theUrl into url myPath
answer "Download completed" with ok
-- to save memory, now unload the URL from memory
-- this also means that if you want to run the test again, it does not just use a cached version
unload pURL
end if
-- check if there was a problem with the download
if pStatus = "error" or pStatus = "timeout" then
put libUrlErrodData(theUrl) into myErr
answer error "The file could not be downloaded."&& myErr with "ok"
end if
end downloadComplete
Let me know if this works (it should). If not, I'll have another look.

Complex assertion of table in Capybara

I have a table in my app.
Using Capybara and Cucumber, how do I assert that values 4.5 and 1.1 happen only in the Mike's row?
Is such assertion possible in Capybara?
Thanks!
You can use within to scope where you are searching for a specific value:
For example, to assert that value 4.5 happens in the second column of Mike's row, try the following:
within("table tr:nth-child(2)") do
find("td:nth-child(2)").text.should == 4.5
end
You can wrap these in helper methods for ease of use if you would like:
def within_row(num, &block)
within("table tr:nth-child(#{num})", &block)
end
def column_text(num)
find("td:nth-child(#{num})").text
end
Now you could make the same assertion about Mike's row by doing the following:
within_row(2) do
column_text(2).should == 4.1
end
Hopefully you will find one of these techniques useful for what you are trying to do.
Yes, it's possible and easy:
def td_text(n)
find(:xpath, "./td[#{n}]").text
end
h = {2 => 4.5, 3 => 1.1}
all('table tr').each do |row|
within row do
if td_text(1) == 'Mike'
h.each { |i, value| td_text(i).should == value.to_s }
else
h.each { |i, value| td_text(i).should_not == value.to_s }
end
end
end
Here's full script that you can use for testing
Update: I thought about it more. The code above will be very slow as every invocation of find and text in td_text will make new query to browser.
The only way to mitigate it that I see is to use JS and Nokogiri:
source = page.evaluate_script("document.getElementsByTagName('table')[0].innerHTML")
doc = Nokogiri::HTML(source)
def td_text(row, n)
  row.xpath("./td[#{n}]").text
end
h = {2 => 4.5, 3 => 1.1}
doc.css('tr').each do |row|
  if td_text(row, 1) == 'Mike'
    h.each { |i, value| td_text(row, i).should == value.to_s }
  else
    h.each { |i, value| td_text(row, i).should_not == value.to_s }
  end
end
The first variant of code runs about 200 milliseconds at my machine though the second one - 8 milliseconds. Good optimization!

Resources