Page 1 of 1

nagios , alert of grafana

Posted: Tue Mar 09, 2021 4:19 pm
by Elamrani
hello , i want configure nagios for get alert of grafana , but i Don't find information how i can do that , please help :|

Re: nagios , alert of grafana

Posted: Wed Mar 10, 2021 6:15 pm
by benjaminsmith
HI,

Do you mean alerts or performance graphs? In most cases, it's performance graphs.

Take a look at: https://support.nagios.com/kb/article/n ... u-802.html

Re: nagios , alert of grafana

Posted: Fri Mar 12, 2021 11:34 am
by Elamrani
Thanks for your reply , i need a plugin for nagios so that it can check if there are alerts in grafana , if yes nagios gets this alerts thanks so much

Re: nagios , alert of grafana

Posted: Tue Mar 16, 2021 7:49 am
by Connorrr
I usually put this code into an script into /usr/local/bin/. If this is confusing to you, refer on this link https://linuxboss.wordpress.com/2015/06 ... afana-how/

portable propane generator top kitchen cabinets top rated personal injury lawyer

Re: nagios , alert of grafana

Posted: Fri Mar 19, 2021 10:37 am
by Elamrani
Thanks ,but it's not works :cry:

Re: nagios , alert of grafana

Posted: Fri Mar 19, 2021 2:01 pm
by mcapra
What versions of Grafana and Nagios Core are you using?

With Nagios XI, through the REST API, you could configure a generic webhook in Grafana to feed information into an event handler. Not sure there's anything equivalent in Nagios Core unless you've got NRDP set up. To get the full content of every Grafana alert into Nagios Core, an event handler of some sort is really the way to go.

Here's a quick Bash script I ginned up as a Nagios plugin that hits the Grafana REST API's alerts endpoint with curl to grab the active alerts, and uses grep+wc to count the number of active alerts:

Code: Select all

GRAFANA_URL='http://localhost:3000'
GRAFANA_USERNAME='your_username_here'
GRAFANA_PASSWORD='your_password_here'

CURL_COMMAND=$(curl -XGET -s --show-error -u "$GRAFANA_USERNAME:$GRAFANA_PASSWORD" "$GRAFANA_URL/api/alerts" 2>&1)
CURL_STATUS=$?

if [ $CURL_STATUS -ne 0 ]; then
    echo "UNKNOWN - Error calling Grafana API: ${CURL_COMMAND}"
    exit 3;
fi

if [[ "$CURL_COMMAND" == *\"message\":* ]]; then
    echo "UNKNOWN - Grafana API returned: $CURL_COMMAND"
    exit 3;
fi


COUNT_COMMAND=$(grep -o '"id":[0-9]*,' <<< ${CURL_COMMAND} | wc -l)
if [ "$COUNT_COMMAND" -eq "0" ]; then
   echo "OK - No Grafana alerts found.";
   exit 0;
else
    echo "CRITICAL - ${COUNT_COMMAND} alerts found in Grafana."
    exit 2;
fi

echo "UNKNOWN - An unexpected error occurred in the script."
exit 3;
Should just need to edit the GRAFANA_ variables to match your Grafana installation. Gets the "works on my machine" gold star of approval against Grafana 7.4.5; YMMV.

Code: Select all

## invalid connection
# sh check_grafana.sh 
UNKNOWN - Error calling Grafana API: curl: (7) Failed to connect to localhost port 3000: Connection refused

# sh test.sh
CRITICAL - 1 alerts found in Grafana.
# sh test.sh
OK - No Grafana alerts found.

## with invalid creds
# sh test.sh
UNKNOWN - Grafana API returned: {"message":"invalid username or password"}
# echo $?
3
You could probably do clever things with cron, jq, and the Nagios Core external commands file to implement some sort of pull method rather than a push method. I'd recommend a push method in this case; Grafana does that pretty well out of the box.

Re: nagios , alert of grafana

Posted: Mon Mar 22, 2021 4:37 am
by Elamrani
thanks so much for your help , thanks
i did something wrong

GRAFANA_URL='http://llsndn3b.sandbox.gcloud.belgium.be/'
GRAFANA_USERNAME='admin'
GRAFANA_PASSWORD='student'

CURL_COMMAND=$(curl -XGET -s --show-error -u "$GRAFANA_USERNAME:$GRAFANA_PASSWORD" "$GRAFANA_URL/api/alerts" 2>&1)
CURL_STATUS=$?

if [ $CURL_STATUS -ne 0 ]; then
echo "UNKNOWN - Error calling Grafana API: ${CURL_COMMAND}"
exit 3;
fi

if [[ "$CURL_COMMAND" == *\"message\":* ]]; then
echo "UNKNOWN - Grafana API returned: $CURL_COMMAND"
exit 3;
fi


COUNT_COMMAND=$(grep -o '"id":[0-9]*,' <<< ${CURL_COMMAND} | wc -l)
if [ "$COUNT_COMMAND" -eq "0" ]; then
echo "OK - No Grafana alerts found.";
exit 0;
else
echo "CRITICAL - ${COUNT_COMMAND} alerts found in Grafana."
exit 2;
fi

echo "UNKNOWN - An unexpected error occurred in the script."
exit 3;
-------------------------------------------------------------------------------
in nagios :
ssh check_alerts_grafana.sh
his give my
check_alerts_grafana.sh: line 9: [: missing']'
wc: invalid option --'1'
check_alerts_grafana.sh: line 20 [: : integer expresion expected
CRITICAL - ALERTS found in grafana
what you thinks where's problem ?
thanks you so so much for you help ,