Monitor web service with Nagios check_http - web-services

I am trying to monitor a web service with check_http plug-in of Nagios.
The url I am trying to monitor includes url parameter.
And it turns out that check_http plugin will ignore the url parameter when checking.
Herewith my configuration.
'check_http' command definition
define command{
command_name check_http
command_line $USER1$/check_http -I $HOSTADDRESS$ $ARG1$
}
define service{
use local-service ; Name of service template to use
host_name pear
service_description HTTP
check_command check_http!-u http://pear.com/total?eId=12345&env=abcde
notifications_enabled 0
}

Try replacing the value passed into -u with a relative path instead of the full URL.
In this example the hostname (-H) will be supplied by $HOSTADDRESS$ which is taken from the address field of the pear host definition.
The value passed into the -u parameter should be a relative path, for example: /total?eId=12345&env=abcde.
We'll add the -u to the check_http_with_args command definition so we don't have to pass it as part of our parameters in our service definition.
define host {
host_name pear
alias pear
address pear.com
use linux-server
contact_groups admins
notification_interval 0
notification_period 24x7
notifications_enabled 1
register 1
}
define command{
command_name check_http_with_args
command_line $USER1$/check_http -H $HOSTADDRESS$ -u $ARG1$
}
define service {
service_description pear_total_http
use generic-service
check_command check_http_with_args!/total?eId=12345&env=abcde
host_name pear
contact_groups admins
notification_interval 0
notification_period 24x7
notifications_enabled 1
flap_detection_enabled 1
register 1
}
In the end, the command that Nagios execute should get translated into something that looks like this:
/usr/local/nagios/libexec/check_http -H pear.com -u /total?eId=12345&env=abcde
You can try executing the above from the command line to make sure it works for you.
Note: Replace the path to check_http with the actual path corresponding to your installation location on your Nagios server.
Relevant parts of the check_http man pages we're referencing:
-H, --hostname=ADDRESS
Host name argument for servers using host headers (virtual host)
Append a port to include it in the header (eg: example.com:5000)
...
-u, --url=PATH
URL to GET or POST (default: /)
Source: https://www.monitoring-plugins.org/doc/man/check_http.html
EDIT:
To answer your questions from the comment, the -k or --header= will allow you to pass in headers.
-k, --header=STRING
Any other tags to be sent in http header. Use multiple times for additional headers
Source: https://www.monitoring-plugins.org/doc/man/check_http.html
So to specify an Accept header, I'd modify the following to look like:
define command{
command_name check_http_with_args
command_line $USER1$/check_http -H $HOSTADDRESS$ -u "$ARG1$" -k "$ARG2$"
}
define service {
service_description pear_total_http
use generic-service
check_command check_http_with_args!/total?eId=12345&env=abcde!Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
host_name pear
contact_groups admins
notification_interval 0
notification_period 24x7
notifications_enabled 1
flap_detection_enabled 1
register 1
}
...adding -k "$ARG2$" to the command_line of the command definition and adding the Accept: <MIME types> (for example Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8) to the check_command of the service definition.
Also, I wrapped $ARG1$ of -u "$ARG1$" part of the command_line in double quotation marks as I suspect the & in eId=12345&env=abcde is causing the shell to think the end of the command is terminated just before the &. Wrapping the parameter in double quotes should make it see the whole string as one whole argument.

Related

Edit/Match a string n lines below some specific match in Bash?

I have a complex problem. Below is the ndxconfig.ini file I want to Edit
# /etc/ndxconfig.ini will override this file
# if APP_ID is added in service propery, service discovery will be using marathon;
# HOST/PORT specified will override values retrieved from marathon
[MARATHON]
HOSTS = {{ ','.join(groups['marathon'])}}
PORT = 8080
PROTOCOL = http
SECRET = SGpQIcjK2P7RYnrdimhhhGg7i8MdmUqwvA2JlzbyujFS4mR8M88svI7RfNWt5rnKy4WHnAihEZmmIUb940bnlYmnu47HdUHE
[MYSQL]
; APP_ID = /neon/infra/mysql
HOST = {{keepalived_mysql_virtual_ip}}
PORT = 3306
SECRET = tIUFN1rjjDBdEXUsOJjPEtdieg8KhwTzierD48JsgDeYc84DD6Uy5a6kzHfKolq1MNS1DKwlSqxENk33UulJd9DPHPzYCxFm
I want to change specifically marathon protocol conf from http to https. Not other's protocol conf. I have to match PROTOCOL = http 3 lines below the [MARATHON] line. I researched and couldn't find any solution. There's only 1 line below sed solutions.
One idea stuck mine was somehow specially grep [MARATHON] and 3 lines below and tail 1 line. I don't know.
How can fix this? Please Help.
Solution found here
sed '/\[MARATHON\]/{N;N;N;s/http/https/;}' <file>
If you have python available, you can use crudini:
crudini --set ndxconfig.ini MARATHON protocol https
This screams for ed, which treats the file as a whole, not processing it a line at a time like sed (And also doesn't depend on the availability of the non-posix -i extension to sed for in-place editing of files):
ed -s ndxconfig.ini <<'EOF'
/MARATHON/;/^$/ s/^PROTOCOL = http$/PROTOCOL = https/
w
EOF
This will replace the PROTOCOL line in the block that starts with a line matching MARATHON and ending with a blank line. The protocol entry can be the second, third, fourth, etc. line; doesn't matter. If the protocol is already https, it won't do anything (Except print a question mark)

cat lines into file after regex

I want to add the info below into the file usr/local/nagios/etc/hosts.cfg but want to do it just below ##company in the hosts.cfg file. My setup script will contain the info that needs to be added
I have spend hours trying to get sed to just add a line into a file after a marker but to no avail
define host{
use linux-box
host_name $host_name
alias $alias
address $ip
parents $parent
notification_period 24x7
notification_interval 5
}
Previously I used
cat <> /path /filename
EOT
but now I need to do it in specif places in the file
Given the following file:
# some content
###company
If I run the following command:
sed -i 's/###company/&\ndefine host {\nuse host\nhost_name HOSTNAME/' file
Now, the contents of file are:
# some content
###company
define host {
use host
host_name HOSTNAME
Is this what you're looking for?

How to configure OpenSMTPD with Amazon SES?

Amazon has instructions for postfix and sendmail, but not OpenSMTPD, so adding them here.
Tested with OpenBSD 5.8
Verify your domain and a sender in AWS SES console. Save your SMTP Settings.
Set up the SMTP authentication details in the mail secrets database (replacing $smtpUsername:$smtpPassword with the values from step 1)
# touch /etc/mail/secrets
# chmod 640 /etc/mail/secrets
# chown root:_smtpd /etc/mail/secrets
# echo "ses $smtpUsername:$smtpPassword" >> /etc/mail/secrets
# makemap /etc/mail/secrets
Configure OpenSMTPD:
# nano /etc/mail/smtpd.conf
listen on lo0
table aliases db:/etc/mail/aliases.db
table secrets db:/etc/mail/secrets.db
accept for local alias <aliases> deliver to mbox
accept from local for any relay via tls+auth://ses#email-smtp.us-east-1.amazonaws.com auth <secrets>
Restart OpenSMTPD:
# rcctl restart smtpd
Test it:
# sendmail -v -f verified-sender#verified-domain.com to#example.com
Subject: test subject
test body
^D
Errors?
watch your line-breaks in smtpd.conf
# smtpd -n to check for syntax errors in smtpd.conf
Try port 587 if your machine is blocking port 25 (add :587 to end of aws url in smtpd.conf)

Comment out/delete blocks in large amount of files

I'm attempting to write something that can process Nagios config files containing a block of text, and either add # to the start of each line, or delete the block. (To function as a kind of mass check removal in Nagios itself).
Ex:
define service {
service_description Service 1
use Template 1
host_name Host 1
check_command Command A
}
define service {
service_description Service 2
use Template 1
host_name Host 1
check_command Command B
}
define service {
service_description Service 3
use Template 1
host_name Host 1
check_command Command C
}
Would need to change to this (or equivalent):
define service {
service_description Service 1
use Template 1
host_name Host 1
check_command Command A
}
#define service {
# service_description Service 2
# use Template 1
# host_name Host 1
# check_command Command B
#}
define service {
service_description Service 3
use Template 1
host_name Host 1
check_command Command C
}
Is there a way to regex match the block between "define service {" and "}", and containing either "Service 2" or "Command "B", and append/delete the block via sed/awk/perl, etc?
Thanks.
sed '
# take each paragraph one by one
/define service {/,/}/{
# inside paragraphe, add each line (one at a time) in buffer
H
# if not the end of paragraphe, delete the line (from output) (and cycle to next line)
/}/!d
# empty current line (the last of paragraphe) and swap with the whole buffer
s/.*//;x
# if it contain Service2 (and Command B on next line) goto to label comm
/Service2/ b comm
/Command B/ b comm
# goto label head (so no Service2 nor command b in paragraphe)
b head
:comm
# comment each line of paragraphe (multiline with \n as new line)
s/\n/&#/g
: head
# remove first character (a new line due to use of H and not h on first line)
s/.//
}
# default behaviour that print the content
' YourFile
Self commented
Here's a regex that creates the match you want:
/define service\s\{.*(Service 2|Command B).*\}/s
You can test it here. I have no experience with sed, awk or perl, so I will not make any attempt to create a replacement.
sed -n '/define service {/,/}/{:1;N;/\}/!b 1;/Command B\|Service 2/{s/\n/\n#/g;s/^/#/g;p;d;b 1};p;d;b 1}' file_name
This is how it works
picks the block within braces;keeps appending to pattern space till a closing brace is found;searches the patter space for the two key words; if found then appends a # in front of every new line in pattern space and prints the content. Clears the pattern space and repeats the provess

Varnishlog log only specified IP

I want to log varnish backend request which matches specified IP (for example 127.0.0.1).
So i have
"varnishlog -b -I BereqHeader:X-Forwarded-For: 127.0.0.1'"
Which actualy logs only the "BereqHeader:X-Forwarded-For:" part. I want to log full request, not only IP part.
That was first question, the second one is: how to disable loging empty request? I mean, if i have regex filter then i have a lot of request looking like this "* << BeReq >> 307454" and i obviously dont want to see them.
I have a solution. Log the data by
varnishlog -b -I BereqHeader:'X-Forwarded-For: 123.215.32.76' -i [other tags to log] > file.varnishlog
and then grep it by
cat file.varnishlog | grep -Pzo '* {3}<< BeReq {4}>>.\n- BereqHeader.+\n(-.\n)*'
which'll give us expected results.