Skip to content

Commit

Permalink
Added new examples
Browse files Browse the repository at this point in the history
- added new examples
- JDK 17 changes
  • Loading branch information
ashah-splunk committed Sep 22, 2023
1 parent 36c36a3 commit 8fb7dda
Show file tree
Hide file tree
Showing 26 changed files with 1,165 additions and 303 deletions.
5 changes: 0 additions & 5 deletions java/Makefile
@@ -1,8 +1,3 @@
# ifeq ($(OS), Windows_NT)
# SEPERATOR=;
# else
# SEPERATOR=:
# endif
clean:
mvn clean

Expand Down
159 changes: 143 additions & 16 deletions java/src/main/java/com/splunk/examples/conf/Program.java
Expand Up @@ -2,44 +2,171 @@

import com.splunk.*;

import java.util.Map;

public class Program {

private static void list(Service service) {
ConfCollection confs = service.getConfs();
int count = 0;
for (EntityCollection conf : confs.values()){
for (EntityCollection<Entity> conf : confs.values()){
System.out.println(conf.getName());
}
}

private static void createStanza(Service service, String confName, String stanzaName){
ConfCollection confs = service.getConfs();
EntityCollection<Entity> conf = confs.get(confName);
if(conf == null) {
Command.error("Conf " + confName + " does not exists");
return;
}
if(conf.containsKey(stanzaName)){
Command.error("Stanza " + stanzaName + " already exists in the Conf " + confName);
return;
}
conf.create(stanzaName);
}

private static void readStanza(Service service, String confName, String stanzaName){
ConfCollection confs = service.getConfs();
EntityCollection<Entity> conf = confs.get(confName);
if(conf == null){
Command.error("Conf " + confName + " does not exists");
return;
}
Entity stanza = conf.get(stanzaName);
if(stanza == null){
Command.error("Stanza " + stanzaName + " does not exists in the Conf " + confName);
return;
}
for (Map.Entry entry: stanza.entrySet()) {
System.out.println(entry.getKey() + " := " + entry.getValue());
}
}

private static void updateStanza(Service service, String confName, String stanzaName, Args args){
ConfCollection confs = service.getConfs();
EntityCollection<Entity> conf = confs.get(confName);
if(conf == null){
Command.error("Conf " + confName + " does not exists");
return;
}
Entity stanza = conf.get(stanzaName);
if(stanza == null){
Command.error("Stanza " + stanzaName + " does not exists in the Conf " + confName);
return;
}
stanza.update(args);
}

private static void deleteStanza(Service service, String confName, String stanzaName){
ConfCollection confs = service.getConfs();
EntityCollection<Entity> conf = confs.get(confName);
if(conf == null){
Command.error("Conf " + confName + " does not exists");
return;
}
Entity stanza = conf.get(stanzaName);
if(stanza == null){
Command.error("Stanza " + stanzaName + " does not exists in the Conf " + confName);
return;
}
stanza.remove();
}

System.out.println(count++ + " " + conf.getName());
private static void createConf(Service service, String confName){
ConfCollection confs = service.getConfs();
if(confs.containsKey(confName)) {
Command.error("Conf " + confName + " already exists");
}
confs.create(confName);
}

private static void create(Service service, String name){
private static void readConf(Service service, String confName){
ConfCollection confs = service.getConfs();
if(confs.containsKey(name)){
Command.error("Conf " + name + " already exists");
EntityCollection<Entity> conf = confs.get(confName);
if(conf == null) {
Command.error("Conf " + confName + " does not exists");
return;
}
System.out.println("Conf " + conf.getName());
for(Entity stanza : conf.values()){
System.out.println("[" + stanza.getName() + "]");
}
confs.create(name);
}

public static void main(String[] args){
Command command = Command.splunk("conf").parse(args);
Service.setValidateCertificates(false);
HttpService.setValidateCertificates(false);
Service service = Service.connect(command.opts);
EntityCollection<Application> apps = service.getApplications();
if(!apps.containsKey("example-app")){
apps.create("example-app");
}
Args applicationArgs = new Args(command.opts);
applicationArgs.put("sharing", "app");
applicationArgs.put("owner", "nobody");
applicationArgs.put("app", "example-app");
Service appService = Service.connect(applicationArgs);

if(command.args.length == 0){
list(service);

if (command.args.length == 0){
list(appService);
return;
}

if(command.args.length != 2){
Command.error("Action and conf-name required");
String action = command.args[0];

if (command.args.length == 2 && action.equals("create")) {
String confName = command.args[1];
createConf(appService, confName);
return;
}

String action = command.args[0];
String name = command.args[1];
if (command.args.length == 2 && action.equals("read")) {
String confName = command.args[1];
readConf(appService, confName);
return;
}

if(command.args.length == 3 && (action.equals("create") || action.equals("delete") || action.equals("read"))) {

String confName = command.args[1];
String stanzaName = command.args[2];

if (action.equals("create")) {
createStanza(appService, confName, stanzaName);
return;
}

if (action.equals("read")) {
readStanza(appService, confName, stanzaName);
return;
}

deleteStanza(appService, confName, stanzaName);
return;
}

if(command.args.length == 4 && action.equals("update")) {
String confName = command.args[1];
String stanzaName = command.args[2];
String data = command.args[3];

String[] keyValuePairs = data.split(",");

Args queryArgs = new Args();

for (String pair : keyValuePairs){
String[] entry = pair.split("=");
if(entry.length!=2){
Command.error("Please provide valid body for update");
return;
}
queryArgs.put(entry[0],entry[1]);
}

if(action.equals("create")){
create(service,name);
updateStanza(appService, confName, stanzaName, queryArgs);
return;
}

Expand Down
Expand Up @@ -45,17 +45,17 @@
public class Program {
public static void main(String[] args) {
Command command = Command.splunk("info").parse(args);
Service.setValidateCertificates(false);
HttpService.setValidateCertificates(false);
Service service = Service.connect(command.opts);

String mySplunkRESTPath = "apps/local";

EntityCollection myCollection = new EntityCollection(service, mySplunkRESTPath, Entity.class, new Args());
EntityCollection<Entity> myCollection = new EntityCollection<>(service, mySplunkRESTPath, Entity.class, new Args());

System.out.println("Found " + myCollection.size() + " Splunk apps:");

for (Object myEntity : myCollection.values()) {
Entity entity = (Entity) myEntity;
for (Entity myEntity : myCollection.values()) {
Entity entity = myEntity;
System.out.println("\t" + entity.getName());
}
}
Expand Down
25 changes: 25 additions & 0 deletions java/src/main/java/com/splunk/examples/event_types/Program.java
@@ -0,0 +1,25 @@
package com.splunk.examples.event_types;

import com.splunk.*;

import java.util.Map;

public class Program {

public static void main(String[] args) {
Command command = Command.splunk("event_types").parse(args);
HttpService.setValidateCertificates(false);
Service service = Service.connect(command.opts);

EventTypeCollection eventTypes = service.getEventTypes();
for(EventType eventType : eventTypes.values()){
System.out.println(eventType.getName());
for (int i = 0; i < eventType.getName().length(); i++) System.out.print("=");
System.out.println();
for(Map.Entry<String, Object> entry : eventType.entrySet()){
System.out.println(entry.getKey() + " : " + entry.getValue());
}
System.out.println();
}
}
}
14 changes: 8 additions & 6 deletions java/src/main/java/com/splunk/examples/export/Program.java
Expand Up @@ -26,6 +26,7 @@
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -42,7 +43,7 @@ public class Program {
static String lastTime;
static int nextEventOffset;

static public void main(String[] args) {
public static void main(String[] args) {
try {
run(args);
}
Expand All @@ -54,7 +55,7 @@ static public void main(String[] args) {

static Map<String, Integer> getStartNextCSVEvent(int location, String str) {

Map<String, Integer> pair = new HashMap<String, Integer>();
Map<String, Integer> pair = new HashMap<>();
pair.put("start", -1);
pair.put("end", -1);

Expand Down Expand Up @@ -231,7 +232,7 @@ static void run(String[] argv) throws Exception {
command.addRule("search", String.class, "Search string to export");

command.parse(argv);
Service.setValidateCertificates(false);
HttpService.setValidateCertificates(false);
Service service = Service.connect(command.opts);

Args args = new Args();
Expand Down Expand Up @@ -294,9 +295,10 @@ else if (command.args[index].equals("json"))

if (fptr < 0)
fptrEof = 0; // We didn't find a valid event, so start over.
else
else {
args.put("latest_time", lastTime);
addEndOfLine = true;
}

FileChannel fc = raf.getChannel();
fc.truncate(fptrEof);
Expand All @@ -321,9 +323,9 @@ else if (command.args[index].equals("json"))
InputStream is = service.export(search, args);

// Use UTF8 sensitive reader/writers
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
FileOutputStream os = new FileOutputStream(file, true);
Writer out = new OutputStreamWriter(os, "UTF-8");
Writer out = new OutputStreamWriter(os, StandardCharsets.UTF_8);

// Read/write 8k at a time if possible
char [] xferBuffer = new char[8192];
Expand Down
32 changes: 32 additions & 0 deletions java/src/main/java/com/splunk/examples/fired_alerts/Program.java
@@ -0,0 +1,32 @@
package com.splunk.examples.fired_alerts;

import com.splunk.*;

import java.util.Arrays;

public class Program {
public static void main(String[] args) {
Command command = Command.splunk("fired_alerts").parse(args);
HttpService.setValidateCertificates(false);
Service service = Service.connect(command.opts);

FiredAlertGroupCollection firedAlertGroups = service.getFiredAlertGroups();
for(FiredAlertGroup firedAlertGroup : firedAlertGroups.values()){
System.out.println(firedAlertGroup.getPath());
for (int i = 0; i < firedAlertGroup.getPath().length(); i++) System.out.print("=");
System.out.println();
for(FiredAlert firedAlert : firedAlertGroup.getAlerts().values()){
System.out.println("FiredAlert Name : " + firedAlert.getName());
System.out.println("FiredAlert Action : " + Arrays.toString(firedAlert.getAction()));
System.out.println("FiredAlert AlertType : " + firedAlert.getAlertType());
System.out.println("FiredAlert ExpirationTime : " + firedAlert.getExpirationTime());
System.out.println("FiredAlert SavedSearchName : " + firedAlert.getSavedSearchName());
System.out.println("FiredAlert Severity : " + firedAlert.getSeverity());
System.out.println("FiredAlert SID : " + firedAlert.getSid());
System.out.println("FiredAlert TriggeredAlertCount : " + firedAlert.getTriggeredAlertCount());
System.out.println("FiredAlert TriggerTime : " + firedAlert.getTriggerTime());
System.out.println("FiredAlert IsDigestMode : " + firedAlert.isDigestMode() + "\n");
}
}
}
}
Expand Up @@ -20,18 +20,18 @@
public class Program {
public static void main(String[] argv) {
try {
run(argv);
run();
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}

static void run(String[] argsIn) throws Exception {
static void run() throws Exception {
Command command;
Service service;
Service.setValidateCertificates(false);
HttpService.setValidateCertificates(false);

command = Command.splunk("input");
service = Service.connect(command.opts);
Expand Down
7 changes: 4 additions & 3 deletions java/src/main/java/com/splunk/examples/genevents/Program.java
Expand Up @@ -20,6 +20,7 @@

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
Expand Down Expand Up @@ -53,7 +54,7 @@ public static void main(String[] args) {
}

static String makeEvent(String stamp, int i, int j) {
return String.format("%s: event bunch %d, number %d\n", stamp, i, j);
return String.format("%s: event bunch %d, number %d%n", stamp, i, j);
}

static void buildRules(Command command, String[] argsIn) {
Expand All @@ -79,7 +80,7 @@ static void run(String[] argsIn) throws Exception {

command = Command.splunk("genevents");
buildRules(command, argsIn);
Service.setValidateCertificates(false);
HttpService.setValidateCertificates(false);
service = Service.connect(command.opts);

// Determine ingest method and other input arguments.
Expand Down Expand Up @@ -135,7 +136,7 @@ static void run(String[] argsIn) throws Exception {
stream = tcpInput.attach();
}
ostream = stream.getOutputStream();
writerOut = new OutputStreamWriter(ostream, "UTF-8");
writerOut = new OutputStreamWriter(ostream, StandardCharsets.UTF_8);
}

// Generate 10 batches of 5000 events each.
Expand Down

0 comments on commit 8fb7dda

Please sign in to comment.