0x00 监控指标
1.指标介绍
2.指标命名
0x01 指标标签
1.标签介绍
2.标签应用
3.标签使用实例
4.导出器指标说明
4.1 Linux主机监控指标
4.2 Windows主机监控指标
4.3 容器监控指标说明
0x00 监控指标1.指标介绍Q: 什么是Prometheus指标(Metrics)?
答: 在Prometheus中指标是基础它标志着采集或抓取监控项,并且指标的数值是有上升或者下降的变化,从而使得监控某一个时间段中某个监控项的变化分布情况。此时此刻,可能你对"指标"这个词有些混乱,在Prometheus中它根据不同的上下文表示不同的含义,它可以是指标系列、子项或者时间序列, 然而对于Gauge类型数据来说都是代表的一个含义。
基础示例:
#@ 1.时间序列latency_seconds_sum{path="/bar"}#@ 2.子项包含了_sum/_count 时序latency_seconds{path="/bar"}#@ 3.指标系列(只包含指标名称)latency_seconds
2.指标命名描述: 在使用prometheus时常常会给导出器采集的数据配置一个指标名称,所以指标命名对于数据采集或者使用有一定的重要性,即构建指标标准名称可以按照以下规则进行。
指标名称整体结构: library_name_unit_suffix
规则语法:
蛇形命名法: 指标名称的每个部分都应该是小写的并采用下划线进行分隔,如node_uname_info。
名称字母: 指标名称必须以字母开头并且可以跟着任意数目的字母、数字和下划线,如[a-zA-Z_:][a-zA-Z0-9_:]正则表达式
# - 正常示例node_cpu_core_throttles_total# - 在测控指标名称时不建议使用冒号(__),因为它是prometheus内部使用而保留的。bottomk(__rank_number__, __input_vector__)__meta_kubernetes_node_address_InternalIP="192.168.12.226"# - 在测控指标名称时不建议使用冒号(:),因为它是为用户在记录规则中使用而保留的。
指标名称: 指标所代表的含义应该是名字本身,需要实现通过指标名称就能快速知道该值得含义,并且一个名称最好通过下划线进行分割并且通常是从左到右含义越具体越好,如http_request_duration_seconds_sum,注意不要再指标名称中添加标签名(可能会导致聚合查询是出错)。
指标后缀: 常规得_total、_count_,__sum,__bucket等后缀是留给counter和summary和histogram指标类型使用得。所以除了在counter类型指标上始终具有_total后缀之外不要将这些后缀放在指标名称得末尾如http_request_duration_seconds_sum。
指标单位: 为了避免某个指标究竟是以秒作为单位还是以毫秒为单位应该在命名中带上单位,如container_cpu_system_seconds_total。(Prometheus本身使用秒、毫秒、微秒和纳秒作为指标名称)
库: 指标名称是个有效得全局命名空间它可以避免库之间得冲突并指出指标出处,如prometheus_http_requests_total或者go_gc_duration_seconds表示指标通过prometheus以及go库中得到,注意不能将应用名称作为应用中所有指标得前缀。
0x01 指标标签1.标签介绍描述: Label能够让我们知道监控项目的来源端口方法等等, 同时label也为prometheus提供了丰富的聚合和查询等功能。
标签分类一类是测控标签,另一类是目标标签,在使用PromSQL查询时没有什么区别但是为了更好有效的使用标签,区分二者就更好了。
测控标签: 表面意思来自测控设置的标签中,可以在应用程序或者库内部模块中设置。例如收到的HTTP请求类比、以及访问的数据库。
目标标签: 它是确定了特定的监控目标即Prometheus抓取指标数据的目标,其标签作为抓取指标过程中的一部分。
标签模式描述: Prometheus 支持64位的浮点数作为时序数据,不支持如字符串等其它数据类型,但是标签值是字符串类型我们可以将其使用某些特征信息展示中,并可以采用PromQL表达式。
Tips : 你可以为指标设置一个或多个标签并且标签是无序的,所以你可以安装任何给定标签聚合而忽略其它标签,甚至一次性聚合多个标签。
Tips :注意保留标签(以下划线开头)和__name__(它实际上是表达式up的语法糖格式)不要进行使用应该避免此类命名。
2.标签应用描述: 在 Prometheus 中标签往往有以下几种应用场景:
在 Prometheus 采集时进行数据指标的分类,并按照一定的规则保留或者丢弃采集的数据。
在 Prometheus UI 界面上采用PromQL表达式设置不同标签进行监控项的找寻,并且可以进行聚合以及求取平均值。
在 Prometheus 中进行了静态设置采集节点时使用。
[{"targets": ["127.0.0.1:9100"],"labels": {"instance": "test","idc": "beijing"}}]
在 alertmanager 警报时按照设置的标签名称和值进行判断并执行相对应的报警通知。
其中最常用的就是在网站的Http Rquest请求统计中需要记录http路径访问的次数, 为了解决多种同类型、同工作的指标的数量,我们通常采用标签来进行处理, 例如http_requests_total{path="/login"}
Tips: 汇总指标的分位数打破了有关总和或平均值的规则,因此你无法对分为数进行数学运算。
Tips: job 和 instance 是目标始终以及默认具有的两个标签,job默认来自job_name配置选项。
3.标签使用实例描述: 在Prometheus中进行自动发现设置时利用relabel_configs来保留或者丢弃匹配到的标签指标。
# 函数用法label_replace() label_join()# 基础示例:支持 replace、labelmap、keep、drop 等许多操作relabel_configs- source_labels: [] # 使用默认值简洁的删除team标签。 target_label: team- source_labels: [__mata_consul_tag] # 使用prod、staging和dev标记填充env标签。 regex: '.*,(prod|staging|dev),.*' target_lable: env# - 1.简单匹配替换- source_labels: [] # 为所有目标添加上k8s_cluster=3标签 replacement: 3 target_label: "k8s_cluster" action: replace- source_labels: [team] # 重置标签将team=monitoring替换为team=monitor regex: monitoring replacement: monitor target_label: team action: replace# - 2.匹配到该标签时保留该目标 ( 正 则 匹 配 )- source_labels: [__meta_kubernetes_endpoints_label_app_kubernetes_io_name] action: keep regex: ^(kube-state-metrics)$- source_labels: [team] regex: dev|testing|monitor # Tips: 使用 | 管道符号替代逐个操作。 action: keep - source_labels: [__meta_consul_tag] # 仅仅包含prod标签的Consul服务。 regex: '.*,prod,.*' action: keep# - 3.匹配到该标签时丢弃 ( 正 则 匹 配 )- source_labels: [__meta_kubernetes_endpoints_label_app_kubernetes_io_name] regex: ^(kube-state-metrics)$ action: drop- source_labels: [job, team] # 不监控monitor团队的prom的相关任务目标 (使用多个source标签) regex: prom;monitor action: drop# - 4.正则替换 ( 将匹配到的源数据进行替换到目标标签之中 )- source_label: [__meta_consul_address] # 使用 consul的IP和9100端口作为地址。 regex: '(.*)' replacement: '${1}:9100' target_label: __address__- source_label: [__meta_consul_node] # 在实例标签中使用节点名称。 regex: '(.*)' replacement: '${1}:9100' target_label: instance- source_labels: [__address__,__meta_kubernetes_pod_annotation_prometheus_io_port] action: replace regex: ([^:]+)(?::\d+)?;(\d+) replacement: $1:$2 target_label: __address__- source_labels: [team] # 使用replace重置标签操作将team标签中的ing去除 (利用了元组的方式) regex: '(.*)ing' replacement: '${1}' target_label: team action: replace- source_labels: [] # 使用替换重置操作删除team标签 regex: '(.*)' replacement: '${1}' target_label: team action: replace# - 5.匹配正则表达式`所有的标签名称然后将匹配标签的值复制到`replacement` - action: labelmap regex: __meta_kubernetes_pod_label_(.+) replacement: '${1}' action: labelmapTips : 指标重新标记metric-relabel_configs作用于从目标中抓取时间序列的重新标记,前面我们看到的keep/drop/replace/labelmap还有labeldrop/labelkeep(适用于标签名称而不是标签值)等操作都可以在metric_relabel_configs中使用。
metric-relabel_configs:# -1. 删除代价大的指标- source_labels: [__name__] regex: http_request_size_bytes action: drop# - 2.删除直方图桶以减少数据大小- source_labels: [__name__, le] regex: 'prometheus_tsdb_compaction_duration_seconds_bucket;(4|32|256)' action: drop# - 3.使用labeldrop删除具有给定前缀的所有标签- regex: 'container_label_.*' action: labledrop-
4.导出器指标说明4.1 Linux主机监控指标项目地址: https://github.com/prometheus/node_exporter
默认开启的功能
名称说明系统arp从 proc/net/arp 中收集 ARP 统计信息Linuxconntrack从 proc/sys/net/netfilter/ 中收集 conntrack 统计信息Linuxcpu收集 cpu 统计信息Darwin, Dragonfly, FreeBSD, Linuxdiskstats从 proc/diskstats 中收集磁盘 I/O 统计信息Linuxedac错误检测与纠正统计信息Linuxentropy可用内核熵信息Linuxexecexecution 统计信息Dragonfly, FreeBSDfilefd从 proc/sys/fs/file-nr 中收集文件描述符统计信息Linuxfilesystem文件系统统计信息,例如磁盘已使用空间Darwin, Dragonfly, FreeBSD, Linux, OpenBSDhwmon从 sys/class/hwmon/ 中收集监控器或传感器数据信息Linuxinfiniband从 InfiniBand 配置中收集网络统计信息Linuxloadavg收集系统负载信息Darwin, Dragonfly, FreeBSD, Linux, NetBSD, OpenBSD, Solarismdadm从 proc/mdstat 中获取设备统计信息Linuxmeminfo内存统计信息Darwin, Dragonfly, FreeBSD, Linuxnetdev网口流量统计信息,单位 bytesDarwin, Dragonfly, FreeBSD, Linux, OpenBSDnetstat从 proc/net/netstat 收集网络统计数据,等同于 netstat -sLinuxsockstat从 proc/net/sockstat 中收集 socket 统计信息Linuxstat从 proc/stat 中收集各种统计信息,包含系统启动时间,forks, 中断等Linuxtextfile通过 –collector.textfile.directory 参数指定本地文本收集路径,收集文本信息anytime系统当前时间anyuname通过 uname 系统调用, 获取系统信息anyvmstat从 proc/vmstat 中收集统计信息Linuxwifi收集 wifi 设备相关统计数据Linuxxfs收集 xfs 运行时统计信息Linux (kernel 4.4+)zfs收集 zfs 性能统计信息Linux
默认关闭的功能
名称说明系统bonding收集系统配置以及激活的绑定网卡数量Linuxbuddyinfo从 proc/buddyinfo 中收集内存碎片统计信息Linuxdevstat收集设备统计信息Dragonfly, FreeBSDdrbd收集远程镜像块设备(DRBD)统计信息Linuxinterrupts收集更具体的中断统计信息Linux,OpenBSDipvs从 proc/net/ip_vs 中收集 IPVS 状态信息,从 proc/net/ip_vs_stats 获取统计信息Linuxksmd从 sys/kernel/mm/ksm 中获取内核和系统统计信息Linuxlogind从 logind 中收集会话统计信息Linuxmeminfo_numa从 proc/meminfo_numa 中收集内存统计信息Linuxmountstats从 proc/self/mountstat 中收集文件系统统计信息,包括 NFS 客户端统计信息Linuxnfs从 proc/net/rpc/nfs 中收集 NFS 统计信息,等同于 nfsstat -cLinuxqdisc收集队列推定统计信息Linuxrunit收集 runit 状态信息anysupervisord收集 supervisord 状态信息anysystemd从 systemd 中收集设备系统状态信息Linuxtcpstat从 proc/net/tcp 和 proc/net/tcp6 收集 TCP 连接状态信息LinuxwifiExposes WiFi device and station statistics.LinuxzoneinfoExposes NUMA memory zone metrics.Linux
Tips:简单的指标说明:
node_boot_time:系统启动时间node_cpu:系统CUP使用情况node_disk_*:磁盘ionode_filesystem_*:文件系统使用量node_load1:系统负载node_memory_*:系统内存使用量node_network_*:网络宽带node_time:当前系统时间go_*:node exporter中go相关指标*process_ :node exporter自身进程相关指标
4.2 Windows主机监控指标指标参考: https://github.com/prometheus-community/windows_exporter#collectors
NameDescriptionEnabled by defaultadActive Directory Domain ServicesadfsActive Directory Federation ServicescacheCache metricscpuCPU usage✓cpu_infoCPU Informationcs"Computer System" metrics (system properties, num cpus/total memory)✓containerContainer metricsdfsrDFSR metricsdhcpDHCP ServerdnsDNS ServerexchangeExchange metricsfsrmquotaMicrosoft File Server Resource Manager (FSRM) Quotas collectorhypervHyper-V hostsiisIIS sites and applicationslogical_diskLogical disks, disk I/O✓logonUser logon sessionsmemoryMemory usage metricsmsmqMSMQ queuesmssqlSQL Server Performance Objects metricsnetframework_clrexceptions.NET Framework CLR Exceptionsnetframework_clrinterop.NET Framework Interop Metricsnetframework_clrjit.NET Framework JIT metricsnetframework_clrloading.NET Framework CLR Loading metricsnetframework_clrlocksandthreads.NET Framework locks and metrics threadsnetframework_clrmemory.NET Framework Memory metricsnetframework_clrremoting.NET Framework Remoting metricsnetframework_clrsecurity.NET Framework Security Check metricsnetNetwork interface I/O✓osOS metrics (memory, processes, users)✓processPer-process metricsremote_fxRemoteFX protocol (RDP) metricsserviceService state metrics✓smtpIIS SMTP ServersystemSystem calls✓tcpTCP connectionstimeWindows Time ServicethermalzoneThermal informationterminal_servicesTerminal services (RDS)textfileRead prometheus metrics from a text file✓vmwarePerformance counters installed by the Vmware Guest agent
4.3 容器监控指标说明项目地址: https://github.com/google/cadvisor指标参考: https://github.com/google/cadvisor/blob/master/docs/storage/prometheus.md
cAdvisor 常用监控指标
指标名称类型含义container_cpu_load_average_10sgauge过去10秒容器CPU的平均负载container_cpu_usage_seconds_totalcounter容器在每个CPU内核上的累积占用时间 (单位:秒)container_cpu_system_seconds_totalcounterSystem CPU累积占用时间(单位:秒)container_cpu_user_seconds_totalcounterUser CPU累积占用时间(单位:秒)container_fs_usage_bytesgauge容器中文件系统的使用量(单位:字节)container_fs_limit_bytesgauge容器可以使用的文件系统总量(单位:字节)container_fs_reads_bytes_totalcounter容器累积读取数据的总量(单位:字节)container_fs_writes_bytes_totalcounter容器累积写入数据的总量(单位:字节)container_memory_max_usage_bytesgauge容器的最大内存使用量(单位:字节)container_memory_usage_bytesgauge容器当前的内存使用量(单位:字节container_spec_memory_limit_bytesgauge容器的内存使用量限制machine_memory_bytesgauge当前主机的内存总量container_network_receive_bytes_totalcounter容器网络累积接收数据总量(单位:字节)container_network_transmit_bytes_totalcounter容器网络累积传输数据总量(单位:字节)
# 关注 WeiyiGeek 微信公众号 #
历史文章:
8-Kubernetes入门基础之存储Volume介绍(一)
9-Kubernetes入门基础之存储Volume介绍(二)
10-Kubernetes入门基础之调度器与亲和性介绍
11-Kubernetes入门基础之集群安全介绍