define nagios command with "&&" - nagios

Have a following nagios command, with this command_line:
command_line curl -H "Content-Type: application/json" -d "{\"title\": \" ** $NOTIFICATIONTYPE$ alert - $HOSTNAME$ is $HOSTSTATE$ **\", \"text\": \"***** Nagios Admin *****\n\nHost: $HOSTNAME$ ($HOSTALIAS$)\nAddress: $HOSTADDR$" && php /usr/local/nagios/libexec/eventhandlers/manage-host-tickets.php hoststate="$HOSTSTATE$" hoststatetype="$HOSTSTATETYPE$" eventhost="$HOSTNAME$" hostattempts="$HOSTATTEMPT$" maxhostattempts="$MAXHOSTATTEMPTS$" hostproblemid="$HOSTPROBLEMID$" lasthostproblemid="$LASTHOSTPROBLEMID$"
They are two separate tasks that are separated by "&&" and work individually.
This way don't work. I am calling this command in the hosts event handler.

Related

Use curl to post file from pipe

How might i take the output from a pipe and use curl to post that as a file?
E.g. the following workds
curl -F 'file=#data/test.csv' -F 'filename=test.csv' https://mydomain#apikey=secret
I'd like to get the file contents from a pipe instead but I can't quite figure out how to specify it as a file input. My first guess is -F 'file=#-' but that's not quite right.
cat data/test.csv | curl -F 'file=#-' -F 'filename=test.csv' https://mydomain#apikey=secret
(Here cat is just a substitute for a more complex sequence of events that would get the data)
Update
The following works:
cat test/data/test.csv | curl -XPOST -H 'Content-Type:multipart/form-data' --form 'file=#-;filename=test.csv' $url
If you add --trace-ascii - to the command line you'll see that curl already uses that Content-Type by default (and -XPOST doesn't help either). It was rather your fixed -F option that did the trick!

Imap: Get multiple sections from email with one command

I crossposted this in the curl mailing list (curl-users).
I know how to get multiple parts of an email header like this:
curl --url "imaps://imap.ionos.de/INBOX;UID=216;SECTION=HEADER.FIELDS%20(DATE%20FROM%20TO%20SUBJECT)" -u "user:password"
But is it possible to get multiple sections (received date and text in
my case) at once? I want to combine these two commands:
1: Fetch email receiving date
curl --url "imaps://imap.ionos.de/INBOX;UID=216;SECTION=HEADER.FIELDS%20(DATE)" -u "user:password"
2: Fetch email text
curl --url "imaps://imap.ionos.de/INBOX;UID=216;SECTION=TEXT" -u "user:password"
I tried something like this:
curl --url "imaps://imap.ionos.de/INBOX;UID=216;SECTION=HEADER.FIELDS%20(DATE);SECTION=TEXT" -u "user:password"
–––––––––––––––––––––
Thank you diciu, this works just fine. I have an additional question:
When i add --ouput output.txt the file only contains the last FETCH (SECTION=TEXT in this case). Is it possible to output all FETCH results to one output file? I know i can use >> output.txt to append but i use that to write a log file within the same command.
Here my full command:
curl --url "imaps://imap.ionos.de/INBOX;UID=295;SECTION=HEADER.FIELDS%20(DATE)" "imaps://imap.ionos.de/INBOX;UID=295;SECTION=TEXT"-u "user:password" --output output.txt --verbose >> logfile.log 2>&1
You have to add the two requests one after another, e.g.
curl --url "imaps://imap.ionos.de/INBOX;UID=216;SECTION=HEADER.FIELDS%20(DATE)" "imaps://imap.ionos.de/INBOX;UID=216;SECTION=TEXT" -u "user:password"
curl will issue the two commands on the same session, that is after SELECT-ing the folder it will yield two FETCH commands.

cURL Cloudant attachment example please

Please forgive me for the potentially basic question but I am a z/OS person trying to learn cURL and Cloudant. I have gotten the following example to work to add a record to a database (using DOS from Windows) :
curl -X POST -b /tmp/cloudant.cookie -H "Content-Type: application/json" -d "{\"_id\":\"2\",\"empName\":\"John Doe\",\"phone\":\"646-598-4133\",\"age\":\"28\"}" --url https://xxxxxxxxxx-bluemix.cloudant.com/rcdb
Now I would like to add a _attachment image1.jpg dile to that record...
Could anyone please tell me what the syntax on windows would be...trying a few combinations but nothing so far works.
To add an attachment follow the instructions in the Cloudant documentation at https://docs.cloudant.com/attachments.html
Example:
Assuming you have already created a document with ID "2" and revision number "1-954695fb9642f02975d76b959d0b5e98" in database rcdb, run the following command:
curl -X PUT -H "Content-Type: image/jpeg" --data-binary "#image1.jpg" --url https://xxxxxxxxxx-bluemix.cloudant.com/$DATABASE/$DOCUMENT_ID/$ATTACHMENT?rev=$REV
replacing $DATABASE with rcdb, $DOCUMENT_ID with 2, $REV with 1-954695fb9642f02975d76b959d0b5e98 and $ATTACHMENT with the desired attachment property name, e.g. mypic.

check_http with directory results in "Name or service not known"

I have a nagios system, working well and i wanted to check a specific url with check_http.
The command is defined:
define command{
command_name check_http_with_folder
command_line $USER1$/check_http -H $HOSTADRESS$ -u http://$HOSTADRESS$$ARG1$
}
and i call it correct ... But it throws me an
"Name or service not known"
When i call it from my nagios machine from command line, it works well and i get an status result 200, so all okay.
The problem is now, that i want the nagios command working and not throwing an error.
Any Ideas?
P.S. The problem is only in the part with the -u xxx param, without it (in the normal check_http command without -u) it all works well.
You've misspelled $HOSTADDRESS$ in your command definition. It needs 2 D's. Also, you might want to ensure there is a slash in between $HOSTADDRESS$ and $ARG1$ in the value you pass in to your -u command argument, or make sure that $ARGS1$ has a preceding slash.
Building on Joe's observations...
Note the corrections:
define command{
command_name check_http_with_folder
command_line $USER1$/check_http -H $HOSTADDRESS$ -u $ARG1$
}
Then the $HOSTADDRESS$ should be just that. For example, www.example.com. And $ARG1$ should be the location at the host only. For example, /blog/index.php. The check_http check will build it into an actual http request.

why is checking a URL failing when run through icinga?

I created my own command to check a specific URL
define command{
command_name check_url
command_line /usr/lib/nagios/plugins/check_http -f follow -H '$HOSTNAME$' -I '$HOSTADDRESS$' -u '$ARG1$'
}
If I run my command from the command line, it works:
/usr/lib/nagios/plugins/check_http -f follow -H www.example.com -u http://www.example.com/server-status
HTTP OK: HTTP/1.1 200 OK - 4826 bytes in 0.011 second response time |time=0.010625s;;;0.000000 size=4826B;;;0
But when run through Icinga, I'm getting
HTTP WARNING: HTTP/1.1 404 NOT FOUND - 314 bytes in 0.011 second response time
My guess is for check_http plugin for -u option you should provide the url appended after the server name not the whole url.
Ex.
/usr/lib/nagios/plugins/check_http -f follow -H www.example.com -u /server-status
Your manual test is not equivalent to your command definition.
The distinction with -H/-I is subtle, but very important.
When I have problems like this, where Icinga is abstracting exactly how it is executing the command, I find it helpful to find out precicely what Icinga is executing. I would accomplish this as follows:
Move check_http to a temporary location
# mv /usr/lib/nagios/plugins/check_http /usr/lib/nagios/plugins/check_http_actual
Make a bash script that Icinga will call instead of the actual check_http script
# vi /usr/lib/nagios/plugins/check_http
In that file, create this simple bash script, which simply echos the command line arguments it was called with, then exits:
#!/bin/bash
echo $#
Then of course, make that bash script executable:
# chmod +x /usr/lib/nagios/plugins/check_http
Now in Icinga, run the check_http command. At that point, the return status shown in the Icinga web interface will show exactly how Icinga is calling check_http. Seeing the raw command, it should be obvious as to what Icinga is doing wrong. Once you correct Icinga's mistake, you can simply move the original check_http script back into place:
# mv /usr/lib/nagios/plugins/{check_http_actual,check_http}

Resources