Jump to content

[SOLVED] escaping from a function


mbeals

Recommended Posts

I'm writing a script that uses snmpwalk to pull in info off a server.  The gateway in front of the server has a failover such that if one network interface drops, the other comes alive.  So I effectively have two ips, one of which is active.  To do this I have a function that attempts to snmpwalk several OIDS for a given IP.  If the function succeeds, then it returns 1.  If the snmpwalk fails, I need to stop execution of the function so that it doesn't attempt to keep walking the same IP several more times.  Currently I use die() in the error handler, but that kills the entire script....I just need to stop execution of the function while allowing the script to move on to try the next IP address.  How do I construct an error handler to kill a function, but then allow the parent script to keep processing?

 

function walkProp($ip) {


function walkError($errno, $errstr)
	 { 
	die();
	 }

global $uptime, $MAC, $RX, $STAT, $SNR;

set_error_handler("walkError");
$uptime = snmpwalk($ip, "***, "1.3.6.1.2.1.1.9.1.4");
$MAC = snmpwalk($ip, "***", "1.3.6.1.2.1.10.127.1.3.3.1.2");
$RX = snmpwalk($ip, "***", "1.3.6.1.2.1.10.127.1.3.3.1.6");
$STAT = snmpwalk($ip, "***", "1.3.6.1.2.1.10.127.1.3.3.1.9");
$SNR = snmpwalk($ip, "***", "1.3.6.1.2.1.10.127.1.3.3.1.13");
restore_error_handler();

return '1';
}


if(walkProp($ip1)){
echo "IP 1 active";
}elseif(walkProp($ip2)){
echo "IP 2 active";
}else{
echo "connection down";
}

 

 

Link to comment
Share on other sites

i tried a return like this:

 

function walkProp($ip) {


function customError()
	 { 
	return false;
	 }

global $uptime, $MAC, $RX, $STAT, $SNR;

set_error_handler("customError");

$uptime = snmpwalk($ip, "c9networks", "1.3.6.1.2.1.1.9.1.4");
$MAC = snmpwalk($ip, "c9networks", "1.3.6.1.2.1.10.127.1.3.3.1.2");
$RX = snmpwalk($ip, "c9networks", "1.3.6.1.2.1.10.127.1.3.3.1.6");
$STAT = snmpwalk($ip, "c9networks", "1.3.6.1.2.1.10.127.1.3.3.1.9");
$SNR = snmpwalk($ip, "c9networks", "1.3.6.1.2.1.10.127.1.3.3.1.13");
restore_error_handler();


return '1';
}

 

but it keeps marching through the function code as if an error handler wasn't even there.

Link to comment
Share on other sites

The function is the one I just posted.  There are 5 consecutive SNMPwalk calls.

 

If the snmpwalk call is successful, then the data is recorded into the global variable and the function moves on to the next one.  If all 5 are successful then the function exits, returning 1.

 

If a snmpwalk fails, I want to halt execution of the function....preventing it from attempting to snmpwalk the same ip address again and again only to have it fail each time....and exit the function, returning 0 instead.

 

So when I run the function for IP1, if the IP is alive and host reachable, then the data retrieved by the snmpwalk is stored in a set of vars and my program can go on to finish processing that data.  If IP1 fails, then it moves on to IP2 and repeats

Link to comment
Share on other sites

Oh, well I think you can just use a simple if statement for each statement. If any returns false, then you can return 0 right there and once a function hits the return line, the function stops.

 

So like:

if (!$uptime = snmpwalk($ip, "c9networks", "1.3.6.1.2.1.1.9.1.4")) return 0;

else if (!$MAC = snmpwalk($ip, "c9networks", "1.3.6.1.2.1.10.127.1.3.3.1.2")) return 0;

.

.

.

 

Get it?

Link to comment
Share on other sites

ken,

I'll try that.  Thanks for the idea.

 

Daukan, that's fine for a single call to snmpwalk, but I'm calling it 5 times in a row.  If the first call fails (to get one value), I don't want to try and call it again (to obtain the rest of the values).

Link to comment
Share on other sites

<?php

function walkProp($ip) {

  global $uptime, $MAC, $RX, $STAT, $SNR;

  $uptime = snmpwalk($ip, "c9networks", "1.3.6.1.2.1.1.9.1.4") || return false;
  $MAC = snmpwalk($ip, "c9networks", "1.3.6.1.2.1.10.127.1.3.3.1.2") || return false;
  $RX = snmpwalk($ip, "c9networks", "1.3.6.1.2.1.10.127.1.3.3.1.6") || return false;
  $STAT = snmpwalk($ip, "c9networks", "1.3.6.1.2.1.10.127.1.3.3.1.9") || return false;
  $SNR = snmpwalk($ip, "c9networks", "1.3.6.1.2.1.10.127.1.3.3.1.13") || return false;

  return true;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.