1
+ #!/usr/bin/env php
1
2
<?php
2
- #!/usr/bin/php -q
3
+ /*
4
+ +-------------------------------------------------------------------------+
5
+ | Copyright (C) 2004-2022 The Cacti Group |
6
+ | |
7
+ | This program is free software; you can redistribute it and/or |
8
+ | modify it under the terms of the GNU General Public License |
9
+ | as published by the Free Software Foundation; either version 2 |
10
+ | of the License, or (at your option) any later version. |
11
+ | |
12
+ | This program is distributed in the hope that it will be useful, |
13
+ | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
+ | GNU General Public License for more details. |
16
+ +-------------------------------------------------------------------------+
17
+ | Cacti: The Complete RRDtool-based Graphing Solution |
18
+ +-------------------------------------------------------------------------+
19
+ | This code is designed, written, and maintained by the Cacti Group. See |
20
+ | about.php and/or the AUTHORS file for specific developer information. |
21
+ +-------------------------------------------------------------------------+
22
+ | http://www.cacti.net/ |
23
+ +-------------------------------------------------------------------------+
24
+ */
3
25
4
- /* do NOT run this script through a web browser */
5
- $ no_http_headers = true ;
6
-
7
- /* display no errors */
8
26
error_reporting (0 );
9
27
10
- include_once (dirname (__FILE__ ) . '/../include/cli_check.php ' );
11
- include_once (dirname (__FILE__ ) . '/../lib/snmp.php ' );
12
- include_once (dirname (__FILE__ ) . '/../lib/ping.php ' );
13
-
14
28
if (!isset ($ called_by_script_server )) {
29
+ include_once (dirname (__FILE__ ) . '/../include/cli_check.php ' );
30
+ include_once (dirname (__FILE__ ) . '/../lib/snmp.php ' );
31
+ include_once (dirname (__FILE__ ) . '/../lib/ping.php ' );
32
+
15
33
array_shift ($ _SERVER ['argv ' ]);
34
+
16
35
print call_user_func_array ('ss_fping ' , $ _SERVER ['argv ' ]);
36
+ } else {
37
+ include_once (dirname (__FILE__ ) . '/../lib/snmp.php ' );
38
+ include_once (dirname (__FILE__ ) . '/../lib/ping.php ' );
17
39
}
18
- //End header.
19
40
20
- function ss_fping ($ hostname , $ ping_sweeps= 6 , $ ping_type= 'ICMP ' , $ port= 80 ) {
41
+ function ss_fping ($ hostname = '' , $ ping_sweeps = 6 , $ ping_type = 'ICMP ' , $ port = 80 ) {
21
42
/* record start time */
22
- list ($ micro ,$ seconds ) = explode (' ' , microtime ());
23
- $ ss_fping_start = $ seconds + $ micro ;
43
+ $ ss_fping_start = microtime (true );
24
44
25
45
$ ping = new Net_Ping ;
26
46
27
- $ time = array ();
28
- $ total_time = 0 ;
47
+ $ time = array ();
48
+ $ total_time = 0 ;
29
49
$ failed_results = 0 ;
30
50
31
51
$ ping ->host ['hostname ' ] = gethostbyname ($ hostname );
32
- $ ping ->retries = 1 ;
33
- $ ping ->port = $ port ;
52
+ $ ping ->retries = 1 ;
53
+ $ ping ->port = $ port ;
54
+
34
55
$ max = 0.0 ;
35
56
$ min = 9999.99 ;
36
57
$ dev = 0.0 ;
37
58
38
59
$ script_timeout = read_config_option ('script_timeout ' );
39
- $ ping_timeout = read_config_option ('ping_timeout ' );
40
-
41
- switch ($ ping_type ) {
42
- case 'ICMP ' :
43
- $ method = PING_ICMP ;
44
- break ;
45
- case 'TCP ' :
46
- $ method = PING_TCP ;
47
- break ;
48
- case 'UDP ' :
49
- $ method = PING_UDP ;
50
- break ;
60
+
61
+ $ ping_timeout = db_fetch_cell_prepared ('SELECT ping_timeout
62
+ FROM host
63
+ WHERE hostname = ? ' ,
64
+ array ($ hostname ));
65
+
66
+ if (empty ($ ping_timeout )) {
67
+ $ ping_timeout = read_config_option ('ping_timeout ' );
68
+ }
69
+
70
+ switch (strtoupper ($ ping_type )) {
71
+ case 'ICMP ' :
72
+ $ method = PING_ICMP ;
73
+
74
+ break ;
75
+ case 'TCP ' :
76
+ $ method = PING_TCP ;
77
+
78
+ break ;
79
+ case 'UDP ' :
80
+ $ method = PING_UDP ;
81
+
82
+ break ;
51
83
}
52
84
53
85
$ i = 0 ;
54
86
while ($ i < $ ping_sweeps ) {
55
- $ result = $ ping ->ping (AVAIL_PING , $ method , read_config_option ( ' ping_timeout ' ) , 1 );
87
+ $ result = $ ping ->ping (AVAIL_PING , $ method , $ ping_timeout , 1 );
56
88
57
89
if (!$ result ) {
58
90
$ failed_results ++;
@@ -66,8 +98,7 @@ function ss_fping($hostname, $ping_sweeps=6, $ping_type='ICMP', $port=80) {
66
98
$ i ++;
67
99
68
100
/* get current time */
69
- list ($ micro ,$ seconds ) = explode (' ' , microtime ());
70
- $ ss_fping_current = $ seconds + $ micro ;
101
+ $ ss_fping_current = microtime (true );
71
102
72
103
/* if called from script server, end one second before a timeout occurs */
73
104
if (isset ($ called_by_script_server ) && ($ ss_fping_current - $ ss_fping_start + ($ ping_timeout /1000 ) + 1 ) > $ script_timeout ) {
@@ -80,7 +111,7 @@ function ss_fping($hostname, $ping_sweeps=6, $ping_type='ICMP', $port=80) {
80
111
return 'min:U avg:U max:U dev:U loss:100.00 ' ;
81
112
} else {
82
113
$ loss = ($ failed_results /$ ping_sweeps ) * 100 ;
83
- $ avg = $ total_time /($ ping_sweeps -$ failed_results );
114
+ $ avg = $ total_time /($ ping_sweeps -$ failed_results );
84
115
85
116
/* calculate standard deviation */
86
117
$ predev = 0 ;
0 commit comments