|
1 | 1 | <?php
|
2 | 2 |
|
| 3 | +function check_extract($data, $key, $default) { |
| 4 | + if(isset($data[$key])) { |
| 5 | + return $data[$key]; |
| 6 | + } else { |
| 7 | + return $default; |
| 8 | + } |
| 9 | +} |
| 10 | + |
3 | 11 | function check_http_contains($data) {
|
4 | 12 | //keys: substring, url (also http_helper params)
|
5 | 13 | if(!isset($data['substring']) || !isset($data['url'])) {
|
@@ -74,25 +82,18 @@ function check_http_ok($data) {
|
74 | 82 | }
|
75 | 83 |
|
76 | 84 | function check_ssl_expire($data) {
|
77 |
| - //keys: hostname, optional days (default 7) and timeout (default 10) |
| 85 | + //keys: hostname, optional port (default 443), days (default 7), and timeout (default 10) |
78 | 86 | if(!isset($data['hostname'])) {
|
79 | 87 | die("check_ssl_expire: missing hostname");
|
80 | 88 | }
|
81 | 89 |
|
82 | 90 | $hostname = $data['hostname'];
|
83 |
| - $days = 7; |
84 |
| - $timeout = 10; |
85 |
| - |
86 |
| - if(isset($data['days'])) { |
87 |
| - $days = $data['days']; |
88 |
| - } |
89 |
| - |
90 |
| - if(isset($data['timeout'])) { |
91 |
| - $timeout = $data['timeout']; |
92 |
| - } |
| 91 | + $port = check_extract($data, 'port', 443); |
| 92 | + $days = check_extract($data, 'days', 7); |
| 93 | + $timeout = check_extract($data, 'timeout', 10); |
93 | 94 |
|
94 | 95 | $get = stream_context_create(array("ssl" => array("capture_peer_cert" => TRUE)));
|
95 |
| - $read = stream_socket_client("ssl://$hostname:443", $errno, $errstr, 10, STREAM_CLIENT_CONNECT, $get); |
| 96 | + $read = stream_socket_client("ssl://$hostname:$port", $errno, $errstr, 10, STREAM_CLIENT_CONNECT, $get); |
96 | 97 |
|
97 | 98 | if($read === false) {
|
98 | 99 | return array('status' => 'fail', 'message' => "failed to read from host [$hostname]");
|
@@ -139,11 +140,7 @@ function check_tcp_connect($data) {
|
139 | 140 |
|
140 | 141 | $target = $data['target'];
|
141 | 142 | $port = $data['port'];
|
142 |
| - $timeout = 5; |
143 |
| - |
144 |
| - if(isset($data['timeout'])) { |
145 |
| - $timeout = $data['timeout']; |
146 |
| - } |
| 143 | + $timeout = check_extract($data, 'timeout', 5); |
147 | 144 |
|
148 | 145 | if(($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
|
149 | 146 | echo "check_tcp_connect: warning: failed to create a socket!\n";
|
|
0 commit comments