This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to Parse a string in a rule condition

I have the need to take a string that is used as a Display Name for a database instance in Foglight for SQL Server and parse it.  For instance, the Display name might be something like C-Tier1-DatabaseInstance.   I am building a rule where I have the need to split the string in separate variables to build conditional statements.  What is the syntax to do something like this?

  • Hi,

    You can try something like this :

    hostName = 'C-Tier1-DatabaseInstance'
    hostNameElements = hostName.split('-')

    Then you can find the parts like this :
    print hostNameElements [0]
    print hostNameElements [1]
    print hostNameElements [2]


    Regards
  • Thanks George. That was what I was looking for. Now i need to make it dynamic. I want to put the code into the run condition of alarm to set rule variables dynamically. Here is the code I am working with:

    def display_name= 'C-Tier2-DatabaseInstance' <<----NEED TO MAKE DYNAMIC
    def portfolio = display_name.split("-")[0];
    def tier = display_name.split("-")[1];
    def instance = display_name.split("-")[2];

    def DBGroup = portfolio
    switch (DBGroup) {
    case "A": emailAddr = "DatabasePortfolioA@exchange.schwab.com"
    break
    case "B": emailAddr= "DatabasePortfolioB@exchange.schwab.com"
    break
    case "C": emailAddr= "DatabasePortfolioC@exchange.schwab.com"
    break
    case "D": emailAddr= "DatabasePortfolioD@exchange.schwab.com"
    break
    default: emailAddr= "SQLOperations@exchange.schwab.com"
    }

    def Priority = tier
    switch (Priority) {
    case "Tier1": Priority = 1
    break
    case "Tier2": Priority = 2
    break
    default: Priority = 3
    }

    def urgency = Priority
    switch (urgency) {
    case 1: urgency = "High"
    break
    case 2: urgency = "Medium"
    break
    default: urgency = "Low"
    }



    I need the condition to read the display name and then do the work. Is there a way to add topology objects to the rule condition so that this condition will be evaluated for every instance in our environment? I am trying to put together a generic piece of code that we can use for all of the customized rules that we copy from existing rules. The data that we are extrapolating will be used to populate a template within our incident ticketing system
  • I have this piece of code that I want to put into the rule condition that will dynamically determine information from the display name:

    def display_name= 'C-Tier2-DatabaseInstance'  <-------------------------  Needs to be dynamic

    def  portfolio = display_name.split("-")[0];

    def tier = display_name.split("-")[1];

    def instance = display_name.split("-")[2];

    def DBGroup = portfolio

    switch (DBGroup) {

     case "A": emailAddr = "DatabasePortfolioA"

     break

     case "B": emailAddr= "DatabasePortfolioB"

     break

     case "C": emailAddr= "DatabasePortfolioC"

     break

     case "D": emailAddr= "DatabasePortfolioD"

     break

     default: emailAddr= "SQLOperations@exchange.schwab.com"

    }

    def Priority = tier

    switch (Priority) {

     case "Tier1": Priority = 1

     break

     case "Tier2": Priority = 2

     break

     default: Priority = 3

    }

    def   urgency = Priority

    switch (urgency) {

     case  1: urgency = "High"

     break

     case  2: urgency = "Medium"

     break

     default: urgency = "Low"

    }

    Is there a way to add topology objects to the rule condition section to that I can pull the display name in?  Are the global variables?

     

  • Hi,

    Assuming that you are working with Rule :  DBSS - Jobs Failed ...

    This rule uses the following topology type as scope : DBSS_Agent_Job_List

    So scope is of type DBSS_Agent_Job_List

     

     

    You can use the following code to take the host name of the database :

    scope.dbss_host.get("name")

     

     

    In General you can use the script console to write & test your code before applying to any rule :

    In your case :

    Open Script Console :

    Do a query for   !DBSS_Agent_Job_List

    Check your instances on the middle part,

    then select an instance of DBSS_Agent_Job_List and check it's properties on the bottom.

     

     

    Having selected an instance (this is your scope ! )

    You can create a new script  and write your code ...

    Press run and you can test it ...

     

     

    Regards

  • Thanks for the tip on how to test the script. I had it set up as a script, just couldn't figure out how to dynamically gather the name. Now I am trying to take this script and put it into a rule condition. Are the variables that I defined above like portfolio, tier, priority, etc. available to the rest of the rule when the check of the rule is performed? Can I use these values to pass the information into an snmp trap or to set up message in the actions that I can email that reads something like ...The backup job on <instance> failed. Please notify the <Portfolio> team of the <priority> failure.
  • Hi,

    The scope variable is available.
    I think that all other variables defined inside the condition of the rule are not available elsewhere.

    You can try building your variables as Rules Variables (the last tab)
    For example : create a Rule Variable with name DBSS_HostName as expression :
    return scope.dbss_host.get("name");
    Then you can use @DBSS_HostName in your code or email header or body.



    Sometimes i even build the rule alarm message as a rule variable (ex : ruleAlarmMessage ) using other rule variables from the same script.

    Then i use the following as the Alarm Message
    @ruleAlarmMessage

    Occasionally i have seen some weird behavior with rules variables not being evaluated when used in alert email message.
    As a workaround i used the Severity Level Variables, but if you have multiseverity conditions you have to set them multiple times.

    Try first with as Rule Variable.


    Regards
  • Hi,

    Example

    4 Rules Variables :

    1:
    Name : DBSS_HostName (Expression Type)
    Code : return scope.dbss_host.get("name");


    2:
    Name : myEmailAddr (Expression Type)
    Code :
    switch (@DBSS_HostName.split("-")[0]) {
    case "A": emailAddr = "DatabasePortfolioA@exchange.schwab.com"
    break
    case "B": emailAddr= "DatabasePortfolioB@exchange.schwab.com"
    break
    case "C": emailAddr= "DatabasePortfolioC@exchange.schwab.com"
    break
    case "D": emailAddr= "DatabasePortfolioD@exchange.schwab.com"
    break
    default: emailAddr= "SQLOperations@exchange.schwab.com"
    }
    return emailAddr;

    You can use this as the @emailAddr as the email recipient.


    3:Name : myPriority (Expression Type)
    Code :
    switch (@DBSS_HostName.split("-")[1]) {
    case "Tier1": Priority = 1
    break
    case "Tier2": Priority = 2
    break
    default: Priority = 3
    }
    return Priority;


    etc


    Regards
  • Therein lies the problem. I need to be able to either use the variables defined in the condition throughout the rule or be able to put the logic into the Rules Variables. The key to what I am trying to do is to parse the display name to determine the other variables. Am I trying to do too much with a rule?
  • Hi,

    I have replied with an example of what you can do, but when i submitted the reply i received a notice saying that my reply will be reviewed by an administrator !! ?? (probably some kind of spam protection).
    Please wait until it shows up ...

    Regards