• 0

Can I run a powershell script?


Question

Hello,

Our company's IT team uses LogicMonitor.  I have a background using Nagios.  With Nagios, I can run a PowerShell script that returns one of three possible values (OK, WARNING, CRITICAL).  I have a script that calls an internal application's API, takes the resulting JSON and parses it looking for specific items.  It does some logic on the result and returns a status of OK/WARNING/CRITICAL based on the results.  This PowerShell script would be run from the monitoring server.  I was told LogicMonitor can only do checks via SNMP & WMI.  Can it do custom checks like this?  What resource can you point me to to learn more about how to do this?

 

Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 0
  • Administrators

Sure! Let's assume you have the right credentials to create LogicModules in LogicMonitor (if you don't, ask the IT team that administers LM).  Let's also assume that you want to track the status of this thing over time.

For reference: https://www.logicmonitor.com/support/logicmodules/datasources/creating-managing-datasources/creating-datasources

You would need to create something called a DataSource. The DataSource is built through the LogicMonitor GUI. You need to configure four main areas:

1. General Information like what to call the datasource and with which device(s) you will associate the resulting data (this is done through the AppliesTo). You mention that you run the script on the collecting server. If that's the simple case, you can just configure the AppliesTo to match one or another collector. However, if the script fetches data about multiple servers (even if it doesn't touch the target server), you may consider having it apply to those servers. You can then establish a token (variable) in your powershell script that would have its value be the name of whichever server you were trying to monitor. This is like having a script that says "ping ##SYSTEM.HOSTNAME##" and the collector would execute the script once for every device the DataSource applies to, substituting the real hostname for the token each time it's executed.

It is also in this general section that you would choose how often to collect data and the "Collector". This "Collector" simply means what method you want to use to collect the data. In your case, it sounds like you are interested in one result per target server, so you could simply pick "SCRIPT" for the "Collector".

2. Since you are interested in only one result per target, you wouldn't need active discovery. Skip to step three. For completeness, know that Active Discovery is where you would configure LM to figure out the names and IDs of objects on the target server you want to monitor. For example, you could write a script that uses the invoke-command cmdlet to issue "wmic logicaldisk get name" to get the names of each logical drive on the server. The invoke-command cmdlet would use the ##SYSTEM.HOSTNAME## token to specify the target server. The result of that command would be received by LM and one "instance" created for each logical drive on each remote server.

3. This section is where you would provide your script. Make sure you pick PowerShell instead of the default Groovy. Your script would need to Write-Host the data (that's how LM reads the results of your script). You'd write the result out like this (the results have to be numerical in order for LM to bring it in*):

#For OK:
Write-Host 0

#For Warning:
Write-Host 1

#For Critical
Write-Host 2

4. The last step is to define what to do with the data that is written back to LM. Create a new datapoint called "MyCustomStatus" (or whatever you want to call it) and select "Content the script writes to the standard output".  You'd want to set a threshold to have LM open an alarm, so go down to the Alert Threshold section and choose ">=" and put "1 2 2" in the text box to the right. This means that a warning alarm will be opened when the result is 1 and a critical alert when the result is 2. Save the datapoint and save your DataSource.

The collector should start running your script within a few minutes, depending on the collection interval you picked. If you navigate to the "Resources" page (on the left grey bar) and find the device you applied the datasource to, you should see your new datasource under the device. Click on your datasource under the device and select the "Raw Data" tab. You should start seeing rows show up there (you might have to sit on the refresh button for a few minutes). 

The only remaining thing would be to create a graph to display your data in a more customized fashion than the default graph that is generated when you build a datasource. If you want help with that, let me know.

 

*There are different types of LogicModules that 

Link to post
Share on other sites
  • 0
Posted (edited)

This is great!  I could use a little more clarification on a couple of items, if you don't mind. 

  1. 1. Can I return more than just a numerical value (0/1/2), like a string?  A few examples of how it would look in Nagios might be "OK", "Warning: Site has not been backed up in 2 days", or "Critical: Site has not been backed up in 7 days".  In other words, can I return a string that gives more clarification to "Warning" or "Critical"?
  2. 2. Can you provide me a resource that references all macros (Nagios calls them Macros, not sure what LM calls them) that I can use in the script? You provided me an example macro of ##SYSTEM.HOSTNAME##

 

Edited by breakerbreaker
Link to post
Share on other sites
  • 0
  • Administrators
17 hours ago, breakerbreaker said:

This is great!  I could use a little more clarification on a couple of items, if you don't mind. 

  1. 1. Can I return more than just a numerical value (0/1/2), like a string?  A few examples of how it would look in Nagios might be "OK", "Warning: Site has not been backed up in 2 days", or "Critical: Site has not been backed up in 7 days".  In other words, can I return a string that gives more clarification to "Warning" or "Critical"?
  2. 2. Can you provide me a resource that references all macros (Nagios calls them Macros, not sure what LM calls them) that I can use in the script? You provided me an example macro of ##SYSTEM.HOSTNAME##

 

You can return a string, but Datasources, only take numbers. If the script does return a string, you would have to convert it to a number using post-processing looking for the particular string. So it would turn it back into a number. You would then need to look into changing strategies over to an EventSource. The EventSource would output words (like a log file) and you'd write a check to look for particular words to open a specific kind of alarm.  

LM calls them tokens. https://www.logicmonitor.com/support/logicmodules/datasources/creating-managing-datasources/tokens-available-for-data-collection/ You're only limited by your imagination because any device property value can be used as a token in the script. Properties can be set on the device level, or on a group level and inherited by each device in the group, or at a higher group level and inherited by all descendants. 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.