使用PowerShell检查Tomcat服务器网页
下面有一份好的工作副本。我还修正了变量不能缩写 但是,在“良好”部分下(仅在“其他”部分之外),如果存在内容问题,则希望内容也被检查三次 如果存在web异常,它将正确检查三(3)次。但是,如果服务器已启动并正在运行,则如果重新检查了它要查找的字符串,则不会进行内容检查: 为了清晰起见,我重新写了这篇文章:catch[Net.WebException]我确实试着写我要找的部分,但没能让它工作。我可能不正确,因为[Net.WebException]可能会对此进行错误处理使用PowerShell检查Tomcat服务器网页,powershell,tomcat,smtp,alert,Powershell,Tomcat,Smtp,Alert,下面有一份好的工作副本。我还修正了变量不能缩写 但是,在“良好”部分下(仅在“其他”部分之外),如果存在内容问题,则希望内容也被检查三次 如果存在web异常,它将正确检查三(3)次。但是,如果服务器已启动并正在运行,则如果重新检查了它要查找的字符串,则不会进行内容检查: 为了清晰起见,我重新写了这篇文章:catch[Net.WebException]我确实试着写我要找的部分,但没能让它工作。我可能不正确,因为[Net.WebException]可能会对此进行错误处理 el
else
{
#BAD CONTENT
if ($Retrycount -gt 2){
if (!(Test-Path "$($mypath)\$servern$flag")) {
"$(get-date -Format 'hh:mm, dd/MM/yyyy') - $($string)" | Out-File "$($mypath)\$servern$flag"
Add-Content $fileName "<tr>"
Add-Content $fileName "<td align='center'> $servern ($ip) </td>"
Add-Content $fileName "<td align='center'> $Date </td>"
Add-Content $fileName "<td align='center'> $type </td>"
Add-Content $fileName "<td align='center'> $stringTest is not found </td>"
Add-Content $fileName "</tr>"
Write-Host $stringTest ' is not found'
}
# STOP LOOPING AND LETS GO TO THE NEXT COMPUTER OR QUIT
$Stoploop = $true
}
Write-Host "$RetryCount $servern is not up and running retrying in 10 seconds..."
Start-Sleep -Seconds 10
$Retrycount = $Retrycount + 1
}
}
$Stoploop = $true
}
catch [Net.WebException] {
现在,让我们展示当前的工作代码:
# http://www.thomasmaurer.ch/2010/07/powershell-simple-retry-logic/
$mypath = "d:\WORK\ps";
$serverlist = "$($mypath)\WebCheck_computers.txt";
$fileName = "$($mypath)\webtest.htm";
$checkfile = "$($mypath)\chk.htm";
$stringTest = "have a good database connection";
$flag = "webtestflag.txt";
##########################################################################################################
### CLEAR FILES BEFORE STARTING
Remove-Item $fileName
New-Item -ItemType file $fileName -Force
New-Item -ItemType file $checkfile -Force
##########################################################################################################
### FUNCTIONS
Function writeHtmlHeader {
param($fileName)
$date = (get-date).ToString('MM/dd/yyyy hh:mm:ss')
Add-Content $fileName "<html>"
Add-Content $fileName "<head>"
Add-Content $fileName "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $fileName '<title>Webpage Health Report</title>'
Add-Content $fileName '<STYLE TYPE="text/css">'
Add-Content $fileName "<!--"
Add-Content $fileName "td {"
Add-Content $fileName "font-family: Tahoma;"
Add-Content $fileName "font-size: 11px;"
Add-Content $fileName "border-"
Add-Content $fileName "border-right: 1px solid #999999;"
Add-Content $fileName "border-bottom: 1px solid #999999;"
Add-Content $fileName "border-"
Add-Content $fileName "padding-"
Add-Content $fileName "padding-right: 0px;"
Add-Content $fileName "padding-bottom: 0px;"
Add-Content $fileName "padding-"
Add-Content $fileName "}"
Add-Content $fileName "body {"
Add-Content $fileName "margin-"
Add-Content $fileName "margin-"
Add-Content $fileName "margin-right: 0px;"
Add-Content $fileName "margin-bottom: 10px;"
Add-Content $fileName ""
Add-Content $fileName "table {"
Add-Content $fileName "border: thin solid #000000;"
Add-Content $fileName "}"
Add-Content $fileName "-->"
Add-Content $fileName "</style>"
Add-Content $fileName "</head>"
Add-Content $fileName "<body>"
Add-Content $fileName "<table width='100%' border='1'>"
Add-Content $fileName "<tr bgcolor='#CCCCCC'>"
Add-Content $fileName "<td colspan='4' height='25' align='center'>"
Add-Content $fileName "<font face='tahoma' color='#003399' size='4'><strong>Web Page Health Report - $date</strong></font>"
Add-Content $fileName "</td>"
Add-Content $fileName "</tr>"
}
# Function to write the HTML Footer to the file
Function writeHtmlFooter {
param($fileName)
Add-Content $fileName "</table>"
Add-Content $fileName "</body>"
Add-Content $fileName "</html>"
}
Function sendEmail {
param($htmlFileName)
$from=New-Object System.Net.Mail.MailAddress "Support<support@company.com>"
$to= New-Object System.Net.Mail.MailAddress "Support<support@company.com>"
$subject="Server WebPage HealthCheck - $Date"
$smtphost="1.1.1.1"
$body = Get-Content $htmlFileName
$smtp= New-Object System.Net.Mail.SmtpClient $smtphost
$msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body
$msg.isBodyhtml = $true
$smtp.send($msg)
}
# Function to write the HTML Header to the file
Function writeTableHeader {
param($fileName)
Add-Content $fileName "</table>"
}
# Function to write the HTML Header to the file
Function writeTableHeader {
param($fileName)
Add-Content $fileName "<tr bgcolor=#CCCCCC>"
Add-Content $fileName "<td align='center'>Server Name</td>"
Add-Content $fileName "<td align='center'>Time Test Started</td>"
Add-Content $fileName "<td align='center'>Web Request Type</td>"
Add-Content $fileName "<td align='center'>Web Page Result</td>"
Add-Content $fileName "</tr>"
}
##########################################################################################################
### SCRIPT
remove-item $fileName
writeHtmlHeader $fileName
writeTableHeader $fileName
#writeTableFooter $fileName
#writeHtmlFooter $fileName
foreach ($computer in Get-Content $serverlist) {
$servern=$computer.split(",")[0]
$ip=$computer.split(",")[1]
$type = $computer.split(",")[2]
$url = $computer.split(",")[3]
$Date = Get-Date
$servern
if ($Computer -notmatch 'DB') {
$Stoploop = $false
[int]$Retrycount = "0"
do {
try {
$w = New-Object net.WebClient
$p = New-Object System.Net.WebProxy 'http://proxy:8080'
$p.UseDefaultCredentials = $true
$w.proxy=$p
$w.DownloadFile($url,$checkfile)
if (Test-Path $checkfile) {
## http://stackoverflow.com/questions/18633666/powershell-using-contains-to-check-if-files-contain-a-certain-word
$file = Get-Content $checkfile
$containsWord = $file | %{$_ -match $stringTest}
If($containsWord -contains $true -and $stringTest -gt 3) {
#GOOD CONTENT
if (Test-Path "$($mypath)\$servern$flag") {
remove-item "$($mypath)\$servern$flag"
}
}
else
{
#BAD CONTENT
if (!(Test-Path "$($mypath)\$servern$flag")) {
"$(get-date -Format 'hh:mm, dd/MM/yyyy') - $($string)" | Out-File "$($mypath)\$servern$flag"
Add-Content $fileName "<tr>"
Add-Content $fileName "<td align='center'> $servern ($ip) </td>"
Add-Content $fileName "<td align='center'> $Date </td>"
Add-Content $fileName "<td align='center'> $type </td>"
Add-Content $fileName "<td align='center'> $stringTest is not found </td>"
Add-Content $fileName "</tr>"
Write-Host $stringTest ' is not found'
}
}
}
$Stoploop = $true
}
catch [Net.WebException] {
# AN EXCEPTION WAS PREVIOUSLY FOUND - DO NOT NEED TO GO THROUGH ALL THREE TESTS
if (Test-Path "$($mypath)\$servern$flag") {
$Stoploop = $true
}
# SERVER IS DEFINITELY OFF-LINE AND WE NEED TO GENERATE A REPORT
if ($Retrycount -gt 2){
if (!(Test-Path "$($mypath)\$servern$flag")) {
"$(get-date -Format 'hh:mm, dd/MM/yyyy') - $($string)" | Out-File "$($mypath)\$servern$flag"
Add-Content $fileName "<tr>"
Add-Content $fileName "<td align='center'> $servern ($ip) </td>"
Add-Content $fileName "<td align='center'> $Date </td>"
Add-Content $fileName "<td bgcolor='#FF0000' align='center'><font color='Yellow'> $_.Exception.ToString() </font></td>"
Add-Content $fileName "</tr>"
Write-Host $_.Exception.ToString()
Write-Host $($mypath)\$servern$flag
Write-Host "$RetryCount $servern is not responding after 3 retries."
}
# STOP LOOPING AND LETS GO TO THE NEXT COMPUTER OR QUIT
$Stoploop = $true
}
else {
# LOOP COUNTER IS NOT DONE YET - KEEP GOING
Write-Host "$RetryCount $servern is not up and running retrying in 10 seconds..."
Start-Sleep -Seconds 10
$Retrycount = $Retrycount + 1
}
}
}
While ($Stoploop -eq $false)
}
}
#writeHtmlHeader $fileName
writeHtmlFooter $fileName
$date = (get-date).ToString('MM/dd/yyyy hh:mm:ss')
$file = Get-Content $fileName
##867
if ($(Get-Item $fileName).length -gt 1000) {
sendEmail $fileName
}
##########################################################################################################
### SCRIPT IS DONE
#http://www.thomasmaurer.ch/2010/07/powershell-simple-retry-logic/
$mypath=“d:\WORK\ps”;
$serverlist=“$($mypath)\WebCheck\u computers.txt”;
$fileName=“$($mypath)\webtest.htm”;
$checkfile=“$($mypath)\chk.htm”;
$stringTest=“具有良好的数据库连接”;
$flag=“webtestflag.txt”;
##########################################################################################################
###启动前清除文件
删除项$fileName
新项目-项目类型文件$fileName-强制
新建项目-项目类型文件$checkfile-强制
##########################################################################################################
###功能
函数writeHtmlHeader{
参数($fileName)
$date=(get date).ToString('MM/dd/yyyy hh:MM:ss')
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“网页运行状况报告”
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“网页运行状况报告-$date”
添加内容$fileName“”
添加内容$fileName“”
}
#函数将HTML页脚写入文件
函数writeHtmlFooter{
参数($fileName)
添加内容$fileName“”
添加内容$fileName“”
添加内容$fileName“”
}
发送电子邮件的功能{
参数($htmlFileName)
$from=新对象System.Net.Mail.MailAddress“支持”
$to=新对象System.Net.Mail.MailAddress“支持”
$subject=“服务器网页健康检查-$Date”
$smtphost=“1.1.1.1”
$body=获取内容$htmlFileName
$smtp=新对象System.Net.Mail.SmtpClient$smtphost
$msg=New Object System.Net.Mail.MailMessage$from,$to,$subject,$body
$msg.isBodyhtml=$true
$smtp.send($msg)
}
#函数将HTML头写入文件
函数writeTableHeader{
参数($fileName)
添加内容$fileName“”
}
#函数将HTML头写入文件
函数writeTableHeader{
参数($fileName)
添加内容$fileName“”
添加内容$fileName“服务器名称”
添加内容$fileName“测试开始时间”
添加内容$fileName“Web请求类型”
添加内容$fileName“网页结果”
添加内容$fileName“”
}
##########################################################################################################
###剧本
删除项$fileName
writeHtmlHeader$fileName
writeTableHeader$fileName
#writeTableFooter$fileName
#writeHtmlFooter$fileName
foreach($Get Content$serverlist中的计算机){
$servern=$computer.split(“,”[0]
$ip=$computer.split(“,”[1]
$type=$computer.split(“,”[2]
$url=$computer.split(“,”[3]
$Date=获取日期
$servern
如果($Computer-不匹配'DB'){
$Stoploop=$false
[int]$Retrycount=“0”
做{
试一试{
$w=新对象net.WebClient
$p=新对象System.Net.WebProxy'http://proxy:8080'
$p.UseDefaultCredentials=$true
$w.proxy=$p
$w.DownloadFile($url,$checkfile)
if(测试路径$checkfile){
## http://stackoverflow.com/questions/18633666/powershell-using-contains-to-check-if-files-contain-a-certain-word
$file=获取内容$checkfile
$CONTAINSBROW=$file |%{$\匹配$stringTest}
If($containsWord-包含$true-和$stringTest-gt 3){
#满意
if(测试路径“$($mypath)\$servern$flag”){
删除项“$($mypath)\$servern$标志”
}
}
其他的
{
#不良内容
if(!(测试路径“$($mypath)\$servern$flag”)){
“$(获取日期-格式为'hh:mm,dd/mm/yyyy')-$($string)”|输出文件“$($mypath)\$servern$标志”
添加内容$fileName“”
添加内容$fileName“$servern($ip)”
添加内容$fileName“$Date”
添加内容$fileName“$type”
添加内容$fileName“$stringTest未找到”
添加内容$fileName“”
写入主机$stringTest“未找到”
}
}
}
$Stoploop=$true
}
catch[Net.WebException]{
#以前发现了一个异常-不需要通过所有三个测试
if(测试路径“$($mypath)\$servern$flag”){
$Stoploop=$true
}
#服务器肯定是离线的,我们需要生成一个报告
如果($Retrycount-gt 2){
if(!(测试路径“$($mypath)\$servern$flag”)){
“$(获取日期-格式为'hh:mm,dd/mm/yyyy')-$($string)”|输出文件“$($mypath)\$servern$fla”
# http://www.thomasmaurer.ch/2010/07/powershell-simple-retry-logic/
$mypath = "d:\WORK\ps";
$serverlist = "$($mypath)\WebCheck_computers.txt";
$fileName = "$($mypath)\webtest.htm";
$checkfile = "$($mypath)\chk.htm";
$stringTest = "have a good database connection";
$flag = "webtestflag.txt";
##########################################################################################################
### CLEAR FILES BEFORE STARTING
Remove-Item $fileName
New-Item -ItemType file $fileName -Force
New-Item -ItemType file $checkfile -Force
##########################################################################################################
### FUNCTIONS
Function writeHtmlHeader {
param($fileName)
$date = (get-date).ToString('MM/dd/yyyy hh:mm:ss')
Add-Content $fileName "<html>"
Add-Content $fileName "<head>"
Add-Content $fileName "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $fileName '<title>Webpage Health Report</title>'
Add-Content $fileName '<STYLE TYPE="text/css">'
Add-Content $fileName "<!--"
Add-Content $fileName "td {"
Add-Content $fileName "font-family: Tahoma;"
Add-Content $fileName "font-size: 11px;"
Add-Content $fileName "border-"
Add-Content $fileName "border-right: 1px solid #999999;"
Add-Content $fileName "border-bottom: 1px solid #999999;"
Add-Content $fileName "border-"
Add-Content $fileName "padding-"
Add-Content $fileName "padding-right: 0px;"
Add-Content $fileName "padding-bottom: 0px;"
Add-Content $fileName "padding-"
Add-Content $fileName "}"
Add-Content $fileName "body {"
Add-Content $fileName "margin-"
Add-Content $fileName "margin-"
Add-Content $fileName "margin-right: 0px;"
Add-Content $fileName "margin-bottom: 10px;"
Add-Content $fileName ""
Add-Content $fileName "table {"
Add-Content $fileName "border: thin solid #000000;"
Add-Content $fileName "}"
Add-Content $fileName "-->"
Add-Content $fileName "</style>"
Add-Content $fileName "</head>"
Add-Content $fileName "<body>"
Add-Content $fileName "<table width='100%' border='1'>"
Add-Content $fileName "<tr bgcolor='#CCCCCC'>"
Add-Content $fileName "<td colspan='4' height='25' align='center'>"
Add-Content $fileName "<font face='tahoma' color='#003399' size='4'><strong>Web Page Health Report - $date</strong></font>"
Add-Content $fileName "</td>"
Add-Content $fileName "</tr>"
}
# Function to write the HTML Footer to the file
Function writeHtmlFooter {
param($fileName)
Add-Content $fileName "</table>"
Add-Content $fileName "</body>"
Add-Content $fileName "</html>"
}
Function sendEmail {
param($htmlFileName)
$from=New-Object System.Net.Mail.MailAddress "Support<support@company.com>"
$to= New-Object System.Net.Mail.MailAddress "Support<support@company.com>"
$subject="Server WebPage HealthCheck - $Date"
$smtphost="1.1.1.1"
$body = Get-Content $htmlFileName
$smtp= New-Object System.Net.Mail.SmtpClient $smtphost
$msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body
$msg.isBodyhtml = $true
$smtp.send($msg)
}
# Function to write the HTML Header to the file
Function writeTableHeader {
param($fileName)
Add-Content $fileName "</table>"
}
# Function to write the HTML Header to the file
Function writeTableHeader {
param($fileName)
Add-Content $fileName "<tr bgcolor=#CCCCCC>"
Add-Content $fileName "<td align='center'>Server Name</td>"
Add-Content $fileName "<td align='center'>Time Test Started</td>"
Add-Content $fileName "<td align='center'>Web Request Type</td>"
Add-Content $fileName "<td align='center'>Web Page Result</td>"
Add-Content $fileName "</tr>"
}
##########################################################################################################
### SCRIPT
remove-item $fileName
writeHtmlHeader $fileName
writeTableHeader $fileName
#writeTableFooter $fileName
#writeHtmlFooter $fileName
foreach ($computer in Get-Content $serverlist) {
$servern=$computer.split(",")[0]
$ip=$computer.split(",")[1]
$type = $computer.split(",")[2]
$url = $computer.split(",")[3]
$Date = Get-Date
$servern
if ($Computer -notmatch 'DB') {
$Stoploop = $false
[int]$Retrycount = "0"
do {
try {
$w = New-Object net.WebClient
$p = New-Object System.Net.WebProxy 'http://proxy:8080'
$p.UseDefaultCredentials = $true
$w.proxy=$p
$w.DownloadFile($url,$checkfile)
if (Test-Path $checkfile) {
## http://stackoverflow.com/questions/18633666/powershell-using-contains-to-check-if-files-contain-a-certain-word
$file = Get-Content $checkfile
$containsWord = $file | %{$_ -match $stringTest}
If($containsWord -contains $true -and $stringTest -gt 3) {
#GOOD CONTENT
if (Test-Path "$($mypath)\$servern$flag") {
remove-item "$($mypath)\$servern$flag"
}
}
else
{
#BAD CONTENT
if (!(Test-Path "$($mypath)\$servern$flag")) {
"$(get-date -Format 'hh:mm, dd/MM/yyyy') - $($string)" | Out-File "$($mypath)\$servern$flag"
Add-Content $fileName "<tr>"
Add-Content $fileName "<td align='center'> $servern ($ip) </td>"
Add-Content $fileName "<td align='center'> $Date </td>"
Add-Content $fileName "<td align='center'> $type </td>"
Add-Content $fileName "<td align='center'> $stringTest is not found </td>"
Add-Content $fileName "</tr>"
Write-Host $stringTest ' is not found'
}
}
}
$Stoploop = $true
}
catch [Net.WebException] {
# AN EXCEPTION WAS PREVIOUSLY FOUND - DO NOT NEED TO GO THROUGH ALL THREE TESTS
if (Test-Path "$($mypath)\$servern$flag") {
$Stoploop = $true
}
# SERVER IS DEFINITELY OFF-LINE AND WE NEED TO GENERATE A REPORT
if ($Retrycount -gt 2){
if (!(Test-Path "$($mypath)\$servern$flag")) {
"$(get-date -Format 'hh:mm, dd/MM/yyyy') - $($string)" | Out-File "$($mypath)\$servern$flag"
Add-Content $fileName "<tr>"
Add-Content $fileName "<td align='center'> $servern ($ip) </td>"
Add-Content $fileName "<td align='center'> $Date </td>"
Add-Content $fileName "<td bgcolor='#FF0000' align='center'><font color='Yellow'> $_.Exception.ToString() </font></td>"
Add-Content $fileName "</tr>"
Write-Host $_.Exception.ToString()
Write-Host $($mypath)\$servern$flag
Write-Host "$RetryCount $servern is not responding after 3 retries."
}
# STOP LOOPING AND LETS GO TO THE NEXT COMPUTER OR QUIT
$Stoploop = $true
}
else {
# LOOP COUNTER IS NOT DONE YET - KEEP GOING
Write-Host "$RetryCount $servern is not up and running retrying in 10 seconds..."
Start-Sleep -Seconds 10
$Retrycount = $Retrycount + 1
}
}
}
While ($Stoploop -eq $false)
}
}
#writeHtmlHeader $fileName
writeHtmlFooter $fileName
$date = (get-date).ToString('MM/dd/yyyy hh:mm:ss')
$file = Get-Content $fileName
##867
if ($(Get-Item $fileName).length -gt 1000) {
sendEmail $fileName
}
##########################################################################################################
### SCRIPT IS DONE