前言:监控web网站方法有很多种,这篇文章说一下对windows服务器 asp.net网站的监控
采用的方案,Powershell + Influxdb + Grafana
1、PowerShell + Influxdb
PowerShell用来收集IIS指标,10秒采集一次,然后写入Influxdb,主要代码如下:需要注意PowerShell需要升级到5.0才能支持influxdb
标签分别是:Server主机名 ,AppName是网站名称
function waitsec{
$step= #设置间隔
$add= #设置延时
$t=(get-date)
$step-(($t.Hour*+$t.Minute*+$t.Second)%$step)+$add
}
function GetData($cluster,$dept,$group,$project,$type)
{
$commandSet=@(
"\Web Service(*)\Current Anonymous Users",
"\Web Service(*)\Current Connections",
"\Web Service(*)\Current NonAnonymous Users",
"\Web Service(*)\Current Blocked Async I/O Requests",
"\Web Service(*)\Maximum Anonymous Users",
"\Web Service(*)\Measured Async I/O Bandwidth Usage",
"\Web Service(*)\Total Blocked Async I/O Requests",
"\Web Service(*)\Total Get Requests",
"\Web Service(*)\Total Method Requests",
"\Web Service(*)\Total Method Requests/sec",
"\Web Service(*)\Total Post Requests",
"\Web Service(*)\Total Put Requests",
"\Web Service(*)\Delete Requests/sec",
"\Web Service(*)\Get Requests/sec",
"\Web Service(*)\Options Requests/sec",
"\Web Service(*)\Post Requests/sec",
"\Web Service(*)\Put Requests/sec",
"\Web Service(*)\Other Request Methods/sec",
"\HTTP Service Request Queues(*)\CurrentQueueSize",
"\HTTP Service Request Queues(*)\RejectedRequests",
"\.NET CLR Exceptions(*)\# of Exceps Thrown / sec",
"\Process(w3wp*)\Thread Count",
"\Process(w3wp*)\% Processor Time",
"\Process(w3wp*)\Working Set - Private",
"\Process(w3wp*)\Working Set",
"\Process(w3wp*)\Private Bytes"
)
$res= get-counter -counter $commandSet
$index=
$metricAppName=""
$timestamp=[int] (Get-Date (Get-Date).ToUniversalTime() -uformat "%s")
$host_name = hostname
$table_name=""
while($res.countersamples[$index])
{
$Metrics1=@{}
$value= $res.countersamples[$index].cookedvalue
$metric=$res.countersamples[$index].path
$metricAppName=$res.countersamples[$index].InstanceName
$tempArray=$metric.replace("\\","").split("\")
$metric=$tempArray[]
$Metrics1.$metric = $value
if($tempArray[].startswith('web service'))
{
$table_name = "iis_web_service"
}
Elseif($tempArray[].startswith('http service'))
{
$table_name = "iis_http_service"
}
Elseif($tempArray[].startswith('.net clr exceptions'))
{
$table_name = "iis_net_clr_exceptions"
}
Elseif($tempArray[].startswith('process(w3wp'))
{
$table_name = "iis_process"
}
Write-Influx -Measure $table_name -Tags @{Server = $host_name; AppName = $metricAppName;} -Metrics $Metrics1 -Database monitor -Server http://influxdb:9096
$index = $index +
}
}
write-host "running...... please wait" (waitsec)"S"
Start-Sleep -s (waitsec)
while(){
#执行代码
get-date
(GetData)
#……
Start-Sleep -s (waitsec)
}
写入influxdb后的iis_http_service表的数据格式:
2、Grafana 配置展示
这里主要对webapi展示了当前连接数,当前排队数,和请求速率。
整体效果:
还可以在grafana告警里面设置一个WebHook,进行处理告警后的逻辑,比如:当这台机器压力比较大时可以对其进行从负载均衡移除 等等。