Getting Data In

Sample C++ Boost code to send msg to Splunk

dilipkha
New Member

Hi,

I'm trying to run this below sample code to send msg to Splunk, but getting error - Host not found.

Am I doing right?

I'm able to ping the splunk server (171.134.154.114) from my dev Linux server.

However, I'm able to use Curl command successfully and able to see my msg in Splunk dashboard.

doc/html/boost_asio/example/http/client/sync_client.cpp - 1.47.0

./sync_client 171.134.154.114 /services/collector
arg[1]:171.134.154.114
Exception: resolve: Host not found (authoritative) [asio.netdb:1]

 

Labels (1)
0 Karma

tscroggins
SplunkTrust
SplunkTrust

Hi @dilipkha,

Under the hood, Boost calls getaddrinfo on Linux, which should accept IP addresses as strings.

When I compile the example with g++ 8.5.0 and Boost 1.66.0 on my RHEL 8 host, the program works as expected using http as the service:

 

$ g++ -o sync_client -lboost_system -lpthread sync_client.cpp
$ chmod 0775 sync_client
$ host httpbin.org
httpbin.org has address 23.22.173.247
httpbin.org has address 52.206.0.51
$ ./sync_client 23.22.173.247 /get
Date: Sun, 28 Jan 2024 04:47:22 GMT
Content-Type: application/json
Content-Length: 225
Connection: close
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Host": "23.22.173.247",
    "X-Amzn-Trace-Id": "Root=1-65b5dc5a-2e13219829bcecc360851dcb"
  },
  "origin": "x.x.x.x",
  "url": "http://23.22.173.247/get"
}

 

In your implementation, you may need to use a different query constructor on line 35, e.g.:

 

tcp::resolver::query query(argv[1], "8089", boost::asio::ip::resolver_query_base::numeric_host | boost::asio::ip::resolver_query_base::numeric_service);

 

Note that I've also replaced "http" with "8089" to use the default Splunk management port. On most systems, the http service resolves to port 80. See e.g. /etc/services.

0 Karma

tscroggins
SplunkTrust
SplunkTrust

Also note that sync_client.cpp isn't a TLS client. It will only work with a plaintext HTTP server. Connecting to a TLS endpoint should return:

Exception: read_until: Connection reset by peer
0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Recap | Agentic Operations Start with Context: Build the Right Data Foundation

Agentic Operations Start with Context: Build the Right Data Foundation   By Courtney Wright, Product Marketing ...

Recap | Assisted, Augmented or Agentic? Choose Your Splunk Starting Point

Assisted, Augmented or Agentic? Choose Your Splunk Starting Point   By Courtney Wright, Product Marketing ...

Session 2 | Beyond the Thread: Operationalizing AI with Confidence

Session 2   Beyond the Thread: Operationalizing AI with Confidence    The true power of the Cisco Data Fabric ...