in reply to MRTG script should be easy

It would be much easier to help you if you give us an example of the output of your ping. Mine for example has no "," in it. So I assume mine looks different from yours. The output of ping on Solaris is even worse as it is just: "Machine is alive" ;-)

Replies are listed 'Best First'.
Re: Re: MRTG script should be easy
by halley (Prior) on May 20, 2003 at 16:24 UTC

    Whenever I write code that tries to parse some complex string, I put a comment just above the code which includes a literal representative sample. This is according to the principle, "strategy in comments, tactics in code."

    --
    [ e d @ h a l l e y . c c ]

Re: Re: MRTG script should be easy
by jalspach (Acolyte) on May 20, 2003 at 18:48 UTC
    Thank you all very much, for your kind assistance. I lost my camel book and, I guess, my brain with it. :-) Here is the output of my ping:
    PING dslreports.com (209.123.109.175) from 10.0.1.1 : 56(84) bytes of +data. 64 bytes from www.dslreports.com (209.123.109.175): icmp_seq=0 ttl=51 +time=89.483 msec 64 bytes from www.dslreports.com (209.123.109.175): icmp_seq=1 ttl=51 +time=119.926 msec 64 bytes from www.dslreports.com (209.123.109.175): icmp_seq=2 ttl=51 +time=89.899 msec 64 bytes from www.dslreports.com (209.123.109.175): icmp_seq=3 ttl=51 +time=99.920 msec --- dslreports.com ping statistics --- 4 packets transmitted, 4 packets received, 0% packet loss round-trip min/avg/max/mdev = 89.483/99.807/119.926/12.344 ms
    and here is the output of my script (obviously the numbers are not exactly the same since I did them one after the other):
    0% packet loss round-trip min/avg/max/mdev = 89.908/91.193/95.024/2.221 ms 91 0 0
    Having said that, I think I may look at the net:ping option in general and the graciously supplied script in particular. I briefly thought about NET:PING last night but this script was so close to working as it was, I hated to start from scratch and rewrite it. Especially if it turned out, I was just missing something small. Thank you all again, if any of you ever run for something...you have my vote :-) James
      Okay. So here is how I would do it:
      use strict; my (%result); # Example lines searched for # 4 packets transmitted, 4 packets received, 0% packet loss # round-trip min/avg/max/mdev = 89.483/99.807/119.926/12.344 ms while (<DATA>) { SWITCH: { /transmitted/ && do { # This will "eat up" all "packets"-information $result{$2}=$1 while /(\d+)\s+packets\s+(\S+)/ +g; last SWITCH; }; /round-trip/ && do { # this will find all values seperated by / # and will store them under their key /(\S+)\s*=\s*(\S+)/; @result{split m(/),$1}= split m(/),$2; last SWITCH; }; } } # Your %result-keys depend on the output of ping print $result{'transmitted'}-$result{'received'}," packet(s) lost\n"; print $result{'avg'}," average\n"; print Dumper(\%result); __END__ PING dslreports.com (209.123.109.175) from 10.0.1.1 : 56(84) bytes of +data. 64 bytes from www.dslreports.com (209.123.109.175): icmp_seq=0 ttl=51 +time=89.483 msec 64 bytes from www.dslreports.com (209.123.109.175): icmp_seq=1 ttl=51 +time=119.926 msec 64 bytes from www.dslreports.com (209.123.109.175): icmp_seq=2 ttl=51 +time=89.899 msec 64 bytes from www.dslreports.com (209.123.109.175): icmp_seq=3 ttl=51 +time=99.920 msec --- dslreports.com ping statistics --- 4 packets transmitted, 4 packets received, 0% packet loss round-trip min/avg/max/mdev = 89.483/99.807/119.926/12.344 ms
      Example output:
      0 packet(s) lost 99.807 average $VAR1 = { 'received,' => '4', 'avg' => '99.807', 'min' => '89.483', 'mdev' => '12.344', 'max' => '119.926', 'transmitted,' => '4' };