Skip navigation
All People > Jerome Lefebvre > Jerome Lefebvre のブログ > 2019 > March

To properly monitor the health of PI Vision, one must look at the health of the databases indexed by PI Web API. This means, periodically looking at https://{piwebapihost}/piwebapi/search/sources page and seeing if the last crawl date is recent or if any database is showing an error. This information is available via GetSources GET search/sources  PI Web API method. PI Web API Crawler does not expose this information via performance counters (but I believe this would be helpful if it did!)


What we can do instead is to write a little bit of Powershell to access the PI Web AP crawler page, and write the health information to tags or attributes.

Once that is done, we can display this in reports or sent out alert emails, etc.

In the sample script below, I am simply storing the lastcrawl date of a PI Data Archive server. In AF, I can then check if that value becomes stale and alert somebody to act on it when it does.

You can then run this script periodically every few hours or so and learn about issues before users are aware of it.



# Retrive the last crawl date of a PI Data Archive
function Get-Data([string]$username, [string]$password, [string]$url) {
  $credPair = "$($username):$($password)"
  $encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($credPair))
  $headers = @{ Authorization = "Basic $encodedCredentials" }
  $responseData = Invoke-WebRequest -Uri $url -Method Get -Headers $headers -UseBasicParsing
  return $responseData
$piwebapihost = ""
$pi = "PISRV1"

$url = $piwebapihost + "piwebapi/search/sources?name=pi:" + $piwebapihost
$dataRaw = Get-Data -username w -password ! -url

$json = $dataRaw | ConvertFrom-Json
$lastCrawl = $json.Items[0].LastCrawl

Write-Host Last crawl date: $lastCrawl

#Convert and store the value to store in tag with datetime type
$format = 'yyyy-MM-ddThh:mm:ss.fffffffZ'
$value = ([datetime]::parseexact($lastCrawl, 'yyyy-MM-ddThh:mm:ss.fffffffZ', $null)).ToUniversalTime()

$pi = Connect-PIDataArchive -PIDataArchiveMachineName masterpi
$tag = Get-PIPoint -Connection $pi -Name PIWebAPIStatus2
Add-PIValue -PIPoint $tag -Value $value -Time (ConvertFrom-AFRelativeTime -RelativeTime *)