• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

Matching two preg_match's together

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
Rich Rumble
Asked:
Rich Rumble
  • 3
1 Solution
 
dasmaerCommented:
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
 
Rich RumbleSecurity SamuraiAuthor Commented:
I'll mess with that, thanks! Update soon
-rich
0
 
Rich RumbleSecurity SamuraiAuthor Commented:
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
 
Rich RumbleSecurity SamuraiAuthor Commented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now