Prometheus之PromSQL

1.  基础查询

    1.  表达式:<metric name>{label=value}

    2.  匹配模式

        label=value

        label!=value

        label=~regx        

2.  时间查询

    <metric name>{label=value} [5m]

    时间范围选择器

      s

      m

      h

      d

      w

      y

3.  聚合查询

    • sum (求和)
    • min (最小值)
    • max (最大值)
    • avg (平均值)
    • stddev (标准差)
    • stdvar (标准方差)
    • count (计数)
    • count_values (对value进行计数)
    • bottomk (后n条时序)
    • topk (前n条时序)
    • quantile (分位数)

    sum(http_requests_total{})    

4.  操作符

    1.  数学运算符      

    • + (加法)
    • - (减法)
    • * (乘法)
    • / (除法)
    • % (求余)
    • ^ (幂运算)

    2.  比较运算符  

    • == (相等)
    • != (不相等)
    • > (大于)
    • < (小于)
    • >= (大于等于)
    • <= (小于等于)

    3.  逻辑运算符

        and、or、unless(排除)     

5.  内置函数

    ceil 函数:ceil函数会将返回结果的值向上取整数。

      ceil(avg(promhttp_metric_handler_requests_total{code="200"}))

    floor 函数:floor 函数与ceil相反,将会进行向下取整的操作。

      floor(avg(promhttp_metric_handler_requests_total{code="200"}))

    rate用于取某个时间区间内每秒的平均增量数,它会以该时间区间内的所有数据点进行统计。

      rate(http_requests_total{}[1m])

    irate函数是通过时间区间中最后两个样本数据来计算区间向量的增长速率,从而避免范围内的平均值拉低峰值的情况。

      irate(http_requests_total{}[1m])

6.  常用监控项

    1.  CPU监控

        CPU使用率

        (1 -  sum(increase(node_cpu_seconds_total{mode="idle"}[1m])) by (instance)   /   sum(increase(node_cpu_seconds_total[1m])) by (instance))  * 100

    2.  memory监控

        内存使用率  

        (1-((node_memory_Buffers_bytes + node_memory_Cached_bytes + node_memory_MemFree_bytes) / node_memory_MemTotal_bytes )) *100                

    3.  硬盘监控

        可用磁盘空间使用率小于0.2 node_filesystem_free_bytes / node_filesystem_size_bytes <0.2

        磁盘IO  ((rate(node_disk_read_bytes_total[1m])+rate(node_disk_read_bytes_total[1m]))/1024/1024) >0

    4.  网络监控

        网络IO  rate(node_network_transimit_bytes[1m]) /1024 /1024
    5.  TCP连接数监控

        pushgateway+脚本     

#!/bin/bash
instance_name=`hostname -f | cut -d'.' -f1`

if [ $instance_name == "localhost" ];then
echo "Must FQDN hostname"
exit 1
fi
# For waitting connections
label="count_netstat_wait_connections"
count_netstat_wait_connections=`netstat -an | grep -i wait | wc -l`
#定义新的数值 netstat中 wait 的数量
echo "$label : $count_netstat_wait_connections"
echo "$label $count_netstat_wait_connections" | curl --data-binary @- http://172.16.1.237:9091/metrics/job/pushgateway/instance/$instance_name

        定时任务      

#ceshi
* * * * * /bin/bash /usr/local/node_exporter/pushgateway.sh
* * * * * sleep 10;/bin/bash /usr/local/node_exporter/pushgateway.sh
* * * * * sleep 20;/bin/bash /usr/local/node_exporter/pushgateway.sh
* * * * * sleep 30;/bin/bash /usr/local/node_exporter/pushgateway.sh
* * * * * sleep 40;/bin/bash /usr/local/node_exporter/pushgateway.sh
* * * * * sleep 50;/bin/bash /usr/local/node_exporter/pushgateway.sh

    6.  文件描述符监控

        (node_filefd_allocated /node_filefd_maximum)*100

    7.  网络延时监控

        pushgateway+shell  

lostpk=`timeout 5 ping -q -A -s 500 -W 1000 -c 100 www.baidu.com|grep transmitted | awk '{print $6}'`
rrt=`timeout 5 ping -q -A -s 500 -W 1000 -c 100 www.baidu.com| grep transmitted | awk '{print $10}'` 
value_lostpk=`echo $lostpk | sed "s/%//g"`
value_rrt=`echo $rrt | sed "s/ms//g" `
echo "lostpk_"$instance_name"_to_prometheus : $value_lostpk"
echo "lostpk_"$instance_name"_to_prometheus $value_lostpk" | curl --data-binary @- http://172.16.1.237:9091/metrics/job/pushgateway/instance/localhost:9092
echo "rrt_"$instance_name"_to_prometheus : $value_rrt"
echo "rrt_"$instance_name"_to_prometheus $value_rrt" | curl -- data-binary @- http://172.16.1.237:9091/metrics/job/pushgateway/instance/localhost:9092

        定时任务       

* * * * * /bin/bash /usr/local/node_exporter/ping.sh
* * * * * sleep 10;/bin/bash /usr/local/node_exporter/ping.sh
* * * * * sleep 20;/bin/bash /usr/local/node_exporter/ping.sh
* * * * * sleep 30;/bin/bash /usr/local/node_exporter/ping.sh
* * * * * sleep 40;/bin/bash /usr/local/node_exporter/ping.sh
* * * * * sleep 50;/bin/bash /usr/local/node_exporter/ping.sh     
posted @ 2023-05-11 18:02  奋斗史  阅读(663)  评论(0)    收藏  举报