forked from uakfdotb/bearmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checks.php
161 lines (126 loc) · 4.61 KB
/
checks.php
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
function check_extract($data, $key, $default) {
if(isset($data[$key])) {
return $data[$key];
} else {
return $default;
}
}
function check_http_contains($data) {
//keys: substring, url (also http_helper params)
if(!isset($data['substring']) || !isset($data['url'])) {
die("check_http_contains: missing substring\n");
}
$result = check_http_helper($data);
if($result['status'] == 'fail') {
return $result;
} else if(strpos($result['content'], $data['substring']) !== false) {
return array('status' => 'success');
} else {
return array('status' => 'fail', 'message' => "target [{$data['url']}] does not contain string [{$data['substring']}]");
}
}
function check_http_helper($data) {
if(!isset($data['url'])) {
die("check_http_helper: missing url\n");
}
$handle = curl_init($data['url']);
if($handle === false) {
return array('status' => 'fail', 'message' => "curl_init: failed to establish connection to target [$url]");
}
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handle, CURLOPT_AUTOREFERER, true);
$timeout = 10;
if(isset($data['timeout'])) {
$timeout = $data['timeout'];
}
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($handle, CURLOPT_TIMEOUT, $timeout);
$response = curl_exec($handle);
if($response === false) {
return array('status' => 'fail', 'message' => "curl_exec: failed to read target [$url]");
}
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
return array('status' => 'success', 'code' => $httpCode, 'content' => $response);
}
function check_http_status($data) {
//keys: status, url (also http_helper params)
if(!isset($data['status']) || !isset($data['url'])) {
die("check_http_status: missing status");
}
$result = check_http_helper($data);
if($result['status'] == 'fail') {
return $result;
} else if($result['code'] == $data['status']) {
return array('status' => 'success');
} else {
return array('status' => 'fail', 'message' => "target [{$data['url']}] returned unexpected status [{$result['code']}], expected [{$data['status']}]");
}
}
function check_http_ok($data) {
$data['status'] = 200;
return check_http_status($data);
}
function check_ssl_expire($data) {
//keys: hostname, optional port (default 443), days (default 7), and timeout (default 10)
if(!isset($data['hostname'])) {
die("check_ssl_expire: missing hostname");
}
$hostname = $data['hostname'];
$port = check_extract($data, 'port', 443);
$days = check_extract($data, 'days', 7);
$timeout = check_extract($data, 'timeout', 10);
$get = stream_context_create(array("ssl" => array("capture_peer_cert" => TRUE)));
$read = stream_socket_client("ssl://$hostname:$port", $errno, $errstr, 10, STREAM_CLIENT_CONNECT, $get);
if($read === false) {
return array('status' => 'fail', 'message' => "failed to read from host [$hostname]");
}
$cert = stream_context_get_params($read);
$result = openssl_x509_parse($cert["options"]["ssl"]["peer_certificate"]);
if(!isset($result['validTo_time_t'])) {
return array('status' => 'fail', 'message' => "parsed SSL information missing validTo");
}
$remaining = $result['validTo_time_t'] - time();
$remaining_hours = round($remaining / 3600);
if($remaining_hours < $days * 24) {
return array('status' => 'fail', 'message' => "SSL certificate will expire in $remaining_hours hours");
} else {
return array('status' => 'success');
}
}
function check_ping($data) {
//keys: target
if(!isset($data['target'])) {
die("check_ping: missing target");
}
$target = $data['target'];
$result = shell_exec("ping " . escapeshellarg($target) . " -c 3 -w 3");
if(strpos($result, '100% packet loss') !== false) {
return array('status' => 'fail', 'message' => "No response from $target");
} else {
return array('status' => 'success');
}
}
function check_tcp_connect($data) {
//keys: target, port; optional: timeout
if(!isset($data['target']) || !isset($data['port'])) {
die("check_tcp_connect: missing target or port");
}
$target = $data['target'];
$port = $data['port'];
$timeout = check_extract($data, 'timeout', 5);
if(($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "check_tcp_connect: warning: failed to create a socket!\n";
return array('status' => 'success'); //can't do anything?
}
socket_set_option($sock, SOL_SOCKET, SO_SNDTIMEO, array('sec' => $timeout, 'usec' => 0));
if(socket_connect($sock, $target, $port) === false) {
socket_close($sock);
return array('status' => 'fail', 'message' => "Connection to $target:$port failed");
}
socket_close($sock);
return array('status' => 'success');
}
?>