Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Now, for a number of reasons I need to ping several hosts on a regular basis for a dashboard display.

I use this PHP function to do it:

function PingHost($strIpAddr) {
        exec(escapeshellcmd('ping -q -W 1 -c 1 '.$strIpAddr), $dataresult, $returnvar);
        if (substr($dataresult[4],0,3) == "rtt") {
                //We got a ping result, lets parse it.
                        $arr = explode("/",$dataresult[4]);
                        return ereg_replace(" ms","",$arr[4]);
        } elseif (substr($dataresult[3],35,16) == "100% packet loss") {
                //Host is down!
                        return "Down";
        } elseif ($returnvar == "2") {
                return "No DNS";
        }
}

The problem is that whenever there is an unknown host, I will get an error logged to my apache error log (/var/log/apache/error.log). How would I go about disabling logs for this particular function ?

Disabling logs in the vhost is not an option since logs for that vhost are relevant, just not the pings.

Thanks,

share|improve this question

1 Answer 1

up vote 1 down vote accepted

you need to change something into your function - basically exec doesn't catch standard error which gets written into the log file - you need to change the function as follow

function PingHost($strIpAddr) {
    exec(escapeshellcmd('ping -q -W 1 -c 1 '.$strIpAddr.' 2>&1'), $dataresult, $returnvar);
    if (substr($dataresult[4],0,3) == "rtt") {
            //We got a ping result, lets parse it.
                    $arr = explode("/",$dataresult[4]);
                    return ereg_replace(" ms","",$arr[4]);
    } elseif (substr($dataresult[3],35,16) == "100% packet loss") {
            //Host is down!
                    return "Down";
    } elseif ($returnvar == "2") {
            return "No DNS";
    }
    if($returnvar == 68)
            return "Unknown host";

}

share|improve this answer
    
Shell commands have 2 types of standard output: stdout and stderr. With that command you redirect stderr to stdout. I think it should work. Another option could have been ' 2>/dev/null' –  LatinSuD Dec 31 '10 at 13:23
    
Unfortunately, neither adding .' 2>&1', .' 2>/dev/null' or .' > /dev/null 2>&1' work. Nice idea though, I hadn't thought about that. If I add these, what I get is something like "ping: unknown host 2>/dev/null" –  fooraide Dec 31 '10 at 14:03
    
Ok, I found it - you've pointed me on the right track. Adding .' 2>/dev/null' and removing "escapeshellcmd" worked. Thanks ! –  fooraide Dec 31 '10 at 14:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.