Files
sql-scripts/TPDT-268 - ACP in task sequence/dba_storedProcedures/sp_audit_login.sql
2024-03-07 16:52:14 +01:00

145 lines
4.3 KiB
Transact-SQL

USE [HCITools]
GO
IF EXISTS (SELECT * FROM sys.objects o JOIN sys.schemas s ON o.schema_id = s.schema_id WHERE o.name = 'sp_audit_login' AND OBJECTPROPERTY(object_id,N'IsProcedure') = 1 AND s.name = 'dba')
DROP PROCEDURE [dba].[sp_audit_login]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dba].[sp_audit_login]
@in_ApplicationName NVARCHAR(255),
@in_client_process_id INT,
@in_host_name NVARCHAR(255),
@in_login_name NVARCHAR(255),
@in_NT_domain_name NVARCHAR(255),
@in_NT_user_name NVARCHAR(255),
@in_session_login_name NVARCHAR(255),
@in_spid INT,
@in_event_time NVARCHAR(255),
@in_audit_login_text TINYINT,
@in_text_data NVARCHAR(MAX)
AS
/*=============================================================================
Explication du traitement realise par la SP
-------------------------------------------
Cette SP permets de faire le lien entre l'alerte WMI [Audit login] et la table de logs [HCITools].[dba].[Audit_login].
Tous les événements de connexions comportant des problèmes sont enregistrés.
Parametres
----------
Creation : 27.02.2019 / SPE
Modifications :
=============================================================================*/
SET NOCOUNT ON;
/*------------------- Declaration des variables --------------------*/
DECLARE @errno int,
@errmsg varchar(255)
/*-------------------------- Vérifications des variables d'entrée ---------------------------*/
IF @in_ApplicationName is null
BEGIN
SELECT @errno = 70003,
@errmsg = 'You must provide the parameter @in_ApplicationName!'
goto error_99
END
IF @in_client_process_id is null
BEGIN
SELECT @errno = 70003,
@errmsg = 'You must provide the parameter @in_client_process_id!'
goto error_99
END
IF @in_host_name is null
BEGIN
SELECT @errno = 70003,
@errmsg = 'You must provide the parameter @in_host_name!'
goto error_99
END
IF @in_spid is null
BEGIN
SELECT @errno = 70003,
@errmsg = 'You must provide the parameter @in_spid!'
goto error_99
END
IF @in_event_time is null
BEGIN
SELECT @errno = 70003,
@errmsg = 'You must provide the parameter @in_event_time!'
goto error_99
END
/*------------ Affectation des parametres aux variables ------------*/
SET @in_event_time = substring(@in_event_time,0,5) + '-' + substring(@in_event_time,5,2) + '-' + substring(@in_event_time,7,2) + ' ' + substring(@in_event_time,9,2) + ':' + substring(@in_event_time,11,2) + ':' + substring(@in_event_time,13,2)
/*-------------------------- Traitement ---------------------------*/
BEGIN TRY
/* Check if ID is missing on text table */
IF NOT EXISTS(SELECT 1 FROM dba.Audit_login_text WHERE Audit_login_text_ID = @in_audit_login_text)
BEGIN
INSERT INTO dba.Audit_login_text(Audit_login_text_ID,ALT_text,ALT_short_text,ALT_level)
VALUES (@in_audit_login_text,'Unknown','Unknown',2)
END
INSERT INTO [dba].[audit_login] (
[AL_application_name],
[AL_client_process_id],
[AL_host_name],
[AL_login_name],
[AL_NT_domain_name],
[AL_NT_user_name],
[AL_session_login_name],
[AL_spid],
[AL_event_time],
[AL_audit_login_text_id],
[AL_text_data])
VALUES (@in_ApplicationName,
@in_client_process_id,
@in_host_name,
@in_login_name,
@in_NT_domain_name,
@in_NT_user_name,
@in_session_login_name,
@in_spid,
@in_event_time,
@in_audit_login_text,
@in_text_data)
END TRY
BEGIN CATCH
SELECT @errno = 70003,
@errmsg = 'Audit login INSERT error!'
goto error_99
END CATCH;
/*------------------ Retour au programme appelant -----------------*/
RETURN(@@error);
/*---------------------- Traitement des erreurs ----------------------*/
error_99:
RAISERROR (@errmsg, 16, 1);
RETURN(@errno);
GO