靈活實現方案: python
1: telegraf 爲go 語言寫得佔用內存小 收集主機各項監控數據 定時寫入 時序DB influxdb ------------------------"telegraf + influxdb + grafana"docker
2: 也可自已用python 編寫監控腳本收集主機各項數據寫入 influxdb ------------------------------------------------------- "python + influxdb + grafana"bash
咱們選第一種: 第二種我也試了 重點在python data-type 上 int string 寫入 influxdb db 時要注意 ui
#主機清單 說明url
172.16.1.6 #安裝 telegraf 並設置配置文件 收集系統各項監控數據 而後寫入遠程主機(172.16.1.50) influxdb 內spa
172.16.1.50 #安裝 docker , docker-compose 運行start-DockerCompose_grafana.sh 生成docker-compose_grafana.yml 並運行 .net
特別說明: debug
#1, 172.16.1.50運行 docker-compose 會去docker-hub 拉取最新得 grafana images 鏡像 因此必須建立 grafana 用戶組 rest
#2,grafana 鏡像從4.1.2 之後容器內都是以普通用戶運行 更換低版鏡像並不須要在宿主機上建立grafana user 會在當前目錄下生成 docker-compose-grafana.yml docker-compose filecode
相關文檔: http://docs.grafana.org/installation/docker/#installing-using-docker
#172.16.1.50主機必須條件 , docker , docker-compose
運行start-DockerCompose_grafana.sh 生成docker-compose_grafana.yml 並自動運行: docker-compose -f docker-compose_grafana.yml up -d
cat start-DockerCompose_grafana.sh
#!/bin/bash
GRAFANA_DIR=/grafana-dir
GRAFANA_plugins=/plugins
INSPECT_NAME=`id -u grafana >> /dev/null 2>&1`
INSPECT_ID=`id 472 >> /dev/null 2>&1 `
INSPECT_FILE=`-f ./docker-compose_grafana.yml >> /dev/null 2>&1`
function newfile () {
cat >> ./docker-compose_grafana.yml <<EOF
version: '2'
services:
grafana:
image: grafana/grafana
restart: always
container_name: "grafana"
ports:
- 3000:3000
volumes:
- $GRAFANA_DIR/data:/var/lib/grafana:rw
- $GRAFANA_DIR$GRAFANA_plugins:/var/lib/grafana/plugins:rw
- /etc/localtime:/etc/localtime #同步宿主機和容器時間
links:
- influxdb
depends_on:
- influxdb
influxdb:
image: tutum/influxdb:latest
restart: always
container_name: "influxdb"
environment:
- ADMIN_USER="root"
- INFLUXDB_INIT_PWD="237356573"
- PRE_CREATE_DB="telegraf"
volumes:
- /etc/localtime:/etc/localtime
#未作數據卷 如要保存db data 必須作數據卷
ports:
- 8086:8086
- 8083:8083
EOF
}
if [ ! $INSPECT_NAME ] && [ ! $INSPECT_ID ];
then
echo "run"
groupadd -g 472 grafana && useradd -M -u 472 -g 472 grafana
mkdir -p $GRAFANA_DIR/{data,plugins}
chown -R grafana:grafana $GRAFANA_DIR
newfile
docker-compose -f ./docker-compose_grafana.yml up -d
elif[ $INSTPECT_FILE ];
docker-compose -f ./docker-compose_grafana.yml up -d
else
newfile
echo "not run grafana-DockerCompose.yml"
fi
2: 172.16.1.6 agent 安裝 telegraf 並設置配置文件
2.1下載
wget http://get.influxdb.org/telegraf/telegraf-0.11.1-1.x86_64.rpm
二、2安裝
yum localinstall telegraf-0.11.1-1.x86_64.rpm -y
2.3修改配置文件:
cat /etc/telegraf/telegrag.conf #擴展目錄 /etc/telegraf/telegrag.d/[global_tags]
[global_tags] [agent] interval = "8s" round_interval = true metric_batch_size = 1000 metric_buffer_limit = 10000 collection_jitter = "0s" flush_interval = "10s" flush_jitter = "0s" precision = "" debug = false quiet = false logfile = "" hostname = "" omit_hostname = false [[outputs.influxdb]] urls = ["http://172.16.1.50:8086"] database = "telegraf" retention_policy = "" write_consistency = "any" timeout = "5s" username = "root" password = "237356573" [[inputs.net]] interfaces = ["ens33"] [[inputs.cpu]] percpu = true totalcpu = true collect_cpu_time = false report_active = false [[inputs.disk]] ignore_fs = ["tmpfs", "devtmpfs", "devfs"] [[inputs.diskio]] [[inputs.kernel]] [[inputs.mem]] [[inputs.processes]] [[inputs.swap]] [[inputs.system]] [[inputs.netstat]]
相關配置說明:https://www.jianshu.com
啓動服務: systemctl start Telegraf
查看相關服務是否已啓動:
登錄 http://172.16.1.50:8083 檢測監控數據是否寫入db
登錄 http://172.16.1.50:3000 配置grafana
user: admin password: admin
配置 grafana DB
填入地址 IP 由於咱們已經在docker 容器內吧 db 容器 links 別名爲 influxdb 因此 URL:httpd://influxdb:8086
導入模板 dashboard 面板 模板下載地址:https://grafana.com/dashboards/1443
最後來張大圖吧 若有個別view 不顯 請微調 數據源 telegraf 官網 上有各項監控數據 指標 相關配置說明 找到相關字段
相關資料:
官網參考地址
https://docs.influxdata.com/telegraf/v0.11/
telegraf軟件下載地址: