欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 网站监控方案

程序员文章站 2022-12-25 18:02:55
前言:监控web网站方法有很多种,这篇文章说一下对windows服务器 asp.net网站的监控 采用的方案,Powershell + Influxdb + Grafana 1、PowerShell + Influxdb PowerShell用来收集IIS指标,10秒采集一次,然后写入Influxd ......

  前言:监控web网站方法有很多种,这篇文章说一下对windows服务器 asp.net网站的监控

 采用的方案,powershell + influxdb + grafana

1、powershell + influxdb

 powershell用来收集iis指标,10秒采集一次,然后写入influxdb,主要代码如下:需要注意powershell需要升级到5.0才能支持influxdb

 标签分别是:server主机名 ,appname是网站名称

 1 function waitsec{  
 2     $step=10 #设置间隔  
 3     $add=0 #设置延时  
 4     $t=(get-date)  
 5     $step-(($t.hour*3600+$t.minute*60+$t.second)%$step)+$add  
 6 }  
 7 function getdata($cluster,$dept,$group,$project,$type)
 8 {
 9     $commandset=@(
10     "\web service(*)\current anonymous users",
11     "\web service(*)\current connections",
12     "\web service(*)\current nonanonymous users",
13     "\web service(*)\current blocked async i/o requests",
14     "\web service(*)\maximum anonymous users",
15     "\web service(*)\measured async i/o bandwidth usage",
16     "\web service(*)\total blocked async i/o requests",
17     "\web service(*)\total get requests",
18     "\web service(*)\total method requests",
19     "\web service(*)\total method requests/sec",
20     "\web service(*)\total post requests",
21     "\web service(*)\total put requests",
22     "\web service(*)\delete requests/sec",
23     "\web service(*)\get requests/sec",
24     "\web service(*)\options requests/sec",
25     "\web service(*)\post requests/sec",
26     "\web service(*)\put requests/sec",
27     "\web service(*)\other request methods/sec",
28     "\http service request queues(*)\currentqueuesize",
29     "\http service request queues(*)\rejectedrequests",
30     "\.net clr exceptions(*)\# of exceps thrown / sec",
31     "\process(w3wp*)\thread count",
32     "\process(w3wp*)\% processor time",
33     "\process(w3wp*)\working set - private",
34     "\process(w3wp*)\working set",
35     "\process(w3wp*)\private bytes"
36     )
37     $res= get-counter -counter $commandset
38     $index=0
39     $metricappname=""
40     $timestamp=[int] (get-date (get-date).touniversaltime() -uformat "%s")
41     $host_name = hostname
42     $table_name=""
43     while($res.countersamples[$index])
44     { 
45         $metrics1=@{}
46         $value= $res.countersamples[$index].cookedvalue  
47         $metric=$res.countersamples[$index].path 
48         $metricappname=$res.countersamples[$index].instancename
49         $temparray=$metric.replace("\\","").split("\")
50         $metric=$temparray[2]
51         $metrics1.$metric = $value
52         if($temparray[1].startswith('web service'))
53         {   
54             $table_name = "iis_web_service"
55         }
56         elseif($temparray[1].startswith('http service'))
57         {
58             $table_name = "iis_http_service"
59         }
60         elseif($temparray[1].startswith('.net clr exceptions'))
61         {
62             $table_name = "iis_net_clr_exceptions"
63         }
64         elseif($temparray[1].startswith('process(w3wp'))
65         {
66             $table_name = "iis_process"
67         }
68            write-influx -measure $table_name -tags @{server = $host_name;  appname = $metricappname;} -metrics $metrics1 -database monitor -server http://influxdb:9096
69         $index = $index + 1
70     }
71 }
72 write-host "running...... please wait" (waitsec)"s"
73 start-sleep -s (waitsec) 
74 while(1){  
75     #执行代码  
76     get-date  
77     (getdata)
78     #……  
79     start-sleep -s (waitsec)  
80 }

写入influxdb后的iis_http_service表的数据格式:

asp.net 网站监控方案

2、grafana 配置展示

这里主要对webapi展示了当前连接数,当前排队数,和请求速率。

asp.net 网站监控方案

整体效果:

asp.net 网站监控方案

 

还可以在grafana告警里面设置一个webhook,进行处理告警后的逻辑,比如:当这台机器压力比较大时可以对其进行从负载均衡移除 等等。

 

 

 

asp.net 网站监控方案