server sql 作業 使用專題及常見問題 - CSDN

2020-12-25 CSDN技術社區


問題 (Problem)

One of the things that a DBA needs to do is to monitor the status of SQL ServerAgent Jobs. This can be done instance by instance, but this takes time.  Inthis article we look at how to collect the last run information for all of the SQLServer instances that are being monitored with this solution.

DBA需要做的一件事是監視SQL Server代理作業的狀態。 可以逐個實例完成此操作,但這需要時間。 在本文中,我們研究如何為使用此解決方案監視的所有SQL Server實例收集上次運行信息。

(Solution)

This module will collect the information of the very last execution status ofeach SQL Server Agent Job for all SQL Server instances within the inventory.MasterServerListtable.

此模塊將為清單.MasterServerList表中的所有SQL Server實例收集每個SQL Server代理作業的最後執行狀態的信息。

先決條件 (Prerequisites)

In order to use this module, you need to createthe core objects found here and also setupthis module to collect SQL Agent job information.

為了使用此模塊,您需要創建在此處找到的核心對象 ,還需要設置此模塊以收集SQL Agent作業信息 。

資料庫對象 (Database Objects)

One new table will be created and it is a table to store the information of thelast execution of the SQL Server Agent Jobs from each instance.

將創建一個新表,該表將存儲每個實例中最後一次執行SQL Server代理作業的信息。

This is the structure of the table, so you can get an idea of what data willbe stored.

這是表的結構,因此您可以了解將存儲哪些數據。

Note: If you want to add more fields to this table, make sure to adjust the structurewithin the PowerShell script and adapt the respective logic that will handle theadditional fields.

注意:如果要向該表添加更多欄位,請確保調整PowerShell腳本中的結構並調整將處理其他欄位的相應邏輯。

桌子 (Tables)

  • monitoring.Jobs

    監視工作

    • serverId - serverid ties back to inventory.MasterServerList
    • serverId -serverid 綁定到清單。MasterServerList
    • job_name - name of the SQL Agent jobjob_name -SQL代理作業的名稱
    • last_run_date_time - last run date and timelast_run_date_time-上次運行的日期和時間
    • last_run_duration - length of the runlast_run_duration-運行時間
    • last_run_status - status of the last runlast_run_status-上次運行的狀態
    • last_run_status_message - any messages from the runlast_run_status_message-運行中的任何消息
    • next_run_date_time - next date and time the job willrunnext_run_date_time-作業將運行的下一個日期和時間
    • data_collection_timestamp - when data was last collected.data_collection_timestamp-上次收集數據的時間。

PowerShell腳本 (PowerShell Script)

The PowerShell script that creates the above object and inserts data into themonitoring.Jobs table is called:

創建上述對象並將數據插入到監視中的PowerShell腳本稱為Jobs表:

The script has some validations that will help you check if some key elementsare missing for the script to run successfully. For instance, it will confirm thatthe inventory.MasterServerList table exists and that it has at least 1 active instanceregistered to be able to have something to work with, and it will also check thatthe inventory.Jobs table exists within the central repository database.

該腳本具有一些驗證,可以幫助您檢查是否缺少某些關鍵元素才能使腳本成功運行。 例如,它將確認stocking.MasterServerList表存在並且至少註冊了一個活動實例以便可以使用某些東西,並且還將檢查中央存儲庫資料庫中是否存在stock.Jobs表。 。

If you have followed along the other modules, you will notice that we have beenstoring all objects in "C:\temp", but you can use any folder you want.If you make a change to the central folder location, you will need to edit the firstline in the following PowerShell script to specify the new folder location.

如果您遵循了其他模塊,則會注意到我們已經將所有對象存儲在「 C:\ temp」中,但是您可以使用所需的任何文件夾。 如果更改中央文件夾位置,則需要在以下PowerShell腳本中編輯第一行以指定新文件夾位置。

如何使用 (How to Use)

Navigate to the folder where you created the files and you can run the PowerShellscript as follows:

導航到創建文件的文件夾,然後可以運行PowerShell腳本,如下所示:

選項1 (Option 1)

選項2 (Option 2)

  • Open a command window and navigate to the folder where you saved the abovefiles and run

    打開命令窗口,然後導航到保存上述文件的文件夾並運行

powershell "C:\temp\Monitor-MSSQL-Instance-Jobs.ps1"

選項3 (Option 3)

  • Schedule this as a SQL Server Agent Job to run the PowerShell script on apredefined basis.

    將其安排為SQL Server代理作業,以在預定義的基礎上運行PowerShell腳本。

選項4 (Option 4)

  • Schedule this as a Windows Task Scheduler job to run the PowerShell scripton a predefined basis.

    將其調度為Windows Task Scheduler作業,以在預定義的基礎上運行PowerShell腳本。

檢查資料庫和對象的創建 (Check Creation of Database and Objects)

After running the PowerShell script, we can see the objects that are created.

運行PowerShell腳本後,我們可以看到創建的對象。

If we query both the inventory.Jobs table and monitoring.Jobs table, we can seethe data that has been collected.

如果我們同時查詢庫存。作業表和監視。作業表,則可以看到已收集的數據。

Note: The PowerShell script will store only the informationfrom the last execution, no historical data is retained. If you』d like to keep information from previousexecutions, you would have to modify the script and adapt it to your particularuse case.

注意: PowerShell腳本將僅存儲上一次執行的信息,不保留任何歷史數據。 如果您想保留以前執行的信息,則必須修改腳本並將其調整為適合您的特定用例。

檢查錯誤 (Checking for Errors)

To check for errors query the monitoring.ErrorLog table using the following query:

要檢查錯誤,請使用以下查詢查詢Monitoring.ErrorLog表:

SELECT *FROM monitoring.ErrorLogWHERE script = Monitor-MSSQL-Instance-Jobs'

If you』d like to know the SQL Server instance that got the errors, youwould have to issue the query like this:

如果您想知道出現錯誤SQL Server實例,則必須發出如下查詢:

SELECT CASE WHEN msl.instance = 'MSSQLSERVER' THEN msl.server_name ELSE CONCAT(msl.server_name,'\',msl.instance) END AS instance, e.script, e.message, e.error_timestampFROM monitoring.ErrorLog eJOIN inventory.MasterServerList msl ON msl.serverId = e.serverIdWHERE e.script = Monitor-MSSQL-Instance-Jobs'

有用的查詢 (Useful Queries)

By collecting all the data related to the execution of jobs across all your instances,you might answer things like:

通過收集所有實例中與作業執行相關的所有數據,您可能會回答以下問題:

哪些作業上次執行失敗? (Which jobs failed the last time that they were executed?)

SELECT CASE WHEN msl.instance = 'MSSQLSERVER' THEN msl.server_name ELSE CONCAT(msl.server_name,'\',msl.instance) END AS instance, m.job_name, m.last_run_date_time, m.last_run_duration, m.last_run_status, m.last_run_status_message, m.next_run_date_timeFROM monitoring.Jobs mJOIN inventory.Jobs i ON m.job_name = i.job_name AND m.serverId = i.serverIdJOIN inventory.MasterServerList msl ON msl.serverId = m.serverIdWHERE i.is_enabled = 1 AND m.last_run_status <> 'Succeeded';

Note: Keep in mind that this will report failures only for the jobs within theinventory.Jobs table that are currently enabled. It really doesn’t make senseto know this information for the disabled jobs, but if that’s what you need,then feel free to make the necessary adjustments.

注意:請記住,這將僅報告當前啟用的清單。作業表中的作業失敗。 真正了解殘疾人的工作信息並沒有任何意義,但是如果您需要此信息,請隨時進行必要的調整。

This is a very powerful one, and I highly recommend that you use this as a steppingstone to build a notification mechanism around this data. You could easily havea way to periodically know which jobs are not succeeding, across your entire infrastructure,without necessarily having to configure an email alert within each individual jobat each individual instance (imagine you have hundreds/thousands of jobs withinyour scope without alerts setup).

這是一個非常強大的功能,我強烈建議您將其用作圍繞此數據建立通知機制的墊腳石。 您可以輕鬆地通過整個基礎架構輕鬆地定期了解哪些作業不成功,而不必在每個單獨的實例中的每個單獨的作業中配置電子郵件警報(假設您的範圍內有成百上千個沒有警報的作業建立)。

哪些工作需要最長時間才能完成? (Which jobs are taking the longest time to complete?)

SELECT TOP 10 *FROM monitoring.JobsORDER BY last_run_duration DESC

Note: If you are able to identify long running jobs, then probably you havea performancetuning opportunity there. It might not be necessarily the case, but you shouldbe able to tell within your own particular environment.

注意:如果您能夠確定長時間運行的作業,則可能在那裡有性能調整的機會。 不一定是這種情況,但是您應該能夠在自己的特定環境中分辨。

下載腳本 (Download Scripts)

Download the scripts for this module

下載此模塊的腳本

下一步 (Next Steps)



Last Updated: 2020-07-27
上次更新時間:2020-07-27









關於作者 (About the author)








翻譯自: https://www.mssqltips.com/sqlservertip/6495/sql-server-agent-job-monitoring-for-last-execution-with-powershell/

相關焦點

  • SQL Server 2014
    解壓「cn_sql_server_2014_X64.iso」64位系統解壓「cn_sql_server_2014_X64.iso」。右擊「cn_sql_server_2014_X64.iso」點擊「解壓到cn_sql_server_2014_X64.iso」。
  • 如何使用 SQL Server FILESTREAM 存儲非結構化數據?
    但文件的安全性、管理其訪問權並對其進行維護方面存在一定問題。後來的二進位大對象(Binary Large Objects,BLOB)概念在一定程度上有助於存儲非結構化數據。這個概念的主要優點是資料庫中的集成管理和事務一致性。在這種情況下,安全問題(以前的文件解決方案)得到了解決。但仍存在一些問題,即2GB的限制以及日誌文件過載。
  • SQL Server 安裝步驟
    下載完成的 SQL server 2016如下圖實例配置,使用默認即可Analysis Service配置--推薦使用默認(添加當前用戶)Reporting Services配置--推薦使用默認設置管理工具要到微軟官網下載:https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms?view=sql-server-2017
  • SQL Server 2019安裝教程
    具有使用方便可伸縮性好與相關軟體集成程度高等優點。Microsoft SQL Server 是一個全面的資料庫平臺,使用集成的商業智能 (BI)工具提供了企業級的數據管理。Microsoft SQL Server 資料庫引擎為關係型數據和結構化數據提供了更安全可靠的存儲功能,使您可以構建和管理用於業務的高可用和高性能的數據應用程式。
  • 不一樣的 SQL Server 日期格式化
    如果未提供 culture 參數,則使用當前會話的語言。 可以使用 SET LANGUAGE 語句隱式或顯式設置此語言。culture 接受 .NET Framework 支持的任何區域性作為參數;它不局限於 SQL Server 顯式支持的語言。 如果 culture 參數無效,FORMAT 將引發錯誤。
  • oracle sql 布爾值專題及常見問題 - CSDN
    0x01 decode 函數布爾盲注decode(欄位或欄位的運算,值1,值2,值3)這個函數運行的結果是,當欄位或欄位的運算的值等於值1時,該函數返回值2,否則返回3當然值1,值2,值3也可以是表達式,這個函數使得某些sql
  • 0827-7.1.4-如何在CDP中使用Spark SQL CLI
    "fiecho $sparksqlecho ${sparksql} | spark-shell2.使用方法在腳本中進行了說明,-f參數直接接sql文本,-e可以直接輸入sql語句進行執行。3.單點問題,所有Spark SQL查詢都走唯一一個Spark Thrift節點上的同一個Spark Driver,任何故障都會導致這個唯一的Spark Thrift節點上的所有作業失敗,從而需要重啟Spark Thrift Server。
  • powershell 使用代理 - CSDN
    DBA需要做的一件事是監視SQL Server代理作業的狀態。 可以逐個實例完成此操作,但這需要時間。 在本文中,我們研究如何為使用此解決方案監視的所有SQL Server實例收集上次運行信息。 為了使用此模塊,您需要創建在此處找到的核心對象 ,還需要設置此模塊以收集SQL Agent作業信息 。
  • 【參賽作品22】關於遷移SQL server到openGauss的問題和解決
    背景2020年9月份,在將SQL server內容遷移到openGauss資料庫的過程中,由於openGauss開源不久,我在遷移的過程中遇到了許多問題。通過對源碼的閱讀和實際調試,最後逐個解決。在這裡,我對兩個資料庫之間的不同和相同分別做對比,提供一個實際參考。
  • mysql 矩陣類型專題及常見問題 - CSDN
    您可以使用多列來定義主鍵,列間以逗號分隔。ENGINE 設置存儲引擎,CHARSET 設置編碼。connect()方法連接資料庫db = pymysql.connect(host='127.0.0.1',user='root',password='123456',port=3306)# 獲取mysql的操作遊標cursor = db.cursor()# 使用execute()方法執行sql語句cursor.execute('select version()')# 使用fetchone()方法來獲得第一條數據
  • Windows下Laravel 7.0連接sql server - php中文網
    laravel 7.0 默認連接 mysql, 同時項目需要連接 sql servel (另一個系統應用)$users = DB::connection (『php_sqlsrv』)->select (…);
  • github覆蓋本地專題及常見問題 - CSDN
    # 如果自己的修改還沒有用git add 緩存,則使用這個來放棄這些尚未緩存的修改git pull origin master # 我沒記錯的話應該是這個指令,就是把github倉庫的項目同步到本地,如果本地沒有修改,會顯示already-up-to-date這類信息參考 [3] 和 [4]。
  • cdn邊緣計算專題及常見問題 - CSDN
    您必須說,速度足夠快,而且我從未遇到任何問題! 你猜怎麼了? 他們已經在網絡中處於邊緣並且離您更近了,簡而言之,他們正在使用Content Delivery Network(CDN )。 CDN如何運作? (How does CDN work?)
  • SQL Server的Descending Indexes降序索引
    OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GO 插入測試數據DECLARE @i INT;DECLARE @sql
  • 使用SQL Server In-Memory存儲ASP.NET的會話狀態
    問題使用Session State的替代方案這些內容超出了本文的範圍。 對於依賴Session State的應用程式也有陷阱,最常見的是訪問每個用戶,每個請求的Session基礎數據。這種獨特的訪問是維持Session State的一致性的一種方式,並且是通過設計實現了這種方式。
  • c定時 linux專題及常見問題 - CSDN
    按己想像的去執行了,如果沒問題再改成需要的任務執行的時間。強調:有些任務是不允許頻繁執行的,例如:定時往資料庫裡插入數據,這樣的任務在測試機上測試好,然後正式線上出現問題的機會就少了。6.2 調整系統時間調適任務用正確的執行任務的時間。
  • 5分鐘學會SQL SERVER行轉列、列轉行,PIVOT操作
    編輯實例的數據使用的是和 SQL筆試50題同樣的數據, 使用的平臺是SQLFIDDLE(提供在線資料庫),鑑於近期全球病情的影響,各種網站都有一定機率無法提供服務,本次提供了SQLite資料庫,已存入測試數據。
  • c++信號與槽專題及常見問題 - CSDN
    開源庫下載:包含說明文檔,源碼,實例:https://download.csdn.net/download/u012372584/131624962、直接編譯會有錯誤,需要對源碼中的一句進行更改:將第419行 :typedef sender_set::const_iterator const_iterator; 更改為:typedef typename sender_set
  • 一道簡單的sql語句題
    結果才發現,數據分析崗位大多注重的是資料庫的能力,比如sql語句的考察,hive的考察,以及一些運營思維的考察,所以第一次面試就很悲劇啦,不過題目還是很有代表性的。其他的不寫了,這裡只分享一個關於sql的題目。1、問題引出現在有兩個數據表,一個數據表記錄司機的信息,比如司機id,司機姓名,司機註冊時間等等,一個數據表記錄一天的訂單情況,比如訂單ID,訂單司機id,訂單時間。
  • SQL Server應用程式的高級Sql注入
    似乎通過刪除用戶輸入的字符串中的單引號或者通過一些方法避免它們出現可以解決這個問題。誠然如此,但是要實施這個解決方法還有很多的困難。= "select * from users where username = '" + username + "' and password = '" + password + "'"; trace( "query: " + sql ); rso.open( sql, cn ); if (rso.EOF) { rso.close(); %〉