PowerShell 服务ping检测mysql版
直接上代码
[void][system.reflection.Assembly]::LoadFrom("C:\v2\mysql.data.dll")
$connection = New-Object MySql.Data.MySqlClient.MySqlConnection
$connection.ConnectionString = "host=172.16.10.103;uid=ping;pwd=kLvs7V_zIcT6ny0V"
$command = New-Object MySql.Data.MySqlClient.MySqlCommand($sql,$connection)
$command.CommandTimeout=600
$command.CommandText="select * from ping.host"
$dt1=New-Object System.Data.DataTable
$dt2=New-Object System.Data.DataTable
$da=New-Object MySql.Data.MySqlClient.MySqlDataAdapter($command)
$null=$da.Fill($dt1)
$command.CommandText="select * from ping.pingstat limit 1"
$da=New-Object MySql.Data.MySqlClient.MySqlDataAdapter($command)
$null=New-Object MySql.Data.MySqlClient.MySqlCommandBuilder($da)
$null=$da.Fill($dt2)
$getping={param($daddr)
$time=(get-date)
$ping = New-Object System.Net.NetworkInformation.Ping
$ping.send($daddr,4000)|select @{N="DAddr";E={$_.Address}},@{N="DName";E={$daddr}},Status,RoundtripTime,@{N="Ptime";E={$time}}
}
$results=New-Object system.collections.arraylist
$rsp=[System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool()
[void]$rsp.SetMaxRunspaces(2000)
[void]$rsp.SetMinRunspaces(300)
$rsp.Open()
$netid=gwmi Win32_NetworkAdapter |?{$_.NetConnectionStatus}| Select-Object DeviceID
$hostname=ForEach ($id in $netid ){gwmi Win32_NetworkAdapterConfiguration|? {$_.index -eq $id.DeviceID -and $_.DefaultIPGateway -ne $null}}
$ipadd=$hostname.IPAddress[0].tostring()
$ipadd
$serlist=$dt1|?{$_.ipaddr -ne $ipadd -and $_.flag}
$pcount=300
$t=(get-date)
Start-Sleep -s (299-(($t.Minute*60+$t.Second)%300))
for($i=0;$i -lt $pcount;$i++){
sleep -m (1000-(get-date).Millisecond)
foreach($server in $serlist){
$gpc=[powershell]::Create()
$gpc.RunspacePool=$rsp
[void]$gpc.AddScript($getping)
[void]$gpc.AddParameter("daddr",$server.ipaddr)
$AsyncResult=$gpc.BeginInvoke()
$result=New-Object psobject|select output,result,thread
$result.output=$null
$result.result=$AsyncResult
$result.thread=$gpc
[void]$results.add($result)
}
$i
}
$ptime=$t.AddSeconds(600-($t.Minute*60+$t.Second)%300)
do{
sleep 3
$tr=$Results|%{$_.result}|?{!($_.IsCompleted)}
Write-Host "总进程数$($Results.count) 完成数$($Results.count-$t.length) 未完成数$($t.length)……"
}while($tr)
foreach($thread in $Results){
$thread.output=$thread.thread.EndInvoke($thread.result)
}
$rsp.Close()
$pingstat=$Results|%{$_.output}|sort dname,Ptime
$dnames=$pingstat|group dname|select name
foreach($dname in $dnames){
$ping=$pingstat|?{$_.DName -eq $dname.name -and $_.Status -eq "Success"}|Measure -property RoundtripTime -max -min -average
$newrow=$dt2.newrow()
$newrow["sid"]=($dt1|?{$_.ipaddr -eq $ipadd}).id
$newrow["did"]=($dt1|?{$_.ipaddr -eq $dname.name}).id
$newrow["pmin"]=[int]$ping.Minimum
$newrow["pavg"]=[int]$ping.Average
$newrow["pmax"]=[int]$ping.Maximum
$newrow["loss"]=$pcount-$ping.Count
$newrow["ptime"]=$ptime
$dt2.rows.add($newrow)
}
[void]$da.Update($dt2)
foreach($ping in $pingstat){
if($ping.Status -ne "Success"){
$sid=($dt1|?{$_.ipaddr -eq $ping.SAddr}).id
$did=($dt1|?{$_.ipaddr -eq $ping.DName}).id
$status=$ping.Status
$ptime=($ping.Ptime).ToString("yyyy-MM-dd HH:mm:ss")
$lasttime=($ping.Ptime.AddSeconds(-1)).ToString("yyyy-MM-dd HH:mm:ss")
$command.CommandText="UPDATE ping.errorlog
SET count = count+1
,ptime ='$ptime'
WHERE sid=$sid and did=$did and status='$status' and ptime='$lasttime'"
if($connection.State -ne "Open"){$connection.Open()}
if(!$command.ExecuteNonQuery()){
$command.CommandText="INSERT INTO ping.errorlog (sid,did,count,status,ptime) VALUES ($sid,$did,1,'$status','$ptime')"
$null=$command.ExecuteNonQuery()
}
}
}
exit