10 Replies Latest reply on May 31, 2018 2:45 AM by cahunt

    Powershell to query a service and add multiple nodes

    namreg

      Hello,

       

      I need to create an application to monitor a service "nxlog" but this service belongs to more than 100 computers.

       

      I am wondering what is the easiest way to add those nodes that have that service? Instead of choosing one by one node through the Application wizard  ?

       

      Thanks for your help,

        • Re: Powershell to query a service and add multiple nodes
          cahunt

          Create the template, by adding at least one Powershell Component Monitor for your script. Then from the Mgmt apply template to nodes. Select all your nodes. OR Use the option to Apply the Application Template to a Group if all your nodes are in a group already*. You can also use a Service Monitor component (service Status - SNMP or Windows Service Monitor) to do this without PS.  This is a good post for understanding how to manage your Scripts in SW Components, SAM Script Component Monitors - Everything you need to know.  Main thing is each component needs a statistic, and a message detail output.

           

          *This option seems great, but it has not always worked for me. Check your servers after application to make sure that they were all applied. If only the first few in the group now have the app assigned then remove that and use the selection of nodes. Setting up a custom property for your nodes or having a naming syntax that can be used for searches will always help.

           

          Let us know if you need any further help!

           

          -CharlesH

          Loop1 Systems: SolarWinds Training and Professional Services

          1 of 1 people found this helpful
            • Re: Powershell to query a service and add multiple nodes
              namreg

              Hmm, I created an application template called Service-nxlog. And, i added 17 hosts manually. But, i do not see where to enter the powershell code ? Thanks.

                • Re: Powershell to query a service and add multiple nodes
                  cahunt

                  Dang it, I almost put up screen shots for you.. Sorry I should have went with the instinct.

                   

                  Edit the template, Add a Component then choose which component you want by name.

                   

                   

                   

                   

                  This shot is from adding a component to a new template - but its the same menu option..

                   

                   

                   

                  Then choose your type; If you arrange by category name PS shows up under the 'Custom Script Monitors'

                   

                  The Windows Service Monitor is under the Windows Component Monitors

                   

                    • Re: Powershell to query a service and add multiple nodes
                      namreg

                      Ok, got it. should i add just this in the script body ?

                       

                      Get-Service nxlog

                        • Re: Powershell to query a service and add multiple nodes
                          cahunt

                          You need some output for the Component to work;

                           

                          You should be able to use the script below; just put your service name in the 'Script Arguments' Field within the component.

                           

                          $ServiceName = $args.get(0);

                           

                           

                          $ServiceStatus = Get-Service -Name $ServiceName

                           

                           

                          if ($ServiceStatus.Status -ne "Running")

                            {

                              Write-Host "Message: '$ServiceName' service is Not Running."

                              Write-Host Statistic: 1

                              exit 0

                          }

                          if ($ServiceStatus.Status -eq "running")

                            {

                              Write-Host "Message: '$ServiceName' service is Running."

                              Write-Host Statistic: 0

                              exit 0

                            }

                            • Re: Powershell to query a service and add multiple nodes
                              cahunt

                              With this script, and exit codes being 0 the component will always be up as long as it works. Set the thresholds to = and place a 1 in the critical, so when the service is not running the component will be marked Critical.

                               

                              You can set the exit code to 1 (same as the Statistic) and remove the threshold to mark the component down when the service is not running if you prefer a down or do not like using the thresholds to change status.

                                • Re: Powershell to query a service and add multiple nodes
                                  namreg

                                  Thanks a lot.

                                  Ok got it. But, how do i apply it to the nodes where they have the service ?

                                   

                                  Do I have to choose the nodes one by one ?

                                   

                                  What if the node does not have the service at all ? If it does not have the service I want it to be ignored.

                                   

                                  Thanks again,

                                    • Re: Powershell to query a service and add multiple nodes
                                      cahunt

                                      Once you have the template - assign to nodes, or assign to group - note my mention about the groups possible 'issue' in some of the first comments.

                                       

                                      you can search, or group your nodes from the assign templates page and select multiple nodes to apply the Application Template to at a single time.

                                      So , sort of yes - you either have to select the full grouping of nodes (by whatever property you group the nodes by) Then use the GREEN ARROW to put them in the right box on the page,  once those nodes are added rearrange your selection or search to find, select and add more nodes to the right using the GREEN ARROW.... then assign credentials to use and apply.

                                       

                                        • Re: Powershell to query a service and add multiple nodes
                                          uaggarwal

                                          Hi Cahunt,

                                           

                                          This information is quite a lot useful to me.

                                           

                                          Can you help me in knowing if there is a way we can monitor multiple different services on multiple servers, i.e I am having 60 servers with me, and each server is having unique service that needs to be monitored.

                                           

                                          Kindly let me know if something is there that could help me out?

                                           

                                          Thanks in Advance.

                                            • Re: Powershell to query a service and add multiple nodes
                                              cahunt

                                              If you need to you can use the Real Time Process Explorer or the Service Manager from the node details page to fish out the servers with the services you need to monitor. It can be a pain going through them one by one. This is where your application or server team will come in handy as well, especially if they have done some Application Mapping. Even if Application Mapping has not been done, they should be able to tell you which servers are dependent on certain processes.

                                               

                                              I would setup a template for each unique setup, then apply accordingly in bulk. Your Arguments in the script will set the services that you need to track with each template.

                                               

                                              Sorry for the late reply. Let me know how clear that is to you and if you need more help.