如何使用Prometheus监控您的Ubuntu14.04服务器

来自菜鸟教程
跳转至:导航、​搜索

介绍

Prometheus 是 SoundCloud 开发的开源监控系统。 与其他监控系统(如 InfluxDB 和 Graphite)一样,Prometheus 将其所有数据存储在时间序列数据库中。 但是,它提供了多维数据模型和强大的查询语言,使系统管理员不仅可以轻松微调其指标的定义,还可以生成更准确的报告。

此外,Prometheus 项目还包括 PromDash(一种基于浏览器的工具,可用于开发自定义仪表板)和一个实验性的 AlertManager,能够通过电子邮件、Flowdock、Slack、HipChat 等发送警报。

在本教程中,您将学习如何安装、配置和使用 Prometheus Server、Node Exporter 和 PromDash。

先决条件

要遵循本教程,您将需要:

  • 一个具有 sudo 非 root 用户的 64 位 Ubuntu 14.04 Droplet。

注意:如果您必须使用 32 位服务器,请确保将本教程中提到的所有文件名和链接中的 -amd64 替换为 -386


第 1 步 — 安装 Prometheus 服务器

首先,创建一个新目录来存储您在本教程中下载的所有文件并移至该目录。

mkdir ~/Downloads
cd ~/Downloads

使用 wget 从 GitHub 下载 Prometheus 服务器和时间序列数据库的最新版本。

wget "https://github.com/prometheus/prometheus/releases/download/0.15.1/prometheus-0.15.1.linux-amd64.tar.gz"

Prometheus 监控系统由几个组件组成,每个组件都需要单独安装。 将所有组件保存在一个父目录中是一个好主意,因此创建一个子目录和一个额外的子目录来存储 Prometheus 服务器的所有二进制文件。

mkdir -p ~/Prometheus/server

输入刚刚创建的目录。

cd ~/Prometheus/server

使用 tar 提取 prometheus-0.15.1.linux-amd64.tar.gz

tar -xvzf ~/Downloads/prometheus-0.15.1.linux-amd64.tar.gz

这样就完成了 Prometheus 服务器的安装。 通过键入以下内容来验证安装:

./prometheus -version

您应该在屏幕上看到以下消息:

普罗米修斯输出

prometheus, version 0.15.1 (branch: master, revision: 64349aa)
  build user:       julius@julius-thinkpad
  build date:       20150727-17:56:00
  go version:       1.4.2

第 2 步 — 安装节点导出器

Prometheus 是为监控 Web 服务而开发的。 为了监控 Ubuntu 服务器的指标,您应该安装一个名为 Node Exporter 的工具。 顾名思义,Node Exporter 以 Prometheus 可以理解的格式导出大量指标(例如磁盘 I/O 统计信息、CPU 负载、内存使用情况、网络统计信息等)。

Prometheus 目录中创建一个名为 node_exporter 的新目录,然后进入其中:

mkdir -p ~/Prometheus/node_exporter
cd ~/Prometheus/node_exporter

使用 wget 下载 GitHub 上可用的最新版本的 Node Exporter,并将其放在 Downloads 目录中。

wget https://github.com/prometheus/node_exporter/releases/download/0.11.0/node_exporter-0.11.0.linux-amd64.tar.gz -O ~/Downloads/node_exporter-0.11.0.linux-amd64.tar.gz

您现在可以使用 tar 命令提取 node_exporter-0.11.0.linux-amd64.tar.gz

tar -xvzf ~/Downloads/node_exporter-0.11.0.linux-amd64.tar.gz

第 3 步 — 将节点导出器作为服务运行

为了便于启动和停止 Node Exporter,现在让我们将其转换为服务。

/usr/bin 中创建指向 node_exporter 二进制文件的软链接。

sudo ln -s ~/Prometheus/node_exporter/node_exporter /usr/bin

使用 nano 或您喜欢的文本编辑器创建一个名为 node_exporter.conf 的 Upstart 配置文件。

sudo nano /etc/init/node_exporter.conf

该文件应包含指向 node_exporter 可执行文件的链接,并指定何时启动可执行文件。 因此,添加以下代码:

/etc/init/node_exporter.conf

# Run node_exporter

start on startup

script
   /usr/bin/node_exporter
end script

此时,Node Exporter 作为服务可用,可以使用 service 命令启动:

sudo service node_exporter start

Node Exporter 启动后,使用浏览器在 http://your_server_ip:9100/metrics 上查看其可用的 Web 界面。 您应该会看到一个包含大量文本的页面:

# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0.00023853100000000002
go_gc_duration_seconds{quantile="0.25"} 0.00023998700000000002
go_gc_duration_seconds{quantile="0.5"} 0.00028122
. . .

第 4 步 — 启动 Prometheus 服务器

进入安装 Prometheus 服务器的目录:

cd ~/Prometheus/server

在启动 Prometheus 之前,您必须先为其创建一个名为 prometheus.yml 的配置文件。

nano ~/Prometheus/server/prometheus.yml

将以下代码复制到文件中。

~/Prometheus/server/prometheus.yml

scrape_configs:
  - job_name: "node"
    scrape_interval: "15s"
    target_groups:
    - targets: ['localhost:9100']

这将创建一个 scrape_configs 部分并定义一个名为 node 的作业。 它在 targets 数组中包含 Node Exporter 的 Web 界面的 URL。 scrape_interval 设置为 15 秒,以便 Prometheus 每 15 秒抓取一次指标。

您可以为您的工作命名任何您想要的名称,但将其命名为“node”允许您使用 Node Exporter 的默认控制台模板。

保存文件并退出。

将 Prometheus 服务器作为后台进程启动。

nohup ./prometheus > prometheus.log 2>&1 &

请注意,您将 Prometheus 服务器的输出重定向到名为 prometheus.log 的文件。 您可以使用 tail 命令查看文件的最后几行:

tail ~/Prometheus/server/prometheus.log

服务器准备就绪后,您将在文件中看到以下消息:

prometheus.log 摘录

INFO[0000] Starting target manager...         file=targetmanager.go line=75
INFO[0000] Listening on :9090                 file=web.go line=118

使用浏览器访问 Prometheus 的主页 http://your_server_ip:9090。 您将看到以下主页。

要确保 Prometheus 正在从 Node Exporter 抓取数据,请单击页面顶部的 Graph 选项卡。 在打开的页面上,在显示 Expression 的文本字段中输入指标的名称(例如 node_procs_running)。 然后,按下蓝色的 Execute 按钮。 单击下方的 GraphConsole 旁边),您应该会看到该指标的图表:

Prometheus 有控制台模板,可让您查看一些常用指标的图表。 只有在 Prometheus 的配置中将 job_name 的值设置为 node 时,才能访问这些控制台模板。

访问 http://your_server_ip:9090/consoles/node.html 以访问节点控制台并单击您的服务器 localhost:9100 以查看其指标:

第 5 步 — 安装 PromDash

虽然 Prometheus 服务器允许您查看图形和试验表达式,但它通常仅用于调试目的或运行一次性查询。 在 Prometheus 的时间序列数据库中可视化数据的首选方法是使用 PromDash,该工具允许您创建自定义仪表板,这些仪表板不仅具有高度可配置性,而且外观更好看。

进入Prometheus目录:

cd ~/Prometheus

PromDash 是一个 Ruby on Rails 应用程序,其源文件可在 GitHub 上找到。 为了下载和运行它,你需要安装 Git、Ruby、SQLite3、Bundler,它是一个 gem 依赖管理器,以及它们的依赖。 使用 apt-get 来执行此操作。

sudo apt-get update && sudo apt-get install git ruby bundler libsqlite3-dev sqlite3 zlib1g-dev

您现在可以使用 git 命令下载源文件。

git clone https://github.com/prometheus/promdash.git

进入promdash目录。

cd ~/Prometheus/promdash

使用 bundle 安装 PromDash 所需的 Ruby gem。 由于我们将在本教程中配置 PromDash 以使用 SQLite3,因此请确保使用 --without 参数排除 MySQL 和 PostgreSQL 的 gem:

bundle install --without mysql postgresql

因为 PromDash 依赖于几个 gem,所以您必须等待几分钟才能完成此命令。 完成后,您应该会看到以下消息。

捆绑输出

. . .
Your bundle is complete!
Gems in the groups mysql and postgresql were not installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

第 6 步 — 设置 Rails 环境

创建一个目录来存储与 PromDash 关联的 SQLite3 数据库。

mkdir ~/Prometheus/databases

PromDash 使用名为 DATABASE_URL 的环境变量来确定与其关联的数据库的名称。 键入以下内容,以便 PromDash 在 databases 目录中创建一个名为 mydb.sqlite3 的 SQLite3 数据库:

echo "export DATABASE_URL=sqlite3:$HOME/Prometheus/databases/mydb.sqlite3" >> ~/.bashrc

在本教程中,您将在生产模式下运行 PromDash,因此将 RAILS_ENV 环境变量设置为 production

echo "export RAILS_ENV=production" >> ~/.bashrc

将我们所做的更改应用到 .bashrc 文件。

. ~/.bashrc

接下来,使用 rake 工具在 SQLite3 数据库中创建 PromDash 的表。

rake db:migrate

因为 PromDash 使用 Rails Asset Pipeline,所以 PromDash 项目的所有资产(CSS 文件、图像和 Javascript 文件)都应该被预编译。 为此,请键入以下内容:

rake assets:precompile

第 7 步 — 启动和配置 PromDash

PromDash 在轻量级 Web 服务器 Thin 上运行。 通过键入以下命令将服务器作为守护程序启动:

bundle exec thin start -d

等待服务器启动几秒钟,然后访问 http://your_server_ip:3000/ 以查看 PromDash 的主页。

在开始创建自定义仪表板之前,您应该让 PromDash 知道您的 Prometheus 服务器的 URL。 您可以通过单击顶部的 Servers 选项卡来执行此操作。 单击 New Server,然后在表单中为您的 Prometheus 服务器指定任何名称。 将 Url 字段设置为 http://your_server_ip:9090 并将 服务器类型 字段设置为 Prometheus

最后点击【X18X】创建服务器【X35X】完成配置。 您的页面会显示 Server was created. 并且您可以在顶部菜单中单击返回到 Dashboards

第 8 步 — 创建仪表板

因为 Promdash 仪表板应该属于 Promdash 目录,所以首先通过单击 New Directory 创建一个新目录。 在显示的表单中,为您的目录命名,例如 My Dashboards,然后单击 Create Directory

提交表格后,您将被带回主页。 现在单击 New Dashboard 按钮以创建新仪表板。 在显示的表单中,为您的仪表板命名,例如 Simple Dashboard,然后从下拉菜单中选择您刚刚创建的目录。

提交表单后,您将能够看到新的仪表板。

您的仪表板已经有一个图表,但需要对其进行配置。 将鼠标悬停在图表的标题上(显示为 Title)将显示各种图标,让您可以配置图表。 要更改其标题,您可以单击 Graph and Axis Settings 图标(左起第四个)并在 Graph Title 字段中输入新标题。

单击左侧第二个 Datasources 图标,将一个或多个表达式添加到图形中。 单击 Add Expression,然后在显示 Enter Expression 的字段中,输入 node_procs_running

现在单击图表标题中的 Refresh 图标(最左侧)以更新图表。 您的仪表板现在包含一个完全配置的图表。 您可以通过单击底部的 Add Graph 按钮来添加更多图形。

进行所有更改后,请确保单击右侧的 Save Changes 按钮以使更改永久生效。 下次访问 PromDash 的主页时,您将能够看到指向仪表板的链接:

结论

您现在在 Ubuntu 14.04 服务器上运行了一个功能齐全的 Prometheus 生态系统,您可以使用 PromDash 创建满足您要求的监控仪表板。

即使您在单个 Ubuntu 机器上安装了所有组件,您也可以通过在每台机器上仅安装 Node Exporter 并将新 Node Exporter 的 URL 添加到 prometheus.yml

您可以通过参考其 文档 了解有关 Prometheus 的更多信息。