监控之Prometheus与Grafana

简介:Prometheus(由go语言(golang)开发)是一套开源的监控&报警&时间序列数据库的组合。适合监控容器平台。因为kubernetes(俗称k8s)的流行带动了prometheus的发展。

官网:https://prometheus.io/docs/introduction/overview/

1.安装Prometheus

Prometheus对服务器时间有要求,所以要确保ntp时间是开启的。

# yum install -y ntp
# systemctl restart ntpd
# systemctl enable ntpd

关闭一些该关闭的,配好该配的源。

# cd /software
# wget https://github.com/prometheus/prometheus/releases/download/v2.14.0/prometheus-2.14.0.linux-amd64.tar.gz
# tar xf prometheus-2.14.0.linux-amd64.tar.gz -C /usr/local/
# mv /usr/local/prometheus-2.14.0.linux-amd64/ /usr/local/prometheus
# /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &

Server is ready to receive web requests.

终端被占用,一脚回车
# ss -nltp
9090被占用。

访问 http://服务器IP:9090  可以进入prometheus的后台web。

2.客户机组件安装

官网指路:

        基础扩展:https://prometheus.io/download/

如果不够用。

        增强扩展:https://prometheus.io/docs/instrumenting/exporters/

这东西是你想监控什么,需要先在那台机器上安装一个组件,网址在上面,根据需要下载安装。

比如我要监控linux基础硬件信息,我需要下载 node_exporter 。

# /software
# wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
# tar xf node_exporter-0.18.1.linux-amd64.tar.gz -C /usr/local/
# mv /usr/local/node_exporter-0.18.1.linux-amd64/ /usr/local/node_exporter
# ls /usr/local/node_exporter/
LICENSE  node_exporter  NOTICE

# nohup /usr/local/node_exporter/node_exporter &
# ss -nltp
9100被占用

回到prometheus服务器机

# vim /usr/local/prometheus/prometheus.yml
在下面相似的区域里加同样结构的三行

 - job_name: 'agent1'					# 取一个job名称来代表被监控的机器
   static_configs:
   - targets: ['10.1.1.14:9100']		# 这里改成被监控机器的IP,后面端口接9100

# pkill prometheus
See you next time!
一脚回车

# /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
一脚回车

若没有报错,
浏览器访问 http://prometheus服务器IP:9090/targets
绿了,UP了就是成功。

3.样例:监控mysql

比如监控一个mysql

mysql服务器,安装mysql,启动,略。

# cd /software
# wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
# tar xf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/
# mv /usr/local/mysqld_exporter-0.12.1.linux-amd64/ /usr/local/mysqld_exporter
#  ls /usr/local/mysqld_exporter/
LICENSE  mysqld_exporter  NOTICE

# mysql -u root -p
mysql > grant select,replication client,process ON *.* to 'mysql_prom'@'localhost' identified by '123';flush privileges;quit;

vim /usr/local/mysqld_exporter/.my.cnf

[client]
user=mysql_prom
password=123

# nohup /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf &
[4] 17250
成功

回到prometheus服务器机

# vim /usr/local/prometheus/prometheus.yml
在下面相似的区域里加同样结构的三行

 - job_name: 'db_prom'					# 取一个job名称来代表被监控的mariadb
   static_configs:
   - targets: ['10.1.1.58:9104']				# 这里改成被监控机器的IP,后面端口接9104

# pkill prometheus
See you next time!
一脚回车

# /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
一脚回车

若没有报错,
浏览器访问 http://prometheus服务器IP:9090/targets
绿了,UP了就是成功。

4.安装 grafana 图形界面

官网指路:https://grafana.com/grafana/download

告诉清楚centos7怎么下载了。

# wget https://dl.grafana.com/oss/release/grafana-6.5.1-1.x86_64.rpm
# yum install grafana-6.5.1-1.x86_64.rpm
# systemctl start grafana-server
# systemctl enable grafana-server
# ss -nltp
3000端口被占用视为成功。

浏览器访问 http://grafana服务器IP:3000
蓝色调的页面
初始用户名:admin
初始密码:admin

第一次登录强制你修改密码。

1.新建数据源 Add data source

http://grafana服务器IP:3000/datasources/new

a.选择火炬,Prometheus
b.填表
    Name=Prometheus(默认,后面可能要改)
    URL=http://prometheus服务器IP:9090
    Scrape interval貌似是刷新间隔??
    其他内容选填或不填
c.save

 现在逻辑是这样的。

图形工具Grafana搜索你的Prometheus:9090,监控9090里配置文件配置的各种监控IP:端口,对传回数据,进行筛选,绘图,报警。

5.监控一个数据

左侧点大加号 -> Create DashBoard
    URL:http://grafana服务器IP:3000/dashboard/new

点击Choose Visualization
左侧四个圆形按钮
1.Queries
2.Visualization
3.General
4.Alert

Queries:
    Query选你前面填的数据源名字。
    Metrics里填写一个PromSql。其实就是写入你要检测的项目,内容会自动匹配。具体语法略。
Visualization:
    选择一个图形,填写一些样式颜色,比例之类的。
General:
    我也就会改个Title
Alert:
    报警(重点)
    Create Alert
    Name自己填
    Evaluate every多久侦测一次
    For出问题保持多久后报警(注意6.X版本里才有这个,5版本里没有)
    WHEN 数据 OF query (Queries里的A/B/C, 多长时间, now) IS ABOVE 阈值
    
    下部分Notifications -> Send to
        后面介绍

保存在页面最上方最上面

6.报警

还是用睿象云的CA报警,CA链接Grafana

点击 集成 -> 监控工具 -> Grafana

官网内容如下:

一、在Grafana中配置Webhook URL
1、在Grafana中创建Notification channel,选择类型为Webhook;
    (首页里点左边的铃铛)
    URL: http://IP:3000/alerting/notification/new

2、推荐选中Send on all alerts和Include image,Cloud Alert体验更佳;
3、将第一步中生成的Webhook URL填入Webhook settings Url;
URL格式:http://api.aiops.com/alert/api/event/grafana/v1/{appKey}/
4、Http Method选择POST;
5、填个好Name
6、Send Test(绿色为成功)
7、Save;


这段填完,前面的
Alert -> Notifications -> Send to里就有一个 好Name 出现了

7.导入模板

左侧点大加号 -> Create import
    URL:http://grafana服务器IP:3000/dashboard/import

点击绿色按钮 Upload .json file 上传一个JSON文件

点击 Select a Prometheus data source 选择数据源

点击 Import 确认。

如果成功,会自动跳到DashBoard页面,酷炫。

问题来了。JSON文件哪来的??

GIT里的https://github.com/percona/grafana-dashboards


# git clone https://github.com/percona/grafana-dashboards.git
可以下载到桌面
.../dashboards/
这里头全是现成的JSON文件,内容是热心为网友设计好的各类数据监控板。可以根据需求直接使用。


再回头,前面Alert报警的时候,promSQL里必须是定值,不能是变量
比如:node_load1{instance="$node"}
$node改成IP,时间改成具体时常。
不改不会错,Alert会直接拒绝让你使用的~

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值