prometheus简单监控Linux,mysql,nginx

915999-20190724161410010-143912926.png

prometheus安装

下载安装

#官网下载 解压即可使用
https://prometheus.io/download/
#docker 方式安装
sudo docker run -n prometheus -d -p 9090:9090 prom/prometheus

配置文件

 /etc/prometheus/prometheus.yml 或 可执行文件当前目录下/prometheus.yml

完整配置文件

  scheme: http
  static_configs:
  - targets:
    - localhost:9090
- job_name: node1_self
  honor_timestamps: true
  scrape_interval: 15s
  scrape_timeout: 10s
  metrics_path: /metrics
  scheme: http
  static_configs:
  - targets:
    - 192.168.3.103:9100
- job_name: mysql
  honor_timestamps: true
  scrape_interval: 15s
  scrape_timeout: 10s
  metrics_path: /metrics
  scheme: http
  static_configs:
  - targets:
    - 192.168.3.103:9104
    labels:
      instance: db1
- job_name: nginx
  honor_timestamps: true
  scrape_interval: 15s
  scrape_timeout: 10s
  metrics_path: /status/format/prometheus
  scheme: http
  static_configs:
  - targets:
    - 192.168.3.139:80
    labels:
      instance: web1
  basic_auth:
    username: UserName
    password: PassWord

重启服务

重启服务或发信号重新加载配置
killall -HUP prometheus

官方exports 大全

https://prometheus.io/docs/instrumenting/exporters/

Linux服务器配置

下载安装node_exporter (下载解压即可使用)

https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
./node_exporter
测试node_exporter
curl http://localhost:9100/metrics

mysql的exporter下载和配置

可以在mysql机器上安装也可以在别的机器上安装
`. 老样子下载解压
https://github.com/prometheus/mysqld_exporter/releases

  1. 要配置一下被监控的mysql账户信息
    最好单独配置权限
    为 mysqld_exporter 创建一个单独的用户
    并赋予它受限的权限(PROCESS、REPLICATION CLIENT、SELECT)
    最好还限制它的最大连接数(MAX_USER_CONNECTIONS)
CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'password' WITH MAX_USER_CONNECTIONS 3;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';

$ cat .my.cnf

[client]
host=localhost
port=3306
user=root
password=123456
  1. 运行mysqld_exporter
    `./mysqld_exporter --config.my-cnf=".my.cnf"

  2. 在 prometheus服务端的配置文件prometheus.yml中找到 scrape_config子项 添加一个 job
    如:
  - job_name: mysql
    static_configs:
      - targets: ['192.168.1.7:9104']
        labels:
          instance: db1
  1. 发送 重载配置文件信号
    killall -SIGHUP prometheus
  2. 到prometheus 网页中 导航栏->status->target 查看刚才添加的是否成功!

nginx exporter 安装和配置

好多方式都可以.lua脚本,openresty 等
我们选择 编译nginx的nginx-module-vts 这就意味着我们要自己手动编译了.

  1. 下载nginx源码后解压.
    wget https://github.com/nginx/nginx/releases/tag/release-1.17.1
    tar -xvf nginx-release-1.17.1.tar.gz
    cd nginx-release-1.17.1
  2. 下载或克隆nginx-module-vts 模块 https://github.com/vozlt/nginx-module-vts
  3. 编译安装nginx
    安装依赖(centos)
  • openssl-devel
  • pcre-devel
  • gcc

./auto/configure --add-module=/home/pi/nginx-module-vts --with-http_ssl_module --with-debug
干掉nginx
make -j4 使用4个线程编译.树莓派有四个线程
make install 安装nginx默认
4.配置nginx的配置文件
vim /usr/local/nginx/conf/nginx.conf

user root;
#user  nobody;
worker_processes  2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}



http {
    include       mime.types;
    default_type  application/octet-stream;
    vhost_traffic_status_zone;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
    charset utf-8;
        #access_log  logs/host.access.log  main;

        location / {
            root   html;
        auth_basic "needAuth";
        auth_basic_user_file /usr/local/nginx/conf/passwd.db;
        }
    location /status {
        vhost_traffic_status_display;
        vhost_traffic_status_display_format html;
    }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

   
    }

}

配置http密码
apt install apache2-utils -y
htpasswd -c /usr/local/nginx/conf/passwd.db UserName
启动nginx
先看下模块有没编译进来

cd /usr/local/nginx/
/usr/local/nginx/sbin/nginx -V |grep nginx-module-vts

能看到信息就代表模块编译成功.

  1. 运行nginx
    /usr/local/nginx/sbin/nginx
    4.1 查看nginx机器的ip
    ip a
  2. 在prometheus中增加一个监控nginx的任务
    添加配置内容在配置文件 prometheus.yml 注意因为我们nginx配置了验证,所以在prometheus中也要添加验证.要不没有办法支持访问
- job_name: nginx
  honor_timestamps: true
  scrape_interval: 15s
  scrape_timeout: 10s
  metrics_path: /status/format/prometheus
  scheme: http
  static_configs:
  - targets:
    - 192.168.3.139:80
    labels:
      instance: web1
  basic_auth:
    username: UserName
    password: PassWord
  1. 发送 重载配置文件信号
    killall -SIGHUP prometheus
  2. 到prometheus 网页中 导航栏->status->target 查看刚才添加的是否成功!

之后可以使用配合Grafana可以愉快玩耍了.

转载于:https://www.cnblogs.com/lovesKey/p/11239341.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、prometheus简介 Prometheus是一个开源的系统监控和告警系统,现在已经加入到CNCF基金会,成为继k8s之后第二个在CNCF维护管理的项目,在kubernetes容器管理系统中,通常会搭配prometheus进行监控prometheus支持多种exporter采集数据,还支持通过pushgateway进行数据上报,Prometheus再性能上可支撑上万台规模的集群。 二、prometheus架构图 三、prometheus组件介绍 1.Prometheus Server: 用于收集和存储时间序列数据。 2.Client Library: 客户端库,检测应用程序代码,当Prometheus抓取实例的HTTP端点时,客户端库会将所有跟踪的metrics指标的当前状态发送到prometheus server端。 3.Exporters: prometheus支持多种exporter,通过exporter可以采集metrics数据,然后发送到prometheus server端 4.Alertmanager: 从 Prometheus server 端接收到 alerts 后,会进行去重,分组,并路由到相应的接收方,发出报警,常见的接收方式有:电子邮件,微信,钉钉, slack等。 5.Grafana:监控仪表盘 6.pushgateway: 各个目标主机可上报数据到pushgatewy,然后prometheus server统一从pushgateway拉取数据。 四、课程亮点 五、效果图展示 六、讲师简介 先超(lucky):高级运维工程师、资深DevOps工程师,在互联网上市公司拥有多年一线运维经验,主导过亿级pv项目的架构设计和运维工作 主要研究方向: 1.云计算方向:容器 (kubernetes、docker),虚拟化(kvm、Vmware vSphere),微服务(istio),PaaS(openshift),IaaS(openstack)等2.系统/运维方向:linux系统下的常用组件(nginx,tomcat,elasticsearch,zookeeper,kafka等),DevOps(Jenkins+gitlab+sonarqube+nexus+k8s),CI/CD,监控(zabbix、prometheus、falcon)等 七、课程大纲
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值