mbeals Posted January 16, 2008 Share Posted January 16, 2008 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/86346-solved-escaping-from-a-function/ Share on other sites More sharing options...
GingerRobot Posted January 16, 2008 Share Posted January 16, 2008 You just need a return statement. A return statement literally returns control of the script back to where the function was called. Quote Link to comment https://forums.phpfreaks.com/topic/86346-solved-escaping-from-a-function/#findComment-441206 Share on other sites More sharing options...
mbeals Posted January 16, 2008 Author Share Posted January 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/86346-solved-escaping-from-a-function/#findComment-441210 Share on other sites More sharing options...
Ken2k7 Posted January 16, 2008 Share Posted January 16, 2008 Can you explain your problem more accurately and more specifically? I don't understand What do you mean 'kill a function'? Can you put up an example of what you want for success and fail attempts? Quote Link to comment https://forums.phpfreaks.com/topic/86346-solved-escaping-from-a-function/#findComment-441278 Share on other sites More sharing options...
mbeals Posted January 16, 2008 Author Share Posted January 16, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/86346-solved-escaping-from-a-function/#findComment-441301 Share on other sites More sharing options...
Ken2k7 Posted January 16, 2008 Share Posted January 16, 2008 So by snmpwalk fails, do you mean if one fails or if they all fail? Quote Link to comment https://forums.phpfreaks.com/topic/86346-solved-escaping-from-a-function/#findComment-441307 Share on other sites More sharing options...
mbeals Posted January 16, 2008 Author Share Posted January 16, 2008 if one fails. As soon as snmpwalk returns an error, I want the function to give and just return 0. Quote Link to comment https://forums.phpfreaks.com/topic/86346-solved-escaping-from-a-function/#findComment-441312 Share on other sites More sharing options...
Ken2k7 Posted January 16, 2008 Share Posted January 16, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/86346-solved-escaping-from-a-function/#findComment-441315 Share on other sites More sharing options...
Daukan Posted January 16, 2008 Share Posted January 16, 2008 You can give an argument in snmpwalk() to limit it to one try manual snmpwalk() Quote Link to comment https://forums.phpfreaks.com/topic/86346-solved-escaping-from-a-function/#findComment-441317 Share on other sites More sharing options...
interpim Posted January 16, 2008 Share Posted January 16, 2008 will something like this work? if(error){ exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/86346-solved-escaping-from-a-function/#findComment-441323 Share on other sites More sharing options...
mbeals Posted January 16, 2008 Author Share Posted January 16, 2008 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). Quote Link to comment https://forums.phpfreaks.com/topic/86346-solved-escaping-from-a-function/#findComment-441326 Share on other sites More sharing options...
mbeals Posted January 16, 2008 Author Share Posted January 16, 2008 will something like this work? if(error){ exit(); } I tried that originally. That (as well as using exit() or die() in the error handler) causes the entire script to exit. So if I hit an error, it wouldn't try the second IP. Quote Link to comment https://forums.phpfreaks.com/topic/86346-solved-escaping-from-a-function/#findComment-441331 Share on other sites More sharing options...
trq Posted January 16, 2008 Share Posted January 16, 2008 <?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; } Quote Link to comment https://forums.phpfreaks.com/topic/86346-solved-escaping-from-a-function/#findComment-441364 Share on other sites More sharing options...
mbeals Posted January 16, 2008 Author Share Posted January 16, 2008 it was honestly that easy? thanks Quote Link to comment https://forums.phpfreaks.com/topic/86346-solved-escaping-from-a-function/#findComment-441411 Share on other sites More sharing options...
mbeals Posted January 16, 2008 Author Share Posted January 16, 2008 actually.....that didn't work. gave a parse error, but if (!$uptime = snmpwalk($ip, "c9networks", "1.3.6.1.2.1.1.9.1.4")){ return 0;} worked like a charm Quote Link to comment https://forums.phpfreaks.com/topic/86346-solved-escaping-from-a-function/#findComment-441417 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.