Telegraf和Grafana监控多平台上的SQL Server-自定义监控数据收集

问题

上一篇文章中,咱们使用Telegraf自带的Plugin配置好了的监控,可是自带的Plugin并不能彻底覆盖咱们想要的监控指标,就须要收集额外的自定义的监控数据,实现的方法有:html

  • 开发本身的Telegraf Pluginlinux

  • 使用能够执行自定义脚本的inputs pluginsql

此处收集的监控项很少,收集间隔也不是很频繁,因此我选择Telegraf预置的Inputs.exec plugin实现。它很是灵活,能够执行任意命令和脚本。在脚本中实现获取监控数据的逻辑,而后使用inputs.exec执行。获取数据以后,须要按InfluxDB Line Protocol 格式组织数据,才能写入到Influxdb。这种格式的组织方式:shell

[measurement],[tags] [fields] [timestamp]

  • measurement,相似于SQL中表的概念,数据存放的容器数据库

  • tags,K-V格式用于标记数据记录,通常它们的值不常常变化,如主机名。同时tags上会创建索引,查询效率会好一些.windows

  • fields,K-V格式,表示真正收集的不一样时间点的数据项,如CPU Loadbash

  • timestamp,UNIX 时间戳,Influxdb是时序数据库,全部数据都要与时间关联起来。sqlserver

  • measurement和tag之间用逗号分隔,fields 与它们用空格(whitespace)分隔性能

无论是运行在Linux仍是Windows上的SQL,一般使用T-SQL查询实例内部的数据和使用操做系统脚本查询实例外部的数据以实现监控。接下来,以执行T-SQL获取自定义监控数据为例,看看在Windos和Linux上分别如何实现。spa

解决方案

首先在被监控的实例上把相应的逻辑写成存储过程,而后经过inputs.exec调用之。

例如我在目标实例的influx库中建立了一个存储过程influx.usp_getInstanceInfo获取一些实例的配置信息。而后须要在telegraf配置文件中启用inputs.exec调用这个存储过程。存储过程的输出数据以下:

sqlserver_property,host=SQL19N1,sql_instance=SQL19N1 host_platform="Linux",host_distribution="CentOS Linux",host_release=7,edition="Developer Edition (64-bit)",product_version="15.0.4033.1",collation="SQL_Latin1_General_CP1_CI_AS",is_clustered=f,is_hadr=t,cpu_count=2,scheduler_count=2,physical_memory_kb=6523904,max_workers_count=512,max_dop=0,max_memmory=2147483647 1590915136000000000

数据写入到sqlserver_property,tags包括host,sql_instance,后面的全是fields。

  • SQL On Linux

使用SQLCMD调用存储过程,把SQLCMD命令写到一个bash文件中/telegraf/get_sqlproperty.sh

#!/bin/bash

/opt/mssql-tools/bin/sqlcmd -S SQL19N1 -U telegraf -P <yourpassword> -d influx -y 0 -Q "EXEC influx.usp_getInstanceInfo"

修改telegraf.conf中的inputs.exec, 而后重启telegraf生效:

由于收集的是实例属性信息,收集间隔设置的比较长。

[[inputs.exec]]
#   ## Commands array
   commands = [
        "/telegraf/get_sqlproperty.sh"
        ]
        
   timeout = "5s"
   interval="24h"
   data_format = "influx"
  • SQL On Windows

Windows上首选使用PowerShell实现,把执行SQL的命令写到C:\Monitoring\scripts\get_sqlproperty.ps1。col_res是存储过程输出的列名。

(Invoke-Sqlcmd -ServerInstance SQL17N1 -Username telegraf -Password "<yourpassword>" -Database influx -Query "exec influx.usp_getInstanceInfo" ).col_res

修改telegraf.conf中的inputs.exec, 而后重启telegraf生效.

须要特别注意的问题:

  • 指定文件路径时,要使用Linux路径表达的forward slash(/), 而不是Windows中的 back slash(\)

  • ps1文件路径使用单引号(single quote)

  • 避免文件路径中有空格(whitespace)

[[inputs.exec]]
#   ## Commands array
   commands = [
        "powershell 'C:/Monitoring/scripts/get_sqlproperty.ps1' "
        ]
        
   timeout = "5s"
   interval="24h"
   data_format = "influx"

配置完成后,看看measurement和数据:

总结

  • 在inputs.exec中最好是调用脚本,而不是命令。这样当你须要变动数据收集逻辑,直接修改脚本便可,而不须要修改Telegraf的配置文件,避免重启服务和配置干扰

  • 被调用的脚本的输出,要是stdout,才能被正确写入influxdb

  • Windows 上文件路径和符号escape要特别注意

  • 若是对收集性能特别敏感或者收集频率特别高时,使用Go自定义Plugin

  • 本文内容仅表明我的观点,与任何公司和组织无关

相关文章
相关标签/搜索