Click here to Skip to main content
15,887,374 members
Articles / Programming Languages / C#
Tip/Trick

Automate Splunk Licence Monitoring

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
13 Jul 2016CPOL 7.6K   3  
Creating a Windows service to monitor Splunk licence usage using splunk SDK

Introduction

This is a Windows Service to automatically monitor and disable data indexing for splunk if the total indexed data exceeds the given amount.

Unzip packages1 and packages2 inside LicenceControllerSplunk\packages.

Background

Use this if you want to no longer worry about splunk licence violations.

Using the Code

Basically, it's divided in four parts.

1. Monitor Indexed Data

C#
JobArgs Args = new JobArgs
      {
          EarliestTime = "@d",
          LatestTime = "now"
      };

      await service.LogOnAsync("username", "password");

      var job = await service.Jobs
          .CreateAsync("| rest splunk_server=local /services/licenser/pools |
          eval usedMB = round(used_bytes/1024/1024,2) | stats sum(usedMB) AS Total | sort - Total",
          args: Args, mode: ExecutionMode.Normal);

      using (var message = await job.GetSearchResponseMessageAsync
            (outputMode: OutputMode.JsonRows))
      {
          var content = await message.Content.ReadAsStringAsync();
          list.Add(content);
      }
      await service.LogOffAsync();

2. Disable All UDP Input because they are the ones that generate huge amounts of data to index

C#
try
          {
              await service.LogOnAsync("username", "password");

              var collection = service.CreateEntityCollection("data", "inputs", "udp");
              await collection.GetAllAsync();


              foreach (var entity in collection)
              {
                  dynamic dataInput = entity.Content;

                  if (dataInput.Disabled == "0")
                  {
                      try
                      {
                          await entity.SendAsync(HttpMethod.Post, "disable");
                      }
                      catch
                      {
                      }
                  }
              }
              await service.LogOffAsync();
          }
          catch
          {
          }

3. Enable All UDP Inputs after midnight (because the total amount of data to index is refreshed)

C#
try
           {
               await service.LogOnAsync("username", "password");

               var collection = service.CreateEntityCollection("data", "inputs", "udp");
               await collection.GetAllAsync();


               foreach (var entity in collection)
               {
                   dynamic dataInput = entity.Content;

                   if (dataInput.Disabled == "1")
                   {
                       try
                       {
                           await entity.SendAsync(HttpMethod.Post, "enable");
                       }
                       catch
                       {
                       }
                   }
               }
               await service.LogOffAsync();
           }
           catch
           {
           }

4. The implementation in this case disables all UDP inputs if indexed data is more than 7 GB

C#
SplunkJSON Indexed = new SplunkJSON();
        InvestigateIndexedVolume getdata = new InvestigateIndexedVolume();
        DisableDataInputUDP disable = new DisableDataInputUDP();
        EnableDataInputUDP enable = new EnableDataInputUDP();
        List<string> SplunkData = new List<string>();

 public async Task SleepAndCheckIndexedData()
        {
            var now = DateTime.Now;
            var tomorrow = now.AddDays(1);
            var durationUntilMidnight = tomorrow.Date - now;
            int duration = Convert.ToInt32(durationUntilMidnight.TotalMinutes);
            SplunkData = await getdata.InquireIndexedVolume();
            Indexed = JsonConvert.DeserializeObject<SplunkJSON>(SplunkData[0]);

            float MB = float.Parse(Indexed.rows[0][0], CultureInfo.InvariantCulture.NumberFormat);

            if (MB > 7168)
            {               
                    await disable.DisableDataUDP();
                    Thread.Sleep(60000*duration);
                    await enable.EnableDataUDP();
            }

            Thread.Sleep(3600000);
        } 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior) Student
Tunisia Tunisia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --