Skip to content

Commit c87aaf9

Browse files
author
Jack Lu
committed
checks: simplify optional parameter extraction.
1 parent ce5fb86 commit c87aaf9

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

checks.php

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
<?php
22

3+
function check_extract($data, $key, $default) {
4+
if(isset($data[$key])) {
5+
return $data[$key];
6+
} else {
7+
return $default;
8+
}
9+
}
10+
311
function check_http_contains($data) {
412
//keys: substring, url (also http_helper params)
513
if(!isset($data['substring']) || !isset($data['url'])) {
@@ -74,25 +82,18 @@ function check_http_ok($data) {
7482
}
7583

7684
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)
7886
if(!isset($data['hostname'])) {
7987
die("check_ssl_expire: missing hostname");
8088
}
8189

8290
$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);
9394

9495
$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);
9697

9798
if($read === false) {
9899
return array('status' => 'fail', 'message' => "failed to read from host [$hostname]");
@@ -139,11 +140,7 @@ function check_tcp_connect($data) {
139140

140141
$target = $data['target'];
141142
$port = $data['port'];
142-
$timeout = 5;
143-
144-
if(isset($data['timeout'])) {
145-
$timeout = $data['timeout'];
146-
}
143+
$timeout = check_extract($data, 'timeout', 5);
147144

148145
if(($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
149146
echo "check_tcp_connect: warning: failed to create a socket!\n";

0 commit comments

Comments
 (0)