/// <summary> /// The main program /// </summary> /// <param name="argv">The command line arguments</param> public static void Main(string[] argv) { // Load connection info for Splunk server in .splunkrc file. var component = new SplunkComponent(); component.OpenInput("main"); component.CloseInput(false); var cli = Command.Splunk("search"); cli.AddRule("search", typeof(string), "search string"); cli.Parse(argv); if (!cli.Opts.ContainsKey("search")) { System.Console.WriteLine("Search query string required, use --search=\"query\""); Environment.Exit(1); } var service = Service.Connect(cli.Opts); var jobs = service.GetJobs(); var job = jobs.Create((string)cli.Opts["search"]); while (!job.IsDone) { Thread.Sleep(1000); } var outArgs = new JobResultsArgs { OutputMode = JobResultsArgs.OutputModeEnum.Xml, // Return all entries. Count = 0 }; using (var stream = job.Results(outArgs)) { using (var rr = new ResultsReaderXml(stream)) { foreach (var @event in rr) { System.Console.WriteLine("EVENT:"); foreach (string key in @event.Keys) { System.Console.WriteLine(" " + key + " -> " + @event[key]); } } } } }
/// <summary> /// An example program to perform a oneshot search. /// </summary> /// <param name="argv">The command line arguments</param> public static void Main(string[] argv) { // Load connection info for Splunk server in .splunkrc file. var cli = Command.Splunk("search_oneshot"); cli.AddRule("search", typeof(string), "search string"); cli.Parse(argv); if (!cli.Opts.ContainsKey("search")) { System.Console.WriteLine( "Search query string required, use --search=\"query\""); Environment.Exit(1); } var service = Service.Connect(cli.Opts); var outArgs = new JobResultsArgs { OutputMode = JobResultsArgs.OutputModeEnum.Xml, // Return all entries. Count = 0, }; using (var stream = service.Oneshot( (string)cli.Opts["search"], outArgs)) { using (var rr = new ResultsReaderXml(stream)) { foreach (var @event in rr) { System.Console.WriteLine("EVENT:"); foreach (string key in @event.Keys) { System.Console.WriteLine( " " + key + " -> " + @event[key]); } } } } }
public string GetAllResults() { var outArgs = new JobResultsArgs { OutputMode = JobResultsArgs.OutputModeEnum.Xml, // Return all entries. Count = 0 }; using (var stream = job.Results(outArgs)) { var setting = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment, }; using (var rr = new ResultsReaderXml(stream)) { foreach (var @event in rr) { events.Add(@event); } } using (var rr = XmlReader.Create(stream, setting)) { return rr.ReadOuterXml(); } } return null; }
/// <summary> /// Returns the stream I/O handle for the results from this /// job. /// </summary> /// <param name="args">The optional parameters.</param> /// <returns>The results input <see cref="Stream"/> I/O handle. /// </returns> public Stream Results(JobResultsArgs args) { return this.Results((Args) args); }
/// <summary> /// Returns the stream I/O handle for the results from this /// job. /// </summary> /// <param name="args">The optional parameters.</param> /// <returns>The results input <see cref="Stream"/> I/O handle. /// </returns> public Stream Results(JobResultsArgs args) { return(this.Results((Args)args)); }
public void JobResultStream() { var cli = SplunkSDKHelper.Command.Splunk("search"); cli.AddRule("search", typeof(string), "search string"); cli.Opts["search"] = "search index=_internal * | head 10 "; var service = Service.Connect(cli.Opts); var jobs = service.GetJobs(); var job = jobs.Create((string)cli.Opts["search"]); while (!job.IsDone) { System.Threading.Thread.Sleep(1000); } var outArgs = new JobResultsArgs { OutputMode = JobResultsArgs.OutputModeEnum.Xml, Count = 0 }; try { using (var stream = job.Results(outArgs)) { using (var rr = new ResultsReaderXml(stream)) { foreach (var @event in rr) { System.Console.WriteLine("EVENT:"); GC.Collect(); foreach (string key in @event.Keys) { System.Console.WriteLine(" " + key + " -> " + @event[key]); } } } } } catch (Exception e) { Assert.Fail(string.Format("Reading Job result throw exception : {0} ", e)); } try { using (var stream = service.Export((string)cli.Opts["search"])) { using (var rr = new ResultsReaderXml(stream)) { foreach (var @event in rr) { System.Console.WriteLine("EVENT:"); GC.Collect(); foreach (string key in @event.Keys) { System.Console.WriteLine(" " + key + " -> " + @event[key]); } } } } } catch (Exception e) { Assert.Fail(string.Format("Export result throw exception : {0} ", e)); } }