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

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

可以看到當(dāng)前 node exporter 獲取到的當(dāng)前主機(jī)的所有監(jiān)控?cái)?shù)據(jù)。 其中 HELP 用于解釋當(dāng)前指標(biāo)的含義,TYPE 則說明當(dāng)前指標(biāo)的數(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']
即配置了兩個(gè)任務(wù)。一個(gè)名為 prometheus,其從「localhost:9090」地址讀取數(shù)據(jù)。另一個(gè)名為 prometheus2,其從「localhost:8080」地址讀取數(shù)據(jù)。 然后重啟 Prometheus。
瀏覽器訪問:http://localhost:9090,在搜索框輸入up,點(diǎn)擊execute,即可看到我們配置的兩個(gè)任務(wù):

3 自定義寫入的數(shù)據(jù)
新建 SpringBoot 項(xiàng)目。完整項(xiàng)目地址:
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
學(xué)習(xí)更多編程知識,請關(guān)注我的公眾號:
[代碼的路]
-
JAVA
+關(guān)注
關(guān)注
20文章
3001瀏覽量
116409 -
Prometheus
+關(guān)注
關(guān)注
0文章
36瀏覽量
2053
發(fā)布評論請先 登錄
使用Prometheus和Grafana的企業(yè)級監(jiān)控落地實(shí)戰(zhàn)
Prometheus告警規(guī)則編寫與Alertmanager通知配置實(shí)戰(zhàn)
使用VictoriaMetrics的Prometheus遠(yuǎn)程存儲(chǔ)方案
CW32系列MCU在Eclipse GCC + JLink下的使用示例分享
在Ubuntu上安裝iverilog 12.0方法
燒錄工具操作教程:新手也能快速掌握~
Zabbix與Prometheus運(yùn)維監(jiān)控系統(tǒng)的對比
如何構(gòu)建高可用Prometheus監(jiān)控體系
【VisionFive 2單板計(jì)算機(jī)試用體驗(yàn)】安裝openplc
詳解Prometheus的數(shù)據(jù)類型
使用Prometheus與Grafana實(shí)現(xiàn)MindIE服務(wù)可視化監(jiān)控功能
prometheus下載安裝教程
評論