-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MS SQL DB metricset #8250
Closed
Closed
MS SQL DB metricset #8250
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6ac24df
Atomic commit of the current work in progress MS SQL metricset
sayden 996b7db
Change headers of files to show the proper license
sayden d79badb
MS SQL Server metricset initial commit for PR review
sayden aaee852
Move files to x-pack folder
sayden 69c64b0
Atomic commit of mssql progress
sayden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//// | ||
This file is generated! See scripts/docs_collector.py | ||
//// | ||
|
||
[[metricbeat-module-mssql]] | ||
== mssql module | ||
|
||
experimental[] | ||
|
||
This is the mssql module. | ||
|
||
|
||
|
||
[float] | ||
=== Example configuration | ||
|
||
The mssql module supports the standard configuration options that are described | ||
in <<configuration-metricbeat>>. Here is an example configuration: | ||
|
||
[source,yaml] | ||
---- | ||
metricbeat.modules: | ||
- module: mssql | ||
metricsets: ["io"] | ||
enabled: false | ||
period: 10s | ||
hosts: ["localhost"] | ||
|
||
---- | ||
|
||
[float] | ||
=== Metricsets | ||
|
||
The following metricsets are available: | ||
|
||
* <<metricbeat-metricset-mssql-io,io>> | ||
|
||
include::mssql/io.asciidoc[] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//// | ||
This file is generated! See scripts/docs_collector.py | ||
//// | ||
|
||
[[metricbeat-metricset-mssql-io]] | ||
=== mssql io metricset | ||
|
||
experimental[] | ||
|
||
include::../../../module/mssql/io/_meta/docs.asciidoc[] | ||
|
||
|
||
==== Fields | ||
|
||
For a description of each field in the metricset, see the | ||
<<exported-fields-mssql,exported fields>> section. | ||
|
||
Here is an example document generated by this metricset: | ||
|
||
[source,json] | ||
---- | ||
include::../../../module/mssql/io/_meta/data.json[] | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Module: mssql | ||
# Docs: https://www.elastic.co/guide/en/beats/metricbeat/master/metricbeat-module-mssql.html | ||
|
||
- module: mssql | ||
metricsets: ["io"] | ||
enabled: false | ||
period: 10s | ||
hosts: ["localhost"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FROM microsoft/mssql-server-linux:2017-latest | ||
RUN apt-get update && apt-get install -y netcat | ||
HEALTHCHECK --interval=1s --retries=90 CMD nc -z localhost 1433 | ||
|
||
ENV ACCEPT_EULA='Y' | ||
ENV SA_PASSWORD='1234_asdf' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
- module: mssql | ||
metricsets: ["db"] | ||
enabled: false | ||
period: 10s | ||
host: "127.0.0.1" | ||
port: 1443 | ||
username: "sa" | ||
password: "1234_asdf" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
This is the mssql module. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
- key: mssql | ||
title: "mssql" | ||
description: > | ||
experimental[] | ||
|
||
mssql module | ||
fields: | ||
- name: mssql | ||
type: group | ||
description: > | ||
fields: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"@timestamp": "2017-10-12T08:05:34.853Z", | ||
"beat": { | ||
"hostname": "host.example.com", | ||
"name": "host.example.com" | ||
}, | ||
"metricset": { | ||
"module": "mssql", | ||
"name": "db", | ||
"rtt": 115 | ||
}, | ||
"mssql": { | ||
"db": { | ||
"database": { | ||
"id": "1" | ||
}, | ||
"log_space_usage": { | ||
"since_last_backup_bytes": 925696, | ||
"total_bytes": 2088960, | ||
"used_bytes": 1257472, | ||
"used_percent": 60.19607925415039 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is the db metricset of the module mssql. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
- name: db | ||
type: group | ||
description: > | ||
db | ||
fields: | ||
- name: example | ||
type: keyword | ||
description: > | ||
Example field |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package db | ||
|
||
import ( | ||
s "github.com/elastic/beats/libbeat/common/schema" | ||
c "github.com/elastic/beats/libbeat/common/schema/mapstrstr" | ||
) | ||
|
||
var ( | ||
schema = s.Schema{ | ||
"database": s.Object{ | ||
"id": c.Str("database_id"), | ||
}, | ||
//Returns space usage information for the transaction log. | ||
"log_space_usage": s.Object{ | ||
"total_bytes": c.Int("total_log_size_in_bytes"), | ||
"used_bytes": c.Int("used_log_space_in_bytes"), | ||
"used_percent": c.Float("used_log_space_in_percent"), | ||
"since_last_backup_bytes": c.Int("log_space_in_bytes_since_last_backup"), | ||
}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package db | ||
|
||
import ( | ||
_ "github.com/denisenkom/go-mssqldb" | ||
"github.com/elastic/beats/libbeat/common/cfgwarn" | ||
"github.com/elastic/beats/metricbeat/mb" | ||
"github.com/elastic/beats/x-pack/metricbeat/module/mssqle/mssql" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// init registers the MetricSet with the central registry as soon as the program | ||
// starts. The New function will be called later to instantiate an instance of | ||
// the MetricSet for each host defined in the module's configuration. After the | ||
// MetricSet has been created then Fetch will begin to be called periodically. | ||
func init() { | ||
mb.Registry.MustAddMetricSet("mssql", "db", New, mb.DefaultMetricSet()) | ||
} | ||
|
||
// MetricSet holds any configuration or state information. It must implement | ||
// the mb.MetricSet interface. And this is best achieved by embedding | ||
// mb.BaseMetricSet because it implements all of the required mb.MetricSet | ||
// interface methods except for Fetch. | ||
type MetricSet struct { | ||
*mssql.MetricSet | ||
config *mssql.Config | ||
} | ||
|
||
// New creates a new instance of the MetricSet. New is responsible for unpacking | ||
// any MetricSet specific configuration options if there are any. | ||
func New(base mb.BaseMetricSet) (mb.MetricSet, error) { | ||
cfgwarn.Experimental("The mssql db metricset is experimental.") | ||
|
||
config := mssql.Config{} | ||
if err := base.Module().UnpackConfig(&config); err != nil { | ||
return nil, err | ||
} | ||
|
||
metricSet, err := mssql.NewMetricSet(base) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "error creating mssql metricset") | ||
} | ||
|
||
ms := &MetricSet{MetricSet: metricSet, config: &config} | ||
|
||
return ms, nil | ||
} | ||
|
||
// Fetch methods implements the data gathering and data conversion to the right | ||
// format. It publishes the event which is then forwarded to the output. In case | ||
// of an error set the Error field of mb.Event or simply call report.Error(). | ||
func (m *MetricSet) Fetch(reporter mb.ReporterV2) { | ||
fetcher, err := mssql.NewFetcher(m.config, []string{"SELECT * FROM sys.dm_db_log_space_usage;"}, &schema) | ||
if err != nil { | ||
reporter.Error(errors.Wrap(err, "error creating fetcher")) | ||
return | ||
} | ||
defer fetcher.Close() | ||
|
||
if fetcher.Error != nil { | ||
reporter.Error(fetcher.Error) | ||
} else { | ||
for _, e := range fetcher.Maprs { | ||
reporter.Event(mb.Event{ | ||
MetricSetFields: e, | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
// +build integration | ||
|
||
package db | ||
|
||
import ( | ||
mbtest "github.com/elastic/beats/metricbeat/mb/testing" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"github.com/elastic/beats/libbeat/tests/compose" | ||
) | ||
|
||
func TestFetch(t *testing.T) { | ||
compose.EnsureUp(t, "mssql") | ||
|
||
f := mbtest.NewReportingMetricSetV2(t, getConfig()) | ||
events, errs := mbtest.ReportingFetchV2(f) | ||
assert.Empty(t, errs) | ||
if !assert.NotEmpty(t, events) { | ||
t.FailNow() | ||
} | ||
|
||
t.Logf("Module: %s Metricset: %s", f.Module().Name(), f.Name()) | ||
|
||
for _, event := range events { | ||
userPercent, err := event.MetricSetFields.GetValue("log_space_usage.used_percent") | ||
assert.NoError(t, err) | ||
if userPercentFloat, ok := userPercent.(float64); !ok { | ||
t.Fail() | ||
} else { | ||
assert.True(t, userPercentFloat > 0) | ||
} | ||
} | ||
} | ||
|
||
func getConfig() map[string]interface{} { | ||
return map[string]interface{}{ | ||
"module": "mssql", | ||
"metricsets": []string{"db"}, | ||
"host": "127.0.0.1", | ||
"user": "sa", | ||
"password": "1234_asdf", | ||
"port": 1433, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
// Package mssql is a Metricbeat module that contains MetricSets. | ||
package mssql |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a blank import should be only in a main or test package, or have a comment justifying it