File: //usr/lib64/nagios/plugins/check_a2_saltmaster_api.sh
#!/bin/bash
# Initialize variables
exit_code=0
output=""
# Check salt-master status
salt_master_status=$(systemctl is-active salt-master 2>/dev/null)
salt_master_ports_status=$(netstat -lntu | grep -E -c ":(4505|4506).*LISTEN")
if [ "$salt_master_status" != "active" ]; then
output+="check_saltmaster_status - Saltmaster is not running. "
exit_code=2
fi
if [ "$salt_master_ports_status" -ne 2 ]; then
output+="check_saltmaster_ports_status - Ports 4505/4506 are not listening. "
exit_code=2
fi
# Check salt-api status
salt_api_status=$(systemctl is-active salt-api 2>/dev/null)
salt_api_ports_status=$(netstat -lntu | grep -E -c ":8000.*LISTEN")
if [ "$salt_api_status" != "active" ]; then
output+="check_salt-api_status - Salt-api is not running. "
exit_code=2
fi
if [ "$salt_api_ports_status" -ne 1 ]; then
output+="check_salt-api_port_status - Port 8000 is not listening. "
exit_code=2
fi
# If everything is fine
if [ "$exit_code" -eq 0 ]; then
output="OK - Saltmaster and Salt-api services are running with correct ports open."
fi
# Print final output and exit with the highest severity found
echo "$output"
exit $exit_code