目录

Grafana

grafana

1 概述

1.1 安装

安装以写好ansible-playbooks后续更新到github地址.

1.2 配置

1.2.1 LDAP

#grafana.ini
[auth.ldap]
enabled = true
config_file = /etc/grafana/ldap.toml
allow_sign_up = true

#ldap.toml
 [[servers]]
 host = "xxxx.com"
 port = 389
 use_ssl = false
 start_tls = false
 ssl_skip_verify = false

 bind_dn = "CN=xxx,OU=xxx,OU=xxx,DC=xxx,DC=xxx,DC=com"

 search_filter = "(sAMAccountName=%s)"

 search_base_dns = ["ou=xxx,dc=xxx,dc=xxx,dc=com"]

 [servers.attributes]
 name = "givenName"
 surname = "sn"
 username = "sAMAccountName"
 member_of = "memberOf"
 email =  "email"

 [[servers.group_mappings]]
 group_dn = "xxx"
 org_role = "Admin"

[[servers.group_mappings]]
group_dn = "*"
org_role = "Editor"

[[servers.group_mappings]]
group_dn = "xxxx"
org_role = "Editor"
org_id = 1

2 API

2.2.1 api导出dashboard json文件

背景,因为配置错误,强制保存,导致dashboard页面空白,通过api导出json,重建页面

  • 配置-〉API keys-〉add【viewer】->生成 String Key
  • 参照http api http://docs.grafana.org/http_api/dashboard
    Get dashboard by uid
    GET /api/dashboards/uid/:uid
    
    Will return the dashboard given the dashboard unique identifier (uid).
    
    Example Request:
    
    GET /api/dashboards/uid/cIBgcSjkk HTTP/1.1
    Accept: application/json
    Content-Type: application/json
    Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk

接着执行命令:

 curl -H "Authorization: Bearer xxxxxxxxxx==" http://xxxxxxx.com/api/dashboards/uid/p2onKGOmk
 #返回json string,掐头,保留{"annotations":xxx}相关信息. 手动回滚,再从主页import这段信息
 

2.2.2 首页修改

新建一个page 自定义内容后,拷贝json内容,覆盖此文件

/usr/share/grafana/public/dashboards/home.json

3 datasource

3.1 InfluxDB

3.2 Prometheus

3.3 ES

4 sqlite3 to mysql

  • sqlite3-to-mysql
#!/usr/bin/env sh

if test -z "$VARCHAR"
then
	VARCHAR="255"
fi

sed \
-e '/PRAGMA.*;/ d' \
-e '/BEGIN TRANSACTION.*/ d' \
-e '/COMMIT;/ d' \
-e '/.*sqlite_sequence.*;/d' \
-e "s/ varchar/ varchar($VARCHAR)/g" \
-e 's/"/`/g' \
-e 's/CREATE TABLE \(`\w\+`\)/DROP TABLE IF EXISTS \1;\nCREATE TABLE \1/' \
-e 's/\(CREATE TABLE.*\)\(PRIMARY KEY\) \(AUTOINCREMENT\)\(.*\)\();\)/\1AUTO_INCREMENT\4, PRIMARY KEY(id)\5/' \
-e "s/'t'/1/g" \
-e "s/'f'/0/g" \
$1
  • 导出sqlite3数据
#默认目录sqlite3
/var/lib/grafana/grafana.db

#导出sql数据
#sqlite3 mySqliteDatabase.db .dump > myTemporarySQLFile.sql
#or:
>sqlite3 grafana.db
SQLite version 3.7.17 2013-05-20 00:56:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .output grafana.sql
sqlite> .dump
sqlite> .exit

> ll grafana.sql
-rw-r--r--. 1 root root 55072744 7月  23 14:19 grafana.sql

>wget https://raw.githubusercontent.com/motherapp/sqlite_sql_parser/master/parse_sqlite_sql.py
>chmod 755 sqlite3-to-mysql
>python parse_sqlite_sql.py   grafana.sql  

#执行完生成两个文件
grafana.sql.data.sql
grafana.sql.schema.sql

#新的grafana配置mysql后启动,会自动建表结构。然后再source grafana.sql.data.sql
因为生成的schema.sql建表失败,先不研究py的代码了,以后再看
  • mysql数据导入
>CREATE DATABASE grafana DEFAULT CHARACTER SET utf8;
>use grafana;
>source  ./grafana.sql.data.sql;

5 grafana HA

#nginx 安装忽略,附上upstream配置,使用ip_hash
upstream gf {
       ip_hash;
       server xxx:3001;
       server xxx:3001;
}

server {
        listen 80;
        server_name monitor.xxx.com;

	location / {
		proxy_pass http://gf/;
	}
}