Skip to content

Commit 4f3d192

Browse files
added SNMP coverage
1 parent ba615c4 commit 4f3d192

File tree

6 files changed

+27
-15
lines changed

6 files changed

+27
-15
lines changed

codecov.sh

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,23 @@ rm -rf ./build/*
1414

1515
truncate -s 970M $TMPIMG
1616

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

2130
SOURCES=`pwd`
2231
CC=$TMPDIR/coverage1.info
23-
(cd $TMPDIR ; geninfo $SOURCES/build/CMakeFiles/iesp.dir/ -b $SOURCES/ -o $CC --branch-coverage && mkdir temp && genhtml $CC -o temp)
32+
echo "cd $TMPDIR ; geninfo $SOURCES/build/CMakeFiles/iesp.dir/ -b $SOURCES/ -o $CC --branch-coverage && genhtml $CC -o temp"
33+
(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)
2434

2535
TJSON=$TMPDIR/test.json
2636
gcovr --json-summary $TJSON

main.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void help()
9393
printf("-L x,y set file log level (x) and screen log level (y)\n");
9494
printf("-l x set log file\n");
9595
printf("-D disable digest\n");
96-
printf("-S enable SNMP agent\n");
96+
printf("-S x enable SNMP agent on port x, usually 161\n");
9797
printf("-P x write PID-file\n");
9898
printf("-f become daemon process\n");
9999
printf("-h this help\n");
@@ -126,19 +126,22 @@ int main(int argc, char *argv[])
126126
std::string target_name= "test";
127127
int trim_level = 1;
128128
bool use_snmp = false;
129+
int snmp_port = 161;
129130
bool digest_chk = true;
130131
backend_type_t bt = backend_type_t::BT_FILE;
131132
const char *logfile = "/tmp/iesp.log";
132133
logging::log_level_t ll_screen = logging::ll_error;
133134
logging::log_level_t ll_file = logging::ll_error;
134135
int o = -1;
135-
while((o = getopt(argc, argv, "P:fSDb:d:i:p:T:t:L:l:h")) != -1) {
136+
while((o = getopt(argc, argv, "P:fS:Db:d:i:p:T:t:L:l:h")) != -1) {
136137
if (o == 'P')
137138
pid_file = optarg; // used for scripting
138139
else if (o == 'f')
139140
do_daemon = true;
140-
else if (o == 'S')
141+
else if (o == 'S') {
141142
use_snmp = true;
143+
snmp_port = atoi(optarg);
144+
}
142145
else if (o == 'D')
143146
digest_chk = false;
144147
else if (o == 'b') {
@@ -225,7 +228,7 @@ int main(int argc, char *argv[])
225228
snmp *snmp_ { nullptr };
226229
snmp_data *snmp_data_ { nullptr };
227230
if (use_snmp)
228-
init_snmp(&snmp_, &snmp_data_, &ios, &is, get_diskspace, b, &cpu_usage, &ram_free_kb, &stop);
231+
init_snmp(&snmp_, &snmp_data_, &ios, &is, get_diskspace, b, &cpu_usage, &ram_free_kb, &stop, snmp_port);
229232

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

snmp.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "snmp/snmp.h"
88

99

10-
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)
10+
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)
1111
{
1212
*snmp_data_ = new snmp_data();
1313
(*snmp_data_)->register_oid("1.3.6.1.4.1.2021.13.15.1.1.2", "iESP" );
@@ -42,6 +42,5 @@ void init_snmp(snmp **const snmp_, snmp_data **const snmp_data_, io_stats_t *con
4242
(*snmp_data_)->register_oid("1.3.6.1.4.1.2021.11.9.0", new snmp_data_type_stats_int(cpu_usage));
4343
(*snmp_data_)->register_oid("1.3.6.1.4.1.2021.4.11.0", new snmp_data_type_stats_int(ram_free_kb));
4444

45-
*snmp_ = new snmp(*snmp_data_, stop);
45+
*snmp_ = new snmp(*snmp_data_, stop, port);
4646
}
47-

snmp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
#include "snmp/snmp.h"
33

44

5-
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);
5+
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);

snmp/snmp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ namespace qn = qindesign::network;
2929
#include "snmp_elem.h"
3030

3131

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

3737
sockaddr_in servaddr { };
3838
servaddr.sin_family = AF_INET; // IPv4
3939
servaddr.sin_addr.s_addr = INADDR_ANY;
40-
servaddr.sin_port = htons(161);
40+
servaddr.sin_port = htons(port);
4141

4242
if (bind(fd, reinterpret_cast<const struct sockaddr *>(&servaddr), sizeof servaddr) == -1)
43-
DOLOG(logging::ll_error, "snmp::snmp", "-", "Failed to bind to SNMP UDP port\n");
43+
DOLOG(logging::ll_error, "snmp::snmp", "-", "Failed to bind to SNMP UDP port %d", port);
4444
#else
4545
handle = new qn::EthernetUDP();
4646
handle->begin(161);

snmp/snmp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class snmp
5555
#endif
5656

5757
public:
58-
snmp(snmp_data *const sd, std::atomic_bool *const stop);
58+
snmp(snmp_data *const sd, std::atomic_bool *const stop, const int port);
5959
snmp(const snmp &) = delete;
6060
virtual ~snmp();
6161

0 commit comments

Comments
 (0)