Solved

Matching two preg_match's together

Posted on 2006-11-30
4
339 Views
Last Modified: 2007-10-18
I'm gathering SNMP data by using Exec and using the native snmp support of the OS, and trying to match 4 results so that they can be placed into a DB
Variables:(oid's)
$ifindex=                    ".1.3.6.1.2.1.2.2.1.1";
$ifname=                    ".1.3.6.1.2.1.31.1.1.1.1";
$ifinoctets =                  ".1.3.6.1.2.1.2.2.1.10";
$ifoutoctets =                  ".1.3.6.1.2.1.2.2.1.16";

example data: ($ifindex)
.1.3.6.1.2.1.2.2.1.1.1 1
.1.3.6.1.2.1.2.2.1.1.2 2
.1.3.6.1.2.1.2.2.1.1.3 3
.1.3.6.1.2.1.2.2.1.1.4 4
.1.3.6.1.2.1.2.2.1.1.5 5

example data: ($ifname)
.1.3.6.1.2.1.31.1.1.1.1.1 VL1
.1.3.6.1.2.1.31.1.1.1.1.2 Fa0/1
.1.3.6.1.2.1.31.1.1.1.1.3 Fa0/2
.1.3.6.1.2.1.31.1.1.1.1.4 Fa0/3
.1.3.6.1.2.1.31.1.1.1.1.5 Fa0/4

example data: ($ifinoctets )
.1.3.6.1.2.1.2.2.1.10.1 90803108
.1.3.6.1.2.1.2.2.1.10.2 64
.1.3.6.1.2.1.2.2.1.10.3 1924420750
.1.3.6.1.2.1.2.2.1.10.4 1017964271
.1.3.6.1.2.1.2.2.1.10.5 220740393

example data: ($ifoutoctets )
.1.3.6.1.2.1.2.2.1.16.1 16488007
.1.3.6.1.2.1.2.2.1.16.2 64
.1.3.6.1.2.1.2.2.1.16.3 1892042779
.1.3.6.1.2.1.2.2.1.16.4 771219642
.1.3.6.1.2.1.2.2.1.16.5 1748592334

The ifindex tells me the number that will be appended to each OID so I can match them up.
The oid of .1.3.6.1.2.1.31.1.1.1.1.5 Fa0/4 means, ifindex 5 is fastethernet 0/4
The oid of .1.3.6.1.2.1.31.1.1.1.1.4 Fa0/3 means, ifindex 4 is fastethernet 0/3
The oid and trailing dot can be ignored, what I need is to match the last digit in the OID that is returned, with the space seperated value. For instance, these are the values for FastEthernet 0/4 from my examples above
5, Fa0/4, 220740393, 1748592334 (ifindex, ifname, ifinoctets, ifoutoctets respectively)

Here is the code I currenly have, I'd like suggestions if any to how I can complete the code to match all the data, csv is fine like my example above...
I've not gotten past simply trying to print out matches for ifindex to ifname yet... any ideas?

$outcome1 = exec("/usr/bin/snmpbulkwalk -v2c -Oqn -c $community $host $ifindex", $results);
$ifindex_match = array();
foreach ($results as $result) {
      print "$result <br />
      ";
      $temp_array = explode(" ", $result);
      $ifindex_match[] = $temp_array[1];
};
print "<p>";
foreach ($ifindex_match as $blah) print "$blah<br />";
print "<p>";
$outcome2 = exec("/usr/bin/snmpbulkwalk -v2c -Oqn -c $community $host $ifname", $results2);
foreach ($ifindex_match as $indexmatch) {
      foreach ($results2 as $result2) {
            preg_match('/.1.3.6.1.2.1.31.1.1.1.1.([\d]+)\s([\S]+)/', $result2, $ifname_match);
      };
};

foreach ($ifname_match[1] as $name_match) {
      print "A $name_match <br />";
};

-rich
0
Comment
Question by:Rich Rumble
  • 3
4 Comments
 
LVL 5

Accepted Solution

by:
dasmaer earned 500 total points
ID: 18052522
use split() on the first whitespace:

list($first_part,$second_part) = split(" ",$ifoutoctets);

then get the last digit of the $first_part:
function lastIndexOf($haystack, $needle) {
       $index        = strpos(strrev($haystack), strrev($needle));
       $index        = strlen($haystack) - strlen(index) - $index;
       return $index;
}
$digit = substr($first_part, lastIndexOf($first_part,".")+1, strlen($first_part));

So with ".1.3.6.1.2.1.2.2.1.16.5 1748592334" for example, it first gets split into ".1.3.6.1.2.1.2.2.1.16.5" and "1748592334", and then ".1.3.6.1.2.1.2.2.1.16.5" is stripped of all but the last "5".

Hope this helps

D

0
 
LVL 38

Author Comment

by:Rich Rumble
ID: 18053420
I'll mess with that, thanks! Update soon
-rich
0
 
LVL 38

Author Comment

by:Rich Rumble
ID: 18240938
Sorry it's taken so long to get back to you, I've modified the script a little bit, we found that we could drop the "strpos(strrev" and simply use "strrpos" instead
Any more suggestions? This is what were using now...

function lastIndexOf($haystack, $needle) {
       $index        = strrpos($haystack, $needle);
       $index        = substr($haystack, $index + 1);
       return $index;
};
$host = "1.2.3.4";
$community = "public";

//Device Data
$sysDescr =                                    ".1.3.6.1.2.1.1.1.0";
$device_type =                         ".1.3.6.1.4.1.9.9.25.1.1.1.2.3";
//Interface Data
$element[ifindex] =            ".1.3.6.1.2.1.2.2.1.1";
$element[ifname] =             ".1.3.6.1.2.1.31.1.1.1.1";
$element[ifhcinoctets] =       ".1.3.6.1.2.1.31.1.1.1.6";
$element[ifhcoutoctets] = ".1.3.6.1.2.1.31.1.1.1.10";
$element[ifinoctets] =             ".1.3.6.1.2.1.2.2.1.10";
$element[ifoutoctets] =            ".1.3.6.1.2.1.2.2.1.16";
// Error Data
$erdata[ifinucastpkts] =       ".1.3.6.1.2.1.2.2.1.11";
$erdata[ifinmulticastpkts] =       ".1.3.6.1.2.1.2.2.1.12";
$erdata[ifindiscards] =       ".1.3.6.1.2.1.2.2.1.13";
$erdata[ifinerrors] =             ".1.3.6.1.2.1.2.2.1.14";
$erdata[ifinunknownprotos] =       ".1.3.6.1.2.1.2.2.1.15";
$erdata[ifoutucastpkts] =       ".1.3.6.1.2.1.2.2.1.17";
$erdata[ifoutnucastpkts] =       ".1.3.6.1.2.1.2.2.1.18";
$erdata[ifoutdiscards] =       ".1.3.6.1.2.1.2.2.1.19";
$erdata[ifouterrors] =            ".1.3.6.1.2.1.2.2.1.20";
foreach($element as $element_key => $element_value) {
      $outcome = exec("/usr/bin/snmpbulkwalk -v2c -Oqn -c $community $host $element_value", $results);
      foreach($results as $result) {
            list($givenindex,$givenvalue) = split(" ",$result);
            $indexnum = lastIndexOf($givenindex,".");
            ${$element_key . "_resarray"}[$indexnum] = $givenvalue;
      };
};

foreach($erdata as $erdata_key => $erdata_value) {
      $outcome = exec("/usr/bin/snmpbulkwalk -v2c -Oqn -c $community $host $erdata_value", $results);
      foreach($results as $result) {
            list($givenindex,$givenvalue) = split(" ",$result);
            $indexnum = lastIndexOf($givenindex,".");
            ${$erdata_key . "_resarray"}[$indexnum] = $givenvalue;
      };
};
Thanks!
-rich
0
 
LVL 38

Author Comment

by:Rich Rumble
ID: 18331819
Thanks again for the suggestion, were using a derivative of the code you posted, couldn't have moved forward without it!
-rich
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question