#!/bin/sh
#set -vx

#Script will reset the state all of elements, interfaces, and addresses and will clear all current alarms.

MYSQL=""
DBNAME="sella_nms"
DBUSER="sella_nms"
DBPASS=""
DBHOST="localhost"

if [ -n "${MYSQL}" -a ! -x "${MYSQL}" ]; then
	echo "Unable to execute specified copy of mysql '${MYSQL}'."
	exit 1
elif [ -x /usr/local/bin/mysql ]; then
	MYSQL="/usr/local/bin/mysql"
elif [ -x /usr/bin/mysql ]; then
	MYSQL="/usr/bin/mysql"
else
	MYSQL="`which mysql`"
	if [ $? -ne 0 ]; then
		echo "Unable to locate mysql. Please update your path or set MYSQL within script."
		exit 1
	fi
fi
SQL="
DELETE FROM network_element_attrib WHERE variable LIKE '%-state%'; \
DELETE FROM network_element_interface_attrib WHERE variable LIKE '%-state%'; \
DELETE FROM network_element_interface_ip_attrib WHERE variable LIKE '%-state%'; \
DELETE FROM gui_event; \
"

echo "This script manually clears the SellaNMS state and alarms."
echo
echo "Using ${MYSQL}..."
echo "dbhost: ${DBHOST}"
echo "dbuser: ${DBUSER}"
echo "dbname: ${DBNAME}"
echo

echo "${SQL}" | ${MYSQL} -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME}

if [ $? -eq 0 ]; then
	echo "All states and alarms have been cleared."
	exit 0
else
	echo "Failed to clear states and alarms."
	exit 1
fi
