通过上一篇(四)Prometheus + Grafana 可视化 的学习,我们已经对 prometheus 做了一个精美的可视化,本篇详细讲解 Prometheus + Grafana 监控 apache 服务。
准备环境
# 这里使用的是(四)Prometheus + Grafana 可视化 完成时的环境
centos 7
prometheus 运行正常
grafana 运行正常
[root@fp-21 ~]# getenforce
Permissive
[root@fp-21 ~]# ss -lntp |grep prometheus
LISTEN 0 128 :::9090 :::* users:(("prometheus",pid=10531,fd=6))
[root@fp-21 ~]# ss -lntp |grep grafana
LISTEN 0 128 10.0.0.21:3000 *:* users:(("grafana-server",pid=10690,fd=7))
安装 Apache
[root@fp-21 ~]# yum -y install httpd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.tuna.tsinghua.edu.cn
……
Dependency Installed:
mailcap.noarch 0:2.1.41-2.el7
Complete!
安装 Apache exporter 收集 nginx 的 json 数据,点击下载
# 本地下载也可以
[root@fp-21 ~]# cd /opt/ ; mkdir apache_exporter ; cd apache_exporter
[root@fp-21 apache_exporter]# wget https://github.com/Lusitaniae/apache_exporter/releases/download/v0.5.0/apache_exporter-0.5.0.linux-amd64.tar.gz
[root@fp-21 apache_exporter]# ls
apache_exporter-0.5.0.linux-amd64.tar.gz
# 解压
[root@fp-21 apache_exporter]# tar xf apache_exporter-0.5.0.linux-amd64.tar.gz
[root@fp-21 apache_exporter]# ls
apache_exporter-0.5.0.linux-amd64 apache_exporter-0.5.0.linux-amd64.tar.gz
# 移动位置
[root@fp-21 apache_exporter]# mv apache_exporter-0.5.0.linux-amd64 /usr/local/apache_exporter
[root@fp-21 apache_exporter]# cd !$
cd /usr/local/apache_exporter
[root@fp-21 apache_exporter]# ls
apache_exporter LICENSE
[root@fp-21 apache_exporter]# cp apache_exporter /usr/local/bin
启动 apahce 服务
# 启动
[root@fp-21 apache_exporter]# systemctl start httpd
# 查看进程
[root@fp-21 apache_exporter]# ps -ef |grep httpd
root 15455 1 0 21:06 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 15456 15455 0 21:06 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 15457 15455 0 21:06 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 15458 15455 0 21:06 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 15459 15455 0 21:06 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 15460 15455 0 21:06 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 15462 15029 0 21:07 pts/1 00:00:00 grep --color=auto httpd
# 查看端口
[root@fp-21 apache_exporter]# ss -lntp |grep httpd
LISTEN 0 128 :::80 :::* users:(("httpd",pid=15460,fd=4),("httpd",pid=15459,fd=4),("httpd",pid=15458,fd=4),("httpd",pid=15457,fd=4),("httpd",pid=15456,fd=4),("httpd",pid=15455,fd=4))
启动 apache_exporter
[root@fp-21 ~]# apache_exporter
INFO[0000] Starting apache_exporter (version=0.5.0, branch=HEAD, revision=f6a5b4814ea795ee9eac745c55649cce9e5117a9) source="apache_exporter.go:305"
INFO[0000] Build context (go=go1.9.2, user=root@0fdc4d8924f5, date=20171113-21:19:13) source="apache_exporter.go:306"
INFO[0000] Starting Server: :9117 source="apache_exporter.go:307"
查看是否启动成功
[root@fp-21 ~]# ss -lntp |grep 9117
LISTEN 0 128 :::9117 :::* users:(("apache_exporter",pid=15466,fd=3))
添加监控至 Prometheus
[root@fp-21 ~]# vim /usr/local/prometheus/conf/prometheus.yml
35 - job_name: 'apache_status' # 标签名,监控名
36 static_configs:
37 - targets: ['10.0.0.21:9117'] # IP:端口
查看 Prometheus 状态
报错:无法收集到页面信息
ERRO[0206] Error scraping apache: Status 404 Not Found (404): <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /server-status/ was not found on this server.</p>
</body></html>
source="apache_exporter.go:287"
添加 apache 模板
[root@fp-21 ~]# vim /etc/httpd/conf/httpd.conf
354 LoadModule status_module modules/mod_status.so
355 <location /server-status>
356 SetHandler server-status
357 Order Deny,Allow
358 Deny from nothing
359 Allow from all
360 </location>
访问测试数据页面
再次启动
[root@fp-21 ~]# apache_exporter
INFO[0000] Starting apache_exporter (version=0.5.0, branch=HEAD, revision=f6a5b4814ea795ee9eac745c55649cce9e5117a9) source="apache_exporter.go:305"
INFO[0000] Build context (go=go1.9.2, user=root@0fdc4d8924f5, date=20171113-21:19:13) source="apache_exporter.go:306"
INFO[0000] Starting Server: :9117 source="apache_exporter.go:307"
[root@fp-21 ~]# ss -lntp |grep 9117
LISTEN 0 128 :::9117 :::* users:(("apache_exporter",pid=15709,fd=3))
导入 Grafana 模板