File: ss_fping.php

package info (click to toggle)
cacti 0.8.6i-3
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 4,744 kB
  • ctags: 8,967
  • sloc: php: 39,760; sql: 2,306; xml: 678; sh: 487; perl: 133; makefile: 68
file content (98 lines) | stat: -rw-r--r-- 2,344 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
#!/usr/bin/php -q

//STANDARD SCRIPT SERVER HEADER!!!
$no_http_headers = true;

/* display No errors */
error_reporting(0);

include_once(dirname(__FILE__) . "/../include/config.php");
include_once(dirname(__FILE__) . "/../lib/snmp.php");
include_once(dirname(__FILE__) . "/../lib/ping.php");

if (!isset($called_by_script_server)) {
	array_shift($_SERVER["argv"]);
	print call_user_func_array("ss_fping", $_SERVER["argv"]);
}
//End header.

function ss_fping($hostname, $ping_sweeps=6, $ping_type="ICMP", $port=80) {
	/* record start time */
	list($micro,$seconds) = split(" ", microtime());
	$ss_fping_start = $seconds + $micro;

	$ping = new Net_Ping;

	$time = array();
	$total_time = 0;
	$failed_results = 0;

	$ping->host["hostname"] = gethostbyname($hostname);
	$ping->retries = 1;
	$ping->port = $port;
	$max = 0.0;
	$min = 9999.99;
	$dev = 0.0;

	$script_timeout = read_config_option("script_timeout");
	$ping_timeout = read_config_option("ping_timeout");

	switch ($ping_type) {
	case "ICMP":
		$method = PING_ICMP;
		break;
	case "TCP":
		$method = PING_TCP;
		break;
	case "UDP":
		$method = PING_UDP;
		break;
	}

	$i = 0;
	while ($i < $ping_sweeps) {
		$result = $ping->ping(AVAIL_PING,
					$method,
					read_config_option("ping_timeout"),
					1);

		if (!$result) {
			$failed_results++;
		}else{
			$time[$i] = $ping->ping_status;
			$total_time += $ping->ping_status;
			if ($ping->ping_status < $min) $min = $ping->ping_status;
			if ($ping->ping_status > $max) $max = $ping->ping_status;
		}

		$i++;

		/* get current time */
		list($micro,$seconds) = split(" ", microtime());
		$ss_fping_current = $seconds + $micro;

		/* if called from script server, end one second before a timeout occurs */
		if ((isset($called_by_script_server)) && (($ss_fping_current - $ss_fping_start + ($ping_timeout/1000) + 1) > $script_timeout)) {
			$ping_sweeps = $i;
			break;
		}
	}

	if ($failed_results == $ping_sweeps) {
		return "loss:100.00";
	}else{
		$loss = ($failed_results/$ping_sweeps) * 100;
		$avg = $total_time/($ping_sweeps-$failed_results);

		/* calculate standard deviation */
		$predev = 0;
		foreach($time as $sample) {
			$predev += pow(($sample-$avg),2);
		}
		$dev = sqrt($predev / count($time));

		return sprintf("min:%0.4f avg:%0.4f max:%0.4f dev:%0.4f loss:%0.4f", $min, $avg, $max, $dev, $loss);
	}
}
?>