基于Prometheus的.NET 4.x应用服务监控
【.NET監控】|?總結/Edison Zhou
0Why 監控?
Edison所在團隊95%以上的應用都是基于.NET 4.5開發的,只能跑在Windows Server服務器上的IIS中,公司運維也沒有意愿對Windows Server進行有效的管理和提供監控支持,整得我們無法及時查看有效的性能狀況(老實說,之前啥都沒有)。
因此,我們需要自行搭建一套適合.NET 4.5應用的監控環境,Prometheus是首選。
1準備工作
下載Prometheus:https://prometheus.io/download/,最新版本:2.33.3
下載Grafana:https://grafana.com/grafana/download,最新版本:8.3.6
下載Windows Exporter:https://github.com/prometheus-community/windows_exporter/releases,最新版本:0.18.1,建議選擇msi安裝文件。
Note:針對Windows Server服務器,請選擇對應的Windows版本(amd64后綴)下載安裝。
2Prometheus服務搭建
第一步,安裝Prometheus Windows版本,假設這里安裝在192.168.80.100服務器上。
第二步,打開prometheus.yml配置文件,將需要監控的服務器添加到配置文件中,假設我們需要對192.168.80.1 ~ 10 共計10臺機器進行監控。這里我加了一些label,便于后續我進行篩選。
#?my?global?config global:scrape_interval:?60s?#?Set?the?scrape?interval?to?every?15?seconds.?Default?is?every?1?minute.evaluation_interval:?60s?#?Evaluate?rules?every?15?seconds.?The?default?is?every?1?minute.#?scrape_timeout?is?set?to?the?global?default?(10s)......#?A?scrape?configuration?containing?exactly?one?endpoint?to?scrape: #?Here?it's?Prometheus?itself. scrape_configs:#?The?job?name?is?added?as?a?label?`job=<job_name>`?to?any?timeseries?scraped?from?this?config.-?job_name:?"Prometheus-Service"#?metrics_path?defaults?to?'/metrics'#?scheme?defaults?to?'http'.static_configs:-?targets:?["192.168.80.100:9090"]-?job_name:?'Application-Server-Host'static_configs:-?targets:?['192.168.80.1:9182','192.168.80.2:9182','192.168.80.3:9182','192.168.80.4:9182']labels:type:?Windows-Serverstatus:?InUsegroup:?BIZ-PURCHASER-?targets:?['192.168.80.5:9182','192.168.80.6:9182','192.168.80.7:9182','192.168.80.8:9182']labels:type:?Windows-Serverstatus:?InUsegroup:?BIZ-SUPPLIER-?targets:?['192.168.220.9:9182','192.168.220.10:9182']labels:type:?Windows-Serverstatus:?InUsegroup:?BIZ-JOB第三步,雙擊prometheus.exe啟動(不推薦)。
第四步,將prometheus.exe注冊為windows服務,開機自啟動(推薦),可選方式:借助NSSM將exe配置為Windows服務。
第五步,在本地瀏覽器訪問 http://localhost:9090 進行驗證,能夠正常訪問就說明安裝并啟動成功。
3Windows Exporter安裝配置
由于Windows Exporter默認并沒有設置收集IIS和TCP連接數據,因此我們需要在安裝目錄下執行以下下面的命令:
msiexec /i windows_exporter-0.18.1-amd64.msi ENABLED_COLLECTORS=cpu,cpu_info,cs,logical_disk,net,os,service,system,textfile,iis,process,memory,tcpWindows Exporter的默認端口是9182,一般不建議修改,當然你如果想修改也可以,修改一下上面的命令增加監聽端口的參數,假設改為監聽8000端口:
msiexec /i windows_exporter-0.18.1-amd64.msi ENABLED_COLLECTORS=cpu,cpu_info,cs,logical_disk,net,os,service,system,textfile,iis,process,memory,tcp LISTEN_PORT=8000執行完上面的命令之后,Windows Exporter會開始自動安裝,安裝完成后會注冊為一個Windows服務,我們可以在Windows服務列表中找到它:
更多內容設置請參考:
https://github.com/prometheus-community/windows_exporter
當所有需要監控的服務器上都安裝了Exporter之后,就可以通過Prometheus查看狀態了:
踩坑記
在安裝過程中,有一臺服務器啟動后報錯
collected metric "windows_iis_worker_request_errors_total" { label:
label:label:counter:<0>} was collected before with the same name and label values0>
collected metric "windows_iis_worker_request_errors_total" { label:
label:label:counter:<0>} was collected before with the same name and label values0>
.......
發現只要加上針對iis的監控就會報如上錯誤,去掉就不會報錯。
這個bug已經體現在了:
https://github.com/prometheus-community/windows_exporter/issues/903
但是,我使用的最新版本還是沒有解決這個問題。
因此,我看了一下issue中的comments,針對這臺服務器將版本退回到了0.16.0版本解決了這個問題。
4Grafana服務搭建
第一步,將安裝包拷貝到服務器并解壓,這里假設部署到192.168.80.100服務器上。
第二步,同Prometheus一樣,將grafana-server.exe注冊為windows服務,開機自啟動(推薦),可選方式:借助NSSM將exe配置為Windows服務。
第三步,啟動grafana-server.exe,可以通過訪問 http://localhost:3000 進行驗證,默認賬號:admin/admin。
第四步,配置data source,增加Prometheus數據源。
第五步,導入dashboard模板,這里我選擇了兩個模板,一個是展示Windows Server主機性能指標(10467),另一個則展示IIS應用站點的性能指標(13610)。
點擊Import即可完成dashboard導入。
更多dashboard請參考:
https://grafana.com/grafana/dashboards
5監控面板體驗
Windows Server主機監控
該面板主要針對服務器主機的各項性能指標進行監控展示,主要包括:CPU使用率、內存使用率、磁盤IO、網絡讀寫情況等。
IIS .NET應用監控面板
該面板主要針對服務器主機上部署的IIS應用站點的各項性能指標進行監控展示,主要包括:TCP連接數、總體HTTP請求率、請求用戶數等。
End總結
本文介紹了如何基于Prometheus+Grafana+Windows Exporter監控Windows Server服務器及IIS的.NET應用,除此之外,還可以基于altermanager構建報警機制,實現自定義報警規則及時通知(通過email或釘釘等)。這里由于我司運維已經構建了,我就沒有重復搭了。
年終總結:Edison的2021年終總結
數字化轉型:我在傳統企業做數字化轉型
C#刷題:C#刷劍指Offer算法題系列文章目錄
.NET面試:.NET開發面試知識體系
.NET大會:2020年中國.NET開發者大會PDF資料
總結
以上是生活随笔為你收集整理的基于Prometheus的.NET 4.x应用服务监控的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于事件驱动架构构建微服务第19部分:使
- 下一篇: ASP.NET Core 替换 Acti