BEGIN
DECLARE v_mysql_version VARCHAR(20);
DECLARE v_threads_running INT;
DECLARE v_called_by_user VARCHAR(50);
DECLARE v_sleep int;
DECLARE sql_logging BOOLEAN;
select @@sql_log_bin into sql_logging;
Select user() into v_called_by_user;
Select version() into v_mysql_version;
SELECT COUNT(1) into v_threads_running FROM information_schema.processlist WHERE user = 'system user';
    if v_threads_running > 0
     then
       set @@sql_log_bin=off;
       update mysql.rds_replication_status set called_by_user=v_called_by_user,action='stop slave', mysql_version=v_mysql_version where action is not null;
       commit;
       select sleep(1) into v_sleep;
       STOP SLAVE;
       SELECT COUNT(1) into v_threads_running FROM information_schema.processlist WHERE user = 'system user';
           if v_threads_running = 0
           then
           insert into mysql.rds_history (called_by_user,action,mysql_version) values (v_called_by_user,'stop slave',v_mysql_version);
           commit;
           Select 'Slave is down or disabled' as Message;
           else
           Select 'Slave has encountered an error. Run SHOW SLAVE STATUS\G; to see the error.' as Message;
           end if;
      else
     if v_threads_running < 1
       then
       Select 'Slave is already stopped or may not be configured. Run SHOW SLAVE STATUS\G;' as Message;
       end if;
 end if;
set @@sql_log_bin=sql_logging;
END