Monitoring MySQL with Prometheus and Grafana

107
Monitoring MySQL with Prometheus & Grafana Julien Pivotto (@roidelapluie) Open Source Monitoring Conference November 22nd, 2017

Transcript of Monitoring MySQL with Prometheus and Grafana

Page 1: Monitoring MySQL with Prometheus and Grafana

Monitoring MySQL withPrometheus & Grafana

Julien Pivotto (@roidelapluie)

Open Source Monitoring Conference

November 22nd, 2017

Page 2: Monitoring MySQL with Prometheus and Grafana

SELECT USER();Julien "roidelapluie" Pivotto

@roidelapluie

Sysadmin at inuits

Automation, monitoring, HA

MySQL/MariaDB user/admin/contributor

Grafana and Prometheus user/contributor

Page 3: Monitoring MySQL with Prometheus and Grafana

inuits

Page 4: Monitoring MySQL with Prometheus and Grafana

Monitoring /Observability

Page 5: Monitoring MySQL with Prometheus and Grafana

Monitoring or Observability?Collecting lots of data

Not only for alerting, also for understanding

You don't know what you will need

Page 6: Monitoring MySQL with Prometheus and Grafana

Who decides what to monitor?Pre-Define checks

Check only what os needed

OR

Let the system you monitor decide what toexpose

Alert based on that data

Page 7: Monitoring MySQL with Prometheus and Grafana

Push or pull ?Push: need to know and configure where yourserver is

What if 2 3 10 servers?

What in dev?

Pull: Let any server take the metricsUse service discovery to know what tofetch

Page 8: Monitoring MySQL with Prometheus and Grafana

High availabilityMonitoring is very important

Metamonitoring

What is the price to get a highly availablemonitoring system?

Page 9: Monitoring MySQL with Prometheus and Grafana

Prometheushttps://prometheus.io/

Page 10: Monitoring MySQL with Prometheus and Grafana

PrometheusPrometheus is a Cloud-Native Data-Centric Open-Source Performant Simple metrics collection,analysis and alerting tool.

Nothing more.

Page 11: Monitoring MySQL with Prometheus and Grafana

Cloud NativeVery easy to configure, deploy, maintain

Designed in multiple services

Container ready

Orchestration ready (dynamic config)

Page 12: Monitoring MySQL with Prometheus and Grafana

Open SourceApache 2.0

CNCF

Go

Support for multiple OS

Many "exporters":https://github.com/prometheus/prometheus/wiki/Defau

lt-port-allocations

Page 13: Monitoring MySQL with Prometheus and Grafana

PerformancePrometheus is designed to fetch data in aninterval measured in SECONDS

Designed to handle lots of metrics

New storage engine in 2.0 to scale even betterand support usecases like kubernetes

Page 14: Monitoring MySQL with Prometheus and Grafana

Data CentricA Metric in Prometheus has metadata:

myql_global_status_handlers_total{handler="tmp_write"} 1122

And lots of function to filter, change, remove...those metadata while fetching them.

Page 15: Monitoring MySQL with Prometheus and Grafana

Metrics typeCounters (always go up)

Gauge (go up and down)

Histograms (aggregate by buckets)

Summary (percentiles) (most of the timeuseless)

Page 16: Monitoring MySQL with Prometheus and Grafana

A word aboutPrometheus vs Graphite

Prometheus does not see a metric as an "event".Metrics are current value until they are replaced.You can not see when a metric has been includedin Prometheus.For Events, Prometheus refers to Elasticsearch.

Page 17: Monitoring MySQL with Prometheus and Grafana

How?Creative Commons Attribution 2.0 https://www.flickr.com/photos/75001512@N00/6824648824

Page 18: Monitoring MySQL with Prometheus and Grafana

Warning : this is complicated.

Page 19: Monitoring MySQL with Prometheus and Grafana

Prometheus uses HTTP andPLAIN TEXT.

http(s) is supported

basic auth / token also

(exposing https is not Prometheus job)

Page 20: Monitoring MySQL with Prometheus and Grafana

$ curl http://127.0.0.1:9090/metrics# HELP prometheus_notifications_queue_length# The number of alert notifications in the queue.# TYPE prometheus_notifications_queue_length gaugeprometheus_notifications_queue_length 0# HELP prometheus_notifications_sent_total# Total number of alerts successfully sent.# TYPE prometheus_notifications_sent_total counterprometheus_notifications_sent_total{alertmanager="127.0.0.1:9093"} 4.796464e+06

Page 21: Monitoring MySQL with Prometheus and Grafana

$ curl http://127.0.0.1:9090/metricsprometheus_notifications_queue_length 0prometheus_notifications_sent_total{alertmanager="127.0.0.1:9093"} 4.796464e+06

Page 22: Monitoring MySQL with Prometheus and Grafana

Anything that can embed a httpserver can serve prometheus

metrics.

Page 23: Monitoring MySQL with Prometheus and Grafana

Native Prometheus integrationsKubernetes

Ceph

etcd

telegraph

Grafana

mgmt

...

Page 24: Monitoring MySQL with Prometheus and Grafana

What when you can't?Linux (not counting TUX web server)

MySQL

Third party software

Third party services

Page 25: Monitoring MySQL with Prometheus and Grafana

ExportersCreative Commons Attribution 2.0 https://www.flickr.com/photos/fuzzy/563198182

Page 26: Monitoring MySQL with Prometheus and Grafana

ExportersExporters expose metrics with an HTTP API

They connect to the real target

Bindings available for many languages

Exporters do not save data ; they are not"proxies" and don't "cache" anything

Page 27: Monitoring MySQL with Prometheus and Grafana

Official exportersConsul, Memcached, MySQL, Node/System,HAProxy, AWS Cloudwatch, Collectd, Graphite,Statsd, JMX, influxdb, SNMP, Blackbox

Page 28: Monitoring MySQL with Prometheus and Grafana

Node exporterLinux Metrics

Including: CPU, Memory, Disk space,networking, load, time...

Page 29: Monitoring MySQL with Prometheus and Grafana

TextfileAs part of the node_exporter, you can writemetrics in file

Scripts output, etc ...

Page 30: Monitoring MySQL with Prometheus and Grafana

Blackbox exporterHTTP, DNS, TCP sockets, ICMP...

http://127.0.0.1:9115/probe?target=https://inuits.eu&module=http_2xxprobe_ssl_earliest_cert_expiry 1.52697083e+09

Page 31: Monitoring MySQL with Prometheus and Grafana

MySQLCreative Commons Attribution 2.0 https://www.flickr.com/photos/lurkerm/262541595

Page 32: Monitoring MySQL with Prometheus and Grafana
Page 33: Monitoring MySQL with Prometheus and Grafana
Page 34: Monitoring MySQL with Prometheus and Grafana
Page 35: Monitoring MySQL with Prometheus and Grafana
Page 36: Monitoring MySQL with Prometheus and Grafana
Page 37: Monitoring MySQL with Prometheus and Grafana

FrequencySome queries are expensive

You can decide to fetch some data every 1m,others every 10s...

Page 38: Monitoring MySQL with Prometheus and Grafana

Wait .. Exporters don't cache??How do I fetch some data every

10s and others every 1m?

Page 39: Monitoring MySQL with Prometheus and Grafana

scrape_configs:­ job_name: 'mysql global status'  scrape_interval: 10s  static_configs:  ­ targets:    ­ '172.31.14.3:9104'  params:    collect[]:    ­ global_status

­ job_name: 'mysql performance'  scrape_interval: 1m  static_configs:  ­ targets:    ­ '172.31.14.3:9104'  params:    collect[]:    ­ perf_schema.tableiowaits    ­ perf_schema.indexiowaits    ­ perf_schema.tablelocks

Page 40: Monitoring MySQL with Prometheus and Grafana

MySQL ReplicationMySQL Master <-> MySQL Master

MySQL Master -> MySQL Slave

MySQL Master -> MySQL Slave -> MySQLSlave

MySQL Masters -> MySQL Slaves -> MySQLSlaves -> MySQL Slaves

MySQL Master -> MySQL Slaves

Page 41: Monitoring MySQL with Prometheus and Grafana

pt-heartbeatpt-heartbeart is a daemon that updates an entrywith current timestamp on a mysql server everysecond.

On the replica, you can check the timestamp anddo  NOW ­ timestamp  to get the real lag.

+­­­­­­­­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­+| ts                         | server_id |+­­­­­­­­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­+| 2017­08­17T16:55:01.001030 |         1 |+­­­­­­­­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­+

Page 42: Monitoring MySQL with Prometheus and Grafana

pt-heartbeatGPL

Perl

Part of percona toolkit

Page 43: Monitoring MySQL with Prometheus and Grafana

wait, mysql has that nativelymysql> SHOW SLAVE STATUS\G...Seconds_Behind_Master: 0...

aka mysqld_exporter metric:

 mysql_slave_lag_seconds 

Page 44: Monitoring MySQL with Prometheus and Grafana
Page 45: Monitoring MySQL with Prometheus and Grafana

BugsFixes for Seconds_Behind_Master in: 5.7.18,5.6.36, 5.6.23, 5.6.16.

Page 46: Monitoring MySQL with Prometheus and Grafana

How it worksChecks the heartbeat table (SQL query). It's notcalling the  pt­heartbeat  cli. So it is independantfrom it.

Page 47: Monitoring MySQL with Prometheus and Grafana

CLI flagscollect.heartbeat

collect.heartbeat.database

collect.heartbeat.table

Page 48: Monitoring MySQL with Prometheus and Grafana

Metricsmysql_heartbeat_stored_timestamp_seconds{server_id="1"}mysql_heartbeat_now_timestamp_seconds{server_id="1"}

Page 49: Monitoring MySQL with Prometheus and Grafana

Back to PrometheusCreative Commons Attribution 2.0 https://www.flickr.com/photos/andrepierre/14843110667

Page 50: Monitoring MySQL with Prometheus and Grafana

Exploring Metrics

Page 51: Monitoring MySQL with Prometheus and Grafana

Exploring Metrics

Page 52: Monitoring MySQL with Prometheus and Grafana

Exploring Metrics

Page 53: Monitoring MySQL with Prometheus and Grafana

Exploring Metrics

Page 54: Monitoring MySQL with Prometheus and Grafana

PromQLmysql_global_status_commands_total

Page 55: Monitoring MySQL with Prometheus and Grafana

PromQLmysql_global_status_commands_total{command="select"}

Page 56: Monitoring MySQL with Prometheus and Grafana

PromQLmysql_global_status_commands_total

{command=~"select|set_options"}

Page 57: Monitoring MySQL with Prometheus and Grafana

PromQLmysql_global_status_commands_total{command=~"select|set

_options"}

Page 58: Monitoring MySQL with Prometheus and Grafana

PromQLderiv(mysql_global_status_connections[5m])

Page 59: Monitoring MySQL with Prometheus and Grafana

PromQLmysql_up == 0

Page 60: Monitoring MySQL with Prometheus and Grafana

PromQLsum( avg_over_time(

mysql_info_schema_threads[10m])) by

(instance) ­ sum(

avg_over_time(mysql_info_schema_thread

s[10m] offset 1d)) by (instance)

Page 61: Monitoring MySQL with Prometheus and Grafana

PromQL{__name__=~".+innodb.+cache.*"}

predict_linear(mysql_heartbeat_lag_seconds[5m], 60*2)

sum(rate(mysql_global_status_commands_total{command=~"

(commit|rollback)"}[5m]))

Page 62: Monitoring MySQL with Prometheus and Grafana

Recording and alerting rules

Page 63: Monitoring MySQL with Prometheus and Grafana

RulesPrometheus can record extra metrics

Make expensive calculations

Alert if value is present

Page 64: Monitoring MySQL with Prometheus and Grafana

groups:­ name: mysql  rules:  ­ record: mysql_heartbeat_lag_second    expr: |      mysql_heartbeat_now_timestamp_seconds ­      mysql_heartbeat_stored_timestamp_seconds  ­ alert: MysqlReplicationNotRunning    expr: |      mysql_slave_status_slave_io_running == 0 OR      mysql_slave_status_slave_sql_running == 0    for: 2m    annotations:          summary: "Slave replication is not running"  ­ alert: MySQLReplicationLag    expr: |     (mysql_slave_lag_seconds > 30)     AND on (instance)     (predict_linear(       mysql_slave_lag_seconds[5m], 60*2) > 0)    for: 5m    labels:      priority: immediate

Page 65: Monitoring MySQL with Prometheus and Grafana

AlertmanagerWhen prometheus has an alert, it sends it.

Every minute by default, as long as the alert isongoing

Alertmanager, a separated daemon, do therest of the work

Page 66: Monitoring MySQL with Prometheus and Grafana

What is alertmanager doing?Grouping

Inhibition

Silence

Notify

Page 67: Monitoring MySQL with Prometheus and Grafana

grouping alerts5 nodes are down. Do you want 5 email?

group_by: ['alertname', 'cluster', 'service']

Page 68: Monitoring MySQL with Prometheus and Grafana

inhibitionDatacenter is on fire. Do you want to know thatswitches, hosts, services are down?

­ source_match:    severity: 'critical'  target_match:    severity: 'warning'

Page 69: Monitoring MySQL with Prometheus and Grafana

SilenceI upgrade kernel and reboot the service. Do I wantmails during that time?

Page 70: Monitoring MySQL with Prometheus and Grafana

NotificationsAlertmanager can notify you with:

Mails

Webhooks

3rd party services

SMS (sachet: 3rd party integration usingwebhook)

Page 71: Monitoring MySQL with Prometheus and Grafana

Alerting routesYou can send alerts to defined people basedon routes

Everything to logs mailboxCritical alerts

Network to net team SMS

Svc to app team SMS

Warning alertsNetwork to net team mail

Svc to app team mail

Page 72: Monitoring MySQL with Prometheus and Grafana

High Availability: PrometheusHave different prometheis servers with thesame config

They do not talk to each other

All of them fetch the same data

They monitor each other

Page 73: Monitoring MySQL with Prometheus and Grafana

High availability: AlertmanagerHave multiple alertmanagers with the sameconfig

Prometheis send alerts to all alertmanagers

Alert manager talk to each other not to sendthe same notification

Page 74: Monitoring MySQL with Prometheus and Grafana
Page 75: Monitoring MySQL with Prometheus and Grafana

One tool does one job...Prometheus will collect data

Alertmanager will send notifications

Exporters will expose data

Grafana will graph data

Page 76: Monitoring MySQL with Prometheus and Grafana

GrafanaOpen Source (Apache 2.0)

Web app

Specialized in visualization

Pluggable

Multiple datasources: prometheus, graphite,influxdb...

Has an API!

Page 77: Monitoring MySQL with Prometheus and Grafana

History of GrafanaGrafana is a fork of Kibana 3 ; used to be JS-Driven.

Now fully featured, requires a database, multi-projects/users support, etc...

Page 78: Monitoring MySQL with Prometheus and Grafana

Grafana and PrometheusPrometheus shipped its own consoles

Now it recommends Grafana and deprecatedits own consoles

Page 79: Monitoring MySQL with Prometheus and Grafana

Grafana Dashboards

Page 80: Monitoring MySQL with Prometheus and Grafana

Grafana Dashboards

Page 81: Monitoring MySQL with Prometheus and Grafana

Time Picker

Page 82: Monitoring MySQL with Prometheus and Grafana

Configure Prometheus inGrafana

Page 83: Monitoring MySQL with Prometheus and Grafana

Configure Prometheus inGrafana

Page 84: Monitoring MySQL with Prometheus and Grafana

Prometheus Dashboard

Page 85: Monitoring MySQL with Prometheus and Grafana

Multiple Prometheus instancesYou can add multiple prometheus instanceson grafana

You can add dropdown on the top to selectwhich one you want to use

Use case: prometheus HA, local prometheus(with access mode=direct)

Page 86: Monitoring MySQL with Prometheus and Grafana

Creating Grafana DashboardsTakes time

Requires deep knowledge of the tools

Improved over time

Easy to share (json + online library)

Page 87: Monitoring MySQL with Prometheus and Grafana

Percona Grafana DashboardPercona Open Sourced Grafana Dashboards

Covering MySQL, Mongo and Linux monitoring

Part of a bigger picture, PMM, but usablestandalone

Open Source (AGPL!)

https://github.com/percona/grafana-dashboards

Page 88: Monitoring MySQL with Prometheus and Grafana

Installing Percona Graphes

Method 1

Enable File dashboards in Grafana

Clone grafana-dashboards to the configuredlocation (or make a package)

Method 2

Use the Grafana API to upload the JSON's.

Page 89: Monitoring MySQL with Prometheus and Grafana

MySQL SetupYou'll need mysqld_exporter, with a user

MySQL 5.1+

Performance Schema for full set of metrics

mysqld_exporter-collect.binlog_size=true-collect.info_schema.processlist=true`

Page 90: Monitoring MySQL with Prometheus and Grafana

node_exporter setupnode_exporter-collectors.enabled="diskstats,filefd,filesystem,loadavg,meminfo,netdev,stat,time,uname,vmstat"

Page 91: Monitoring MySQL with Prometheus and Grafana

Prometheus (static file)scrape_configs:  ­ job_name: prometheus    static_configs:      ­ targets: ['localhost:9090']        labels:          instance: prometheus

  ­ job_name: linux    static_configs:      ­ targets: ['10.0.98.43:9100']        labels:          instance: db1

  ­ job_name: mysql    static_configs:      ­ targets: ['10.0.98.43:9104']        labels:          instance: db1

Page 92: Monitoring MySQL with Prometheus and Grafana

Dashboards

Page 93: Monitoring MySQL with Prometheus and Grafana

Dashboards

Page 94: Monitoring MySQL with Prometheus and Grafana

Dashboards

Page 95: Monitoring MySQL with Prometheus and Grafana

We don't need all of them?Because Grafana is just viz, you can importonly the one you want (e.g. exclude Mongo)

You can import later any extra dashboard youneed

Page 96: Monitoring MySQL with Prometheus and Grafana

MySQL Overview

Page 97: Monitoring MySQL with Prometheus and Grafana

MySQL Overview

Page 98: Monitoring MySQL with Prometheus and Grafana

MySQL Overview

Page 99: Monitoring MySQL with Prometheus and Grafana

InnoDB

Page 100: Monitoring MySQL with Prometheus and Grafana

InnoDB

Page 101: Monitoring MySQL with Prometheus and Grafana

InnoDB

Page 102: Monitoring MySQL with Prometheus and Grafana

Replication

Page 103: Monitoring MySQL with Prometheus and Grafana

Other grafana featuresMulti tenant

Deployments

Folders for dashboards (comming soon)

Page 104: Monitoring MySQL with Prometheus and Grafana

Jsonnet library to create dashboard

Brand new, still WIP

https://github.com/grafana/grafonnet-lib

Page 105: Monitoring MySQL with Prometheus and Grafana
Page 106: Monitoring MySQL with Prometheus and Grafana

ConclusionsPrometheus and Grafana are first-classmonitoring tools

Totally different approach than other tools

Embeddable into your apps

Percona Dashboards gets your graphes readyin no-time with minimal efforts

Page 107: Monitoring MySQL with Prometheus and Grafana

Julien Pivottoroidelapluie

[email protected]

Inuitshttps://[email protected]

Contact