1 Reply Latest reply on Nov 14, 2019 4:15 PM by dan jagnow

    How to Properly Select and Pass an Array of AlertObjectId's to the ClearAlert Verb using the C# API

    xtraspecialj

      Hi, looking at the sample code on GitHub there is a method call "GetOneAlert".  We are trying to do something real similar, but we want to get all alerts that are currently not acknowledged so that we can then run the ClearAlert verb against them.  We have modified the SWQL query already to now pull back all non-acknowledged Alerts, but we aren't sure how to convert the sample code to pass a properly formatted set of objects to the "ClearAlert" verb. 

       

      If you look at the little snippet of code I pasted below from the sample, you can see that we tried removing the [0] from the var alertObjectId line, however I'm pretty confident that we need to do more.

       

      I know in PowerShell I would simply do something like this to get the array of AlertObjectId's that I could then pass to the ClearAlert Verb.  Basically I need to know what the equivalent to doing that in CSharp is so that we can pass a properly formatted object to ClearAlert.

      $alertObjectIds = ($QueryResult.Results) | Select-Object -ExpandProperty AlertObjectId

       

      That would then give me a variable array called $alertObjectIds that I could then pass to the verb.  How do we need to modify the C# code below to get something similar?  (I added tons of comments in the code below so you could see what the original code was before we changed it.):

       

       private static int GetAllAlerts(ISwisClient swisClient)
              {
                  const string query = @"SELECT AlertObjectID
      FROM Orion.AlertActive
      WHERE ISNULL(Acknowledged,0)!=1
      ORDER BY TriggeredDateTime DESC";
      
      
                  JToken queryResult = swisClient.QueryAsync(query).Result;
                  Console.WriteLine(queryResult);
      
      
         // THE BELOW CODE USES A [0] TO ONLY PULL THE FIRST ITEM FROM THE RESULTS, SO WE REMOVED THAT PART FROM THE CODE
         // var alertObjectId = (int)queryResult["results"][0]["AlertObjectID"];
         
         // BY REMOVING THE [0] WE FIGURED THAT THE alertObjectId VARIABLE WOULD NOW CONTAIN ALL OF THE AlertObjectIDs RETURNED BY THE QUERY
         var alertObjectId = (int)queryResult["results"]["AlertObjectID"];
                  return alertObjectId;
              }
      
         // WE CHANGED THE METHOD CODE BELOW TO MAKE IT MORE APPROPRIATE FOR THE ClearAlert VERB
         // private static JToken AcknowledgeAlert(ISwisClient swisClient, int alertObjectId, string note)
      
      // THIS IS HOW THE METHOD IS NOW DECLARED:
      private static JToken ClearAlerts(ISwisClient swisClient, int alertObjectId)
              {
      // THE ORIGINAL CODE BELOW WAS CREATED TO INVOKE THE Acknowledge VERB FOR ONE ALERT.
      //JToken invokeResult = swisClient.InvokeAsync("Orion.AlertActive", "Acknowledge", new[] {alertObjectId}, note).Result;
                  
      // WE CHANGED IT TO PASS THE ClearAlert VERB FOR (hopefully) HOW EVER MANY ALERTS CAME BACK AS A RESULT OF OUR QUERY
      // HOWEVER, WHEN IT GETS HERE WE GET AN ERROR ABOUT THE "alertObjectId" NOT BEING THE RIGHT TYPE OF OBJECT
      JToken invokeResult = swisClient.InvokeAsync("Orion.AlertActive", "ClearAlert", new[] {alertObjectId}).Result;
      
      
      return invokeResult;
              }

       

      Thanks so much for any help you can give.

        • Re: How to Properly Select and Pass an Array of AlertObjectId's to the ClearAlert Verb using the C# API
          dan jagnow

          Here is C# code that will do what you're looking for:

           

          using System;
          using System.Collections.Generic;
          using System.Diagnostics;
          using System.Linq;
          using Newtonsoft.Json.Linq;
          
          
          namespace CSRestClient
          {
              internal class Program
              {
                  private const string Hostname = "myserver.mydomain.local";
                  private const string Username = "admin";
                  private const string Password = "superSecretPassword";
          
          
                  private static void Main()
                  {
                      try
                      {
                          var swisClient = new SwisClient(Hostname, Username, Password);
                          List<int> alertIds = GetUnacknowledgedAlertIds(swisClient);
                          bool clearResult = ClearAlerts(swisClient, alertIds);
                          Console.WriteLine($"Clear alert succeeded? {clearResult}");
                      }
                      catch (Exception ex)
                      {
                          Console.WriteLine(ex);
                      }
          
          
                      if (Debugger.IsAttached)
                      {
                          Console.WriteLine("Press enter to exit.");
                          Console.ReadLine();
                      }
                  }
          
          
                  private static List<int> GetUnacknowledgedAlertIds(ISwisClient swisClient)
                  {
                      Console.WriteLine("Retrieving unacknowledged alerts...");
          
          
                      // Build a query to get all the unacknowledged alerts.
                      const string query = @"SELECT AlertObjectID
          FROM Orion.AlertActive
          WHERE ISNULL(Acknowledged,0)!=1
          ORDER BY TriggeredDateTime DESC";
          
          
                      // Execute the query.
                      JToken queryResult = swisClient.QueryAsync(query).Result;
                      Console.WriteLine(queryResult);
          
          
                      // Look at the results.
                      // Iterate through all the records in the results.
                      // For each record, read the alert object ID as an integer.
                      List<int> alertIds = queryResult["results"]
                          .Children()
                          .Select(a => (int) a["AlertObjectID"])
                          .ToList();
          
          
                      // Return all the IDs we just collected.
                      return alertIds;
                  }
          
          
                  private static bool ClearAlerts(ISwisClient swisClient, List<int> alertIds)
                  {
                      Console.WriteLine("Clearing unacknowledged alerts...");
          
          
                      // Clear the alerts.
                      JToken invokeResult = swisClient.InvokeAsync("Orion.AlertActive", "ClearAlert", alertIds.ToArray()).Result;
                      Console.WriteLine(invokeResult);
          
          
                      // The result should be true or false.
                      return (bool) invokeResult;
                  }
              }
          }

           

          When run, it produces output like this:

           

          Retrieving unacknowledged alerts...
          {
            "results": [
              {
                "AlertObjectID": 55
              },
              {
                "AlertObjectID": 2
              },
              {
                "AlertObjectID": 8
              },
              ...
              {
                "AlertObjectID": 5
              },
              {
                "AlertObjectID": 6
              }
            ]
          }
          Clearing unacknowledged alerts...
          True
          Clear alert succeeded? True
          Press enter to exit.
          invokeResult