{"id":3801,"date":"2024-05-02T21:32:53","date_gmt":"2024-05-03T02:32:53","guid":{"rendered":"https:\/\/osric.com\/chris\/accidental-developer\/?p=3801"},"modified":"2024-05-02T21:35:43","modified_gmt":"2024-05-03T02:35:43","slug":"icinga2-and-http_expect_body_regex","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2024\/05\/icinga2-and-http_expect_body_regex\/","title":{"rendered":"icinga2 and http_expect_body_regex"},"content":{"rendered":"<p>This check (along with the other accompanying http variables) tries to confirm that the page includes an IP address or subnet:<\/p>\n<pre><code>http_expect_body_regex = \"^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}($|\\\/)\"<\/code><\/pre>\n<p>However, the updated conf file didn&#8217;t pass validation:<br \/>\n<!--more--><\/p>\n<pre><code>sudo \/usr\/sbin\/icinga2 daemon --validate\r\n...\r\n[lots of output]\r\n...\r\n[2024-05-02 21:12:39 -0500] critical\/config: Error: Bad escape sequence found: \\.\r\n...<\/code><\/pre>\n<p>How am I supposed to check for a literal dot (<code>.<\/code>) without a backslash?<\/p>\n<p>The Icinga2 documentation&#8217;s language reference includes a list of <a href=\"https:\/\/icinga.com\/docs\/icinga-2\/snapshot\/doc\/17-language-reference\/#string-literals-escape-sequences\">string literals escape sequences<\/a>. I just needed to replace the backslash (<code>\\<\/code>) with a double-backslash (<code>\\\\<\/code>).<\/p>\n<p>I updated the variable:<\/p>\n<pre><code>http_expect_body_regex = \"^[0-9]{1,3}\\\\.[0-9]{1,3}\\\\.[0-9]{1,3}\\\\.[0-9]{1,3}($|\\\\\/)\"<\/code><\/pre>\n<p>That still didn&#8217;t pass validation:<\/p>\n<pre><code>sudo \/usr\/sbin\/icinga2 daemon --validate\r\n...\r\n[lots of output]\r\n...\r\n[2024-05-02 21:21:12 -0500] critical\/config: Error: Validation failed for object 'blocklist!https' of type 'Service'; Attribute 'vars' -&gt; 'http_expect_body_regex': Closing $ not found in macro format string '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}($|\\\/)'.\r\n...<\/code><\/pre>\n<p>Now it doesn&#8217;t like the dollar sign (<code>$<\/code>). The dollar sign isn&#8217;t listed in the language reference as a character that needs escaping though.<\/p>\n<p>Fortunately, I ran into this Icinga2 community post: <a href=\"https:\/\/community.icinga.com\/t\/which-characters-within-a-string-need-to-be-escaped-in-icingas-configs\/6567\">Which characters within a string need to be escaped in Icinga&#8217;s configs?<\/a><\/p>\n<p>That page indicates that the dollar sign is used in macros and can be escaped with another dollar sign.<\/p>\n<p>I updated the variable again:<\/p>\n<pre><code>http_expect_body_regex = \"^[0-9]{1,3}\\\\.[0-9]{1,3}\\\\.[0-9]{1,3}\\\\.[0-9]{1,3}($$|\\\\\/)\"<\/code><\/pre>\n<p>That passed validation!<\/p>\n<p>There is a note in the Icinga2 documentation under <a href=\"https:\/\/icinga.com\/docs\/icinga-2\/latest\/doc\/03-monitoring-basics\/#runtime-macros\">Monitoring Basics &#8211; Runtime Macros<\/a>:<\/p>\n<blockquote><p>When using the $ sign as single character you must escape it with an additional dollar character ($$).<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>While configuring an Icinga2 service to check the response body of a web page using http_expect_body_regex, I ran into a couple errors with characters that needed to be escaped: the backslash and the dollar sign. This post has the details on how to escape those characters, with references to the Icinga2 documentation.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[484],"tags":[495],"class_list":["post-3801","post","type-post","status-publish","format-standard","hentry","category-monitoring","tag-icinga2"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/3801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=3801"}],"version-history":[{"count":10,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/3801\/revisions"}],"predecessor-version":[{"id":3812,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/3801\/revisions\/3812"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=3801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=3801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=3801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}