bachoA4o has asked for the wisdom of the Perl Monks concerning the following question:

Hello , I'm trying to make a monitor that counts in and and out unicast packets :
my $in; my $out; while (1){ $in = system("snmpget -Oqv -v2c -c randomstr localhost 1.3.6.1.2.1.31. +1.1.1.7.3"); $out = system("snmpget -Oqv -v2c -c randomstr localhost 1.3.6.1.2.1.31 +.1.1.1.11.3"); system("rrdtool update opit11.rrd N:$in:$out") ; sleep 10; }
When executing the code i see the number of packets on the terminal .When i execute  rrdtool fetch opit11.rrd --start -100 it shows only zeroes 0.0000000000e+00 for the time that the script was running . If somoene can point me where is my mistake ? Thank you !

Replies are listed 'Best First'.
Re: Update RRD
by haukex (Archbishop) on Nov 25, 2018 at 13:47 UTC

    system does not capture the command's output, for that I would recommend capture or rather capturex from IPC::System::Simple. Perl does have this functionality built in, but I usually recommend staying away from it, for reasons I explained here (along with other modules and sample code). In addition, you should validate that $in and $out look as you expect before feeding them into rrdtool. Since AFAICT they should normally be numbers, I'm using Regexp::Common to validate them.

    use warnings; use strict; use IPC::System::Simple qw/systemx capturex/; use Regexp::Common qw/number/; while (1) { my $in = capturex(qw/ snmpget -Oqv -v2c -c randomstr localhost 1. +3.6.1.2.1.31.1.1.1.7.3 /); my $out = capturex(qw/ snmpget -Oqv -v2c -c randomstr localhost 1. +3.6.1.2.1.31.1.1.1.11.3 /); for ($in,$out) { chomp; die "Bad number '$_'" unless /^$RE{num}{real}$/; } systemx('rrdtool','update','opit11.rrd',"N:$in:$out"); sleep 10; }
Re: Update RRD
by bachoA4o (Sexton) on Nov 25, 2018 at 14:02 UTC
    I've managed to find the problem . For some reason the database was not seeing the data as 'int' . I've made explicit conversion to 'int' and its working now . I'm still open if someone have advices for a better code :).
      its working now

      Sorry, but I have to doubt that $in = system("snmpget ...") is really "working". system returns the command's exit code as it would be returned by wait(2). The snmpcmd manpage doesn't make any mention of exit codes, but the convention is that commands exit with 0 (zero) on success - so with the code shown in the question, $in and $out will probably be 0 most of the time, and some other number on failures.

        You are right ! I`ve used backticks , forgot to mention it in my comment .