備份prometheus數據:
修改 prometheus 的啟動參數
--storage.tsdb.path 和 --web.enable-admin-api,
之后調用api:
curl -XPOST http://prometheusip:port/api/v1/admin/tsdb/snapshot,
可以將數據備份到 --storage.tsdb.path 指定的目錄下的 snapshots 子目錄中,
利用api方式制作成 snapshot 后,還原時將 snapshot 中的文件覆蓋到
--storage.tsdb.path 指定的目錄下,重啟 prometheus 即可
prometheus_http_requests_total{code="200"} #表示查詢metric名稱為 prometheus_http_requests_total,code 為 "200" 的數據
prometheus_http_requests_total{code!="200"} #表示查詢 code 不為 "200" 的數據
prometheus_http_requests_total{code=~"2.."} #表示查詢 code 為 "2xx" 的數據
prometheus_http_requests_total{code!~"2.."} #表示查詢 code 不為 "2xx" 的數據
注意,運算符也有優先級,它們遵從(^)> (*, /, %) > (+, -) > (==, !=, <=, <, >=, >) > (and, unless) > (or) 的原則。
內置函數:
prometheus 內置不少函數,方便查詢以及數據格式化,例如將結果由浮點數轉為整數的 floor 和 ceil,
floor(avg(prometheus_http_requests_total{code="200"}))
ceil(avg(prometheus_http_requests_total{code="200"}))
查看 prometheus_http_requests_total 5分鍾內平均每秒的數據,
rate(prometheus_http_requests_total[5m])
算術運算符:+、-、*、/、%、^ ,比如 prometheus_http_requests_total * 2 表示將 prometheus_http_requests_total 所有數據乘以2
比較運算符:==、!=、>、<、>=、<= ,比如 prometheus_http_requests_total > 100 表示 prometheus_http_requests_total 結果中大於 100 的數據
邏輯運算符:and、or、unless ,比如 prometheus_http_requests_total == 5 or prometheus_http_requests_total == 2 表示 prometheus_http_requests_total 結果中等於 5 或者 2 的數據
聚合運算符:sum、min、max、avg、stddev、stdvar、count、count_values、bottomk、topk、quantile,比如 max(prometheus_http_requests_total) 表示 prometheus_http_requests_total 結果中最大的數據
常用內置函數:
abs(v instant-vector) 返回所有樣本值均轉換為絕對值的輸入即時向量v
absent(v instant-vector) 如果傳遞給它的即時向量v有任何元素,則返回一個空向量;如果傳遞給它的即時向量v沒有元素,則返回值為1的單元素向量
absent_over_time(v range-vector) 如果傳遞給它的范圍向量v有任何元素,則返回一個空向量;如果傳遞給它的范圍向量v沒有元素,則返回值為1的單元素向量
avg_over_time(range-vector) 指定時間間隔內范圍向量所有元素樣本值的平均值
ceil(v instant-vector) 將即時向量v中所有元素的樣本值向上取整到最接近的整數
changes(v range-vector) 對於范圍向量v中的時間序列,返回其值在提供的時間范圍內變化的次數作為一個即時向量
clamp_max(v instant-vector, max scalar) 將即時向量v中所有元素的樣本值鎖定上限為標量max
clamp_min(v instant-vector, min scalar) 將即時向量v中所有元素的樣本值鎖定下限為標量min
count_over_time(range-vector) 指定時間間隔內范圍向量所有元素樣本值的計數
day_of_month(v=vector(time()) instant-vector) 返回UTC中每個給定時間的月份。返回值是1到31
day_of_week(v=vector(time()) instant-vector) 返回UTC中每個給定時間的星期幾。返回值是從0到6,其中0表示星期日
days_in_month(v=vector(time()) instant-vector) 返回UTC中每個給定時間的月份中的天數。返回值是28到31
delta(v range-vector) 計算范圍向量v中每個時間序列元素的第一個值與最后一個值之間的差,並返回具有給定增量和相同標簽的即時向量。delta 應僅與Gauge一起使用
deriv(v range-vector) 使用簡單的線性回歸來計算范圍向量v中時間序列的每秒導數。deriv 應僅與Gauge一起使用
exp(v instant-vector) 計算即時向量v中的所有元素的指數函數。特殊情況是:Exp(+Inf) = +Inf、Exp(NaN) = NaN
floor(v instant-vector) 將即時向量v中所有元素的樣本值向下取整到最接近的整數
hour(v=vector(time()) instant-vector) 返回UTC中每個給定時間的一天中的小時。返回值是從0到23
idelta(v range-vector) 計算范圍向量v中最后兩個樣本之間的差,並返回具有給定增量和相同標簽的即時向量。idelta 應僅與Gauge一起使用
increase(v range-vector) 計算范圍向量v中時間序列的增加。單調性中斷(例如由於目標重新啟動而導致的計數器重置)會自動進行調整。increase 應僅與Counter一起使用
irate(v range-vector) 計算范圍向量v中時間序列的每秒瞬時增加率。單調性中斷(例如由於目標重新啟動而導致的計數器重置)會自動進行調整
label_join(v instant-vector, dst_label string, separator string, src_label_1 string, src_label_2 string, ...) 對於即時向量v中的每個時間序列,使用分隔符separator將所有源標簽src_labels的值連接在一起,並返回帶有標簽值的目的標簽dst_label的時間序列。src_labels可以有任意多個
label_replace(v instant-vector, dst_label string, replacement string, src_label string, regex string) 對於即時向量v中的每個時間序列,使用正則表達式regex匹配標簽 src_label。如果匹配,則返回時間序列,並將標簽dst_label替換為replacement的擴展。$1用第一個匹配的子組替換,$2再用第二個匹配的子組替換。如果正則表達式不匹配,則時間序列不變
max_over_time(range-vector) 指定時間間隔內范圍向量所有元素樣本值的最大值
min_over_time(range-vector) 指定時間間隔內范圍向量所有元素樣本值的最小值
minute(v=vector(time()) instant-vector) 返回UTC中每個給定時間的小時分鍾。返回值是從0到59
month(v=vector(time()) instant-vector) 返回UTC中每個給定時間的一年中的月份。返回值是從1到12,其中1表示一月
rate(v range-vector) 計算范圍向量v中時間序列的每秒平均增長率。單調性中斷(例如由於目標重新啟動而導致的計數器重置)會自動進行調整
resets(v range-vector) 對於范圍向量v中的每個時間序列,將提供的時間范圍內的計數器重置次數作為即時向量返回,兩個連續樣本之間值的任何下降都被視為計數器重置。resets 應僅與Counter一起使用
round(v instant-vector, to_nearest=1 scalar) 將即時向量v中所有元素的樣本值四舍五入為最接近的整數
scalar(v instant-vector) 給定一個單元素即時向量v,返回該單個元素的樣本值作為標量。如果即時向量v不是單元素向量,scalar則將返回NaN
sort(v instant-vector) 將即時向量v中元素的樣本值升序排列
sort_desc(v instant-vector) 與sort相同,但以降序排列
sum_over_time(range-vector) 指定時間間隔內范圍向量所有元素樣本值的總和
time() 返回自1970年1月1日UTC以來的秒數
timestamp(v instant-vector) 返回即時向量v的每個樣本的時間戳,作為自1970年1月1日UTC以來的秒數
vector(s scalar) 返回標量s作為不帶標簽的向量
year(v=vector(time()) instant-vector) 返回UTC中每個給定時間的年份
up{job="prometheus"} #僅查詢prometheus的存活狀態
up{job="node"} #僅查詢node_exporter的存活狀態
up{job="alertmanager"} #僅查詢prometheus的存活狀態
up{instance=~"192.168.30.135.*"} #僅查詢192.168.30.135上組件的存活狀態
up{instance=~"192.168.30.136.*"} #僅查詢192.168.30.136上組件的存活狀態
up{instance=~"192.168.30.137.*"} #僅查詢192.168.30.137上組件的存活狀態
- 主機名:
node_uname_info #主機信息,包含主機名
count by (nodename) (node_uname_info) #主機名
- 運行時間:
(time() - node_boot_time_seconds) / 3600 / 24 #系統運行時間,單位是d
- 平均負載:
node_load1 #查看當前node_exporter所在節點1分鍾的平均負載
node_load5 #查看當前node_exporter所在節點5分鍾的平均負載
node_load15 #查看當前node_exporter所在節點15分鍾的平均負載
- cpu核數:
count by (instance) (node_cpu_seconds_total{job="node",mode='system'}) #cpu核數
- cpu空閑率:
avg by (instance) (irate(node_cpu_seconds_total{job="node",mode="idle"}[5m])) * 100 #5分鍾內cpu空閑率,單位是%
- cpu使用率
(1 - avg by (instance) (irate(node_cpu_seconds_total{job="node",mode="idle"}[5m]))) * 100 #5分鍾內cpu使用率,單位是%
- 內存總大小:
ceil(node_memory_MemTotal_bytes{job="node"} / (1024 ^ 3)) #內存總大小,單位是GiB
- 可用內存大小:
node_memory_MemAvailable_bytes{job="node"} / (1024 ^ 3) #可用內存大小,單位是GiB
- 內存使用率:
(1 - (node_memory_MemAvailable_bytes{job="node"} / node_memory_MemTotal_bytes{job="node"})) * 100 #內存使用率,單位是%
- 磁盤總大小:
node_filesystem_size_bytes{job="node",fstype=~"ext.*|xfs",mountpoint ="/"} / (1024 ^ 3) # / 分區磁盤大小,單位是GiB
node_filesystem_size_bytes{job="node",fstype=~"ext.*|xfs",mountpoint ="/boot"} / (1024 ^ 3) # /boot 分區磁盤大小,單位是GiB
- 磁盤可用大小:
node_filesystem_avail_bytes{job="node",fstype=~"ext.*|xfs",mountpoint ="/"} / (1024 ^ 3) # / 分區磁盤可用大小,單位是GiB
node_filesystem_avail_bytes{job="node",fstype=~"ext.*|xfs",mountpoint ="/boot"} / (1024 ^ 3) # /boot 分區磁盤可用大小,單位是GiB
- 磁盤使用率:
(node_filesystem_size_bytes {fstype=~"ext.*|xfs",mountpoint ="/"} - node_filesystem_free_bytes {fstype=~"ext.*|xfs",mountpoint ="/"}) / node_filesystem_size_bytes {fstype=~"ext.*|xfs",mountpoint ="/"} * 100
# (node_filesystem_size_bytes {mountpoint ="/"} - node_filesystem_free_bytes {mountpoint ="/"}) / node_filesystem_size_bytes {mountpoint ="/"} * 100
(1 - node_filesystem_avail_bytes{job="node",fstype=~"ext.*|xfs",mountpoint ="/"} / node_filesystem_size_bytes{job="node",fstype=~"ext.*|xfs",mountpoint ="/"}) * 100 # / 分區磁盤使用率,單位是%
(1 - node_filesystem_avail_bytes{job="node",fstype=~"ext.*|xfs",mountpoint ="/boot"} / node_filesystem_size_bytes{job="node",fstype=~"ext.*|xfs",mountpoint ="/boot"}) * 100 # /boot 分區磁盤使用率,單位是%
- 磁盤設備最大讀取速率:
max by (instance) (irate(node_disk_read_bytes_total{job="node"}[5m])) #5分鍾內磁盤設備的最大讀取速率,單位是bytes/s
- 磁盤設備最大寫入速率:
max by (instance) (irate(node_disk_written_bytes_total{job="node"}[5m])) #5分鍾內磁盤設備的最大寫入速率,單位是bytes/s
- 網卡名:
node_network_device_id{device!~"lo|docker.|cali.*"} #主機所有網卡名
- 網卡狀態:
node_network_up{device!~"lo|docker.|cali.*"} #網卡存活狀態
- 網絡下載速率:
max by (instance) (irate(node_network_receive_bytes_total{job="node"}[5m]) * 8) #5分鍾內網絡最大下載速率,單位是bits/s
- 網絡上傳速率:
max by (instance) (irate(node_network_transmit_bytes_total{job="node"}[5m]) * 8) #5分鍾內網絡最大上傳速率,單位是bits/s
- inode總數:
node_filesystem_files{job="node",fstype=~"ext4|xfs",mountpoint="/"} # / 分區磁盤inode總數
node_filesystem_files{job="node",fstype=~"ext4|xfs",mountpoint="/boot"} # /boot 分區磁盤inode總數
- inode可用數:
node_filesystem_files_free{job="node",fstype=~"ext4|xfs",mountpoint="/"} # / 分區磁盤inode可用數
node_filesystem_files_free{job="node",fstype=~"ext4|xfs",mountpoint="/boot"} # /boot 分區磁盤inode可用數
- inode使用率:
(1 - node_filesystem_files_free{job="node",fstype=~"ext4|xfs",mountpoint="/"} / node_filesystem_files{job="node",fstype=~"ext4|xfs",mountpoint="/"}) * 100 # / 分區磁盤inode使用率,單位是%
(1 - node_filesystem_files_free{job="node",fstype=~"ext4|xfs",mountpoint="/boot"} / node_filesystem_files{job="node",fstype=~"ext4|xfs",mountpoint="/boot"}) * 100 # /boot 分區磁盤inode使用率,單位是%
- 最大文件描述符:
node_filefd_maximum{job="node"} #系統最大文件描述符
- 打開文件描述符數:
node_filefd_allocated{job="node"} #打開的文件描述符數
- 文件描述符使用率:
(node_filefd_allocated{job="node"} / node_filefd_maximum{job="node"}) * 100 #文件描述符使用率,單位是%
- tcp相關:
node_netstat_Tcp_ActiveOpens #從 CLOSED 狀態直接轉換到 SYN-SENT 狀態的 TCP 連接數
node_netstat_Tcp_CurrEstab #當前狀態為 ESTABLISHED 或 CLOSE-WAIT 的 TCP 連接數
node_netstat_Tcp_InErrs #TCP 接收的錯誤報文數
node_netstat_Tcp_InSegs #TCP 接收的報文數
node_netstat_Tcp_OutSegs #TCP 發送的報文數
node_netstat_Tcp_PassiveOpens #從 LISTEN 狀態直接轉換到 SYN-RCVD 狀態的 TCP 連接數
node_netstat_Tcp_RetransSegs #TCP 重傳報文數
node_sockstat_TCP_tw #等待關閉的TCP連接數
node_sockstat_sockets_used #已使用的所有協議套接字總量
node_sockstat_TCP_alloc #已分配(已建立、已申請到sk_buff)的TCP套接字數量