Thursday 28 February 2013

Uneven TempDB

With more than a singe TempDB data file it is critical to ensure that all data files stay in the same proportion in order that proportional fill algorithms can operate.

To check for uneven tempdb run the following :
       
;with tab as (  -- identify the tempdb database files that are online and have uneven file sizes 
    select  d.database_id 
    from  sys.master_files mf
    inner join sys.databases d on d.database_id  = mf.database_id 
    where  d.name = 'tempdb'
    and   mf.type_desc = 'ROWS'
    and   d.state = 0    -- ONLINE
    group by d.database_id 
    having  count(mf.file_id) > 1 -- no point checking if only one file (could be a design decision, assume designed)=
    and   avg(mf.size) != min(mf.size)
   )
   select  mf.name
   ,   mf.physical_name
   ,   mf.size*8/1024
   from  tab t
   inner join sys.master_files mf on mf.database_id = t.database_id
   where  mf.type_desc = 'ROWS'
       
 
We need to check all of the datafiles (type_desc='ROWS') and a simple method of deciding if there is an issue is to compare the average file size logged in sys.master_files to the smallest file size - if they are not identical then at least on of the tempdb datafiles has gone out of step.

Fixing it can simply mean increasing all the smaller files to the same size as the largest or stopping the SQL service, deleting the tempdb files and restarting the SQL Service if a data file grew due to a one off event (tempdb is automatically created a start up).

Thursday 21 February 2013

IGNORE_DUP_KEY

I have come across an interesting feature when trying to set up index rebuild processes on SQL Server 2008R2. I am constructing an alter index rebuild statement including appropriate IGNORE_DUP_KEY and other settings and am receiving failures because of IGNORE_DUP_KEY.

It would appear that IGNORE_DUP_KEY=ON is not a valid option when rebuilding an index that is a primary key or has a unique constraint and yet I'm finding not problem finding examples where this is exactly what is configured, notably within Microsoft databases; I first picked this up in the DataCollectiondatabase.

I have managed to get around this because what I do is specifically set IGNORE_DUP_KEY=ON in the alter index rebuild statement but I do not specifically set the OFF value if it is not required and that means that when I rebuild the index without specifying whether to ignore the duplicate key or not the system leaves the system as was configured and I do not need to worry about breaking any system functionality.

Tuesday 19 February 2013

Tempdb


Useful article on tempdb and the numbers of files required : tempdb article. I tend to start with just two files and extend if experiencing performance problems simply because I've had a number of cases where tempdb activity is slower spread across lots of small files than running everything against fewer, large files.

Thursday 7 February 2013

Adventures with Powershell, finally some code

Time to show the code for the SQL Server service state monitoring, there is quite a lot but hopefully it should be easy to follow, if all else fails try it out :
       

 /*
Check State
Create tables 
Chris Page 2013
*/

use [DBAUtility] 
go
-- Table to store the windows server name & SQL instance name
IF OBJECT_ID ( 'SQLInstance', 'U' ) IS null begin
 CREATE TABLE [dbo].[SQLInstance](
  ID    int not null identity (1,1),
  [WindowsServer] varchar(128) not NULL,
  [SQLInstance] varchar(255) not NULL,          -- either same as server or server\instance
  Active   bit               -- 1 = active so checkm 0 = inactive don't check
  constraint PK_SQLInstance primary key clustered (Id) with (fillfactor=100, pad_index=off)
 )
 create unique index Idx_SQLInstance on SQLInstance (WindowsServer,SQLInstance) with (fillfactor=70, pad_index=on)
 
 -- Inserting Initial Data into SQLInstane
 insert into SQLInstance values ('S-UAT-SQL108.TOWERGATE.LOCAL','S-UAT-SQL108',1) -- Default Instance
 insert into SQLInstance values ('L-TPL-3058','L-TPL-3058',1)      -- "server", "server\instance"

end
if OBJECT_ID ('SQLServiceState','U') is null begin
 -- SQL Service Monitoring Output Table
 CREATE TABLE [dbo].[SQLServiceState](
  Id     int identity (1,1) not null,
  [WindowsServerID] int not null,
  [SQLService]  [varchar](128) not NULL,
  [UpdatedOn]   [smalldatetime] not NULL,
  [ServiceStatus]  [varchar](100) not NULL,
  [StartMode]   [varchar](100) not NULL,
  [ServiceAccount] [varchar](1000) NULL,          -- long to allow command to be stored in case of issues
  constraint FK_SQLServiceState_WindowsServer foreign key (WindowsServerID) references SQLInstance (Id)
 )
 alter table SQLServiceState add constraint [DF_SQLServiceState_UpdatedOn] default (getdate()) for UpdatedOn
 create unique index IX_SQLServiceState on SQLServiceState (ID) with (fillfactor=100, pad_index=off)
end
if OBJECT_ID('SQLServiceStateHistory','U') is null begin
 CREATE TABLE [dbo].[SQLServiceStateHistory](
  Id     int not null,            -- not an identity field as data coming from elsewhere
  [WindowsServerID] int not null,
  [SQLService]  [varchar](500) NULL,
  [UpdatedOn]   [smalldatetime] NULL,
  [ServiceStatus]  [varchar](100) NULL,
  [StartMode]   [varchar](100) NULL,
  [ServiceAccount] [varchar](1000) NULL,
  constraint FK_SQLServiceStateHistory_WindowsServer foreign key (WindowsServerID) references SQLInstance (Id)
 )
end

if OBJECT_ID('ProcessLog','U') is null begin           -- *** STANDARD SPEC!
 create table ProcessLog (
  Id     int    not null identity (1,1),     -- UID
  Package    varchar(200) not null,
  Process    varchar(100) not null,
  ActionTaken   varchar(255) not null,
  DateStamp   datetime  not null,   
  Records    int    not null
  constraint PK_ProcessLog primary key clustered (Id) with (fillfactor=90, pad_index=on)
 )

 create index IDX_ProcessLog_Process on ProcessLog (Process) with (fillfactor=60, pad_index=on)
 create index IDX_ProcessLog_DateStamp on ProcessLog (DateStamp) with (fillfactor=60, pad_index=on)
 alter table ProcessLog add constraint [DF_ProcessLog_Records] default (0) for Records
 alter table ProcessLog add constraint [DF_ProcessLog_DateStamp] default (getdate()) for DateStamp
end

if OBJECT_ID('vSQLService','V') is not null begin
 drop view dbo.vSQLService
end 
go
create view vSQLService as 
 select  w.WindowsServer
 ,   w.SQLInstance
 ,   s.SQLService
 ,   s.ServiceAccount
 ,   s.UpdatedOn
 ,   s.StartMode
 ,   s.ServiceStatus
 from  dbo.SQLServiceState s
 inner join dbo.SQLInstance w on w.ID = s.WindowsServerID
       
 
This is creating a table to log servers that need checking (SQLInstance), a table in which to store the latest service state information (SQLServiceState), a table to store all previous service state information (SQLServiceStateHistory) - volumes can be large quickly hence separating the tables, if you want everything up to and including now a union will work nicely and finally a logging table (ProcessLog) which follows a standard layout I have been using to record what happened, when and why. Lastly I have defined a view to put the principal information from SQLInstance and SQLServiceState together to make simple queries against the current data easier (ie I got fed up rekeying while testing). Next is the Check State script that does all the hard work :
       

use [DBAUtility] 
go
/*
Check State
Chris Page 2013
*/
IF OBJECT_ID ( 'usp_CheckStateOfSQLServers', 'P' ) IS NOT NULL 
    DROP PROCEDURE usp_CheckStateOfSQLServers
go
create procedure usp_CheckStateOfSQLServers 
as
begin
 set nocount on
 insert into ProcessLog (Package, Process,ActionTaken) values ('CheckStateOfServers','Process','Start')
 
 declare @chkCMDShell as sql_variant
 ,  @Server   varchar(128)
 ,  @SQLInstance varchar(128)
 ,  @SQLInstanceID int
 ,  @ERROR   bit              -- flag to stop onward processing
 ,  @ErrMsg   varchar(255)
 ,  @ErrState  int
 ,  @ErrSeverity int
 ,  @SQL   varchar(1000)
 
 set @ERROR = 0
 
 begin try
  select @chkCMDShell = value from sys.configurations where name = 'xp_cmdshell'
  if @chkCMDShell = 0
  begin
   EXEC sp_configure 'Show Advanced Options', 1;
   RECONFIGURE with override;
   EXEC sp_configure 'xp_cmdshell', 1
   RECONFIGURE with override;
   EXEC sp_configure 'Show Advanced Options', 0;
   RECONFIGURE with override;
   insert into ProcessLog (Package, Process, ActionTaken) values ('CheckStateOfServers','xp_cmdshell','Allowed')
  end
 end try
 begin catch
  select @ERROR = 1, @ErrMsg = 'Could not enable XP_CMDSHELL '+ERROR_MESSAGE(),@ErrState = ERROR_STATE(),@ErrSeverity = ERROR_SEVERITY()
  raiserror (@ErrMsg,@ErrState, @ErrSeverity)
  insert into ProcessLog (Package, Process, ActionTaken) values ('CheckStateOfServers','xp_cmdshell','Could not be Allowed')
 end catch

 if @ERROR = 0 begin
  begin try                   -- start by archiving results from the last run
   insert into dbo.SQLServiceStateHistory           -- move old entries to history
    select *
    from SQLServiceState
   
   truncate table dbo.SQLServiceState            -- bin the current data having moved to history
  end try
  begin catch
   select @ERROR = 1, @ErrMsg = 'Could not move old ServiceStates to history '+ERROR_MESSAGE(),@ErrState = ERROR_STATE(),@ErrSeverity = ERROR_SEVERITY()
   raiserror (@ErrMsg,@ErrState, @ErrSeverity)
   insert into ProcessLog (Package, Process, ActionTaken) values ('CheckStateOfServers','ServerStateHistory','Could not moved.')
  end catch
 end
 
 if @ERROR = 0 begin
  create table #output (line varchar(max) null)
  
  declare ServerCursor cursor fast_forward for 
   select distinct LTRIM(rtrim(s.WindowsServer))
   ,  LTRIM(RTRIM(s.SQLInstance))
   ,  s.ID
   from dbo.SQLInstance s
   where s.Active = 1
  
  open ServerCursor
  fetch next from ServerCursor into @Server, @SQLInstance, @SQLInstanceID
  while @@FETCH_STATUS = 0 and @ERROR = 0 begin   
   set @sql = 'powershell.exe -c "Get-WmiObject  -ComputerName ' + QUOTENAME(@Server,'''') + ' -Class win32_service |  where {$_.name -like ' + QUOTENAME('*SQL*','''') + '} | select-object  Name,state,systemname,startmode,startname  | foreach{$_.name+''|''+$_.state+''%''+$_.systemname+''*''+$_.startmode+''@''+$_.startname+''!''}"'

   begin try
    insert #output EXEC xp_cmdshell @sql
   end try
   begin catch
     select @ERROR = 1, @ErrMsg = 'xp_cmdshell execution failed '+ERROR_MESSAGE(),@ErrState = ERROR_STATE(),@ErrSeverity = ERROR_SEVERITY()
     raiserror (@ErrMsg,@ErrState, @ErrSeverity)
     insert into ProcessLog (Package, Process, ActionTaken) values ('CheckStateOfServers','xp_cmdshell','Execution failed')
   end catch 

   if @ERROR = 0 begin   
    if (select COUNT(*) from #output o where o.line like 'Get-WmiObject : The RPC server is unavailable%') = 1 begin
     insert into SQLServiceState (SQLService, ServiceStatus, ServiceAccount, StartMode, WindowsServerID) values
      ('All','Uncontactable','Command tried : '+@SQL, 'Uncontactable', @SQLInstanceID)
    end else if (select COUNT(*) from #output o where o.line like 'Get-WmiObject : Access is denied%') = 1 begin
     insert into SQLServiceState (SQLService, ServiceStatus, ServiceAccount, StartMode, WindowsServerID) values
      ('All','Access Denied','Command tried : '+@SQL, 'Access Denied', @SQLInstanceID)
    end else begin
    
     delete from #output  where len(line) < 30 -- Deleting the rows which contains error or has not sufficient data
     update #output set line = line + '!' where line not like '%!%'
     IF (SELECT COUNT(*) FROM #output where line like '%Get-Wmi%') = 0 begin
      insert into SQLServiceState(SQLService,ServiceStatus,StartMode,ServiceAccount, WindowsServerID)
       select  rtrim(ltrim(SUBSTRING(o.line,1,CHARINDEX('|',o.line) -1))) as SQLServiceName 
       ,  (rtrim(ltrim(SUBSTRING(o.line,CHARINDEX('|',o.line)+1, (CHARINDEX('%',o.line) -1)-CHARINDEX('|',o.line)) ))) as ServiceStatus
       --,(rtrim(ltrim(SUBSTRING(line,CHARINDEX('%',line)+1, (CHARINDEX('*',line) -1)-CHARINDEX('%',line)) ))) as WindowsServerName 
       ,  (rtrim(ltrim(SUBSTRING(o.line,CHARINDEX('*',o.line)+1, (CHARINDEX('@',o.line) -1)-CHARINDEX('*',o.line)) ))) as startmode
       ,  (rtrim(ltrim(SUBSTRING(o.line,CHARINDEX('@',o.line)+1, (CHARINDEX('!',o.line) -1)-CHARINDEX('@',o.line)) ))) as startname
       ,  @SQLInstanceID
       from #output o
       where o.line is not null 
       and  LEN(o.line) > 30
     end else begin -- Unknown error
      insert into SQLServiceState (SQLService, ServiceStatus, ServiceAccount, StartMode, WindowsServerID) values
      ('All','Unknown Error','Command tried : '+@SQL, 'Unknown Error', @SQLInstanceID)    
     end
    end
   end
   truncate table #output
   
   fetch next from ServerCursor into @Server, @SQLInstance,  @SQLInstanceID
  end -- WEND
  close ServerCursor
  deallocate ServerCursor
  drop table #output  
 end

 if @chkCMDShell = 0                    -- turn off xp_cmdshell again if we turned it on during execution
 begin try
  EXEC sp_configure 'Show Advanced Options', 1;
  RECONFIGURE;
  EXEC sp_configure 'xp_cmdshell', 0
  RECONFIGURE;
  EXEC sp_configure 'Show Advanced Options', 0;
  RECONFIGURE;
  insert into ProcessLog (Package, Process, ActionTaken) values ('CheckStateOfServers','xp_cmdshell','DisAllowed')
 end try
 begin catch
  set @ERROR = 1
  select @ERROR = 1, @ErrMsg = 'Could not disable XP_CMDSHELL '+ERROR_MESSAGE(),@ErrState = ERROR_STATE(),@ErrSeverity = ERROR_SEVERITY()
  raiserror (@ErrMsg,@ErrState, @ErrSeverity)
  insert into ProcessLog (Package, Process, ActionTaken) values ('CheckStateOfServers','xp_cmdshell','Could not be disabled.')
 end catch
 
 insert into ProcessLog (Package, Process, ActionTaken) values ('CheckStateOfServers','Process','End')
  
end

       
 
this seems like a lot of code but a lot of it is error checking and reporting rather than getting statuses. Note right at the beginning the code turns on xp_cmdshell if it is turned off, if it does turn on xp_cmdshell it does turn it off again at the end to close that security hole again - the implication here is that this task does need to run at sufficiently high security levels to be able to do take this action. My assumption is that the process will be running at the SQL Server Agent level and will probably have the necessary rights anyway - we are after all looking at service statuses and that is a privileged action.

Following turning on xp_cmdshell the powershell command is run once for each server & instance Pfound in the SQLInstance table. Yes I have used a cursor to provide my loop through the SQLInstance table, it is set as fast_forward so works quickly, you could change this to a while loop or something else if you want but the coding is less and tidier with a cursor and for something that is not OLTP I don't see problems on my systems.

The Powershell command is actually pretty simple if you've done any command line scripting (I'll jump back to my Unix comment from a previous post here, it's remarkably Unix like) :
       
powershell.exe -c "Get-WmiObject  -ComputerName ' + QUOTENAME(@Server,'''') + ' -Class win32_service |  where {$_.name -like ' + QUOTENAME('*SQL*','''') + '} | select-object  Name,state,systemname,startmode,startname  | foreach{$_.name+''|''+$_.state+''%''+$_.systemname+''*''+$_.startmode+''@''+$_.startname+''!''}"'
 
 
This can be run independently of the SQL scripts and simply runs Powershell and calls "get-wmiobject" for the given server and lists out the service status names for any service that is SQL related. This is the only problematic hardcoded part (yes could code around it, necessary? well we'll find out in a future version of SQL server I imagine) because it is looking for any services with SQL in the registered service name - so it might also generate false positives on a box with non SQL server SQL services on there (MySQl etc) - I am not able to test that currently but that should be easy to tweak and potentially is not such a bad thing. to help other people encountering Powershell for the first time, there is another command, "get-service" which seems much simpler and tidier than get-wmiobject, however, it cannot run against a remote server.

The output from the Powershell command is saved into a table and the useful parts processed to find the detail we actually need - both the Powershell command and processing of output are unmodified from Jugal's version as they work perfectly.

I handle as many errors as I have encountered, including inability to turn on or use xp_cmdshell, RPC service unavailable, server uncontactable, access denied and a final "unknown" category just in case something new comes up.

Okay, we've got some output in a table, now what? Well I guess someone needs to know about the results so the following script provides two routes, either direct to the console via Management Studio or via an HTML formatted email (designed to handle Outlook as used in my environment but the HTML is entirely standard so should be fine in any client that accepts HTML) :
       

use [DBAUtility] 

go
/*
Check State
Report State
Chris Page 2013
*/
IF OBJECT_ID ( 'usp_ReportStateOfSQLServers', 'P' ) IS NOT NULL 

    DROP PROCEDURE usp_ReportStateOfSQLServers

go

create procedure usp_ReportStateOfSQLServers (

 -- parameters could be 

 @ShowStopped  bit = 1, -- show services not set as running (as opposed to show stopped in case other values are available but not known)

 @ShowUncontactable bit = 1, -- show uncontactable servers (ignores @AutomaticOnly)

 @ShowDenied   bit = 1, -- show servers to whom access was denied

 @ShowUnknown  bit = 1, -- show servers with unknown errors

 @AutomaticOnly  bit = 1, -- show automatic start services (inc delayed) only

 @Destination  varchar(255) = 'chris.page@towergate.co.uk' -- if 'local' then print locally

)

as

begin

 set nocount on

 

 declare @OutputStopped   varchar(max)

 ,  @OutputUncontactable varchar(max)   -- includes access denied

 ,  @OutputAll    varchar(max)

 ,  @WindowsServer   varchar(128)

 ,  @SQLInstance   varchar(255)

 ,  @SQLService    varchar(128)

 ,  @ServiceAccount   varchar(1000)

 ,  @UpdatedOn    smalldatetime

 ,  @StartMode    varchar(100)

 ,  @ServiceStatus   varchar(100)

 

 select  s.WindowsServer

 ,   s.SQLInstance

 ,   s.SQLService

 ,   s.ServiceAccount

 ,   s.UpdatedOn

 ,   case 

     when s.ServiceStatus = 'Access Denied' then 'Check agent service account has rights on remote' 

     when s.ServiceStatus = 'Uncontactable' then 'Check server is up'

     else '' 

    end as StartMode

 ,   s.ServiceStatus

 into  #TempStateList -- store in temp table so local & email destination done in one query & no dynaminc SQL

 from  vSQLService s

 where  ( (@AutomaticOnly = 0 or (@AutomaticOnly=1 and s.StartMode like 'Auto%'))

     and 

     s.ServiceStatus != 'Running'

     and

     @ShowStopped = 1

    )

    or

    (@ShowUncontactable = 0 or (s.ServiceStatus = 'Uncontactable' and @ShowUncontactable=1))

    or

    (@ShowDenied = 0 or (s.ServiceStatus = 'Access Denied' and @ShowDenied=1))

    or

    (@ShowUnknown = 0 or (s.ServiceStatus = 'Unknown Error' and @ShowDenied=0))

 order by s.WindowsServer

 ,   s.SQLInstance

 ,   s.SQLService



 if lower(@Destination)='local' begin

 

  select 'Unreachable Servers'

  select * from #TempStateList t where t.ServiceStatus in ('Uncontactable','Unknown Error' ,'Access Denied')

  select 'Stopped Services'

  select * from #TempStateList t where t.ServiceStatus not in ('Running','Unknown Error','Uncontactable','Access Denied')

 end else begin

 

  declare ReportableServicesCursor cursor fast_forward for

   select * from #TempStateList

   

  set @OutputUncontactable = ''

  set @OutputStopped = ''

  open ReportableServicesCursor

  fetch next from ReportableServicesCursor into @WindowsServer, @SQLInstance, @SQLService, @ServiceAccount, @UpdatedOn, @StartMode, @ServiceStatus

  while @@FETCH_STATUS = 0 begin

   --select @WindowsServer,@SQLInstance,@SQLService,@ServiceAccount,@UpdatedOn,@StartMode,@ServiceStatus

   select @OutputUncontactable=@OutputUncontactable+'<tr>'+

       '<td>'+@WindowsServer+'</td>'+

       '<td>'+@SQLInstance+'</td>'+

       '<td>'+@SQLService+'</td>'+

       '<td>'+cast(@UpdatedOn as varchar)+'</td>'+

       '<td><font color="red">'+@ServiceStatus+'</font></td>'+

       '<td>'+@Startmode+'</td>'+

       '<td>'+@ServiceAccount+'</td>'+

       '</TR>'

   where  @ServiceStatus in ('Uncontactable','Unknown Error' ,'Access Denied')

  

   select @OutputStopped =@OutputStopped+'<tr>'+

       '<td>'+@WindowsServer+'</td>'+

       '<td>'+@SQLInstance+'</td>'+

       '<td>'+@SQLService+'</td>'+

       '<td>'+cast(@UpdatedOn as varchar)+'</td>'+

       '<td>'+@StartMode+'</td>'+

       '<td><font color="red">'+@ServiceStatus+'</font></td>'+

       '<td>'+@ServiceAccount+'</td>'+

       '</TR>'

   where @ServiceStatus  not in ('Running','Unknown Error','Uncontactable','Access Denied')

  

   fetch next from ReportableServicesCursor into @WindowsServer, @SQLInstance, @SQLService, @ServiceAccount, @UpdatedOn, @StartMode, @ServiceStatus

  end 

  close ReportableServicesCursor

  deallocate ReportableServicesCursor

 

  if @OutputUncontactable!='' or @OutputStopped !='' begin -- only output results if there are some

   set @OutputAll = '<body>'

   if @OutputUncontactable != '' begin

    set @OutputAll = @OutputAll+'The following SQL Server instances are unavailable : <br/><br/>'+

        '<table border=1><tr><td><b>Windows Server</b></td><td><b>SQL Instance</b></td><td><b>SQL Service</b></td><td><b>Updated On</b></td>'+

        '<td><b>Service Status</b></td><td><b>Notes</b></td><td><b>Script Used</b></td></tr>'+

        @OutputUncontactable+'</table>'

   end 

   if @OutputStopped != '' begin

    if @OutputAll != '' begin

     set @OutputAll = @OutputAll + '<br/><br/><br/>'

    end

    set @OutputAll = @OutputAll+

     'The following SQL Server services are stopped :<br/><br/>'+

     '<table  border=1><tr><td><b>Windows Server</b></td><td><b>SQL Instance</b></td><td><b>SQL Service</b></td><td><b>Updated On</b></td>'+

        '<td><b>Start Mode</b></td><td><b>Service Status</b></td><td><b>Service Account</b></td></tr>'+

        @OutputStopped+'</table>'

   end

   set @OutputAll = @OutputAll + '</body>'

   begin try

    exec msdb.dbo.sp_send_dbmail @recipients=@Destination, @Importance='High'

    , @subject='SQL Server Service Review', @Body=@OutputAll, @Body_Format='HTML'

   end try

   begin catch -- unable to send mail, might be a config issue (firewall for smtp) or send mail not configured / enabled / no default public profile

    raiserror ('Unable to send emails while service statuses.',16,1) with log

   end catch

  end

 end

end

 
(worth noting here that I used SimpleCode to embed the script & retain the HTML text - it pasted across to my machine and ran perfectly).

This code is a bit of a fudge because having written a means of emailling the results to myself I got fed up waiting for the emails to arrive while testing so needed an output to screen and it made sense to provide the same output. Hence, the code writes the desired output to a temporary table and then processs that differently according to which the chosen destination is.

This function is called as either :
usp_ReportStateOfSQLServers @Destination = 'local' 
-- prints direct to screen
usp_ReportStateOfSQLServers @Destination = 'email@address' 
-- send to email

You will note some additional parameters that vary the "sensitivity" of what is reported - the default settings will tell you about any SQL services set to automatic start (including delayed start) which are currently stopped and include uncontactable / access denied and unknown errors. For my environment all SQL services that are used normally on a server are set to automatic start and we expect all machines to be visible all the time so this gives me that answers I need. - I deliberately leave my laptop  on the list of servers so when I am working remotely I'll get any email that my laptop is uncontactable and that just keeps me aware that the process is alive because it does not send an email if there is nothing to tell me (so if everything is working you won't see an email - be aware, some people like to get a positive response every time, easy enough to change).

You might have noted in the Check State code that records currently in the SQLServiceState are firstly moved to the SQLServiceStateHistory table before current statuses are checked so now we need to manage how large the history table can get :

       
use [DBAUtility] 
go
/*
Check State
Manage State History
Chris Page 2013
*/
IF OBJECT_ID ( 'usp_ManageSQLServerStateHistory', 'P' ) IS NOT NULL 
    DROP PROCEDURE usp_ManageSQLServerStateHistory
go

create procedure usp_ManageSQLServerStateHistory (
 -- parameters could be 
 @KeepAllHistoryForDays   smallint = 7, -- delete anything "normal" after this length of time in days
 @KeepIssueHistoryForDays  smallint = 90-- Keep stopped / uncontactable items for this long (longer than KeepAll)
)
as
begin
 set nocount on
 insert into ProcessLog (Package, Process, ActionTaken) values ('ManageSQLServerStateHistory','Process','Start')
 
 declare @ErrMsg   varchar(255)
 ,  @ErrState  int
 ,  @ErrSeverity int

 if @KeepAllHistoryForDays > @KeepIssueHistoryForDays begin
  insert into ProcessLog (Package, Process, ActionTaken) values ('ManageSQLServerStateHistory','Check periods','Keep All longer than Keep Issues, Keep Issues aligned')
  set @KeepIssueHistoryForDays = @KeepAllHistoryForDays
 end

 begin try
  delete from SQLServiceStateHistory           -- start by deleting any non issues for the shorter period
  where UpdatedOn < DATEadd (DD,@KeepAllHistoryForDAys*-1,GETDATE()) 
  and  ServiceStatus  in ('Running')
 
  delete from SQLServiceStateHistory           -- then delete anything over the longer period
  where UpdatedOn < DATEADD (dd, @KeepIssueHistoryForDays*-1, getdate())
 end try
 begin catch
  select @ErrMsg = 'Could not delete history SQL Server State '+ERROR_MESSAGE(),@ErrState = ERROR_STATE(),@ErrSeverity = ERROR_SEVERITY()
  raiserror (@ErrMsg,@ErrState, @ErrSeverity)
  insert into ProcessLog (Package, Process, ActionTaken) values ('ManageSQLServerStateHistory','Delete History','Failed.')
 end catch

 insert into ProcessLog (Package, Process, ActionTaken) values ('ManageSQLServerStateHistory','Process','End')
end
       
 
This time there are two parameters :

  1. @KeepAllHistoryForDays -- deletes all "running" service records that exist in the database, generally speaking I am not really interested in services that are running as expected so I will only want to keep that data for a short period so I have a default value of 1 week.
  2. @KeepIssueHistoryForDays -- this deletes any records that still exist after this many days, I am working on the principal that maintaining a recent service problem history is useful and can be used in discussion with service providers as evidence to support reliability discussions (yes I am taking a negative view but if the history is empty then all is well!), hence, I expect this data to be kept for longer and have default to 90 days but could potentially be significantly longer depending on local requirements.
Next comes scheduling, this is simply creating a scheduled task on your designated monitoring server, you will need to alter the Operator name to ensure you get the failure email and the email destination listed on the scheduled task :
       
USE [msdb]
GO

/*
Check State
Schedule Checking
Chris Page 2013
*/
/****** Object:  Job [DBA Maintenance Tasks : Check & Report SQL Server Service States]    Script Date: 02/04/2013 13:39:23 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object:  JobCategory [[Uncategorized (Local)]]]    Script Date: 02/04/2013 13:39:23 ******/
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 @jobId BINARY(16)
EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'DBA Maintenance Tasks : Check & Report SQL Server Service States', 
  @enabled=1, 
  @notify_level_eventlog=0, 
  @notify_level_email=2, 
  @notify_level_netsend=0, 
  @notify_level_page=0, 
  @delete_level=0, 
  @description=N'No description available.', 
  @category_name=N'[Uncategorized (Local)]', 
  @owner_login_name=N'chris.page', 
  @notify_email_operator_name=N'SQLAdministration', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [Check]    Script Date: 02/04/2013 13:39:23 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Check', 
  @step_id=1, 
  @cmdexec_success_code=0, 
  @on_success_action=3, 
  @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'usp_checkstateofsqlservers', 
  @database_name=N'DBAUtility', 
  @flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [Report]    Script Date: 02/04/2013 13:39:23 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Report', 
  @step_id=2, 
  @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'usp_ReportStateOfSQLServers @Destination=''email@address''', 
  @database_name=N'DBAUtility', 
  @flags=0
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'DBA Maintenance Tasks : Daily', 
  @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=20120914, 
  @active_end_date=99991231, 
  @active_start_time=90000, 
  @active_end_time=170000
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       
 
Last but not least is logging deployment, one day I will write something clever to keep all deployment logs on SQL Server and be able to output a list of everything that needs to be updated (automating updated might be a step to far in case there is a reason machines are out of step). This is just a screenshot :
All I've done is create a spreadsheet with conditional formatting on column B such that if the date in column B for any server (so B5 onwards) is before the date in B3 is shows the column as red. Not entirely scientific but what I've also noted in red text is that the version notes need to be reviewed before deploying to every server in the estate as potentially the update is minor can wait.

Enjoy.

Wednesday 6 February 2013

Adventures with Powershell, turning it into BAU

Having tamed the security beast for the time being I have now been able to get my version of server monitoring tool in place and running reliably for a few days.

The biggest issue I have encountered though is that our service provider has supplied us with such an array of down servers that I haven't had a chance to roll out the process to any more machines.

I now have a process that breaks into multiple parts (with some documentation for whoever comes after me) broken into several "creation" files to keep functionality clear and simple having chosen a target SQL server (tested on 2008R2 as the host and 2005, 2008R2 targets (Standard, Enterprise & Express editions) :
  1. Tables Script - creates all the tables, indexes and a view to reduce my typing when looking at results. I have included in the process a "ProcessLog" table, I have used an identical design as I have in place on a live application on another box and one day I might well merge the two together (which has prompted me to create a to do list).
  2. Check State process creation - creates the requisite stored procedure on the host machine.
  3. Report State process - creates separate functionality for reporting the state retrieved from the check state process, I separated the two because originally this was configured to email results (HTML formatted) but in testing this got annoying when I had slow / non-functioning email so I added a parameter to present output locally; it was tidier to separate checking a state from reporting on states so allowing me to run the check process once and check the results repeatedly without waiting - handy for multiple users.
  4. Manage history - each time the state checking occurs it moves all the current data to a history table - that way the live data is uncluttered whilst we keep a record. The maintenance process takes two parameters, firstly how long to keep any data and secondly how long to keep "issues" - that way I can, for instance, say I'm only interested in a complete log of service state information for 7 days but I'd like to keep servers which are not reported as active for 3 months. Handy for pointing at in discussions with service providers.
  5. Scheduling process. Via my 1-click install process I generate two "DBA Maintenance Tasks" - one called "Daily" and one "Weekly" (I suspect you know how often they run). But this checking should ultimately run more frequently (that will depend on your enviroment & execution time, criticality / SLAs and how on the ball the server boys are), I am thinking ultimately once a hour for normal use (and I have added another to do item, logging checking periods).
  6. A deployment record. Currently this is a simple Excel sheet that shows the date of the last code update (manually entered) and a list of servers with deployment dates (all manual) with  conditional formatting to highlight out of date deployments (another to do item is to make this a tidier SQL process).
I also have a 1-click install process (I'll write about this one day) that deploys all of my maintenance routines to a specified machine. What I've done to that is add "monitoring server" to the list of variables configured (yes you'll need something reliable) and updated the weekly maintenance routine to add a step for the history management action if the 1-click install is running on the monitoring server (the 1-click install is also coded for redeployment of updated code). I also automatically add a new server to the monitoring list assuming a default checking frequency.

Next I'll go through the code that does the work.


Saturday 2 February 2013

Adventures with Powershell, knocking down the security barrier....

I have now had my routine for checking service status running for a few days but am still discovering new circumstances which need consideration. I have accounted for the obvious "server uncontactable" circumstance which is part of the point of the process but I have since altered my code further to ensure there is a catch all for circumstances I have yet to identify.

One additional circumstance has raised its ugly head above the parapet. On testing one server was not  responding via the script. Running the Powershell script directly against the server resulted in a perfect run from several different machines. But running the same script via xp_cmdshell caused a failure.

I finally had to raise the query on MSDN and promptly got the answer I needed. What I was unaware of is that inspite of running interactively via SQL Server Management Studio, xp_cmdshell runs with the rights of the SQL Server Agent account, which in this instance had not got rights to the destination machine.

Simple fix, I would imagine for production use a dedicated proxy account should be put in place on all monitored boxes to ensure limited rights but we're in action again.