Hello,
Good day.
I am trying to get Availability report of last 7 days from Nagios core. I got a script to automate this report and i have placed it under the below path:
/usr/local/nagios/libexec
Script:
#!/bin/bash
NAGIOS_URL="http://nagios-server/nagios/cgi-bin/avail.cgi"
NAGIOS_USER="username"
NAGIOS_PASS="password"
RECIPIENT_EMAIL="email address"
REPORT_FILE="availability_report.csv"
START_TIME=$(date -d "7 days ago" +%s)
END_TIME=$(date +%s)
# Fetch the availability report in a "dump" (CSV-like) format
# and save it to a file.
curl -s -u "$username:$password" "$http://nagios-server/nagios/cgi-bin/ava ... utput=dump" > "$REPORT_FILE"
# Check if the curl command was successful
if [ $? -eq 0 ] && [ -s "$REPORT_FILE" ]; then
echo "Nagios availability report generated. Sending email..."
# Send the email with the report as an attachment
# The mail command varies by system. This example uses a common syntax.
# Adjust the 'mail' command as needed for your specific system.
echo "Please find the last 7 days Nagios availability report attached." | mail -s "Nagios Availability Report (Last 7 Days)" -A "$REPORT_FILE" "$RECIPIENT_EMAIL"
echo "Email sent to $RECIPIENT_EMAIL."
else
echo "Failed to generate report or report is empty."
fi
# Clean up the report file
rm "$REPORT_FILE"
Output:
I am getting excel over mail but data is not getting displayed. I have tried all the possibilities but no luck.
Please find the below output file over mail which i am receiving:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested. Either you supplied the wrong
credentials (e.g. bad password) or your
browser doesn't understand how to supply
the credentials required.</p>
<hr>
<address>Apache/2.4.58 (Ubuntu) Server at inisnagios01 Port 80</address>
</body></html>
I have verified credentials also but still having issue. Kindly please check and help me in solving this issue.
Thanks in advance.
Srinija.
Unable to fetch Availability report for last 7 days from Nagios core using script
-
srinija3110
- Posts: 4
- Joined: Thu Jun 20, 2024 3:04 am
Re: Unable to fetch Availability report for last 7 days from Nagios core using script
Hi @srinija3110,
The 401 indicates authentication failure. Your script defines NAGIOS_USER and NAGIOS_PASS but the curl command uses $username:$password. Those variables are undefined, so no credentials are being sent.
Run the script with set -x or test curl manually with -v to verify what’s actually being passed.
Let me know if you other questions.
-Emmett
The 401 indicates authentication failure. Your script defines NAGIOS_USER and NAGIOS_PASS but the curl command uses $username:$password. Those variables are undefined, so no credentials are being sent.
Run the script with set -x or test curl manually with -v to verify what’s actually being passed.
Let me know if you other questions.
-Emmett
-
srinija3110
- Posts: 4
- Joined: Thu Jun 20, 2024 3:04 am
Re: Unable to fetch Availability report for last 7 days from Nagios core using script
Hi ekapaner,
Thank you for your response.
I have tried running the script with set -x but no luck getting the same output as mentioned.
If i run the Curl command with -v getting the below output:
root@nagiosserver:/usr/local/nagios/libexec# curl -s -u -v "nagiosuser:password!" "http://nagiosserver/nagios/cgi-bin/avai ... utput=dump" > "$REPORT_FILE"
curl -s -u -v "nagiosuser:passwordcurl -s -u "nagiosuser:password nagios_report.sh" "http://nagiosserver/nagios/cgi-bin/avai ... utput=dump" > "$REPORT_FILE"" "http://nagiosserver/nagios/cgi-bin/avai ... utput=dump" > "$REPORT_FILE"
[1] 3719991
[2] 3719992
+ timeperiod=last7days
+ curl -s -u -v 'nagiosuser:passwordcurl -s -u nagiosuser:password' 'nagios_report.sh http://inisnagios01/nagios/cgi-bin/avai ... tgroup=all'
[3] 3719993
[4] 3719994
+ show_log_entries=
[5] 3719995
+ assumeinitialstates=yes
[6] 3719996
+ assumestateretention=yes
[7] 3719997
+ assumestatesduringnotrunning=yes
+ includesoftstates=no
[8] 3719998
[9] 3719999
+ initialassumedhoststate=3
+ initialassumedservicestate=6
[10] 3720000
+ backtrack=4
+ output='dump > '
+ 'http://nagioserver/nagios/cgi-bin/avail ... utput=dump'
-bash: : No such file or directory
[2] Done timeperiod=last7days
[3] Done show_log_entries=
[4] Done assumeinitialstates=yes
[5] Done assumestateretention=yes
[6] Done assumestatesduringnotrunning=yes
[7] Done includesoftstates=no
[8] Done initialassumedhoststate=3
[9]- Done initialassumedservicestate=6
[10]+ Done backtrack=4
I hope something i am missing. Kindly help me on this.
Thanks in advance.
Srinija A.
Thank you for your response.
I have tried running the script with set -x but no luck getting the same output as mentioned.
If i run the Curl command with -v getting the below output:
root@nagiosserver:/usr/local/nagios/libexec# curl -s -u -v "nagiosuser:password!" "http://nagiosserver/nagios/cgi-bin/avai ... utput=dump" > "$REPORT_FILE"
curl -s -u -v "nagiosuser:passwordcurl -s -u "nagiosuser:password nagios_report.sh" "http://nagiosserver/nagios/cgi-bin/avai ... utput=dump" > "$REPORT_FILE"" "http://nagiosserver/nagios/cgi-bin/avai ... utput=dump" > "$REPORT_FILE"
[1] 3719991
[2] 3719992
+ timeperiod=last7days
+ curl -s -u -v 'nagiosuser:passwordcurl -s -u nagiosuser:password' 'nagios_report.sh http://inisnagios01/nagios/cgi-bin/avai ... tgroup=all'
[3] 3719993
[4] 3719994
+ show_log_entries=
[5] 3719995
+ assumeinitialstates=yes
[6] 3719996
+ assumestateretention=yes
[7] 3719997
+ assumestatesduringnotrunning=yes
+ includesoftstates=no
[8] 3719998
[9] 3719999
+ initialassumedhoststate=3
+ initialassumedservicestate=6
[10] 3720000
+ backtrack=4
+ output='dump > '
+ 'http://nagioserver/nagios/cgi-bin/avail ... utput=dump'
-bash: : No such file or directory
[2] Done timeperiod=last7days
[3] Done show_log_entries=
[4] Done assumeinitialstates=yes
[5] Done assumestateretention=yes
[6] Done assumestatesduringnotrunning=yes
[7] Done includesoftstates=no
[8] Done initialassumedhoststate=3
[9]- Done initialassumedservicestate=6
[10]+ Done backtrack=4
I hope something i am missing. Kindly help me on this.
Thanks in advance.
Srinija A.
Re: Unable to fetch Availability report for last 7 days from Nagios core using script
Hi @srinija3110,
My intent in recommending set -x and curl -v was not for it to solve the issues with the script, but rather help you debug it. They are both useful options for seeing exactly what a script is doing as it runs.
Judging from this output, it looks as though bash is interpreting pieces of the URL and parameters as separate background jobs. I would recommend reviewing how you are quoting the URL so that it is passed as a single argument to curl.
- Emmett
My intent in recommending set -x and curl -v was not for it to solve the issues with the script, but rather help you debug it. They are both useful options for seeing exactly what a script is doing as it runs.
Judging from this output, it looks as though bash is interpreting pieces of the URL and parameters as separate background jobs. I would recommend reviewing how you are quoting the URL so that it is passed as a single argument to curl.
- Emmett