Forum Discussion

breakerbreaker's avatar
5 years ago

Can I run a powershell script?

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?

 

  • FWIW, having also come originally from Nagios, I miss the ability to transmit arbitrary string data back via alerts. Some of this can be emulated with auto properties, but those can be set only during discovery not collection.  I posted a feature request previously to allow definition of enums that can be bound to datapoints (global values and overridden values within specific datasources/datapoints). these could then be used to avoid the current awkward legend method and actually show the intended purpose of DP values where needed via tokens. Imagine a line that showed the actual meaning of the current value instead of a long truncated legend line that makes you dig around for what it means.

    I also think it should be possible to improve the property menus to leverage more advanced typing and UI.  For example, a property might be just a string as now (preferably with better input box control), or it might be a radio button, selection menu, etc. so that folks using properties can easily find what is supported and what values/ranges are allowed.  This also would be something where those hints would be defined within logicmodules primarily, but it should be possible to define them more generally (at least the typing/UI definitions, which could then be bound to properties that are used within modules.  This is not strictly related to the topic, but is about readability and usability so I tossed it in there, too :).

  • On 3/6/2020 at 6:35 AM, Stuart Weenig said:

    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.  

    Eventsources don't support embedded Powershell, though they certainly should.  You can upload a script though.  That said, eventsources are also almost entirely unsuited for monitoring, more like additional information to see along with monitoring. Among other things, you cannot ACK them in a meaningful way due to lack of correlation across eventsource results.  I'm sure the yet-another-premium-module LMLogs will fix all those problems, though.

  • Hi, 

    Yes you can run a PoweShell Script.

    I have a script named myscript.ps1 I have all the necessary frameworks installed I set that execution policy thing I have followed the instructions on this MSDN help page and am trying to run it like so: powershell.exe 'C:\my_path\yada_yada\run_import_script.ps1' (with or without --noexit) which returns exactly nothing, except that the file name is output. No error, no message, nothing. Oh, when I add -noexit, the same thing happens, but I remain within PowerShell and have to exit manually. The .ps1 file is supposed to run a program and return the error level dependent on that program's output. But I'm quite sure I'm not even getting there yet.

     

    Thanks and regards,

    Lavanya Sreepada.

  • Anonymous's avatar
    Anonymous
    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. 

  • 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##

     

  • Anonymous's avatar
    Anonymous

    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 

  • I did come across this article while looking for an answer, but it does not indicate where in Logic monitor I would configure something like this.  Can you help me in that regard?