Good morning,
I’m new in telegaf world and I have a problem when I need to obtain metrics from one database multiple instances in a windows server.
I obtain metrics form the sqlserver correctly, but when I want to obtain a specific metric with one query, the log shows an error in which it tells that I’m configuring wrong the query.
The config file to obtain the specific metric is:
[[inputs.sql.query]]
query = “”"
USE ‘NEUTRA602’ GO
SELECT
CAST(CAST(SUM(CAST(FILEPROPERTY(name, ‘SpaceUsed’)/128.0 AS DECIMAL(38,2)))*100 AS DECIMAL(38,2)) / CAST(SUM([maxsize]/128.0) AS DECIMAL(38,2)) AS DECIMAL(38,2)) AS Used_Percent
FROM sysfiles where filename not like ‘%log%’ and CAST([maxsize]/128.0 AS DECIMAL(10,2)) > 0
“”"
measurement = “sqlserver_datafile_Neutra602”
The error log file:
2020-05-30T07:24:03Z I! Sintaxis incorrecta cerca de ‘NEUTRA602’.
2020-05-30T07:24:03Z W! Error performing query 'USE 'NEUTRA602’GO
SELECT
CAST(CAST(SUM(CAST(FILEPROPERTY(name, ‘SpaceUsed’)/128.0 AS DECIMAL(38,2)))*100 AS DECIMAL(38,2)) / CAST(SUM([maxsize]/128.0) AS DECIMAL(38,2)) AS DECIMAL(38,2)) AS Used_Percent
FROM sysfiles where filename not like ‘%log%’ and CAST([maxsize]/128.0 AS DECIMAL(10,2)) > 0
'…: mssql: Sintaxis incorrecta cerca de ‘NEUTRA602’.
Could anybody tell me what’s the correct syntax in order to telegraf could read the value correctly? Executing this query in Microsoft SQL Server Management Studio it works properly.
Telegraf does not have a plugin called “inputs.sql.query”, is it custom made?
Generally speaking, when writing the Telegraf configuration file you are writing using toml synthax, that’s why you get an error (see toml strings.
In you case wrapping the query sting in single quotes instead of double quotes should work.
query = '''
USE ‘NEUTRA602’ GO
SELECT
CAST(CAST(SUM(CAST(FILEPROPERTY(name, ‘SpaceUsed’)/128.0 AS DECIMAL(38,2)))*100 AS DECIMAL(38,2)) / CAST(SUM([maxsize]/128.0) AS DECIMAL(38,2)) AS DECIMAL(38,2)) AS Used_Percent
FROM sysfiles where filename not like ‘%log%’ and CAST([maxsize]/128.0 AS DECIMAL(10,2)) > 0
'''
Good morning,
The tags are configured as in another config file where I’ve made another custom queries. I’ve attached you another query that it works properly.
I’ve changes the syntax as you’ve told me, but the error in the log is the same.
[[inputs.sql.query]]
query = ‘’’
USE ‘NEUTRA602’ GO
SELECT
CAST(CAST(SUM(CAST(FILEPROPERTY(name, ‘SpaceUsed’)/128.0 AS DECIMAL(38,2)))*100 AS DECIMAL(38,2)) / CAST(SUM([maxsize]/128.0) AS DECIMAL(38,2)) AS DECIMAL(38,2)) AS Used_Percent
FROM sysfiles where filename not like ‘%log%’ and CAST([maxsize]/128.0 AS DECIMAL(10,2)) > 0
‘’’
measurement = “sqlserver_datafile_Neutra602”
[[inputs.sql.query]]
query = “”"
SELECT ISNULL(COUNT( * ), 0) as BlockCounter
FROM sys.dm_exec_sessions AS s
INNER JOIN sys.dm_exec_requests AS r ON r.session_id = s.session_id
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS st
WHERE r.session_id != @@SPID
AND r.blocking_session_id > 0
AND r.total_elapsed_time / (1000.0) > 300
“”"
measurement = “sqlserver_locks_5_minutes”
The log is showing this:
2020-06-02T10:26:03Z I! Sintaxis incorrecta cerca de ‘NEUTRA602’.
2020-06-02T
10:26:03Z W! Error performing query 'USE ‘NEUTRA602’ GO
SELECT
CAST(CAST(SUM(CAST(FILEPROPERTY(name, ‘SpaceUsed’)/128.0 AS DECIMAL(38,2)))*100 AS DECIMAL(38,2)) / CAST(SUM([maxsize]/128.0) AS DECIMAL(38,2)) AS DECIMAL(38,2)) AS Used_Percent
FROM sysfiles where filename not like ‘%log%’ and CAST([maxsize]/128.0 AS DECIMAL(10,2)) > 0
'…: mssql: Sintaxis incorrecta cerca de ‘NEUTRA602’
I’ve changed the query as you told me.
Deleting the word ‘go’ and putting ‘;’ behind the name of the instance.
query = ‘’’
USE ‘NEUTRA602’;
SELECTCAST(CAST(SUM(CAST(FILEPROPERTY(name, ‘SpaceUsed’)/128.0 AS DECIMAL(38,2)))*100 AS DECIMAL(38,2)) / CAST(SUM([maxsize]/128.0) AS DECIMAL(38,2)) AS DECIMAL(38,2)) AS Used_PercentFROM sysfiles where filename not like ‘%log%’ and CAST([maxsize]/128.0 AS DECIMAL(10,2)) > 0
‘’’