12 Replies Latest reply on Apr 18, 2018 6:55 PM by ebradford

    Running External Program

    donpepe

      So I made a little script that runs HTTP posts to our Cisco Spark API. I can run it from the command line just fine.

       

      I just cant seem to get it to run from the alert actions. This is what I put in the path field:

       

      D:\Scripts\spark.py "Alert"

       

      Alert is the argument im passing to the python script.

       

      Any ideas?

        • Re: Running External Program
          sgtp

          are the python env vars configured? Might try doing something like [path to python ]\python d:\scripts\spark.py Alert

          2 of 2 people found this helpful
          • Re: Running External Program
            jvb

            Maybe check this article for some troubleshooting points...

            Tips for executing external scripts and batch files with alerts - SolarWinds Worldwide, LLC. Help and Support

            One point from the article that stands out to me based on what you have posted is:

            • Run all external batch files and scripts from the C: drive of your Orion server.

            Looks like you are running from "D"... Not sure that is the problem but might be useful to check / try.

            2 of 2 people found this helpful
            • Re: Running External Program
              jonr

              I am working on the same thing, I will report my progress in the morning.

              • Re: Running External Program
                jonr

                I will post a thread and put the link in this thread with a how to step by step but this should get you up and running.  The code below I chopped up to give you an idea of how it works, ignore the module and the first uri for rooms.  I use a module and this is old code.  I have a more complex script I use in production which I will share in the thread I post later.  I use markup language and the posts in the spark space look prettier like the screen shots below.  When I get time I will log in to the server and grab an example of the code I use.  Hope this helps.  If anyone has any questions feel free to shoot me a message.

                 

                So first things first.  In order to run the external program you will need the whole path in order to launch the program. This is what I did to get it working:

                 

                I wrote a powershell script that I passed alert variables to in order to post to cisco spark.

                 

                for example:

                 

                In the path you could put:  C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "C:\SparkScripts\Node.ps1" "${NodeName}" "${Status}"

                 

                After the path is where I pass the variables.  The variables are arguments for the script.  Each argument is enclosed by quotes.  You can have multiple alert variables in the quotes.

                 

                The script looks something like this.  Again like I said I will post in the forums my actual script and how I did it tomorrow or Monday, but this should help you get started.

                 

                code block

                 

                I use a powershell module, keep in mind that if you load a module, or libraries or if you are going to use powershell to run python that you put everything you need to run your script in the path.

                 

                The output in my spark room for my finalized scripts look like these examples below.

                 

                 

                 

                 

                 

                This one isn't an alert but is a Rest API call using SWQL that posts node health to a spark space.

                 

                  • Re: Running External Program
                    esandell

                    Hiya Mate,  Do you have a link to the PS scipt you created?  Be great to test this out.

                      • Re: Running External Program
                        jonr

                        I will post my code when I get back in the office.

                        • Re: Running External Program
                          jonr

                          Here is the code I use for the node down alert including the Arguments.

                           

                          You put the argument in the path.  see screenshot.

                           

                           

                          ```

                          ######### SolarWinds Alert to SPark Space Script

                           

                           

                          # // Alert Variables for Arguments //

                           

                           

                          #Example Script Arguments

                           

                           

                          # ${NodeName} ${Status} ${N=SwisEntity;M=CustomProperties.BranchLocations} ${N=SwisEntity;M=Stats.PercentMemoryUsed} ${N=SwisEntity;M=Stats.CPULoad} ${N=SwisEntity;M=Stats.AvgResponseTime}

                          # C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "C:\SparkScripts\Node.ps1"

                          # "${N=Alerting;M=AlertName} ${NodeName} is "${Status}"

                          #C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "C:\SparkScripts\Node.ps1" "Alert: ${N=Alerting;M=AlertName} ${NodeName} is ${Status} at ${N=Generic;M=DateTime;F=DateTime}" "${N=SwisEntity;M=CustomProperties.BranchLocations}" "${N=SwisEntity;M=Stats.PercentMemoryUsed}" "${N=SwisEntity;M=Stats.CPULoad}" "${N=SwisEntity;M=Stats.AvgResponseTime}"

                           

                           

                          #Node Down Script Argument

                           

                           

                          C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File "C:\SparkScripts\Node.ps1" "Alert: ${N=Alerting;M=AlertName} ${NodeName} is ${Status} at ${N=Generic;M=DateTime;F=DateTime}" "${N=SwisEntity;M=CustomProperties.BranchLocations}" "${N=SwisEntity;M=Stats.PercentMemoryUsed}" "${N=SwisEntity;M=Stats.CPULoad}" "${N=SwisEntity;M=Stats.AvgResponseTime}"

                           

                           

                          ### start script

                           

                           

                          $var0=$args[0]

                          $var1=$args[1]

                          $var2=$args[2]

                          $var3=$args[3]

                          $var4=$args[4]

                          $var5=$args[5]

                          $var6=$args[6]

                           

                           

                          $Token = 'Your API Token Here'

                          $uri = "https://api.ciscospark.com/v1/rooms"

                          $content = "application/json"

                          $header = @{ "Authorization" = "Bearer $Token" }

                           

                           

                          # // post messages via api

                           

                           

                          $SpaceID = "Your Space ID Here"

                          $MessageURI = "https://api.ciscospark.com/v1/messages"

                           

                           

                          $params = @{

                          'roomId'=$SpaceID

                          #'markdown'= "Node $var0 is $var1."

                          'markdown'= (

                          "<ul>

                          <li><p>$var0</p></li>

                          <li><p>Branch Location: $var1</p></li>

                          <li><p>Percent Memory Used: $var2</p></li>

                          <li><p>CPU Load: $var3</p></li>

                          <li><p>Average Response Time: $var4</p></li>

                          </ul>")

                           

                           

                           

                           

                           

                           

                          } | ConvertTo-Json

                           

                           

                          Invoke-RestMethod -uri $MessageURI -ContentType $content -Method Post -Headers $header -body $params

                           

                           

                          Here is the Result in Cisco Spark.

                           

                           

                           

                           

                          Here is the code script I use to query node health.

                          ```

                          Param

                          (

                          [string]$node

                          )

                           

                           

                          #for debugging

                          $LogMsg = "$(Get-Date) - Starting process"

                          $LogMsg

                           

                           

                           

                           

                           

                           

                          # load the swis snapin

                          if (-not (Get-PSSnapin | ?{ $_.Name -ceq 'SwisSnapin' })) {

                          Add-PSSnapin 'SwisSnapin'

                          }

                           

                           

                          if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type)

                          {

                          $certCallback = @"

                          using System;

                          using System.Net;

                          using System.Net.Security;

                          using System.Security.Cryptography.X509Certificates;

                          public class ServerCertificateValidationCallback

                          {

                          public static void Ignore()

                          {

                          if(ServicePointManager.ServerCertificateValidationCallback ==null)

                          {

                          ServicePointManager.ServerCertificateValidationCallback +=

                          delegate

                          (

                          Object obj,

                          X509Certificate certificate,

                          X509Chain chain,

                          SslPolicyErrors errors

                          )

                          {

                          return true;

                          };

                          }

                          }

                          }

                          "@

                          Add-Type $certCallback

                          }

                          [ServerCertificateValidationCallback]::Ignore()

                           

                           

                          $SWServer = "Your Orion Server Name or Ip Address"

                          $SWUsername = "Your SolarWinds Username"

                          # I encrypt my password  but you could use get-credentials to prompt for credentials

                          $Key = Get-Content C:\AES.key

                          $SWPassword = Get-Content C:\SWPWD.txt | ConvertTo-SecureString -Key $Key

                          $SWCreds = New-Object -typename System.Management.Automation.PSCredential -argumentlist $SWUsername, $SWPassword

                          $swis = Connect-Swis -host $SWServer -cred $SWCreds

                           

                           

                          ####Query Request####

                           

                           

                          $query3 = "SELECT NodeID, NodeName, IP_Address, MachineType, Vendor, Status,LastBoot, SystemUpTime, ResponseTime, PercentLoss, AvgResponseTime, MinResponseTime, MaxResponseTime, CPULoad, PercentMemoryUsed, PercentMemoryAvailable, LastSync, LastSystemUpTimePollUtc, NextPoll, MinutesSinceLastSync FROM Orion.Nodes WHERE IPAddress like '$node'"

                           

                           

                          $verifyquery = "https://"+$SWServer+":17778/SolarWinds/InformationService/v3/Json/Query?query=$query3"

                           

                           

                          $results = Invoke-RestMethod -Uri $verifyquery -Credential $SWCreds -ContentType "application/json"

                           

                           

                           

                           

                          $res = $results | select -ExpandProperty results

                           

                           

                          # convert status 1 or 2 to 'Up' or 'Down'

                           

                           

                          if ($res.Status -eq "1"){$res.Status = "Up"}else{$res.Status = "Down"}

                           

                           

                          $res|out-string

                           

                           

                          #for debugging

                          $LogMsg = "$(Get-Date) - Stopping process"

                          $LogMsg

                           

                           

                          ## /// Post to Spark Space

                           

                           

                          $SpaceID = "Your Space ID Here"

                           

                           

                          foreach ($res1 in $res){

                           

                           

                          $NodeID = $res1.NodeID

                          $NodeName = $res1.NodeName

                          $IP_Address = $res1.IP_Address

                          $MachineType = $res1.MachineType

                          $Vendor = $res1.Vendor

                          $Status = $res1.Status

                          $LastBoot = $res1.LastBoot

                          $SystemUpTime = $res1.SystemUpTime

                          $ResponseTime = $res1.ResponseTime

                          $PercentLoss = $res1.PercentLoss

                          $AvgResponseTime = $res1.AvgResponseTime

                          $MinResponseTime = $res1.MinResponseTime

                          $MaxResponseTime = $res1.MaxResponseTime

                          $CPULoad = $res1.CPULoad

                          $PercentMemoryUsed = $res1.PercentMemoryUsed

                          $PercentMemoryAvailable = $res1.PercentMemoryAvailable

                          $LastSync = $res1.LastSync

                          $LastSystemUpTimePollUtc = $res1.LastSystemUpTimePollUtc

                          $NextPoll = $res1.NextPoll

                          $MinutesSinceLastSync = $res1.MinutesSinceLastSync

                           

                           

                          $params = @{

                           

                           

                          'roomId'=$SpaceID

                          'markdown'= (

                          "<ul>

                          <li><p>NodeID: $NodeID</p></li>

                          <li><p>NodeName: $NodeName</p></li>

                          <li><p>IP_Address: $IP_Address</p></li>

                          <li><p>MachineType: $MachineType</p></li>

                          <li><p>Vendor: $Vendor</p></li>

                          <li><p>Status: $Status</p></li>

                          <li><p>LastBoot: $LastBoot</p></li>

                          <li><p>SystemUpTime: $SystemUpTime</p></li>

                          <li><p>ResponseTime: $ResponseTime</p></li>

                          <li><p>PercentLoss: $PercentLoss</p></li>

                          <li><p>AvgResponseTime: $AvgResponseTime</p></li>

                          <li><p>MinResponseTime: $MinResponseTime</p></li>

                          <li><p>MaxResponseTime: $MaxResponseTime</p></li>

                          <li><p>CPULoad: $CPULoad</p></li>

                          <li><p>PercentMemoryUsed: $PercentMemoryUsed</p></li>

                          <li><p>PercentMemoryAvailable: $PercentMemoryAvailable</p></li>

                          <li><p>LastSync: $LastSync</p></li>

                          <li><p>LastSystemUpTimePollUtc: $LastSystemUpTimePollUtc</p></li>

                          <li><p>NextPoll: $NextPoll</p></li>

                          <li><p>MinutesSinceLastSync: $MinutesSinceLastSync</p></li>

                          </ul>")

                           

                           

                          } | ConvertTo-Json

                           

                           

                           

                           

                          $Token = "Your Token Here"

                          $uri = "https://api.ciscospark.com/v1/rooms"

                          $content = "application/json"

                          $header = @{ "Authorization" = "Bearer $Token" }

                           

                           

                          # // post messages via api

                          $MessageURI = "https://api.ciscospark.com/v1/messages"

                           

                           

                           

                           

                          Invoke-RestMethod -uri $MessageURI -ContentType $content -Method Post -Headers $header -body $params

                           

                           

                          }

                          ```

                           

                          The results look like the image below