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
Influencer

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
Influencer

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
Get Updates on the Splunk Community!

.conf24 | Day 0

Hello Splunk Community! My name is Chris, and I'm based in Canberra, Australia's capital, and I travelled for ...

Enhance Security Visibility with Splunk Enterprise Security 7.1 through Threat ...

(view in My Videos)Struggling with alert fatigue, lack of context, and prioritization around security ...

Troubleshooting the OpenTelemetry Collector

  In this tech talk, you’ll learn how to troubleshoot the OpenTelemetry collector - from checking the ...