prometheus介绍与gp表膨胀监控

 

一、什么是普罗米修斯?

1.Prometheus是一个最初在SoundCloud上构建的开源系统监视和警报工具包 自2012年成立以来,许多公司和组织都采用了Prometheus,该项目拥有一个非常活跃的开发人员和用户社区它现在是一个独立的开源项目,可以独立于任何公司进行维护。为了强调这一点,并澄清项目的治理结构,Prometheus 于2016年加入 云计算本地计算基金会,作为继Kubernetes之后的第二个托管项目

2.普罗米修斯的主要特点是:

  • 具有由度量名称和键/值对标识的时间序列数据的多维数据模型
  • PromQL,一种灵活的查询语言, 可以利用这一维度
  • 不依赖分布式存储; 单个服务器节点是自治的
  • 时间序列集合通过HTTP上的拉模型发生
  • 推送时间序列通过中间网关支持
  • 通过服务发现或静态配置发现目标
  • 多种图形和仪表板支持模式

3.核心架构图

二、Prometheus Python客户端

三步演示:

 

:安装客户端:

pip install prometheus_client

:将以下内容粘贴到Python解释器中:

from prometheus_client import start_http_server, Summary
import random
import time

# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')

# Decorate function with metric.
@REQUEST_TIME.time()
def process_request(t):
    """A dummy function that takes some time."""
    time.sleep(t)

if __name__ == '__main__':
    # Start up the server to expose the metrics.
    start_http_server(8000)
    # Generate some requests.
    while True:
        process_request(random.random())

三:访问http:// localhost:8000 /查看指标。

提供四种类型的度量:Counter, Gauge, Summary and Histogram. 

Counters:一直上升, 并且随着进程重启而重置。

Gauge: 可以上升也可以下降。

from prometheus_client import Gauge
g = Gauge('my_inprogress_requests', 'Description of gauge')
g.inc()      # 上升1
g.dec(10)    # 减少10
g.set(4.2)   # 设置为4.2

然后利用该程序监控

a=[]
def monitor():
conn = Connection(Config.HOST, Config.PASSWD, Config.USER, Config.DB).connect()
cur = conn.cursor()
cur.execute("SELECT datname FROM pg_database")
rows = cur.fetchall()
for i in rows:
if i[0] != 'template0':
a.append(i[0])
conn.close()

for qq in a:
conn2 = Connection(Config.HOST, Config.PASSWD, Config.USER, qq).connect()
cur = conn2.cursor()
for value in Config.MONITOR_DICT.values():
cur.execute(value)
rows2 = cur.fetchall()
for key in Config.MONITOR_DICT.keys():
if rows2 :
app_database_maintain.labels(event=qq+"_"+key).set(1)
else:
app_database_maintain.labels(event=qq+"_"+key).set(0)
conn2.close()
def main():
start_http_server(6001)
logging.info("gp monitor start app.")

if __name__ == '__main__':
main()

def sleeptime(hour,min,sec):
return hour*3600 + min*60 + sec;
second = sleeptime(0,0,3600);
while 1==1:
time.sleep(second);
monitor()

猜你喜欢

转载自www.cnblogs.com/languid/p/10886619.html