prometheus监控组件单机、配置
原创文章,转载请注明出处,违者必追究其法律责任。

一、环境说明

prometheus官方教程:https://prometheus.io/docs/introduction/overview/  grafana官方教程:https://grafana.com/docs/  node_exporter 监控节点:https://prometheus.io/download/ 从prometheus官网https://prometheus.io/download/、grafana官网https://grafana.com/docs/installation/rpm/下载tar.gz安装包,安装遇到问题以及配置详情参考官网文档:

服务器列表
192.168.0.69 CentOS7.5 4C 8G 50G

安装包准备-192.168.0.69
[root@vm-centos75-0-69 ~]# cd /opt/cosmo/tools/
[root@vm-centos75-0-69 tools]# ll
总用量 104768
-rw-r--r-- 1 root root 58485103 5月  23 17:52 grafana-6.2.0.linux-amd64.tar.gz
-rw-r--r-- 1 root root  7018111 5月  23 19:49 node_exporter-0.17.0.linux-amd64.tar.gz
-rw-r--r-- 1 root root 41771939 5月  23 17:52 prometheus-2.8.1.linux-amd64.tar.gz
-rwxrwxrwx 1 root root  7482304 8月   7 16:19 redis_exporter.tar.gz
-rwxrwxrwx 1 root root  9084903 9月   6 13:16 mongodb_exporter.tar.gz
-rw-r--r-- 1 root root   7121565 8月  30 18:58 mysqld_exporter-0.12.1.linux-amd64.tar.gz
-rw-r--r-- 1 root root   3433895 9月   9 11:34 nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
-rw-r--r-- 1 root root 380327 9月  10 10:53 nginx-module-vts-0.1.18.tar.gz
节点192.168.0.69系统内核版本
[root@vm-centos75-0-69 ~]# uname -a
Linux vm-centos75-0-69 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@vm-centos75-0-69 ~]# uname -r
3.10.0-862.el7.x86_64
[root@vm-centos75-0-69 ~]# more /etc/*release
::::::::::::::
/etc/centos-release
::::::::::::::
CentOS Linux release 7.5.1804 (Core) 
::::::::::::::
/etc/os-release
::::::::::::::
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

::::::::::::::
/etc/redhat-release
::::::::::::::
CentOS Linux release 7.5.1804 (Core) 
::::::::::::::
/etc/system-release
::::::::::::::
CentOS Linux release 7.5.1804 (Core) 

[root@vm-centos75-0-69 ~]# more /etc/issue
\S
Kernel \r on an \m

[root@vm-centos75-0-69 ~]# more /proc/version 
Linux version 3.10.0-862.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP
 Fri Apr 20 16:44:24 UTC 2018
 
cpu信息

[root@vm-centos75-0-69 ~]# lscpu 
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    1
Core(s) per socket:    2
座:                 2
NUMA 节点:         1
厂商 ID:           GenuineIntel
CPU 系列:          6
型号:              63
型号名称:        Intel(R) Xeon(R) CPU E7-4809 v3 @ 2.00GHz
步进:              4
CPU MHz:             2000.000
BogoMIPS:            4000.00
超管理器厂商:  VMware
虚拟化类型:     完全
L1d 缓存:          32K
L1i 缓存:          32K
L2 缓存:           256K
L3 缓存:           20480K
NUMA 节点0 CPU:    0-3
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 movbe popcnt aes hypervisor lahf_lm epb xsaveopt cqm_llc cqm_occup_llc dtherm arat pln pts


内存信息
[root@vm-centos75-0-69 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           7.6G        2.8G        1.9G        8.9M        2.9G        4.5G
Swap:          5.0G          0B        5.0G

# 磁盘信息
[root@vm-centos75-0-69 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   44G  3.7G   41G    9% /
devtmpfs                 3.9G     0  3.9G    0% /dev
tmpfs                    3.9G     0  3.9G    0% /dev/shm
tmpfs                    3.9G  8.9M  3.9G    1% /run
tmpfs                    3.9G     0  3.9G    0% /sys/fs/cgroup
/dev/sda1               1014M  142M  873M   14% /boot
/dev/sr0                 4.2G  4.2G     0  100% /mnt
tmpfs                    783M     0  783M    0% /run/user/0
创建目录
[root@vm-centos75-0-69 ~]# cd /opt/
[root@vm-centos75-0-69 opt]# mkdir -p cosmo/tools
[root@vm-centos75-0-69 opt]# mkdir -p cosmo/com
[root@vm-centos75-0-69 opt]# mkdir -p cosmo/logs

二、配置prometheus

2.1 解压
##### 进入安装包存放目录
[root@vm-centos75-0-69 ~]# cd /opt/cosmo/tools
##### 解压
[root@vm-centos75-0-69 packages]# tar -Pxvf prometheus-2.8.1.linux-amd64.tar.gz
2.2 启动
##### 进入prometheus目录
[root@vm-centos75-0-69 ~]# cd /opt/cosmo/com/prometheus-2.8.1.linux-amd64
##### 使用nohup后台启动命令 --web.enable-lifecycle 是启动运行时重新加载其配置的 配置项:
[root@vm-centos75-0-69 prometheus-2.8.1.linux-amd64]# nohup ./prometheus --web.enable-lifecycle >> /opt/cosmo/logs/prometheus.out 2>&1 &
重新加载配置的启动方式

# prometheus 重新加载配置的命令:curl -X POST http://192.168.0.69:9090/-/reload
2.3 关闭
# 查看prometheus服务状态
[root@vm-centos75-0-69 ~]# ps -ef | grep prometheus
root      2966     1  0 5月23 ?       00:09:40 ./prometheus
root      6325  6300  0 15:20 pts/0    00:00:00 grep --color=auto prometheus
# 关闭prometheus服务
[root@vm-centos75-0-69 ~]# pkill prometheus
2.4 其他配置
# 进入prometheus目录
[root@vm-centos75-0-69 ~]# cd /opt/prometheus-2.8.1.linux-amd64
# 查看prometheus文件
[root@vm-centos75-0-69 prometheus-2.8.1.linux-amd64]# ll
总用量 103480
drwxr-xr-x 2 3434 3434       38 3月  29 03:16 console_libraries
drwxr-xr-x 2 3434 3434      173 3月  29 03:16 consoles
-rw-r--r-- 1 3434 3434    11357 3月  29 03:16 LICENSE
-rw-r--r-- 1 3434 3434     2770 3月  29 03:16 NOTICE
-rwxr-xr-x 1 3434 3434 65268013 3月  29 02:05 prometheus
-rw-r--r-- 1 3434 3434      926 3月  29 03:16 prometheus.yml
-rwxr-xr-x 1 3434 3434 40669851 3月  29 02:06 promtool
把03_配置文件中的prometheus文件夹中的prometheus.yml 修正后 上传文件替换掉原本的prometheus.yml

把03_配置文件中的prometheus文件夹中的file_sd_configs 放到prometheus-2.8.1.linux-amd64下

然后执行curl -X POST http://192.168.0.69:9090/-/reload
下面是自身环境的prometheus.yml配置文件,

根据现场的实际情况修改- job_name: 'prometheus' 下的 static_configs: 中的 - targets: [IP+端口]

  • job_name: 'redis_exporter' 下的 relabel_configs: 中的 -replacement: [IP+端口]
    全局配置节点下的配置对所有其它节点都有效,同时也是其它节点的默认值
global:
    #抓取间隔,15s
    scrape_interval: 15s
    #抓取超时时间,15s
    scrape_timeout: 15s
#配置规则:规则配置包含记录规则配置和告警规则配置,节点下只是列出文件,
#具体配置在各个文件中。记录规则配置接下来会讲,告警规则配置在后面的告警实验中会讲解。
rule_files:
#- /etc/prometheus/rules.yml
#警告配置 :告警配置用于 Alertmanager
alerting:
  alertmanagers:
    - static_configs:
      - targets: 
      # ["monitor-alertmanager.default.svc:9093"]
#抓取配置
scrape_configs:
    # 任务名
  - job_name: 'prometheus'
    # 从这个任务中抓取目标的频率时间值
    scrape_interval: 10s
    # 当抓取这个任务的所有目标时,超时时间值
    scrape_timeout: 10s
    # 指定抓取的http资源路径,默认为/metrics
    metrics_path: '/metrics'
    # 请求协议,默认为http
    scheme: http
    static_configs:
      - targets: ['192.168.0.69:9090']
   
    # 任务名 /node_exporter
  - job_name: 'linux'
    scrape_interval: 10s
    scrape_timeout: 10s
    scheme: http
    file_sd_configs:
    - files: ['./file_sd_configs/node/linux_node.json']
      refresh_interval: 10m
    
          
    # 任务名 /wmi_exporter
  - job_name: 'windows'
    file_sd_configs:
    - files: ['./file_sd_configs/node/windows_node.json']
      refresh_interval: 10m
   
    # 任务名 /jmx Java监控
  - job_name: 'jmx'
    scrape_interval: 10s
    scrape_timeout: 10s
    metrics_path: '/metrics'
    scheme: http
    file_sd_configs:
    - files: ['./file_sd_configs/webserver/tomcat.json']
      refresh_interval: 10m

    #spring_exporter
  - job_name: cosmo-project
    file_sd_configs:
    - files: ['./file_sd_configs/springboot/cosmo-project.json']
    relabel_configs:
    - source_labels: [__metrics_path__]
      target_label: __metrics_path__
    - source_labels: [__address__]
      target_label: __address__
      refresh_interval: 10m
   
    #mysqld_exporter
  - job_name: 'mysqld_exporter'
    scrape_interval: 10s
    scrape_timeout: 10s
    file_sd_configs:
    - files: ['./file_sd_configs/database/mysql.json']
      refresh_interval: 10m
   
    #mongodb_exporter
  - job_name: 'mongodb_exporter'
    scrape_interval: 10s
    scrape_timeout: 10s
    file_sd_configs:
    - files: ['./file_sd_configs/database/mongodb.json']
      refresh_interval: 10m
      
    #redis_exporter
  - job_name: 'redis_exporter'
    file_sd_configs:
      - files: ['./file_sd_configs/database/redis.json']
    metrics_path: /scrape
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 192.168.0.71:9121
    params:
      check-keys: ["metrics:*"]
      
    #nginx_vts_exporter
  - job_name: 'nginx_vts_exporter'    
    file_sd_configs:      
      - files: ['./file_sd_configs/nginx/nginx_vts_exporter.json']        
        refresh_interval: 10m

三、配置grafana

3.1 解压
- 进入安装包存放目录
[root@vm-centos75-0-69 ~]# cd /opt/cosmo/tools
- 解压
[root@vm-centos75-0-69 tools]# tar -Pxvf grafana-6.2.0.linux-amd64.tar.gz
3.2 启动
- 进入grafana下的bin目录
[root@vm-centos75-0-69 ~]# cd /opt/cosmo/com/grafana-6.2.0/bin
# 使用nohup后台启动命令:
[root@vm-centos75-0-69 bin]# nohup ./grafana-server >> /opt/cosmo/logs/grafana.out 2>&1 &
3.3 关闭
- 查看grafana服务状态
[root@vm-centos75-0-69 ~]# ps -ef | grep grafana-server
root      6381  6300  4 15:55 pts/0    00:00:00 ./grafana-server
root      6394  6300  0 15:55 pts/0    00:00:00 grep --color=auto grafana-server
- 关闭grafana服务
[root@vm-centos75-0-69 ~]# pkill grafana-server
3.4 其他配置
- 进入grafana目录
[root@vm-centos75-0-69 ~]# cd /opt/cosmo/com/grafana-6.2.0
- 查看grafana目录结构
[root@vm-centos75-0-69 grafana-6.2.0]# ll
总用量 32
drwxr-xr-x  2 root root    96 5月  22 19:29 bin
drwxr-xr-x  3 root root    81 5月  23 11:11 conf
drwxr-xr-x  5 root root    61 5月  25 15:52 data
-rw-r--r--  1 root root 11343 5月  22 19:19 LICENSE
-rw-r--r--  1 root root   108 5月  22 19:19 NOTICE.md
drwxr-xr-x 12 root root   155 5月  22 19:29 public
-rw-r--r--  1 root root  5743 5月  22 19:19 README.md
drwxr-xr-x  7 root root  4096 5月  22 19:29 scripts
drwxr-xr-x  3 root root    23 5月  22 19:29 tools
-rw-r--r--  1 root root     5 5月  22 19:29 VERSION
# 进入到conf目录下,配置文件放在conf中
[root@vm-centos75-0-69 grafana-6.2.0]# cd conf/
[root@vm-centos75-0-69 conf]# ll
总用量 44
-rw-r--r-- 1 root root 17290 5月  23 11:11 defaults.ini
-rw-r--r-- 1 root root  2269 5月  22 19:19 ldap.toml
drwxr-xr-x 5 root root    60 5月  22 19:29 provisioning
-rw-r--r-- 1 root root 16841 5月  22 19:19 sample.ini
修改defaults.ini文件

然后重启grafana服务
因为defaults.ini文件过长,所以只选取需要修改的部分进行展示

根据实际的现场环境开放的端口号,修改http_port,默认端口为5020

#################################### Server ##############################
[server]
# Protocol (http, https, socket)
protocol = http

# The ip address to bind to, empty will bind to all interfaces
http_addr =

# The http port to use
http_port = 5020

# The public facing domain name used to access grafana from a browser
domain = localhost

# Redirect to correct domain if host header does not match domain
# Prevents DNS rebinding attacks
enforce_domain = false

# The full public facing url
root_url = %(protocol)s://%(domain)s:%(http_port)s/

# Log web requests
router_logging = false

# the path relative working path
static_root_path = public

# enable gzip
enable_gzip = false

# https certs & key file
cert_file =
cert_key =

# Unix socket path
socket = /tmp/grafana.sock

# 修改 security下的 allow_embedding修改为true
# 当false,HTTP报头X-Frame-Options: deny将在Grafana HTTP响应被设置,这将指示浏览器不允许在一个渲染Grafana <frame>,<iframe>,<embed>或<object>。主要目标是降低Clickjacking的风险。默认是false。
#################################### Security ############################
[security]
# default admin user, created on startup
admin_user = admin

# default admin password, can be changed before first start of grafana, or in profile settings
admin_password = admin

# used for signing
secret_key = SW2YcwTIb9zpOOhoPsMm

# disable gravatar profile images
disable_gravatar = false

# data source proxy whitelist (ip_or_domain:port separated by spaces)
data_source_proxy_whitelist =

# disable protection against brute force login attempts
disable_brute_force_login_protection = false

# set to true if you host Grafana behind HTTPS. default is false.
cookie_secure = false

# set cookie SameSite attribute. defaults to `lax`. can be set to "lax", "strict" and "none"
cookie_samesite = lax

# set to true if you want to allow browsers to render Grafana in a <frame>, <iframe>, <embed> or <object>. default is false.
# 默认false,需要修改成true
allow_embedding = true

# 修改auth.anonymous下的enabled 为true,
# 因为enabled默认是不开启匿名访问的,需要开启匿名访问
#################################### Anonymous Auth ######################
[auth.anonymous]
# enable anonymous access 
# 默认值为false 需要修改成true
enabled = true

# specify organization name that should be used for unauthenticated users
org_name = Main Org.

# specify role for unauthenticated users
org_role = Viewer

四、配置node_exporter

4.1 解压
# 进入安装包存放目录
[root@vm-centos75-0-69 ~]# cd /opt/cosmo/tools
# 解压
[root@vm-centos75-0-69 tools]# tar -Pxvf node_exporter-0.17.0.linux-amd64.tar.gz
4.2 启动
# 进入node_exporter安装目录
[root@vm-centos75-0-69 ~]# cd /opt/cosmo/com/node_exporter-0.17.0.linux-amd64
# 启动node_exporter服务
[root@vm-centos75-0-69 node_exporter-0.17.0.linux-amd64]# nohup ./node_exporter --web.listen-address=":5021" >> /opt/cosmo/logs/nodeExporter.out 2>&1 &
4.3 关闭
# 查询node_exporter服务
[root@vm-centos75-0-69 ~]# ps -ef | grep node_exporter
root      1921     1  0 5月23 ?       00:09:59 /opt/node_exporter-0.17.0/node_exporter --web.listen-address=:5021
root      6556  6300  0 18:02 pts/0    00:00:00 grep --color=auto node_exporter
# 删除node_exporter进程
[root@vm-centos75-0-69 ~]# pkill node_exporter

五、配置jmx_exporter

5.1 集成
# 把cosmo_jmx_prometheus.jar和tomcat.yml放入需要监控的tomcat中的lib文件夹下。
# 更改tomcat的bin文件夹下的catalina.sh文件
# 找到
JAVA_OPTS="$JAVA_OPTS -Djava.protocol.handler.pkgs=org.apache.catalina.webresources"
# 在上段代码后,把下段代码粘在下面
JAVA_OPTS="$JAVA_OPTS -javaagent:$CATALINA_HOME/lib/cosmo_jmx_prometheus.jar=5915:$CATALINA_HOME/lib/tomcat.yml"
# 注意上面的端口号:5915
# 如果当前启动服务的端口为5910则监听端口为5915
# 如果当前启动服务的端口为5920则监听端口为5925
# 依此类推
# 然后重启tomcat

六、配置mysqld_exporter

6.1 解压
# 进入安装包存放目录
[root@vm-centos75-0-69 ~]# cd /opt/cosmo/tools
# 解压
[root@vm-centos75-0-69 tools]# tar -Pxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz
6.2 配置、启动
# 进入安装目录
[root@vm-centos75-0-69 ~]# cd /opt/cosmo/com/mysqld_exporter-0.12.1.linux-amd64
[root@vm-centos75-0-69 mysqld_exporter-0.12.1.linux-amd64]# vi .my.cnf
# 创建.my.cnf文件,填入信息
# host ---  mysql数据库所在服务器ip
# user ---  mysql数据库用户名
# password ---  mysql数据库密码
# port ---  mysql数据库端口
[client]
host=192.168.0.133   
user=root          
password=root
port=3306

# 如果权限分权明确,则只给查询权限的用户就可,否则不用理会创建用户
# mysqld_exporter需要连接到Mysql,所以需要Mysql的权限,
# 我们这里可以使用root用户(也可以授权新用户)
# CREATE USER 'mysqlexporter'@'localhost' IDENTIFIED BY 'msyqlexporter';
# GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';

# 启动mysqld_exporter服务
[root@vm-centos75-0-69 mysqld_exporter-0.12.1.linux-amd64]# nohup ./mysqld_exporter --config.my-cnf=.my.cnf  >> /opt/cosmo/logs/mysqlExporter.out 2>&1 &
6.3 关闭
# 查询mysqld_exporter服务
[root@vm-centos75-0-69 ~]# ps -ef | grep mysqld_exporter
root      2661 19011  0 14:39 pts/0    00:00:00 grep --color=auto mysqld_exporter
root     26077 19011  0 11:35 pts/0    00:00:41 ./mysqld_exporter --config.my-cnf=.my.cnf
# 删除mysqld_exporter进程
[root@vm-centos75-0-69 ~]# pkill mysqld_exporter

七、配置redis_exporter

7.1 解压
# 进入存放目录
[root@vm-centos75-0-69 ~]# cd /opt/cosmo/tools
# 解压
[root@vm-centos75-0-69 packages]# tar -Pxvf redis_exporter.tar.gz
7.2 启动
# 进入redis_exporter安装目录 
[root@vm-centos75-0-69 ~]# cd /opt/cosmo/com/
# 启动redis_exporter服务 -- 针对的是redis统一一个密码,如果每个redis的密码不同,则需要创建多个redis_exporter
# --redis.password= redis的密码, 注意更改
[root@vm-centos75-0-69 com]# nohup ./redis_exporter --redis.password=cosmo_redis_74  >> /opt/cosmo/logs/redis_exporter.out 2>&1 &
7.3 关闭
# 查询redis_exporter服务
[root@vm-centos75-0-69 ~]# ps -ef|grep redis_exporter 
root     12375 19011  0 17:52 pts/0    00:00:00 ./redis_exporter
root     12884 19011  0 18:01 pts/0    00:00:00 grep --color=auto redis_exporter
# 删除redis_exporter进程
[root@vm-centos75-0-69 ~]# pkill redis_exporter

八、配置mongodb_exporter

8.1 安装
# 进入安装包存放目录
[root@vm-centos75-0-69 ~]# cd /opt/cosmo/tools
# 下载
[root@vm-centos75-0-69 tools]# tar -Pxvf mongodb_exporter.tar.gz
8.2 启动
# 进入mongodb_exporter安装包存放目录 
[root@vm-centos75-0-69 ~]# cd /opt/cosmo/com/
# mongodb_exporter 授权 
[root@vm-centos75-0-69 com]# chmod 777 mongodb_exporter

# mongodb数据库开通监控用户 --在mongodb上执行的语句
db.getSiblingDB("admin").createUser({
    user: "mongodb_exporter",
    pwd: "123456",
    roles: [
        { role: "clusterMonitor", db: "admin" },
        { role: "read", db: "local" }
    ]
})

# 启动mongodb_exporter服务
root@vm-centos75-0-69 com]# nohup ./mongodb_exporter -mongodb.uri mongodb://mongodb_exporter:123456@192.168.0.74:27017 >> /opt/cosmo/logs/mongodb_exporter.out 2>&1 &
8.3 关闭
# 查询mongodb_exporter服务
[root@vm-centos75-0-69 ~]# ps -ef|grep mongodb_exporter
root     12484 31301  0 13:45 pts/0    00:00:02 ./mongodb_exporter -mongodb.uri mongodb://mongodb_exporter:123456@192.168.0.74:27017
root     13206 31301  0 13:59 pts/0    00:00:00 grep --color=auto mongodb_exporter
# 删除mongodb_exporter进程
[root@vm-centos75-0-69 ~]# pkill mongodb_exporter

将mongodb_exporter设置服务管理

vim /etc/init.d/mongodb_exporter
#!/bin/bash
IP=`ip addr | grep -v virbr |grep -o -e 'inet [0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}'|grep -v "127.0.0"|awk '{print $2}'`
PORT=27017
COMMAND=/opt/cosmo/com/mongodb_exporter
SERVER=mongodb_exporter
USER=mongodb_exporter
PASSWORD=123456
PIDNUM=`pidof $SERVER`
start(){
if [ -z "$PIDNUM" ];then
    $COMMAND -mongodb.uri mongodb://$USER:$PASSWORD@$IP:$PORT >/dev/null 2>&1 &
    echo "Now,the server has runing"
else
    echo "$0 is running"
fi
}
stop(){
if [ -z "$PIDNUM" ];then
    echo "$0 is not running"
else
    echo "shutting down $0"
    kill -9 "$PIDNUM" && echo "PID $PIDNUM was killed."
fi
}
status(){
if [[ -z "$PIDNUM" ]];then
    echo "$0 is not runing"
else
    echo "$0 is runing,it's PID is $PIDNUM"
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo "Usage:$0 {start|stop|status|restart}"
;;
esac

管理服务

#赋予执行权限
chmod a+x /etc/init.d/mongodb_exporter

#查看服务状态
service mongodb_exporter status

#启动服务
service mongodb_exporter start

九、配置nginx_vts_exporter

9.1 解压
nginx-module-vts:Nginx的监控模块,能够提供JSON格式的数据产出。 nginx-vts-exporter:主要用于收集Nginx的监控数据,并给Prometheus提供监控接口,默认端口号9913。

# 进入存放目录
[root@vm-centos75-0-69 ~]# cd /opt/cosmo/tools/

# 解压nginx-module-vts模块
[root@vm-centos75-0-69 tools]# tar -Pxvf nginx-module-vts-0.1.18.tar.gz

# 解压nginx-vts-exporter
[root@vm-centos75-0-69 tools]# tar -Pxvf nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
9.2 编译配置
(在安装nginx的服务器上添加nginx-module-vts)

# 进入nginx-1.16.1存放目录
[root@vm-centos75-0-69 ~]# cd /usr/local/nginx-1.16.1/

# 在nginx 编译时添加vts模块--add-module=/usr/local/nginx-module-vts-0.1.18 
[root@vm-centos75-0-69 nginx-1.16.1]# ./configure --add-module=/usr/local/nginx-module-vts-0.1.18  && make && make install
注:
如果报找不到C编译器,需要下载gcc等依赖(在线或离线下载依赖)
以下是离线下载,安装文件夹是rpm文件夹下的所有.rpm文件
[root@vm-centos75-0-69 rpm]# rpm  -ivh  *.rpm --nodeps --force

# 进入nginx.conf存放目录
[root@vm-centos75-0-69 ~]# cd /usr/local/nginx/conf/
nginx.conf配置添加
添加成功后--->(监控数据的查看地址http://192.168.0.71:8088/status)

vhost_traffic_status_zone;
vhost_traffic_status_filter_by_host on;

server {
    listen        8088; 
    location /status {
        vhost_traffic_status_display;
        vhost_traffic_status_display_format html;
    }
}

9.3 启动

# 进入nginx-vts-exporter存放目录
[root@vm-centos75-0-69 /]# cd /opt/cosmo/com/nginx-vts-exporter-0.10.3.linux-amd64/

# 授权
[root@vm-centos75-0-69 nginx-vts-exporter-0.10.3.linux-amd64]# chmod +x nginx-vts-exporter

# 启动nginx-vts-exporter服务
[root@vm-centos75-0-69 nginx-vts-exporter-0.10.3.linux-amd64]# nohup ./nginx-vts-exporter -nginx.scrape_uri=http://192.168.0.71:8088/status/format/json >> /opt/cosmo/logs/nginx_vts_exporter.out 2>&1 &
9.4 关闭

# 查询nginx-vts-exporter服务
[root@vm-centos75-0-71 nginx-vts-exporter-0.10.3.linux-amd64]# ps -ef|grep nginx-vts-exporter
root     19247  7164  0 16:12 pts/0    00:00:01 ./nginx-vts-exporter -nginx.scrape_uri=http://192.168.0.71:8088/status/format/json
root     22758  7164  0 17:22 pts/0    00:00:00 grep --color=auto nginx-vts-exporter

# 删除nginx-vts-exporter进程
[root@vm-centos75-0-71 ~]# pkill nginx-vts-exporter

No pain, no gain!