Skip to content
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
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions metricbeat/docs/modules/mssql.asciidoc
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[]

23 changes: 23 additions & 0 deletions metricbeat/docs/modules/mssql/io.asciidoc
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[]
----
3 changes: 3 additions & 0 deletions metricbeat/docs/modules_list.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ This file is generated! See scripts/docs_collector.py
|<<metricbeat-metricset-mongodb-metrics,metrics>> experimental[]
|<<metricbeat-metricset-mongodb-replstatus,replstatus>> experimental[]
|<<metricbeat-metricset-mongodb-status,status>>
|<<metricbeat-module-mssql,mssql>> experimental[] |image:./images/icon-no.png[No prebuilt dashboards] |
.1+| .1+| |<<metricbeat-metricset-mssql-io,io>> experimental[]
|<<metricbeat-module-munin,Munin>> beta[] |image:./images/icon-no.png[No prebuilt dashboards] |
.1+| .1+| |<<metricbeat-metricset-munin-node,node>> beta[]
|<<metricbeat-module-mysql,MySQL>> |image:./images/icon-yes.png[Prebuilt dashboards are available] |
Expand Down Expand Up @@ -172,6 +174,7 @@ include::modules/kvm.asciidoc[]
include::modules/logstash.asciidoc[]
include::modules/memcached.asciidoc[]
include::modules/mongodb.asciidoc[]
include::modules/mssql.asciidoc[]
include::modules/munin.asciidoc[]
include::modules/mysql.asciidoc[]
include::modules/nginx.asciidoc[]
Expand Down
9 changes: 9 additions & 0 deletions metricbeat/modules.d/mssql.yml.disabled
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"]

6 changes: 6 additions & 0 deletions x-pack/metricbeat/module/mssql/_meta/Dockerfile
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'
9 changes: 9 additions & 0 deletions x-pack/metricbeat/module/mssql/_meta/config.yml
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"

2 changes: 2 additions & 0 deletions x-pack/metricbeat/module/mssql/_meta/docs.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is the mssql module.

11 changes: 11 additions & 0 deletions x-pack/metricbeat/module/mssql/_meta/fields.yml
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:
25 changes: 25 additions & 0 deletions x-pack/metricbeat/module/mssql/db/_meta/data.json
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
}
}
}
}
1 change: 1 addition & 0 deletions x-pack/metricbeat/module/mssql/db/_meta/docs.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is the db metricset of the module mssql.
9 changes: 9 additions & 0 deletions x-pack/metricbeat/module/mssql/db/_meta/fields.yml
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
25 changes: 25 additions & 0 deletions x-pack/metricbeat/module/mssql/db/data.go
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"),
},
}
)
72 changes: 72 additions & 0 deletions x-pack/metricbeat/module/mssql/db/db.go
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"

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

"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,
})
}
}
}
48 changes: 48 additions & 0 deletions x-pack/metricbeat/module/mssql/db/db_integration_test.go
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,
}
}
6 changes: 6 additions & 0 deletions x-pack/metricbeat/module/mssql/doc.go
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
Loading