zoukankan      html  css  js  c++  java
  • prometheus.(6).数据采集脚本开发

    数据采集脚本开发

    作者声明:本博客内容是作者在学习以及搭建过程中积累的内容,内容采自网络中各位老师的优秀博客以及视频,并根据作者本人的理解加以修改(由于工作以及学习中东拼西凑,如何造成无法提供原链接,在此抱歉!!!)

    作者再次声明:作者只是一个很抠脚的IT工作者,希望可以跟那些提供原创的老师们学习

    pushgateway的介绍

    pushgateway 是另⼀种采⽤被动推送的方式(而不是exporter主动获取)获取监控数据的prometheus插件

    它是可以单独运行在任何节点上的插件(并不⼀定要在被监控客户端)然后通用户自定义开发脚本把需要监控的数据发送给pushgateway,再由pushgateway把数据推送prometheus server

    https://www.bilibili.com/video/av62475074?p=19

    pushgatway的安装和运行和配置

    pushgateway跟prometheus和exporter⼀样,下载地址

    https://prometheus.io/download/#pushgateway

    解压后,直接运行github的官方地址

    https://github.com/prometheus/pushgateway

    使用daemonize方式将pushgateway放入后台运行

    - job_name: 'pushgateway'
      static_configs:
      - targets: [‘server1:9091','server2:9091’] #此处开启了2个
    

    在prometheus.yml配置文件中, 单独定义⼀个job,然后target指向到pushgateway运行所在的机器名和

    pushgateway运行的端口

    自定义编写脚本的方法发送pushgateway采集

    pushgateway本身是没有任何抓取监控数据的功能的它只是被动的等待推送过来

    采集数据脚本

    cat /usr/local/node_exporter/node_exporter_shell.sh

    #!/bin/bash 
    
    instance_name=`hostname -f | cut -d'.' -f1`  #本机机器名变量⽤于之后的标签 
    
    if [ $instance_name == "localhost" ];then  #要求机器名不能是localhost不然标签就没有区分了 
    echo "Must FQDN hostname" 
    exit 1 
    fi
    
    # For waitting connections
    # 定⼀个新的key 
    label="count_netstat_wait_connections"  
    #定义⼀个新的数值netstat中wait的数量 
    count_netstat_wait_connections=`netstat -an | grep -i wait | wc -l` 
    
    echo "$label:$count_netstat_wait_connections" 
    echo "$label $count_netstat_wait_connections"  | curl --data-binary @- http://prometheus.server.com:9091/metrics/job/pushgateway1/instance/$instance_name
    

    这个URL地址中分成如下三个部分:

    http://prometheus.server.com:9091/metrics/job/pushgateway1 
    

    这里是URL的主location

    job/pushgateway1
    

    这里是第二部分第一个标签: 推送到哪⼀个prometheus.yml

    定义的 job里

    {instance=“server01"}

    instance/$instance_name 
    

    这里是第二个标签推送后显示的机器名是什么

    周期采集数据crontab

    crontab默认只能最短一分钟的间隔如果希望小于⼀分钟的间隔15s

    我们使用如下的方法 sleep 10 sleep 20

    使用pushgateway的优缺点

    大米之前就跟大家说过pushgateway这种自定义的采集方式非常的快速而且极其灵活几乎不收到任何约束,其实我个人还是非常希望使用pushgateway来获取监控数据的各类的exporters虽然玲琅满目而且默认提供的数据很多了已经⼀般情况下我在企业中只安装 node_exporter 和 DB_exporter这两个。其他种类的监控数据我倾向于全使用pushgateway的方式采集 (要的就是快速~ 灵活~)不过言归正传习惯并不代表正确性pushgateway虽然灵活但是也是存在一些短板的。

    1. pushgateway 会形成⼀个单点瓶颈,假如好多个脚本同时发送给 ⼀个pushgateway的进程,如果这个进程没了,那么监控数据也就没了。
    2. pushgateway 并不能对发送过来的 脚本采集数据进行更智能的判断假如脚本中间采集出问题了,那么有问题的数据 pushgateway⼀样照单全收。

    发送给prometheus虽然有这么两个所谓的缺点但是实际上通过我2年多的使用

    对于第一条缺点,其实只要服务器不当机那么基本上 pushgateway运行还是很稳定的就算有太多的脚本 同时都发送给⼀个pushgateway 其实也只是接收的速度会变慢 丢数据没有遇到过 (有的时候我甚至觉得比exporters还稳定 exporters倒是有时候会真的就当机或者丢失数据因为exporters开发比较复杂越简单的东西往往越稳定)

    对于第二条缺点,其实只要我们在写脚本的时候细心一些别出错(这也是为什么我推荐就用 bash 写因为简单不容易出错 , python我可不敢打包票) 那么监控数据也不会有错误。

    yaml安装pushgateway

    pushgateway.yaml文件

    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      annotations:
        prometheus.io/scrape: 'true'
      name: pushgateway
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: pushgateway
      template:
        metadata:
          name: pushgateway
          labels:
            app: pushgateway
            visualize: "true"
            run: pushgateway
        spec:
          containers:
          - name: pushgateway
            image: prom/pushgateway
            ports:
            - name: web
              containerPort: 9091
    ---
    apiVersion: v1
    kind: Service
    metadata:
      labels:
        name: pushgateway
        visualize: "true"
        app: pushgateway
      name: pushgateway
    spec:
      selector:
        app: pushgateway
      type: NodePort
      ports:
      - name: scrape
        protocol: TCP
        port: 9091
        nodePort: 30901
    

    prometheus添加配置

      - job_name: 'pushgateway'
        static_configs:
          - targets: ['192.168.2.6:30901']
    

    smokeping

    smartping

    #!/bin/bash
    #Author:Mr.Ding
    #Created Time:2018-08-26 07:23:44
    #Name:ping.sh
    #Description:
     
    shibai="/root/scripts/shell/ping_shibai.txt"
    yanchigao="/root/scripts/shell/yanchigao.txt"
    
    . /etc/init.d/functions
     
    for i in `cat IP_list`
    do
    ping=`ping -c 1 $i|grep loss|awk '{print $6}'|awk -F "%" '{print $1}'`
    Avg="$i 平均延迟(ms):`ping $i -c 3 |grep avg | gawk -F / '{print $5}'`"
    num=`ping $i -c 3 |grep avg | gawk -F / '{print $5}'|gawk -F . '{print $1}'`
    if [ $ping -eq 100  ];then
        action " ping $i faild"  /bin/false >>$shibai
        echo "$Avg"
    else
        action " ping $i ok"    /bin/true
        echo "$Avg"
        if [ $num -ge 1 ];then
            echo "$i延迟为:$num(ms)" >>$yanchigao
        fi
    fi
     done
     
    cat $shibai
    cat $yancigao
    rm -f $shibai
    rm -f $yanchigao
    
  • 相关阅读:
    java中的 equals 与 ==
    String类的内存分配
    SVN用命令行更换本地副本IP地址
    npoi 设置单元格格式
    net core 微服务框架 Viper 调用链路追踪
    打不死的小强 .net core 微服务 快速开发框架 Viper 限流
    net core 微服务 快速开发框架 Viper 初体验20201017
    Anno 框架 增加缓存、限流策略、事件总线、支持 thrift grpc 作为底层传输
    net core 微服务 快速开发框架
    Viper 微服务框架 编写一个hello world 插件02
  • 原文地址:https://www.cnblogs.com/orange-lsc/p/12825581.html
Copyright © 2011-2022 走看看