Fork me on GitHub

Prometheus + consul + grafana 监控体系搭建1

Consul 搭建(忽略,网上很多例子)

Consul 用 Golang 实现,因此具有天然可移植性(支持 Linux、windows 和 Mac OS X ),它的安装包仅包含一个可执行文件,方便部署,与 Docker 等轻量级容器可无缝配合。

consul 启动方式:

nohup  ./consul agent -server -ui -bootstrap-expect 1 -data-dir /data/consul/ -client=0.0.0.0 &

查看节点信息:

./consul operator raft list-peers

浏览器中访问http://localhost:8500即可看到consul管理界面,如下图所示:

 

可以通过./consul command --help查看具体某个指令支持的参数.比如./consul agent --help

 

步骤二:写入源数据至consul 中(从cmdb 写入数据到consul中, 打标签动作)

#!/usr/bin/env python
# enconding:utf-8
import requests
import json

token = requests.get("http://cmdb.patrcik.com/api/auth/login?username=....&password=....").json()['info']['data']['token']
servers = "http://cmdb.patrick.com/api/servers/list?token=%s" % token
r = requests.get(servers).json()['info']['data']
#排除指定IP 端口非9100 node_expoer
exclude = ["172.16.0.21", "172.16.0.20",]
for data in r:
    try:
        # print(data)
        hostname = data.get('hostname', '')
        ip = str(data.get('ip', ''))
        appCodeResponse = requests.get("http://cmdb.parick.com/search/ip?ip={}".format(ip)).json()["data"]
        appCode = appCodeResponse["appCode"] if appCodeResponse.has_key("appCode") else "none"
        host_id = data.get('host_id')
        dc = data.get('dc', 'UC')
        application = data.get('module', 0)
        type = data.get('type', 'tomcat')
        # print(data.get('dept',''))
        try:
            group = data.get('dept', 'missfresh-com').split('-')[1]
        except:
            group = data.get('dept', 'missfresh-com')
        depart = data.get('dept', 'missfresh-com').split('-')[0]
        # if data.get('state') == "RUNNING":
        if ip in exclude:
            port = 9102
        else:
            port = 9100
        body = {'id': host_id,
                'name': hostname,
                'address': ip,
                'tags': [depart, group, application, type, dc, appCode],
                'port': port,
                'checks': [{'http': 'http://{}:{}'.format(ip, port), 'interval': '15s'}]
                }
        r = requests.put('http://127.0.0.1:8500/v1/agent/service/register', data=json.dumps(body))
        # print body, r.status_code
    except Exception, e:
        print(e.message)
        pass

 

注意(对于不通的机器做下线剔除操作):

import requests
import json
import time
from urllib import quote
m = quote('up != 1')
r = requests.get('http://prom.patrick.com/api/v1/query?query=%s' % m).json()
for metric in r['data']['result']:
    if metric['metric']['id'] != 'consul':
        service = metric['metric']['id']
        r = requests.put('http://127.0.0.1:8500/v1/agent/service/deregister/%s' % service)

 

posted @ 2020-05-13 19:22  patrick-py  阅读(891)  评论(0编辑  收藏  举报