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

[Metricbeat] IBM DB2 Module #13106

Closed
sorantis opened this issue Jul 30, 2019 · 11 comments
Closed

[Metricbeat] IBM DB2 Module #13106

sorantis opened this issue Jul 30, 2019 · 11 comments
Labels
enhancement Metricbeat Metricbeat Stalled Team:Integrations Label for the Integrations team

Comments

@sorantis
Copy link
Contributor

sorantis commented Jul 30, 2019

As ancient as it might sound, IBM DB2 is a widely used database engine, that's currently ranked 6 in the DB engines ranking. Today DB2 represents a family of data management products with a focus on AI powered features.
Recently IBM introduced a free version of the database to boost developer engagement. (Download options can be found here).

Today Metricbeat already provides integrations with many database engines, however IBM DB2 is not yet supported. There is a workaround that suggests our users to use the JDBC input with Logstash and collect DB2 metrics. This workaround is not straightforward and requires prior knowledge of configuring JDBS inputs on the user side. Having a Metricbeat module for IBM DB2 would remove this complexity and significantly increase user experience.

User Story:
As a user I want to use Metricbeat for monitoring my DB2 instances so that I could easily observe the state and performance of the DB2 databases.

Suggested targets:

  • Tablespaces.
  • I/O and storage (BUFFERPOOL).
  • Concurrency and locks.
  • SQL performance.
@sorantis sorantis added enhancement Metricbeat Metricbeat Team:Integrations Label for the Integrations team labels Jul 30, 2019
@amandahla
Copy link

@sorantis I would like to help but I don't have DB2 to test.

Do you think I can use this image?
https://hub.docker.com/r/ibmcom/db2

@amandahla
Copy link

amandahla commented Aug 13, 2019

I think we can use ODBC in this case to avoid CGO. I wrote this test but I need help to construct a query to select tablespace data.

  1. Install ODBC and DB2 driver
    Reference: https://medium.com/@zhimin.wen/talking-to-ibm-db2-with-golang-dad8bb6ee4b4

Files:

/etc/odbc.ini 
[MyDB]
Protocol=TCPIP
Hostname=127.0.0.1
Port=50000
Database=testdb
CurrentSchema=db2inst1
Driver=/home/METRICBEATS/db2_odbc_10.5fp10/odbc_cli/clidriver/lib/libdb2o.so

/etc/odbcinst.ini 
[DB2]
Description=DB2 Driver
Driver=/home/METRICBEATS/db2_odbc_10.5fp10/odbc_cli/clidriver/lib/libdb2o.so
fileusage=1
dontdlclose=1

/home/METRICBEATS/db2_odbc_10.5fp10/odbc_cli/clidriver/cfg/db2cli.ini
[MyDB]
Protocol=TCPIP
Hostname=127.0.0.1
Port=50000
Database=testdb
CurrentSchema=db2inst1
Driver=/home/METRICBEATS/db2_odbc_10.5fp10/odbc_cli/clidriver/lib/libdb2o.so
  1. Run docker image to simulate DB2
docker run -itd --name mydb2 --privileged=true -p 50000:50000 -e LICENSE=accept -e DB2INST1_PASSWORD=123456 -e DBNAME=testdb -e SAMPLEDB=true -v /home/METRICBEATS/db2_logs/:/database ibmcom/db2
  1. Run test program
package main

import (
	"fmt"

	_ "github.com/alexbrainman/odbc"
	"github.com/jmoiron/sqlx"
)

// Metric defines tablespace WIP metric
type Metric struct {
	TbName     string `db:"TBSPACE"`
	PageSize   int64  `db:"PAGESIZE"`
	ExtendSize int64  `db:"EXTENTSIZE"`
}

func main() {
	dbName := "MyDB"
	uid := "db2inst1"
	password := "123456"

	db, err := sqlx.Open("odbc", fmt.Sprintf("DSN=%s;uid=%s;pwd=%s", dbName, uid, password))
	if err != nil {
		fmt.Println(err)
	}

	err = db.Ping()
	if err != nil {
		fmt.Println(err)
	}

	rows, err := db.Queryx("select TBSPACE,PAGESIZE,EXTENTSIZE from syscat.tablespaces")
	if err != nil {
		fmt.Println(err)
	}

	metric := Metric{}
	for rows.Next() {
		err := rows.StructScan(&metric)
		if err != nil {
			fmt.Println(err)
		}
		fmt.Printf("%#v\n", metric)
	}
}

Output:

main.Metric{TbName:"SYSCATSPACE", PageSize:4096, ExtendSize:4}
main.Metric{TbName:"TEMPSPACE1", PageSize:4096, ExtendSize:32}
main.Metric{TbName:"USERSPACE1", PageSize:4096, ExtendSize:32}
main.Metric{TbName:"SYSTOOLSPACE", PageSize:4096, ExtendSize:4}

@sorantis
Copy link
Contributor Author

sorantis commented Aug 14, 2019

@amandahla thanks for initiating this!
Not a DB2 expert, but I'd start with the officially provided Docker image and the MON functions.

@amandahla
Copy link

amandahla commented Aug 14, 2019

I'm not sure if we can do this here/now but my husband(@rikatz ) gave me the suggestion to do something generic to collect from any ODBC connection.

So you can set any query to get values.

Would be something like this:

...
rows, err := db.Queryx("SELECT varchar(tbsp_name, 30) as tbsp_name, reclaimable_space_enabled, tbsp_free_pages, tbsp_page_top, tbsp_usable_pages FROM TABLE(MON_GET_TABLESPACE('',-2)) AS t ORDER BY tbsp_free_pages ASC")
	if err != nil {
		fmt.Println(err)
	}
	defer rows.Close()

	// Extracted from
	// https://stackoverflow.com/questions/23507531/is-golangs-sql-package-incapable-of-ad-hoc-exploratory-queries/23507765#23507765
	cols, err := rows.Columns()
	if err != nil {
		fmt.Println(err)
	}

	vals := make([]interface{}, len(cols))
	for i := 0; i < len(cols); i++ {
		vals[i] = new(interface{})
		if i != 0 {
			fmt.Print("\t")
		}
		fmt.Print(cols[i])
	}


	for rows.Next() {
		err = rows.Scan(vals...)
		if err != nil {
			fmt.Println(err)
			continue
		}

		for i := 0; i < len(vals); i++ {
			if i != 0 {
				fmt.Print("\t")
			}
			printValue(vals[i].(*interface{}))
		}
		fmt.Println()

	}

	if rows.Err() != nil {
		fmt.Println(rows.Err())
	}
}

func printValue(pval *interface{}) {
	switch v := (*pval).(type) {
	case nil:
		fmt.Print("NULL")
	case bool:
		if v {
			fmt.Print("1")
		} else {
			fmt.Print("0")
		}
	case []byte:
		fmt.Print(string(v))
	case time.Time:
		fmt.Print(v.Format("2006-01-02 15:04:05.999"))
	default:
		fmt.Print(v)
	}
}

Output:

TBSP_NAME       RECLAIMABLE_SPACE_ENABLED       TBSP_FREE_PAGES TBSP_PAGE_TOP   TBSP_USABLE_PAGES
TEMPSPACE1      0       0       0       1
SYSCATSPACE     1       4992    27772   32764
SYSTOOLSPACE    1       8044    144     8188
USERSPACE1      1       8064    96      8160

jsoriano added a commit that referenced this issue Dec 12, 2019
The SQL module can collect metrics and other data with
custom queries, using the database drivers included in
Metricbeat.

(cherry picked from commit a3136a4)
@botelastic
Copy link

botelastic bot commented Jul 14, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@botelastic botelastic bot added the Stalled label Jul 14, 2020
@sorantis
Copy link
Contributor Author

keep it open

@botelastic botelastic bot removed the Stalled label Jul 15, 2020
@amandahla
Copy link

News here? I did a quick search and unfortunately didn't find a go-native way to connect with DB2 like the others databases, all drivers uses CGO. Same for ODBC :(
Same problem since last year, I guess.

@sorantis
Copy link
Contributor Author

There's a go driver for DB2, not sure how stable it is though.

@amandahla
Copy link

Yes, but it seems that it uses CGO also, not native. :(

@jsoriano
Copy link
Member

For oracle we use a driver that also uses cgo, so maybe we can do something similar for db2. @sayden what do you think?

@botelastic
Copy link

botelastic bot commented Jan 27, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@botelastic botelastic bot added the Stalled label Jan 27, 2022
@botelastic botelastic bot closed this as completed Jul 26, 2022
@zube zube bot removed the [zube]: Done label Oct 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Metricbeat Metricbeat Stalled Team:Integrations Label for the Integrations team
Projects
None yet
Development

No branches or pull requests

4 participants