Advertisement
stephanlinke

[PRTG] Read RSS Feed

Dec 22nd, 2015
1,866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Requires -Version 4
  2. <#
  3.  ___ ___ _____ ___
  4. | _ \ _ \_   _/ __|
  5. |  _/   / | || (_ |
  6. |_| |_|_\ |_| \___|
  7. RSS Feed Checker
  8. (c) SLinke|Paessler  AG
  9. =======================
  10. Description: This sensor will read RSS feeds configured in
  11. the configuration file and alert as soon as one of them is updated.
  12. The FriendlyName attribute will be used as channel name.
  13. #>
  14. $configurationFile = "C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML\prtg.rsscheck.xml"
  15. $configuration = New-Object System.Xml.XmlDocument
  16. #region configuration
  17. $configuration.Load($configurationFile);
  18. $Feeds               = $configuration.configuration.feeds.ChildNodes
  19. [System.Collections.ArrayList]$Updaters = @();
  20. $MaxMessageLength    = 50;
  21. #endregion
  22.  
  23. function Check-RSSFeed(){
  24.  
  25. Write-Host '<?xml version="1.0" encoding="UTF-8"?><prtg>'
  26.  
  27.     foreach($feed in $Script:Feeds){
  28.  
  29.         # get the current messages
  30.         [xml]$RSS = (Invoke-WebRequest -UseBasicParsing -Uri $feed.url)
  31.         [string]$currentMessage = $RSS.SelectSingleNode($feed.node).Value
  32.         [string]$oldMessage = $Configuration.SelectSingleNode([string]::Format("/configuration/feeds/feed[@id='{0}']",$feed.id)).saved
  33.  
  34.         Write-Host "<result><channel>$($Feed.FriendlyName)</channel><unit>count</unit><valuelookup>prtg.rssupdate</valuelookup>"
  35.         if($oldMessage -ne $currentMessage){
  36.            Write-Host "<value>1</value>"
  37.            $Updaters.Add([string]::Format("[{0}] {1}", $Feed.FriendlyName,$currentMessage.Substring(0,$MaxMessageLength))) | Out-Null;
  38.            
  39.            # save the new message to the configuration file
  40.            $Configuration.SelectSingleNode([string]::Format("/configuration/feeds/feed[@id='{0}']",$feed.id)).current = $currentMessage;
  41.            $Configuration.SelectSingleNode([string]::Format("/configuration/feeds/feed[@id='{0}']",$feed.id)).saved = $currentMessage;
  42.            $Configuration.Save($configurationFile);
  43.         }
  44.         else { write-host "<value>0</value>"; $Updaters.Add([string]::Format("[{0}] {1}", $Feed.FriendlyName,$currentMessage.Substring(0,$MaxMessageLength))) | Out-Null; }
  45.        
  46.         Write-Host "</result>"
  47.     }
  48.     Write-Host ("<text>{0}</text></prtg>" -f ($Updaters -join ", "))
  49.  
  50. }
  51. Check-RSSFeed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement