在5分钟内用Prometheus和Grafana监视Apache Web Server

时间:2020-02-23 14:30:57  来源:igfitidea点击:

欢迎使用我们的指南,该指南介绍了如何在5分钟内用Prometheus和Grafana监视Apache Web Server。此设置适用于在各种Linux上运行的任何版本的Apache Web服务器。

如果我们正在遵循本指南,我希望我们已安装并正在运行Prometheus服务器,则可以参考我们的指南以在Ubuntu或者CentOS服务器上全新安装Prometheus服务器。

在CentOS 7和Ubuntu上安装Prometheus Server

请遵循以下设置步骤,将Apache Web Server指标存储在Prometheus上,并使用Grafana进行可视化。

下载并安装Apache Prometheus导出器

如果计算机中尚未安装curl实用程序,请安装它。

--- Ubuntu/Debian --
$sudo apt update && sudo apt install curl

--- CentOS/RHEL/Fedora --
$sudo dnf -y install curl
$sudo yum -y install curl

检查最新版本的Apache Prometheus导出器。

curl -s https://api.github.com/repos/Lusitaniae/apache_exporter/releases/latest   | grep browser_download_url   | grep linux-amd64 | cut -d '"' -f 4 | wget -qi 

提取下载的存档:

tar xvf apache_exporter-*.linux-amd64.tar.gz
sudo cp apache_exporter-*.linux-amd64/apache_exporter /usr/local/bin
sudo chmod +x /usr/local/bin/apache_exporter

确保apache_exporter是可从当前SHELL执行的:

$apache_exporter --version
apache_exporter, version  (branch: , revision: )
  build user:
  build date:
  go version:       go1.13.9

创建Apache Prometheus导出器systemd服务

首先,添加prometheus用户,它将运行该服务:

sudo groupadd --system prometheus
sudo useradd -s /sbin/nologin --system -g prometheus prometheus

然后继续创建一个systemd服务单元文件:

sudo vim /etc/systemd/system/apache_exporter.service

添加以下内容:

[Unit]
Description=Prometheus
Documentation=https://github.com/Lusitaniae/apache_exporter
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=prometheus
Group=prometheus
ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/usr/local/bin/apache_exporter \
  --insecure \
  --scrape_uri=http://localhost/server-status/?auto \
  --telemetry.address=0.0.0.0:9117 \
  --telemetry.endpoint=/metrics

SyslogIdentifier=apache_exporter
Restart=always

[Install]
WantedBy=multi-user.target

该服务将侦听端口9117,并在/metrics URI上公开指标。如果Apache指标不在http://localhost/server-status /?auto上,则需要更改URL。

带有初始化系统

对于CentOS 6.x之类的Init系统,在/etc/init.d /下创建一个init脚本。

sudo vim /etc/init.d/apache_exporter
sudo vim /etc/init.d/apache_exporter

加:

#!/bin/bash
# Author: Josphat Mutai, theitroad@localhost , https://github.com/jmutai
# apache_exporter     This shell script takes care of starting and stopping Prometheus apache exporter 
## chkconfig: 2345 80 80
# description: Prometheus apache exporter  start script
# processname: apache_exporter
# pidfile: /var/run/apache_exporter.pid

# Source function library.
. /etc/rc.d/init.d/functions

RETVAL=0
PROGNAME=apache_exporter
PROG=/usr/local/bin/${PROGNAME}
RUNAS=prometheus
LOCKFILE=/var/lock/subsys/${PROGNAME}
PIDFILE=/var/run/${PROGNAME}.pid
LOGFILE=/var/log/${PROGNAME}.log
DAEMON_SYSCONFIG=/etc/sysconfig/${PROGNAME}

# GO CPU core Limit

#GOMAXPROCS=$(grep -c ^processor /proc/cpuinfo)
GOMAXPROCS=1

# Source config

. ${DAEMON_SYSCONFIG}

start() {
    if [[ -f $PIDFILE ]] > /dev/null; then
        echo "apache_exporter  is already running"
        exit 0
    fi

    echo -n "Starting apache_exporter  service…"
    daemonize -u ${USER} -p ${PIDFILE} -l ${LOCKFILE} -a -e ${LOGFILE} -o ${LOGFILE} ${PROG} ${ARGS}
    RETVAL=$?
    echo ""
    return $RETVAL
}

stop() {
    if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
        echo "Service not running"
        return 1
    fi
    echo 'Stopping service…'
    #kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
    killproc -p ${PIDFILE} -d 10 ${PROG}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${LOCKFILE} ${PIDFILE}
    return $RETVAL
}

status() {
    if [ -f "$PIDFILE" ] || kill -0 $(cat "$PIDFILE"); then
      echo "apache exporter  service running..."
      echo "Service PID: `cat $PIDFILE`"
    else
      echo "Service not running"
    fi
     RETVAL=$?
     return $RETVAL
}

# Call function
case "" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        status
        ;;
    *)
        echo "Usage: 
sudo yum -y install daemonize
{start|stop|restart}" exit 2 esac

安装daemonize软件包:

sudo vim /etc/sysconfig/apache_exporter

创建参数配置文件:

ARGS="--insecure --scrape_uri=http://localhost/server-status/?auto --telemetry.address=0.0.0.0:9117 --telemetry.endpoint=/metrics"

加:

$sudo /etc/init.d/apache_exporter
Usage: /etc/init.d/apache_exporter {start|stop|restart}

测试脚本:

sudo systemctl daemon-reload
sudo systemctl start apache_exporter.service
sudo systemctl enable apache_exporter.service

启动Apache Prometheus导出程序,并使服务在启动时启动

对于Systemd:

sudo /etc/init.d/apache_exporter start
sudo chkconfig apache_exporter on

对于不带Systemd的Init系统。

--- Systemd --
$systemctl status apache_exporter.service
● apache_exporter.service - Prometheus
   Loaded: loaded (/etc/systemd/system/apache_exporter.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2017-09-12 18:24:01 UTC; 17s ago
     Docs: https://github.com/Lusitaniae/apache_exporter
 Main PID: 23926 (apache_exporter)
    Tasks: 6 (limit: 2377)
   Memory: 4.4M
   CGroup: /system.slice/apache_exporter.service
           └─23926 /usr/local/bin/apache_exporter --insecure --scrape_uri=http://localhost/server-status/?auto --telemetry.address=0.0.0.0:9117 --telemetry.endpoint

Sep 12 18:24:01 deb10 systemd[1]: Started Prometheus.
Sep 12 18:24:01 deb10 apache_exporter[23926]: time="2017-09-12T18:24:01Z" level=info msg="Starting apache_exporter (version=, branch=, revision=)" source="apache_
Sep 12 18:24:01 deb10 apache_exporter[23926]: time="2017-09-12T18:24:01Z" level=info msg="Build context (go=go1.13.9, user=, date=)" source="apache_exporter.go:36
Sep 12 18:24:01 deb10 apache_exporter[23926]: time="2017-09-12T18:24:01Z" level=info msg="Starting Server: 0.0.0.0:9117" source="apache_exporter.go:362"
Sep 12 18:24:01 deb10 apache_exporter[23926]: time="2017-09-12T18:24:01Z" level=info msg="Collect from: http://localhost/server-status/?auto" source="apache_expor
Sep 12 18:24:01 deb10 apache_exporter[23926]: time="2017-09-12T18:24:01Z" level=info msg="listening and wait for graceful stop" source="apache_exporter.go:367"

--- Init System --
$sudo /etc/init.d/apache_exporter status
apache exporter  service running...
Service PID: 1970

我们可以使用以下方法验证服务状态:

$sudo chkconfig --list | grep apache_exporter
apache_exporter 0:off   1:off   2:on    3:on    4:on    5:on    6:off

检查服务状态:

$sudo ss -tunelp | grep 9117
tcp    LISTEN     0      128                   :::9117                 :::*      users:(("apache_exporter",1970,6)) ino:1823474168 sk:ffff880341cd7800

确认端口正在侦听请求。

# Apache Servers
  - job_name: apache1
    static_configs:
      - targets: ['10.1.10.15:9117']
        labels:
          alias: server1-apache

  - job_name: apache2
    static_configs:
      - targets: ['10.1.10.16:9117']
        labels:
          alias: server2-apache

将导出程序作业添加到Prometheus

将作业添加到Prometheus服务器以刮取指标。编辑/etc/prometheus/prometheus.yml

sudo systemctl restart prometheus

重新启动prometheus服务以开始抓取

$telnet 10.1.10.15 9117
Trying 10.1.10.15...
Connected to 10.1.10.15.
Escape character is '^]'.
^]

测试从Prometheus服务器对端口9117的访问

##代码##

第5步:将仪表板添加到Grafana

最后一步是创建自己的仪表板以可视化Apache指标。对于此演示,请使用Ricardo F的" Grafana仪表板"。仪表板ID为3894. 我们应该已经将Prometheus数据源添加到了Grafana,或者使用linkAdd Prometheus数据源添加了一个。

添加数据源后,通过导航到"仪表板">"导入"来导入Apache Grafana仪表板。使用3894作为Grafana仪表板ID。

给它起一个描述性名称,然后选择之前添加的Prometheus数据源。

单击导入按钮以开始使用仪表板。几分钟后,指标应开始显示。

选择其他主机以使用指标仪表板顶部的下拉菜单显示指标。在我的下一个Apache监视指南中,我将介绍如何使用InfluxDB和Grafana监视Apache Web服务器。