Linux或macOS下 Grafana+Prometheus监控安装配置
Linux或macOS系统下glances+influxdb+grafana这里可以参考
之前写过一篇
https://www.toutiao.com/i6877726862089388556/
Grafana是一个跨平台的开源的度量分析和可视化工具,可以通过将采集的数据查询然后可视化的展示,并及时通知
很简单直接brew安装,不同的平台安装方式不同。但大致相同。
brew install grafana
➜ ~ brew install grafana
安装之后一般的启动命令如下:
➜ ~ brew services start grafana
==> Successfully started `grafana` (label: homebrew.mxcl.grafana)
默认的登录地址:
会跳转到登录页面,登录初始用户名admin,初始密码admin。
不清楚地址和端口的情况可到/usr/local/etc/grafana/grafana.ini配置文件下找domain和http_port参数。当然我们可以自定义这个配置
安装prometheus
➜ brew install prometheus
看下大概配置:
➜ ~ cat /usr/local/etc/prometheus.args--config.file /usr/local/etc/prometheus.yml--web.listen-address=127.0.0.1:9090--storage.tsdb.path /usr/local/var/prometheus
尝试启动下:
➜ ~ brew services start prometheus
==> Successfully started `prometheus` (label: homebrew.mxcl.prometheus)
查看版本
➜ ~ prometheus --versionprometheus, version 2.22.1 (branch: non-git, revision: non-git) build user: brew@Catalina build date: 20201106-15:31:25 go version: go1.15.3 platform: darwin/amd64
查看端口侦听
➜ ~ lsof -i :9090COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEprometheu 38619 lex 10u IPv4 0x9d21a848ad18e2a9 0t0 TCP localhost:websm (LISTEN)prometheu 38619 lex 13u IPv4 0x9d21a848ccbccee9 0t0 TCP localhost:55857->localhost:websm (ESTABLISHED)prometheu 38619 lex 14u IPv4 0x9d21a848ad1da769 0t0 TCP localhost:websm->localhost:55857 (ESTABLISHED)
测试下取数据:当然这个时候登录页面也是能看到部分信息的
curl http://localhost:9090/metrics
然后去Grafana平台上+Prometheus
添加数据源
搜索Prometheus
大致的配置信息如下:
然后在模版这里我们选择
暂时导入这个,有了这个之后是能看到部分数据的,导入Prometheus 2.0 Stats这个面板,就能看到我们Prometheus的一些基本监控情况
添加第一个监控exporter
我们尝试添加第一个监控exporter——监控当前机器自身的状态,包括硬盘、CPU、流量等。
因为Prometheus已经有了很多现成的常用exporter,所以我们直接用其中的node_exporter。
在Prometheus看来,一台机器或者说一个节点就是一个node,所以该exporter是在上报当前节点的状态
➜ ~ brew install node_exporter
/usr/local/Cellar/node_exporter/1.0.1: 10 files, 18.9MB
这里可以注意下这个版本:node_exporter/1.0.1
后面需要用到
➜ ~ brew services start node_exporter
==> Successfully started `node_exporter` (label: homebrew.mxcl.node_exporter)
查看服务是否OK
➜ ~ lsof -i:9100
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node_expo 41705 lex 3u IPv6 0x9d21a848cccf5ab9 0t0 TCP *:hp-pdl-datastr (LISTEN
运行后可以看到在监听9100端口。这样就可以采集了,现在先访问试试能不能有没有成功运行:
curl http://localhost:9100/metrics
这里也可以看出其实每个exporter本身都是一个http服务,server端会定时来访问获取监控信息。
http://localhost:9090/targets
访问成功的话,我们去prometheus的配置文件(prometheus.yml)中,加上这个target:
先备份配置文件:
cp /usr/local/etc/prometheus.yml /usr/local/etc/prometheus.yml-`date +%F➜ ~ cat /usr/local/etc/prometheus.ymlglobal: scrape_interval: 15sscrape_configs: - job_name: "prometheus" static_configs: - targets: ["localhost:9090"] - job_name: "prometheus-mac" static_configs: - targets: ["localhost:9100"]
重启下服务:
➜ ~ brew services restart prometheusStopping `prometheus`... (might take a while)==> Successfully stopped `prometheus` (label: homebrew.mxcl.prometheus)==> Successfully started `prometheus` (label: homebrew.mxcl.prometheus
查看新的是否添加OK了
新加的target的status是“UP”的话,就说明监听成功了
查看服务是否都已经启动:
➜ ~ lsof -n -P -i TCP -s TCP:LISTEN|egrep "grafana|prometheu"grafana-s 36306 lex 14u IPv6 0x9d21a848ada70859 0t0 TCP *:3000 (LISTEN)prometheu 38619 lex 10u IPv4 0x9d21a848ad18e2a9 0t0 TCP 127.0.0.1:9090 (LISTEN)
我们上面还运行了一个node exporter,现在我们展现一下这个监控信息,
左边竖排点击加号图标中的Import,来导入其他别人写好的面板。
在Grafana的官方面板页面我们找到自己想要的面板,比如这个node exporter的:https://grafana.com/grafana/dashboards/8919记录这个ID
大致情况如下:
然后操作导入:
注意这里的选择:
至此安装基本完成了:
看下效果:
对于有些不出图的地方就需要逐步去排查了。这里不论述了,比如Mac系统可能的分区不是ext或者xfs之类的。就无法展示。
再上一个Glance的监控图:
感兴趣的同学可以搭建看看 哈
搭建的过程会让你理解简单的监控结构和配置文件的大致修改,模版的获取,插件的安装之类。