0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Tomcatのサーバ状態をPowerShellでテキスト取得する

Last updated at Posted at 2022-01-26

##はじめに
Tomcatの稼働状況をモニタリングしたいと思い
まずは、ステータス画面の情報をダウンロード。

↓以下の画面ですね
image.png

★環境
Windows10
PowerShell

##内容
・tomcatの設定
マネジャー画面にアクセスできるユーザを作成する。

「tomcat-users.xml」ファイルを開き以下2行を追加。
ここでは説明のため「admin」ユーザとする

  <role rolename = "manager-gui"/>
  <user username="admin"  password="admin" roles="manager-gui"/>

・PoerShellのファイルを作成する
 内容は以下

$baseurl="http://127.0.0.1:8080/manager/status/all"

$USER = "admin"
$PASS = "admin"
$mytime = Get-Date -Format "yyyyMMddhhmm" #ファイル名のタイムスタンプ
$Destination = "C:\bat\test_" + $mytime + ".html"  #保存先フォルダとファイル名

# 認証用にCredentialを作成
$secpasswd = ConvertTo-SecureString $PASS -AsPlainText -Force
# ID,PASSを設定
$cred = New-Object System.Management.Automation.PSCredential($USER, $secpasswd)

# URL開いてダウンロードする
Invoke-WebRequest -Uri $baseurl  -Credential $cred -OutFile $Destination

上記のファイルをタスクスケジューラで自動実行させる。

###Busyスレッド数をテキストファイルにログ出力する
以下を追記。

#ファイルから文字列検索して結果を変数に
$Str = Select-String "Busyスレッド: [0-9]" $Destination -Encoding UTF8 | ForEach-Object { $_.Matches.Value }

$Str = $mytime + ' ' + $Str
#ファイルから文字列検索して結果をテキストファイル書き込み
Write-Output $Str | Out-File Tomcat-BusyLog.txt -Append | Set-Content -Encoding UTF8

##履歴
2022/1/26 作成
2022/3/27 ログ出力追記

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?