Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ridingintraffic/6693681772fd9379da90dce50f8cd2e4 to your computer and use it in GitHub Desktop.
Save ridingintraffic/6693681772fd9379da90dce50f8cd2e4 to your computer and use it in GitHub Desktop.
splunk-hec.ino
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
// Need to connec to the wifi somehow, set it up here
WiFiClient wifiClient;
String esid = "yournetwork";
String epass = "password";
// splunk settings and http collector token
String collectorToken = "8A5020D5-xxxx-xxxx-875A-E881ED0C6ABB";
String splunkindexer = "<your splunk indexer>";
String eventData="";
//you need a different client per board
String clientName ="01";
void setup()
{
//serial because seeing text telling us its working is good
Serial.begin(115200); // Reduce this if your Arduino has trouble talking so fast
Serial.println("splunk hec");
initWiFi();
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
//^^ did i get an IP "its working!!"
}
void loop()
{
// build the event data, telemtry and metrics type of data goes below
String msgString ="asdf";
eventData="\"clientname\": \""+clientName + "\",\"message_recieved\": \""+String(msgString)+"\"";
Serial.println(eventData);
//send off the data
splunkpost(collectorToken,eventData,clientName,splunkindexer);
delay(10000);
// ^^ hard work, your deserve a nap
}
void initWiFi(){
Serial.println();
Serial.println("Wifi Startup");
esid.trim();
if ( esid.length() > 1 ) {
// test esid
WiFi.disconnect();
delay(100);
WiFi.mode(WIFI_STA);
Serial.print("Connecting to WiFi ");
Serial.println(esid);
WiFi.begin(esid.c_str(), epass.c_str());
if ( testWifi() == 20 ) {
return;
}
}
}
String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}
int testWifi(void) {
int c = 0;
Serial.println("Wifi test...");
while ( c < 30 ) {
if (WiFi.status() == WL_CONNECTED) { return(20); }
delay(500);
Serial.print(".");
c++;
}
Serial.println("WiFi Connect timed out");
return(10);
}
void splunkpost(String collectorToken,String PostData, String Host, String splunkindexer)
{
// recieved the token, post data clienthost and the splunk indexer
String payload = "{ \"host\" : \"" + Host +"\", \"sourcetype\" : \"http_test\", \"index\" : \"main\", \"event\": {" + PostData + "}}";
//Build the request
HTTPClient http;
String splunkurl="http://"+ splunkindexer +":8088/services/collector";
String tokenValue="Splunk " + collectorToken;
// fire at will!!
http.begin(splunkurl);
http.addHeader("Content-Type", "application/json");
Serial.println(tokenValue);
http.addHeader("Authorization", tokenValue);
Serial.println(payload);
String contentlength = String(payload.length());
http.addHeader("Content-Length", contentlength );
http.POST(payload);
http.writeToStream(&Serial);
http.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment