prometheus是由go语言编写的,监控服务器是否正常运行的工具,使用experter工具收集数据,传到prometheus服务器。可以结合grafana图形化和pagerduty报警发送有邮件和信息。
实验环境:
关掉防火墙和selinux
grafana.example.com 192.168.121.50 grafana
Prometheus.example.com 192.168.121.40
client1.example.com 192.168.121.30
一、安装prometheus
官网:
 https://prometheus.io/
1.1下载压缩包

1.2搭建prometheus
#hostnamectl hostname prometheus.example.com
#bash
[root@prometheus ~]# yum -y install chrony tar lrzsz
[root@prometheus ~]# systemctl restart chronyd
 [root@prometheus ~]# systemctl enable chronyd
 [root@prometheus ~]# hwclock -w
[root@prometheus ~]# tar -xzvf prometheus-2.54.0.linux-amd64.tar.gz -C /usr/local/ 是二进制安装包,解压后可以直接使用
[root@prometheus ~]# cd /usr/local/
 [root@prometheus local]# mv prometheus-2.54.0.linux-amd64/ prometheus
# egrep -n : /usr/local/prometheus/prometheus.yml | awk -F'#' '{print $1}'
2:global: 全局配置段
3: scrape_interval: 15s 每15s抓取(采集)数据一次
4: evaluation_interval: 15s 每15秒计算一次规则
8:alerting: Alertmanager报警相关
9: alertmanagers:
10: - static_configs:
11: - targets:
12:
15:rule_files: 规则文件列表
19: 21:scrape_configs: 抓取的配置文件(也就是监控的实例)
23: - job_name: 'prometheus' 监控的实例名称
28: static_configs:
29: - targets: ['localhost:9090'] 监控的实例IP与端口,在这里为监控服务器本身
 [root@prometheus local]# /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &           使用prometheus命令,启动文件为/usr/local/prometheus/prometheus.yml,&后台运行
#ss -anlt 查看9090端口是否启用

在浏览器192.168.121.30:9090访问

process_cpu_seconds_total
查看cpu的情况

二、监控远程主机192.168.121.30,和自己本身192.168.121.40
使用node_exporter节点工具,在官网可以下载

[root@client1 ~]# yum -y install chrony tar lrzsz
#timedatectl 时间同步
使用xsell拖到当前目录,解压
[root@client1 ~]# tar -xzvf node_exporter-1.8.2.linux-amd64.tar.gz -C /usr/local/
[root@client1 ~]# cd /usr/local/
 [root@client1 local]# mv node_exporter-1.8.2.linux-amd64/ node_exporter
[root@client1 local]# nohup /usr/local/node_exporter/node_exporter &
 [root@client1 local]# ss -anlt  使用9100端口,9100端口启用,查看


[root@prometheus local]# cd /usr/local/prometheus/
 [root@prometheus prometheus]# vim prometheus.yml 
  - job_name: "client1"
     static_configs:
       - targets: ["192.168.121.30:9100"]
端口是9100,node_experort的端口

# ps -ef | grep prometheus | grep -v grep
[root@prometheus ~]# ps -ef | grep prometheus | grep -v grep
 root       11043    1366  0 16:13 pts/0    00:00:00 /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml
 [root@prometheus ~]# kill -9 11043
 [root@prometheus ~]# ps -ef | grep prometheus | grep -v grep
 [1]+  Killed                  /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml"
[root@prometheus ~]# /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" & 重新运行,因为没有写到system里面,所以使用二进制命令运行重启
[root@prometheus ~]# ss -anlt

网页刷新查看主机
[root@prometheus ~]# tar -xzvf node_exporter-1.8.2.linux-amd64.tar.gz -C /usr/local/
在192.168.121.40的主机上同样的操作
[root@prometheus ~]# cd /usr/local/
 [root@prometheus local]# mv node_exporter-1.8.2.linux-amd64/ node_exporter
 [root@prometheus local]# nohup /usr/local/node_exporter/node_exporter &    同样后台运行插件节点_exporter
[root@prometheus local]# vim /usr/local/prometheus/prometheus.yml
  - job_name: "promethues"
     static_configs:
       - targets: ["192.168.121.40:9100"]
  
[root@prometheus local]# ps -ef | grep prometheus | grep -v grep
root       11125    1366  0 16:16 pts/0    00:00:00 /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml
 [root@prometheus local]# kill -9 11125
 [root@prometheus local]# /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &


三、下载mysql_exporter组件,在官网

[root@client1 ~]# tar -xzvf mysqld_exporter-0.15.1.linux-amd64.tar.gz -C /usr/local/
[root@client1 ~]# cd /usr/local/
 [root@client1 local]# mv mysqld_exporter-0.15.1.linux-amd64/ mysql_exporter
 [root@client1 local]# yum -y install mariadb mariadb-server

[root@client1 ~]# systemctl restart mariadb.service 
 [root@client1 ~]# systemctl enable mariadb
不用初始化,能进去就行了
#mysql
[root@client1 ~]# mysql
prometheus 服务器找mysqld_exporter,mysqld_exporter再找mariadb。所以这个localhost是指的mysql_exporter
授权给本地用户就行了
MariaDB [(none)]> select user();
MariaDB [(none)]> grant all on *.* to 'mysql_monitor'@'localhost' identified by '123';
授权给监视用户所有权限,对于所有数据库中的所有表,密码为123
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit

[root@client1 ~]# vim /usr/local/mysql_exporter/.my.cnf
创建隐藏文件.my.cnf,隐藏文件前面有个点,写入三行内容,刚设置的数据库的用户和密码
[client]
 user=mysql_monitor
 password=123
 [root@client1 ~]# nohup /usr/local/mysql_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysql_exporter/.my.cnf &
使用mysql_exporter命令,启动文件为刚创建的隐藏文件.my.cnf
[root@client1 ~]# ss -anlt 查看9104端口启用

在服务端
#cd/usr/local
[root@prometheus local]# vim prometheus/prometheus.yml
[root@prometheus ~]# ps -ef | grep prometheus | grep -v grep
 root       11229    1366  0 16:27 pts/0    00:00:03 /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml
 [root@prometheus ~]# kill -9 11229
[root@prometheus ~]# /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
[root@prometheus ~]# ss -anlt 查看9090端口启用时刷新网页

全部变为绿色,启用成功

查看mysql连接数
mysql_global_status_threads_connected

只要一个,添加三个


刷新查看到四个
三、granfana的安装并连接prometheus
前言:granfana是一种可视化工具,将数据图形化,可以结合prometheus、zabbix的数据脚本和参数,形成图形化和告警。
官网:
https://grafana.com/
Download Grafana | Grafana Labs
下载
#yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-11.1.4-1.x86_64.rpm

版本,社区版兼容性更好
[root@node5 ~]# yum -y install lrzsz tar chrony
[root@node5 ~]# systemctl restart chronyd
 [root@node5 ~]# systemctl enable chronyd
 [root@node5 ~]# hwclock -w
[root@node5 ~]# hostnamectl hostname grafana.example.com
 [root@node5 ~]# bash
 更改主机名
#vim /etc/hosts
三台主机都写
192.168.121.30 client1.example.com client1
 192.168.121.40 prometheus.example.com prometheus
 192.168.121.50 grafana.example.com grafana
[root@grafana ~]# rpm -ivh grafana-6.4.2-1.x86_64.rpm
安装grafana的rpm包,需要安装三个依赖包 
 warning: grafana-6.4.2-1.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 24098cb6: NOKEY
 error: Failed dependencies:
     fontconfig is needed by grafana-6.4.2-1.x86_64
     freetype is needed by grafana-6.4.2-1.x86_64
     urw-fonts is needed by grafana-6.4.2-1.x86_64
 [root@grafana ~]# yum -y install fontconfig freetype urw-fonts


浏览器访问
192.168.121.50:3000
默认的admin用户,admin密码
更改复杂程度高的密码




查看




保存

mysql_global_status_threads_connected
填写保存就行

查看

四、导入json模板实现mysql监控

模板网站
https://grafana.com/grafana/dashboards
 选一个自己喜欢的,下面是promethues的模板
选一个自己喜欢的,下面是promethues的模板

点击名字,进入,记住这两个

下载,查看文件位置



保存后,可以看到

第二种,将文件内容复制到框框里面
第三种,复制id号

打开文件,没有vs的用记事本打开也是一样的
ctrl+a全选,ctrl+c/v复制粘贴

将文件内容复制到框框里面


第三种,直接填id号就行了



设置一个名字保存就行了,三种模板导入的方法。
linux的主机详情,等等也有,作者信息也能看到。
全部开源昂昂昂,开源是伟大的。

也有默认的版本

查看

五、granfana加onealert告警,邮件,信息发送
官网:
国内的睿象云服务
http://www.onealert.com/
创建账号
绑定邮箱和手机号码,后面告警要使用


找到grafana,点击加号

填写名字,下滑蓝色,保存就行了




设置邮件和短信两种,发送消息
中文还要我写步骤?????


添加号grafana后




设置告警







 收不到短信和消息,服了,不写了
收不到短信和消息,服了,不写了
  



















