use master
go

/* 
** Setup required for Monitor. Note: to allow a user group other 
** than dbo to use the Monitor, change public in the grant 
** statements below to the desired group (or user).
*/


/*
** Procedure used by Monitor Server when starting.
*/
if exists (select * from sysobjects
           where type = 'P'
           and name = 'mon_rpc_attach')
begin
        drop procedure mon_rpc_attach
end
go

create procedure mon_rpc_attach as
begin
         select @@kernel_addr, @@kernel_size, @@shmem_flags, @@mempool_addr

return 0
end
go

grant execute on mon_rpc_attach to dbo
go

/*
** Procedure used by Monitor client when connecting to Monitor Server.
*/
if exists (select * from sysobjects
           where type = 'P'
           and name = 'mon_rpc_connect')
begin
	drop proc mon_rpc_connect
end
go

create proc mon_rpc_connect as return 0
go

grant exec on mon_rpc_connect to dbo
go

grant select on sysmonitors to dbo
go

/*
** Procedure to authorize users lacking sa_role to do monitoring
*/
if exists (select * from sysobjects
           where type = 'P'
           and name = 'mon_authorize_non_sa')
begin
        drop procedure mon_authorize_non_sa
end
go

create procedure mon_authorize_non_sa as
begin
	if not exists (select * from syslogins where name = "probe")
	begin
		print "Please install two-phase commit and then run this procedure again"
		return 0
	end

	execute sp_role "grant","sa_role",probe
	return 0
end
go

grant execute on mon_authorize_non_sa to dbo
go
