Prometheus 是一個開放性的監(jiān)控解決方案,用戶可以非常方便的安裝和使用 Prometheus 并且能夠非常方便的對其進行擴展。
在Prometheus的架構(gòu)設(shè)計中,Prometheus Server 并不直接服務(wù)監(jiān)控特定的目標,其主要任務(wù)負責數(shù)據(jù)的收集,存儲并且對外提供數(shù)據(jù)查詢支持。因此為了能夠能夠監(jiān)控到某些東西,如主機的CPU使用率,我們需要使用到Exporter。Prometheus周期性的從Exporter暴露的HTTP服務(wù)地址(通常是/metrics)拉取監(jiān)控樣本數(shù)據(jù)。
Exporter可以是一個相對開放的概念,其可以是一個獨立運行的程序獨立于監(jiān)控目標以外,也可以是直接內(nèi)置在監(jiān)控目標中。只要能夠向Prometheus提供標準格式的監(jiān)控樣本數(shù)據(jù)即可。
1 環(huán)境配置
我們在 Windows 下安裝 Prometheus。
1.1 安裝 Prometheus
下載地址:https://prometheus.io/download/
選擇 Windows 安裝包,我選擇的是prometheus-2.41.0.windows-amd64, 下載完成后解壓,直接運行 prometheus.exe 即可。
prometheus默認端口是9090,在瀏覽器訪問:http://localhost:9090,即可看到項目已經(jīng)在運行。

Prometheus 的相關(guān)配置可以在 prometheus.yaml 中修改。
1.2 安裝 NodeExporter
NodeExporter 是 Prometheus 提供的一個可以采集到主機信息的應(yīng)用程序,它能采集到機器的 CPU、內(nèi)存、磁盤等信息。
下載地址: https://prometheus.io/download/
選擇 Windows 版本,我選擇的是windows_exporter-0.20.0-amd64,下載完成后直接運行 windows_exporter-0.20.0-amd64.exe 文件即可。
windows_exporter默認端口是9182,通過瀏覽器訪問:http://localhost:9182/metrics,

可以看到當前 node exporter 獲取到的當前主機的所有監(jiān)控數(shù)據(jù)。 其中 HELP 用于解釋當前指標的含義,TYPE 則說明當前指標的數(shù)據(jù)類型。
2 添加數(shù)據(jù)源
編輯 prometheus 的配置文件 prometheus.yml,將scrape_configs修改為如下內(nèi)容:
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
# node exporter 監(jiān)控源
- job_name: 'prometheus2'
static_configs:
- targets: ['localhost:8080']
即配置了兩個任務(wù)。一個名為 prometheus,其從「localhost:9090」地址讀取數(shù)據(jù)。另一個名為 prometheus2,其從「localhost:8080」地址讀取數(shù)據(jù)。 然后重啟 Prometheus。
瀏覽器訪問:http://localhost:9090,在搜索框輸入up,點擊execute,即可看到我們配置的兩個任務(wù):

3 自定義寫入的數(shù)據(jù)
新建 SpringBoot 項目。完整項目地址:
GitHub地址:https://github.com/Snowstorm0/learn-prometheus
Gitee地址:https://gitee.com/Snowstorm0/learn-prometheus
在 service 層編寫插入數(shù)據(jù)的代碼:
public void insertPrometheus() {
meterRegistry.clear();
setIdList();
setNameMap();
setValueMap();
for (String id : idList) {
List
在 controller 層編寫讀取的代碼:
@RequestMapping(value = "/metric/custom", method = RequestMethod.GET,produces = "text/plain; charset=utf-8")
public Object metric() {
return prometheusMeterRegistry.scrape();
}
用瀏覽器或者Postman訪問: http://localhost:8081/metric/custom
可以看到寫入的數(shù)據(jù):
# HELP insertPrometheus
# TYPE insertPrometheus gauge
insertPrometheus{id="1002",name="錢二",} 1002.0
insertPrometheus{id="1001",name="趙一",} 1001.0
insertPrometheus{id="1003",name="孫三",} 1003.0
這里的數(shù)據(jù)是放在本地的,可以供 Prometheus 讀取。
4 更新數(shù)據(jù)
在 service 層編寫插入數(shù)據(jù)的代碼:
public void updatePrometheus() {
String name = "updatePrometheus";
List
用瀏覽器或者Postman訪問: http://localhost:8081/metric/custom
可以看到寫入的數(shù)據(jù):
updatePrometheus{id="1001",name="測試更新",} 1.0
學習更多編程知識,請關(guān)注我的公眾號:
[代碼的路]
-
JAVA
+關(guān)注
關(guān)注
20文章
3001瀏覽量
116456 -
Prometheus
+關(guān)注
關(guān)注
0文章
36瀏覽量
2054
發(fā)布評論請先 登錄
使用Prometheus和Grafana的企業(yè)級監(jiān)控落地實戰(zhàn)
Prometheus告警規(guī)則編寫與Alertmanager通知配置實戰(zhàn)
使用VictoriaMetrics的Prometheus遠程存儲方案
CW32系列MCU在Eclipse GCC + JLink下的使用示例分享
在Ubuntu上安裝iverilog 12.0方法
燒錄工具操作教程:新手也能快速掌握~
Zabbix與Prometheus運維監(jiān)控系統(tǒng)的對比
如何構(gòu)建高可用Prometheus監(jiān)控體系
【VisionFive 2單板計算機試用體驗】安裝openplc
詳解Prometheus的數(shù)據(jù)類型
使用Prometheus與Grafana實現(xiàn)MindIE服務(wù)可視化監(jiān)控功能
prometheus下載安裝教程
評論