Skip to content

Commit

Permalink
feat(core): add driver additional info parsing & add message formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
y4cer committed Jul 14, 2023
1 parent c1f956c commit 41749c8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ Sample data generator for the AHLS course at IU Summer 2023

## Build
```sh
docker build -f Dockerfile --tag grpc-cmake:1.56.0 .
docker build -f Dockerfile --tag <insert_your_tag_here> .
```

## Pull docker image
```sh
docker pull ghcr.io/y4cer/snmp_driver_generator:main
```

## Run
```sh
docker run -it grpc-cmake:1.56.0 ./build/driver <transfer_rate> <remote_snmp_ip>
docker run -it <the_tag_you_inserted_or_pulled> ./build/driver <sensor_id><requests per second> <remote_snmp_client_ip> <remote_grpc_server_addr>
```
49 changes: 34 additions & 15 deletions main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <iostream>
#include <thread>
#include <format>

#include "grpc/key_value_service_client.h"
#include "snmp/SNMP_client.h"
Expand All @@ -17,32 +18,50 @@ void timer_start(std::function<void(void)> func, unsigned int interval) {
}).detach();
}

void driver_generate(unsigned int rps, const std::string& ip) {
const char* GRPC_SERVER_ADDR = "127.0.0.1:1234";
void driver_generate(unsigned int rps, const std::string& ip,
const std::string& grpc_server_addr,
const std::string& sensor_name,
const std::string& oid = ".1.3.6.1.2.1.1.1.0") {
const unsigned int interval = 1000 / rps;
KeyValueService_client client(
grpc::CreateChannel(GRPC_SERVER_ADDR,
grpc::CreateChannel(grpc_server_addr,
grpc::InsecureChannelCredentials()));
timer_start([&client, &ip]() -> void {
std::vector<std::string> resp = SNMPClient(ip, "public").send_request();
timer_start([&client, &ip, &sensor_name, &oid]() -> void {
std::string key = std::format("SNMP_{}_{}", sensor_name, ip);
std::cout << "KEY: " << key << std::endl;
std::vector<std::string> resp =
SNMPClient(ip, "public").send_request(oid);
for (auto& str : resp) {
client.store_value(ip, str);
}
}
, interval);
std::string val = std::format("{} = {}", oid, str);
std::cout << "VALUE:" << val << std::endl;
std::cout << client.store_value(ip, str) << std::endl;
}} , interval);
while (true);
}

int main(int argc, char** argv) {

if (argc != 3) {
std::cout << "Usage: " << argv[0] << " <requests per second>"
<< " <remote_snmp_client_ip>" << std::endl;
if (argc != 5) {
std::cout << "Usage: " << argv[0] << " <sensor_id> " <<
" <requests per second>" << " <remote_snmp_client_ip>"
<< " <remote_grpc_server_addr>" << std::endl;
return 0;
}
long rps = std::stol(argv[1]);
std::string ip = argv[2];
driver_generate(rps, ip);
std::string sensor_id = argv[1];
long rps;
try {
rps = std::stol(argv[2]);
} catch (std::invalid_argument& e) {
std::cout << "Invalid parameter for rps!\n";
return 1;
} catch (std::out_of_range& e) {
std::cout << "Invalid parameter for rps!\n";
return 1;
}
std::string ip = argv[3];
std::string grpc_server_addr = argv[4];

driver_generate(rps, ip, grpc_server_addr, sensor_id);

return 0;
}
4 changes: 4 additions & 0 deletions snmp/SNMP_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "SNMP_client.h"

#undef DEBUG

SNMPClient::SNMPClient(const std::string& peer_name,
const std::string& community_name) {
init_snmp("snmp_driver");
Expand Down Expand Up @@ -60,7 +62,9 @@ SNMPClient::send_request(const std::string& oid_string) {
memcpy(sp, vars->val.string, vars->val_len);
sp[vars->val_len] = '\0';
response_strings.emplace_back(sp);
#ifdef DEBUG
printf("value #%d is a string: %s\n", count++, sp);
#endif
free(sp);
}
else
Expand Down
2 changes: 1 addition & 1 deletion snmp/SNMP_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SNMPClient {
* @throws std::invalid_argument if the oid is invalid
*/
std::vector<std::string>
send_request(const std::string& oid = ".1.3.6.1.2.1.1.1.0");
send_request(const std::string& oid);
private:
netsnmp_session session;
netsnmp_session *ss;
Expand Down

0 comments on commit 41749c8

Please sign in to comment.