我需要创建一个应用程序或服务,通知日志文件何时增加到一定大小以上。电子邮件或其他内容。
答案 0 :(得分:3)
sys.master_files视图包含日志文件的大小:
select size * 8192 / (1024 * 1024) as [Size in MB]
from sys.master_files
where type_desc = 'LOG'
and database_id = db_id('NameOfYourDatabase')
它以8KB块的形式列出大小。
您可以检查SQL代理计划任务的大小,如果超过特定大小,请发送包含sp_send_dbmail的电子邮件。
答案 1 :(得分:1)
这是整个脚本化代理程序任务,其中包含上述逻辑。请参见顶部设置的3个变量。 (如果您只想生成Windows应用程序日志错误,则运算符名称可以为null)
USE [msdb]
GO
Declare @AdminUserName varchar(255)
Declare @Operator varchar(255)
Declare @EmailNotify bit
Set @AdminUserName = N'YOUR SYSADMIN USER'
set @Operator = N'YOUR OPERATOR NAME'
Set @EmailNotify = 1
/****** Object: Job [Transaction Logfile Size Monitoring] Script Date: 08/02/2012 15:39:11 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object: JobCategory [[Uncategorized (Local)]]] Script Date: 08/02/2012 15:39:11 ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
END
Declare @NotifyLevel int
If @EmailNotify = 1 Begin
set @NotifyLevel =2 end
else begin
set @NotifyLevel=0
set @Operator=null
end
DECLARE @jobId BINARY(16)
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'Transaction Logfile Size Monitoring',
@enabled=1,
@notify_level_eventlog=0,
@notify_level_email=@NotifyLevel,
@notify_level_netsend=0,
@notify_level_page=0,
@delete_level=0,
@description=N'No description available.',
@category_name=N'[Uncategorized (Local)]',
@owner_login_name=@AdminUserName,
@notify_email_operator_name=@Operator, @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [Monitor Logfile Size] Script Date: 08/02/2012 15:39:11 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Monitor Logfile Size',
@step_id=1,
@cmdexec_success_code=0,
@on_success_action=1,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=0,
@retry_interval=0,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'Declare @LogSize int;
Declare @LogName varchar(255);
Declare @LogFile varchar(2000);
select Top 1 @LogName=name,@LogFile=physical_name,@LogSize = convert(float,size) * 8192 / (1024 * 1024) from sys.master_files
where type_desc = ''LOG'' Order by size desc
IF (@LogSize > 10240)
BEGIN
Declare @AlertString varchar(5000)
set @AlertString = @LogName + '' : '' + @LogFile + '' has reached a size of '' + Convert(varchar(20),@LogSize) + '' MB.''
RAISERROR(@AlertString, 18,1) with LOG
END
ELSE
BEGIN
RAISERROR(''Transaction Log monitoring complete'', 0, 1)
END',
@database_name=N'master',
@flags=4
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'Hourly Logfile Check',
@enabled=1,
@freq_type=4,
@freq_interval=1,
@freq_subday_type=8,
@freq_subday_interval=1,
@freq_relative_interval=0,
@freq_recurrence_factor=0,
@active_start_date=20120802,
@active_end_date=99991231,
@active_start_time=0,
@active_end_time=235959,
@schedule_uid=N'18435d90-4ca0-4c7a-a8a9-c81f347ba32c'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO