Skip to content

Commit

Permalink
added SNMP coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertvanheusden committed Oct 14, 2024
1 parent ba615c4 commit 4f3d192
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 15 deletions.
16 changes: 13 additions & 3 deletions codecov.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@ rm -rf ./build/*

truncate -s 970M $TMPIMG

./build/iesp -b file -d $TMPIMG -L warning,warning -l $TMPDIR/iesp.log -D -P $PIDFILE -f
./build/iesp -b file -d $TMPIMG -L warning,warning -l $TMPDIR/iesp.log -D -P $PIDFILE -f -S 1610
snmpwalk -c public -v 1 localhost:1610
PID=`cat $PIDFILE`
(cd $TMPDIR ; /usr/local/libiscsi/bin/iscsi-test-cu -d -S -x -s iscsi://localhost/test/1 > /dev/null)
kill -TERM `cat $PIDFILE`
while true
do
kill -TERM $PID
if [ $? -ne 0 ] ; then
break
fi
sleep 1
done

SOURCES=`pwd`
CC=$TMPDIR/coverage1.info
(cd $TMPDIR ; geninfo $SOURCES/build/CMakeFiles/iesp.dir/ -b $SOURCES/ -o $CC --branch-coverage && mkdir temp && genhtml $CC -o temp)
echo "cd $TMPDIR ; geninfo $SOURCES/build/CMakeFiles/iesp.dir/ -b $SOURCES/ -o $CC --branch-coverage && genhtml $CC -o temp"
(cd $TMPDIR ; geninfo $SOURCES/build/CMakeFiles/iesp.dir/ -b $SOURCES/ -o $CC --branch-coverage --rc geninfo_unexecuted_blocks=1 --ignore-errors mismatch && genhtml $CC -o temp)

TJSON=$TMPDIR/test.json
gcovr --json-summary $TJSON
11 changes: 7 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void help()
printf("-L x,y set file log level (x) and screen log level (y)\n");
printf("-l x set log file\n");
printf("-D disable digest\n");
printf("-S enable SNMP agent\n");
printf("-S x enable SNMP agent on port x, usually 161\n");
printf("-P x write PID-file\n");
printf("-f become daemon process\n");
printf("-h this help\n");
Expand Down Expand Up @@ -126,19 +126,22 @@ int main(int argc, char *argv[])
std::string target_name= "test";
int trim_level = 1;
bool use_snmp = false;
int snmp_port = 161;
bool digest_chk = true;
backend_type_t bt = backend_type_t::BT_FILE;
const char *logfile = "/tmp/iesp.log";
logging::log_level_t ll_screen = logging::ll_error;
logging::log_level_t ll_file = logging::ll_error;
int o = -1;
while((o = getopt(argc, argv, "P:fSDb:d:i:p:T:t:L:l:h")) != -1) {
while((o = getopt(argc, argv, "P:fS:Db:d:i:p:T:t:L:l:h")) != -1) {
if (o == 'P')
pid_file = optarg; // used for scripting
else if (o == 'f')
do_daemon = true;
else if (o == 'S')
else if (o == 'S') {
use_snmp = true;
snmp_port = atoi(optarg);
}
else if (o == 'D')
digest_chk = false;
else if (o == 'b') {
Expand Down Expand Up @@ -225,7 +228,7 @@ int main(int argc, char *argv[])
snmp *snmp_ { nullptr };
snmp_data *snmp_data_ { nullptr };
if (use_snmp)
init_snmp(&snmp_, &snmp_data_, &ios, &is, get_diskspace, b, &cpu_usage, &ram_free_kb, &stop);
init_snmp(&snmp_, &snmp_data_, &ios, &is, get_diskspace, b, &cpu_usage, &ram_free_kb, &stop, snmp_port);

server s(&sd, &c, &is, target_name, digest_chk);

Expand Down
5 changes: 2 additions & 3 deletions snmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "snmp/snmp.h"


void init_snmp(snmp **const snmp_, snmp_data **const snmp_data_, io_stats_t *const ios, iscsi_stats_t *const is, std::function<int(void *)> get_percentage_diskspace, void *const gpd_context, int *const cpu_usage, int *const ram_free_kb, std::atomic_bool *const stop)
void init_snmp(snmp **const snmp_, snmp_data **const snmp_data_, io_stats_t *const ios, iscsi_stats_t *const is, std::function<int(void *)> get_percentage_diskspace, void *const gpd_context, int *const cpu_usage, int *const ram_free_kb, std::atomic_bool *const stop, const int port)
{
*snmp_data_ = new snmp_data();
(*snmp_data_)->register_oid("1.3.6.1.4.1.2021.13.15.1.1.2", "iESP" );
Expand Down Expand Up @@ -42,6 +42,5 @@ void init_snmp(snmp **const snmp_, snmp_data **const snmp_data_, io_stats_t *con
(*snmp_data_)->register_oid("1.3.6.1.4.1.2021.11.9.0", new snmp_data_type_stats_int(cpu_usage));
(*snmp_data_)->register_oid("1.3.6.1.4.1.2021.4.11.0", new snmp_data_type_stats_int(ram_free_kb));

*snmp_ = new snmp(*snmp_data_, stop);
*snmp_ = new snmp(*snmp_data_, stop, port);
}

2 changes: 1 addition & 1 deletion snmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
#include "snmp/snmp.h"


void init_snmp(snmp **const snmp_, snmp_data **const snmp_data_, io_stats_t *const ios, iscsi_stats_t *const is, std::function<int(void *)> percentage_diskspace, void *const gpd_context, int *const cpu_usage, int *const ram_free_kb, std::atomic_bool *const stop);
void init_snmp(snmp **const snmp_, snmp_data **const snmp_data_, io_stats_t *const ios, iscsi_stats_t *const is, std::function<int(void *)> percentage_diskspace, void *const gpd_context, int *const cpu_usage, int *const ram_free_kb, std::atomic_bool *const stop, const int port);
6 changes: 3 additions & 3 deletions snmp/snmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ namespace qn = qindesign::network;
#include "snmp_elem.h"


snmp::snmp(snmp_data *const sd, std::atomic_bool *const stop): sd(sd), stop(stop)
snmp::snmp(snmp_data *const sd, std::atomic_bool *const stop, const int port): sd(sd), stop(stop)
{
#if !defined(ARDUINO) || defined(ESP32)
fd = socket(AF_INET, SOCK_DGRAM, 0);

sockaddr_in servaddr { };
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(161);
servaddr.sin_port = htons(port);

if (bind(fd, reinterpret_cast<const struct sockaddr *>(&servaddr), sizeof servaddr) == -1)
DOLOG(logging::ll_error, "snmp::snmp", "-", "Failed to bind to SNMP UDP port\n");
DOLOG(logging::ll_error, "snmp::snmp", "-", "Failed to bind to SNMP UDP port %d", port);
#else
handle = new qn::EthernetUDP();
handle->begin(161);
Expand Down
2 changes: 1 addition & 1 deletion snmp/snmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class snmp
#endif

public:
snmp(snmp_data *const sd, std::atomic_bool *const stop);
snmp(snmp_data *const sd, std::atomic_bool *const stop, const int port);
snmp(const snmp &) = delete;
virtual ~snmp();

Expand Down

0 comments on commit 4f3d192

Please sign in to comment.