关闭 x
IT技术网
    技 采 号
    ITJS.cn - 技术改变世界
    • 实用工具
    • 菜鸟教程
    IT采购网 中国存储网 科技号 CIO智库

    IT技术网

    IT采购网
    • 首页
    • 行业资讯
    • 系统运维
      • 操作系统
        • Windows
        • Linux
        • Mac OS
      • 数据库
        • MySQL
        • Oracle
        • SQL Server
      • 网站建设
    • 人工智能
    • 半导体芯片
    • 笔记本电脑
    • 智能手机
    • 智能汽车
    • 编程语言
    IT技术网 - ITJS.CN
    首页 » SQL Server »SQL Server自动化运维系列:监控性能指标脚本

    SQL Server自动化运维系列:监控性能指标脚本

    2015-05-25 00:00:00 出处:电子灵魂
    分享

    《SQL Server自动化运维系列:监控性能指标脚本》
    《SQL Server自动化运维系列:监控磁盘剩余空间及SQL Server错误日志》
    《SQL Server自动化运维系列:关于邮件通知那点事》
    《SQL Server自动化运维系列:监控跑批Job运行状态》
    《SQL Server自动化运维系列:关于数据收集》

    需求描述

    一般在生产环境中,有时候需要自动的检测指标值状态,如果发生异常,需要提前预警的,比如发邮件告知,该文就介绍如果通过Power shell实现状态值监控。

    监控值范围

    根据经验,作为DBA一般需要监控如下系统能行指标。

    cpu:
    
        Processor(_Total)% Processor Time
        Processor(_Total)% Privileged Time
    
        SQLServer:SQL StatisticsBatch Requests/sec
        SQLServer:SQL StatisticsSQL Compilations/sec
        SQLServer:SQL StatisticsSQL Re-Compilations/sec
        SystemProcessor Queue Length
        SystemContext Switches/sec
    
      Memory:
    
        MemoryAvailable Bytes
        MemoryPages/sec
        MemoryPage Faults/sec
        MemoryPages Input/sec
        MemoryPages Output/sec
        Process(sqlservr)Private Bytes
        SQLServer:Buffer ManagerBuffer cache hit ratio
        SQLServer:Buffer ManagerPage life expectancy
        SQLServer:Buffer ManagerLazy writes/sec
        SQLServer:Memory ManagerMemory Grants Pending
        SQLServer:Memory ManagerTarget Server Memory (KB)
        SQLServer:Memory ManagerTotal Server Memory (KB)
    
      Disk:
    
        PhysicalDisk(_Total)% Disk Time
        PhysicalDisk(_Total)Current Disk Queue Length
        PhysicalDisk(_Total)Avg. Disk Queue Length
        PhysicalDisk(_Total)Disk Transfers/sec
        PhysicalDisk(_Total)Disk Bytes/sec
        PhysicalDisk(_Total)Avg. Disk sec/Read
        PhysicalDisk(_Total)Avg. Disk sec/Write
    
      SQL Server:
    
        SQLServer:Access MethodsFreeSpace Scans/sec
        SQLServer:Access MethodsFull Scans/sec
        SQLServer:Access MethodsTable Lock Escalations/sec
        SQLServer:Access MethodsWorktables Created/sec
        SQLServer:General StatisticsProcesses blocked
        SQLServer:General StatisticsUser Connections
        SQLServer:LatchesTotal Latch Wait Time (ms)
        SQLServer:Locks(_Total)Lock Timeouts (timeout > 0)/sec
        SQLServer:Locks(_Total)Lock Wait Time (ms)
        SQLServer:Locks(_Total)Number of Deadlocks/sec
        SQLServer:SQL StatisticsBatch Requests/sec
        SQLServer:SQL StatisticsSQL Re-Compilations/sec

    上述指标含义,可以参照我上一篇文章:SQL Server需要监控哪些计数器

    监控脚本

    $server = "(local)"
    $uid = "sa"
    $db="master"
    $pwd="password"
    $mailprfname = "SendEmail"
    $recipients = "787449667@qq.com"
    $subject = "数据库指标异常了!"
    $computernamexml = "f:computername.xml"
    $alter_cpuxml = "f:alter_cpu.xml"
    function GetServerName($xmlpath)
    {
        $xml = [xml] (Get-Content $xmlpath)
        $return = New-Object Collections.Generic.List[string]
        for($i = 0;$i -lt $xml.computernames.ChildNodes.Count;$i++)
        {
            if ( $xml.computernames.ChildNodes.Count -eq 1)
            {
                $cp = [string]$xml.computernames.computername
            }
            else
            {
                $cp = [string]$xml.computernames.computername[$i]
            }
            $return.Add($cp.Trim())
        }
        $return
    }
    
    function GetAlterCounter($xmlpath)
    {
        $xml = [xml] (Get-Content $xmlpath)
        $return = New-Object Collections.Generic.List[string]
        $list = $xml.counters.Counter
        $list
    }
    
    function CreateAlter($message)
    {
        $SqlConnection = New-Object System.Data.SqlClient.SqlConnection 
        $CnnString ="Server = $server; Database = $db;User Id = $uid; Password = $pwd" 
        $SqlConnection.ConnectionString = $CnnString 
        $CC = $SqlConnection.CreateCommand(); 
        if (-not ($SqlConnection.State -like "Open")) { $SqlConnection.Open() } 
    
        $cc.CommandText=" EXEC msdb..sp_send_dbmail 
                 @profile_name  = '$mailprfname'
                ,@recipients = '$recipients'
                ,@body = '$message'
                ,@subject = '$subject'
    " 
        $cc.ExecuteNonQuery()|out-null 
        $SqlConnection.Close();
    }
    
    $names = GetServerName($computernamexml)
    $pfcounters = GetAlterCounter($alter_cpuxml)
    foreach($cp in $names)
    {
        $p = New-Object Collections.Generic.List[string]
        $report = ""
        foreach ($pfc in $pfcounters)
        {
            $b = ""
            $counter ="\"+$cp+$pfc.get_InnerText().Trim()
            $p.Add($counter)
    
        }
        $count = Get-Counter $p
        for ($i = 0; $i -lt $count.CounterSamples.Count; $i++)
        {
            $v = $count.CounterSamples.Get($i).CookedValue
            $pfc = $pfcounters[$i]
            #$pfc.get_InnerText()
            $b = ""
            $lg = ""
            if($pfc.operator -eq "lt")
            {
                if ($v -ge [double]$pfc.alter)
                    {$b = "alter"
                    $lg = "Greater Than"}
            }
            elseif ($pfc.operator -eq "gt")
            {
                if( $v -le [double]$pfc.alter)
                    {$b = "alter"
                    $lg = "Less Than"}
            }
            if($b -eq "alter")
            {
                $path = "\"+$cp+$pfc.get_InnerText()
    
                $item = "{0}:{1};{2} Threshold:{3}" -f $path,$v.ToString(),$lg,$pfc.alter.Trim()
                $report += $item + "`n"
            }
    
        }
        if($report -ne "")
        {
            #生产警告 参数 计数器,阀值,当前值
            CreateAlter $report
        }
    }

    其中涉及到2个配置文件:computernamexml,alter_cpuxml分别如下:

    <computernames>
            <computername>
                    wuxuelei-pc
            </computername>
    </computernames>
    <Counters>
            <Counter alter = "10" operator = "gt" >Processor(_Total)% Processor Time</Counter>
            <Counter alter = "10" operator = "gt" >Processor(_Total)% Privileged Time</Counter>
            <Counter alter = "10" operator = "gt" >SQLServer:SQL StatisticsBatch Requests/sec</Counter>
            <Counter alter = "10" operator = "gt" >SQLServer:SQL StatisticsSQL Compilations/sec</Counter>
            <Counter alter = "10" operator = "gt" >SQLServer:SQL StatisticsSQL Re-Compilations/sec</Counter>
            <Counter alter = "10" operator=  "lt" >SystemProcessor Queue Length</Counter>
            <Counter alter = "10" operator=  "lt" >SystemContext Switches/sec</Counter>
    </Counters>

    其中 alter 就是阀值,如第一条,如果 阀值 > 性能计数器值,就会发出警告。

    其实这种自定义配置的方式,实现了灵活多变的自动化监控标准:

    1、比如可以检测磁盘空间大小

    2、检测运行峰值状态

    3、定时的根据历史运行值,更改生产系统中的阀值大小,也就是所谓的运行基线

    警告实现方式

    1、SQL Agent配置Job方式实现

    2、计划任务

    以上两种配置方式,可以灵活掌握,操作还是蛮简单的,如果不会,可自行google。当然,如果不想干预正常的生产系统,可以添加一个Server专门用来自动化运维检测来用,实现远程监控。

    后续文章中会分析关于Power Shell的远程调用,并且能实现事故当前状态下,自动化截图….自动Send Email……为DBA现场取证第一手材料…方便诊断问题…

    效果图如下

    以上只提供实现方式,如需要内容更新,自己灵活更新。

    脚本下载地址http://files.cnblogs.com/zhijianliutang/DBALter.zip

    上一篇返回首页 下一篇

    声明: 此文观点不代表本站立场;转载务必保留本文链接;版权疑问请联系我们。

    别人在看

    Destoon 模板存放规则及语法参考

    Destoon系统常量与变量

    Destoon系统目录文件结构说明

    Destoon 系统安装指南

    Destoon会员公司主页模板风格添加方法

    Destoon 二次开发入门

    Microsoft 将于 2026 年 10 月终止对 Windows 11 SE 的支持

    Windows 11 存储感知如何设置?了解Windows 11 存储感知开启的好处

    Windows 11 24H2 更新灾难:系统升级了,SSD固态盘不见了...

    小米路由器买哪款?Miwifi热门路由器型号对比分析

    IT头条

    Synology 对 Office 套件进行重大 AI 更新,增强私有云的生产力和安全性

    01:43

    StorONE 的高效平台将 Storage Guardian 数据中心占用空间减少 80%

    11:03

    年赚千亿的印度能源巨头Nayara 云服务瘫痪,被微软卡了一下脖子

    12:54

    国产6nm GPU新突破!砺算科技官宣:自研TrueGPU架构7月26日发布

    01:57

    公安部:我国在售汽车搭载的“智驾”系统都不具备“自动驾驶”功能

    02:03

    技术热点

    如何删除自带的不常用应用为windows 7减负

    MySQL中多表删除方法

    改进的二值图像像素标记算法及程序实现

    windows 7 32位系统下手动修改磁盘属性例如M盘修改为F盘

    windows 7中怎么样在家庭组互传文件

    Linux应用集成MySQL数据库访问技巧

      友情链接:
    • IT采购网
    • 科技号
    • 中国存储网
    • 存储网
    • 半导体联盟
    • 医疗软件网
    • 软件中国
    • ITbrand
    • 采购中国
    • CIO智库
    • 考研题库
    • 法务网
    • AI工具网
    • 电子芯片网
    • 安全库
    • 隐私保护
    • 版权申明
    • 联系我们
    IT技术网 版权所有 © 2020-2025,京ICP备14047533号-20,Power by OK设计网

    在上方输入关键词后,回车键 开始搜索。Esc键 取消该搜索窗口。