1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| USE [msdb]
GO
-------------------------------------------------------------------------------------------------------
DECLARE @operator_name sysname = 'monOperateur'
-------------------------------------------------------------------------------------------------------
DECLARE @agent_state TABLE
(
agent_state varchar(10)
)
INSERT INTO @agent_state
EXEC xp_servicecontrol 'QUERYSTATE', 'SQLServerAgent'
IF EXISTS
(
SELECT *
FROM @agent_state
WHERE agent_state NOT LIKE 'Running%'
)
BEGIN
RAISERROR('Le service SQL Server Agent n''est pas démarré et ne peut donc pas relever les alertes', 16, 1)
RETURN
END
IF NOT EXISTS
(
SELECT *
FROM msdb.dbo.sysoperators
WHERE name = @operator_name
)
BEGIN
RAISERROR('Il n''existe pas d''opérateur de nom %s', 16, 1, @operator_name)
RETURN
END
IF EXISTS
(
SELECT *
FROM msdb.dbo.sysoperators
WHERE name = @operator_name
AND enabled = 0
)
BEGIN
RAISERROR('L''opérateur %s est désactivé', 16, 1, @operator_name)
RETURN
END
----------------------------------
-- Ajouts des alertes sur sévérité
----------------------------------
DECLARE @i tinyint = 16
, @alert_name sysname
WHILE @i <= 25
BEGIN
SELECT @alert_name = 'Severity ' + CAST(@i AS char(2))
EXEC msdb.dbo.sp_add_alert
@name = @alert_name
, @message_id = 0
, @severity = @i
, @enabled = 1
, @delay_between_responses = 60
, @include_event_description_in = 1
, @job_id = N'00000000-0000-0000-0000-000000000000'
EXEC msdb.dbo.sp_add_notification
@alert_name = @alert_name
, @operator_name = @operator_name
, @notification_method = 1
SET @i = @i + 1
END
GO
--------------------------------------------
-- Ajoput des alertes sur problème hardware
--------------------------------------------
DECLARE @i smallint = 823
, @alert_name sysname
WHILE @i <= 825
BEGIN
SELECT @alert_name = 'Message ' + CAST(@i AS char(3))
EXEC msdb.dbo.sp_add_alert
@name = @alert_name
, @message_id = @i
, @severity = 0
, @enabled = 1
, @delay_between_responses = 0
, @include_event_description_in = 1
, @job_id = N'00000000-0000-0000-0000-000000000000'
EXEC msdb.dbo.sp_add_notification
@alert_name = @alert_name
, @operator_name = @operator_name
, @notification_method = 1
SET @i = @i + 1
END |
Partager