數據采集腳本開發
作者聲明:本博客內容是作者在學習以及搭建過程中積累的內容,內容采自網絡中各位老師的優秀博客以及視頻,並根據作者本人的理解加以修改(由於工作以及學習中東拼西湊,如何造成無法提供原鏈接,在此抱歉!!!)
作者再次聲明:作者只是一個很摳腳的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雖然靈活但是也是存在一些短板的。
- pushgateway 會形成⼀個單點瓶頸,假如好多個腳本同時發送給 ⼀個pushgateway的進程,如果這個進程沒了,那么監控數據也就沒了。
- 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